-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathesp32_weather_monitoring_code.py
More file actions
78 lines (64 loc) · 1.85 KB
/
Copy pathesp32_weather_monitoring_code.py
File metadata and controls
78 lines (64 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import network
import time
from umqtt.simple import MQTTClient
import dht
from machine import Pin
# ---------------------------
# USER SETTINGS
# ---------------------------
WIFI_SSID = "Wokwi-GUEST"
WIFI_PASS = ""
AIO_USERNAME = "saiswaroop05" ## Adafruit username
AIO_KEY = "aio_SSUK185R8Cvp8xyekFPjuV6Qf" # adafruit aio key
AIO_FEED_TEMP = AIO_USERNAME + "/feeds/temperature"
AIO_FEED_HUM = AIO_USERNAME + "/feeds/humidity"
# ---------------------------
# DHT22 SENSOR
# ---------------------------
dht_pin = Pin(15, Pin.IN)
sensor = dht.DHT22(dht_pin)
# ---------------------------
# CONNECT TO WIFI
# ---------------------------
def connect_wifi():
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect(WIFI_SSID, WIFI_PASS)
print("Connecting to WiFi...", end="")
while not wifi.isconnected():
print(".", end="")
time.sleep(0.5)
print(" Connected!")
print(wifi.ifconfig())
# ---------------------------
# MQTT CONNECT
# ---------------------------
def connect_mqtt():
client = MQTTClient(
client_id="esp32_dht22",
server="io.adafruit.com",
user=AIO_USERNAME,
password=AIO_KEY,
ssl=False
)
client.connect()
print("Connected to Adafruit IO!")
return client
# ---------------------------
# MAIN PROGRAM
# ---------------------------
connect_wifi()
mqtt_client = connect_mqtt()
while True:
try:
sensor.measure()
temperature = sensor.temperature()
humidity = sensor.humidity()
print("Temp:", temperature, "°C | Hum:", humidity, "%")
# Publish to Adafruit IO
mqtt_client.publish(AIO_FEED_TEMP, str(temperature))
mqtt_client.publish(AIO_FEED_HUM, str(humidity))
print("Published to AIO!")
except Exception as e:
print("Sensor error:", e)
time.sleep(10) # Read every 5 seconds