import network
import time
import machine
from umqtt.simple import MQTTClient
SSID = "Wokwi-GUEST"
PASSWORD = ""
def connect_wifi():
print("Connecting to Wi-Fi...")
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PASSWORD)
while not wlan.isconnected():
pass
print("Connected to wifi")
def main():
connect_wifi()
client = MQTTClient("mostafa","broker.hivemq.com") # handshake
client.connect() # if connection unsuccessfull -> error
print("Connected to broker")
while True:
client.publish("esp32/heartbeat","i'm alive")
print("Done publishing")
time.sleep(2)
main()
# client = MQTTClient("wokwi-esp32", MQTT_BROKER)
# client.connect()
# print("MQTT connected")