ssid = 'REPLACE_WITH_YOUR_SSID'
password = 'REPLACE_WITH_YOUR_PASSWORD'
mqtt_server = '192.168.1.106' #EXAMPLE IP Idr
client_id = ubinascii hexlify (machine.unique_id())
topic_pub_temp = b'esp/dht/temperature'
topic_pub_hum = b'esp/dht/humidity'
last_message = 0 #dernière fois qu'un msg a été envoyé
message_interval = 5 #un nouveau msg sera envoyé toutes les 5s
station = network WLAN network.STA_IF)
station.active (True)
station.connect (ssid, password
while station.isconnected() = False:
pass
print('Connection successful")
sensor = dht.DHT22 (Pin(14))
def connect_mqtt():
global client_id, mqtt_server
client = MQTTClient(client_id, mqtt_server)
#client = MQTTClient(client_id, mqtt_server, user=your_username,
password=your_password)
client.connect()
print('Connected to %s MQTT broker' % (mqtt_server))
return client
def restart_and_reconnect():
print('Failed to connect to MQTT broker. Reconnecting...')
time.sleep(10)
machine.reset()