import machine
import network
#import wifi_credentials
import urequests
import dht
import time
# **************************************
# Create objects:
#led = machine.Pin(2,machine.Pin.OUT)
d = dht.DHT22(machine.Pin(5))
# **************************************
# Configure the ESP32 wifi as STAtion
sta = network.WLAN(network.STA_IF)
if not sta.isconnected():
print('connecting to network...')
sta.active(True)
sta.connect('Wokwi-GUEST')
#sta.connect(wifi_credentials.ssid, wifi_credentials.password)
while not sta.isconnected():
pass
print('network config:', sta.ifconfig())
# **************************************
# Constants and variables:
HTTP_HEADERS = {'Content-Type': 'application/json'}
THINGSPEAK_WRITE_API_KEY = '66KT6TMZV2QV5B2F'
UPDATE_TIME_INTERVAL = 5000 # in ms
last_update = time.ticks_ms()
# initially there would be some delays
# before submitting the first update
# but should be enough to stabilize the
# the DHT sensor.
# ****************************
# Main loop:
while True:
if time.ticks_ms() - last_update >= UPDATE_TIME_INTERVAL:
d.measure()
t = d.temperature()
h = d.humidity()
dht_readings = {'field1':t, 'field2':h}
request = urequests.post(
'http://api.thingspeak.com/update?api_key=' +
THINGSPEAK_WRITE_API_KEY,
json = dht_readings,
headers = HTTP_HEADERS )
request.close()
print(dht_readings)
# led.value(not led.value())
# last_update = time.ticks_ms()