"""
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Raspberry Pi Pico SSD1306 OLED Display (MicroPython) ┃
┃ ┃
┃ ┃
┃ ┃
┃ connected to a Raspberry Pi Pico with two bottons for ┃
| wifi ON or wifi OFF. ┃
┃ ┃
┃ Copyright (c) 2024 Mohammad Alhummada ┃
┃ GitHub: https://github.com/malhummada ┃
┃ License: MIT ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
"""
import network
from machine import Pin
import time
# Set up the pushbutton GPIO pin
wifiOFF = Pin(19, Pin.IN, Pin.PULL_UP) # Assuming GPIO 14, adjust as needed
wifiON = Pin(20, Pin.IN, Pin.PULL_UP) # Assuming GPIO 14, adjust as needed
# Connect to WiFi
wifi_ssid = "Wokwi-GUEST"
wifi_password = ""
wifi = network.WLAN(network.STA_IF)
# wifi.active(True)
# wifi.connect(wifi_ssid, wifi_password)
def disconnect_wifi():
wifi.disconnect()
print("Disconnecting WIFI")
while wifi.isconnected():
print(".", end="")
time.sleep(0.1)
print("Disconnected!")
def conn_wifi():
print("Connecting to WIFI")
wifi.active(True)
wifi.connect(wifi_ssid, wifi_password)
while not wifi.isconnected():
print(".", end="")
time.sleep(0.1)
print(" Connected!")
# Main loop
while True:
# Check if the pushbutton is pressed
if wifiOFF.value() == 0: # Button pressed
disconnect_wifi()
# Wait for button release to prevent multiple disconnections
while wifiOFF.value() == 0:
time.sleep(0.1)
if wifiON.value() == 0: # Button pressed
conn_wifi()
# Wait for button release to prevent multiple disconnections
while wifiON.value() == 0:
time.sleep(0.1)