import network
import time
from umqtt.simple import MQTTClient
name='Wokwi-GUEST'
passw=""
topic_pub='esp/pub'
topic_sub='esp/sub'
def wifi_connection(): # wifi function
wlan=network.WLAN(network.STA_IF) #1 scan
wlan.active(True)
wlan.connect(name , passw)
print('connecting to Wokwi-GUEST.....')
while not wlan.isconnected():
pass
print ('connected')
def esp(topic,data): # sub function هستقبل عليها
print("esp is calling")
if data == b'name' :
print('iam esp')
elif data == b'greeting':
print('hello')
def broker():
wifi_connection()
client = MQTTClient("seif1","mqtt-dashboard.com") #hanshake
client.set_callback(esp) # the function which sub the data
client.connect() # connect
print('connect to broker')
client.subscribe(topic_sub)
while True:
client.check_msg()
client.publish(topic_pub,'is there anybody out there')
time.sleep(2)
broker()