import machine
import time
import math
import network
from umqtt.simple import MQTTClient # <--- ADD THIS LINE!
SSID = "Wokwi-GUEST"
PASSWORD = ""
MQTT_BROKER = "broker.hivemq.com"
TOPIC_PUB = b"esp32/Heartbeat" # Corrected from b"b'esp32/heartbeat" in earlier examples
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():
time.sleep(0.1)
print("Connected:", wlan.ifconfig())
def main():
connect_wifi()
# Now MQTTClient is defined because of the import above
client = MQTTClient(b"wokwi-esp32", MQTT_BROKER) # Client ID should ideally be bytes
client.connect()
print("MQTT connected")
last_heartbeat = time.ticks_ms()
while True:
if time.ticks_diff(time.ticks_ms(), last_heartbeat) > 10000:
client.publish(TOPIC_PUB, b"HALMOUSHY!")
print("Sent heartbeat")
last_heartbeat = time.ticks_ms()
time.sleep(0.1)
main()