import dht
from machine import Pin
import wifi
import time
from umqtt.simple import MQTTClient
d = dht.DHT22(Pin(12, Pin.IN))
# connect wifi
wifi.connect_ap()
# Connect MQTT Broker
# Use MD5 to encrypt the name of the topic to identify on MQTT
client_id = "0f1b760cf7348436d9fbd8448a5d992a"
server = "broker.hivemq.com"
client = MQTTClient(client_id, server)
client.connect()
def loop():
while True:
d.measure()
temperature = d.temperature()
humidity = d.humidity()
print("Send data to broker")
client.publish("IoT_MQTT_Temperature", str(temperature) )
client.publish("IoT_MQTT_Humidity", str(humidity))
# send data each 5s
time.sleep(5)
if __name__ == "__main__":
loop()