import network #https://docs.micropython.org/en/latest/esp32/quickref.html#networking
import dht #https://docs.micropython.org/en/latest/esp32/quickref.html#dht-driver
import machine
import time #https://docs.micropython.org/en/latest/esp32/quickref.html#timers
#https://randomnerdtutorials.com/micropython-mqtt-esp32-esp8266/
#Attention here is the difference in code . you should install umqttsimple on raspberrypi
#from umqttsimple import MQTTClient use this on raspberrypi,esp
from umqtt.simple import MQTTClient #use this on Wokwi
wlan = network.WLAN(network.STA_IF) # create station interface
wlan.active(True) # activate the interface
wlan.connect('Wokwi-GUEST', '') # connect to an AP
time.sleep(3) # sleep for 1 second
print (wlan.ifconfig()) # get the interface's IP/netmask/gw/DNS addresses
d = dht.DHT22(machine.Pin(15))
d.measure()
print("Temp is", d.temperature()) # eg. 23 (°C)
print("Humidity is", d.humidity()) # eg. 41 (% RH)
#for mqtt use https://pypi.org/project/micropython-umqtt.simple/
client=MQTTClient("685","broker.hivemq.com")
client.connect()
#client.publish("M_IoT/685/temp", str(d.temperature()))
while True:
d.measure()
client.publish("M_IoT/685/temp", str(d.temperature()))
client.publish("M_IoT/685/humi", str(d.humidity()))
print('Temp is:',d.temperature())
print('Humidity is:',d.temperature())
time.sleep(5)