import network
from time import sleep
from machine import Pin, I2C
from ssd1306 import SSD1306_I2C
WIDTH =128
HEIGHT= 64
#Create the I2C connection
i2c=I2C(1,scl=Pin(15),sda=Pin(14),freq=400000)
#Initialize the OLED display
oled = SSD1306_I2C(WIDTH,HEIGHT,i2c)
SSID = "Leylines"
PASSWORD = "lindahyr"
BROKER = "broker.emqx.io"
PORT = 1883
# Function to connect to WLAN
def connect_wlan(oled):
# Connecting to the group WLAN
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PASSWORD)
# Attempt to connect once per second
while wlan.isconnected() == False:
oled.text("Connecting...", 0, 0)
oled.show()
sleep(1)
# Print the IP address of the Pico
oled.fill(0)
oled.text("Connected.", 0, 0)
oled.text("IP:" + wlan.ifconfig()[0], 0, 8)
oled.show()
#print("Connection successful. Pico IP:", wlan.ifconfig()[0])
# Main program
if __name__ == "__main__":
#Connect to WLAN
connect_wlan(oled)