import network
import urequests
import dht
from machine import Pin, ADC
from time import sleep
# ---------------- BLYNK CONFIG ----------------
BLYNK_TEMPLATE_ID = "TMPL507o7o76h"
BLYNK_TEMPLATE_NAME = "iot industry"
BLYNK_AUTH_TOKEN = "Y36HVmT3CC-vPaVhEq36gL8Ew8xaNkuN"
# ---------------- WIFI CONFIG ----------------
WIFI_SSID = "Wokwi-GUEST"
WIFI_PASSWORD = ""
# ---------------- CONNECT WIFI ----------------
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect(WIFI_SSID, WIFI_PASSWORD)
print("Connecting to WiFi...", end="")
while not wifi.isconnected():
print(".", end="")
sleep(1)
print("\nWiFi Connected!")
print("IP Address:", wifi.ifconfig()[0])
# ---------------- SENSORS ----------------
sensor = dht.DHT22(Pin(26))
smoke_sensor = ADC(28)
# ---------------- LEDS ----------------
red = Pin(4, Pin.OUT)
green = Pin(2, Pin.OUT)
amber = Pin(3, Pin.OUT)
# ---------------- SEND DATA TO BLYNK ----------------
def send_to_blynk(pin, value):
url = "https://blynk.cloud/external/api/update?token={}&{}={}".format(
BLYNK_AUTH_TOKEN,
pin,
value
)
try:
response = urequests.get(url)
response.close()
print("Sent to Blynk:", pin, value)
except Exception as e:
print("Failed to send data:", e)
# ---------------- MAIN LOOP ----------------
while True:
try:
# Read DHT22
sensor.measure()
temp = sensor.temperature()
humidity = sensor.humidity()
except Exception as e:
print("DHT22 Error:", e)
sleep(2)
continue
# Read gas sensor
gas_value = smoke_sensor.read_u16()
# Convert to voltage
voltage = round((gas_value / 65535) * 3.3, 2)
# Print values
print("Temperature:", temp, "C")
print("Humidity:", humidity, "%")
print("Gas Value:", gas_value)
print("Voltage:", voltage, "V")
# Turn OFF all LEDs first
red.off()
green.off()
amber.off()
# ---------------- TEMPERATURE STATUS ----------------
if temp < 30:
green.on()
print("Temperature Status: Normal")
elif 30 <= temp <= 35:
amber.on()
print("Temperature Status: Warning")
else:
red.on()
print("Temperature Status: Critical")
# ---------------- GAS STATUS ----------------
if gas_value > 30000:
print("Gas Status: DANGER!")
elif 15000 <= gas_value <= 30000:
print("Gas Status: Warning!")
else:
print("Gas Status: Normal")
print("-----------------------------")
# ---------------- SEND VALUES TO BLYNK ----------------
send_to_blynk("V0", temp)
send_to_blynk("V1", humidity)
send_to_blynk("V2", gas_value)
send_to_blynk("V3", voltage)
sleep(2)Loading
pi-pico-w
pi-pico-w