import network
import time
from machine import Pin,ADC
import dht
import ujson
from umqtt.simple import MQTTClient
# dht sensor pin number
dht_sensor = dht.DHT22(Pin(9))
# ldr pin number
ldr_pin = 10
ldr_adc = ADC(Pin(ldr_pin))
ldr_adc.atten(ADC.ATTN_11DB)
# connect to wifi in wokwi
print("Connecting to WiFi", end="")
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect('Wokwi-GUEST', '')
while not sta_if.isconnected():
print(".", end="")
time.sleep(0.1)
print(" Connected!")
def checkwifi():
while not sta_if.isconnected():
print(".", end="")
time.sleep(0.1)
print(" Connected!")
# acces to cloud broker
ubidotsToken = "BBUS-yCNm6C8EUbWwxY9fsWsPhynSc13haM" # replace this with your api key
clientID = "AYESHASIDDIQHA" # replace this with your user name
client = MQTTClient("clientID", "industrial.api.ubidots.com", 1883, user = ubidotsToken, password = ubidotsToken)
predata=None
while True:
checkwifi()
client.connect()
dht_sensor.measure()
temperature = dht_sensor.temperature()
humidity = dht_sensor.humidity()
intensity = ldr_adc.read()
data = {"temperature": temperature, "humidity": humidity,"intensity":intensity}
print(data)
#publish data to mqtt broker
if predata != data:
client.publish(b"/v1.6/devices/ESP_32", ujson.dumps(data))
print('your data had been published')
predata=data
time.sleep(1)
Loading
esp32-s3-devkitc-1
esp32-s3-devkitc-1