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:")
def esp(topic, msg):
print("The esp is called")
if msg == b"name":
print("my name is ESP32")
elif msg == b"greeting":
print("Hello from ESP32")
def main():
connect_wifi()
client = MQTTClient("esp32_shalan", "broker.hivemq.com")
client.set_callback(esp)
client.connect()
print("mqtt connected")
client.subscribe(b"esp32/AMR")
while True:
client.check_msg()
client.publish(b"esp32/Ahmed", b"connect")
print("Sent massege")
time.sleep(3)
main()