from machine import ADC, Pin
import time
import network
import urequests
API_TOKEN = "BBUS-cv6zujlDqFPGb7KmPuNYH5VhXXTkii"
smoke_sensor = ADC(1) # GP26 = ADC0
buzzer = Pin(15, Pin.OUT)
threshold = threshold = 45000
red = Pin(2, Pin.OUT) # Red LED
green = Pin(3, Pin.OUT) # Green LED
motor = Pin(14,Pin.OUT) #fan
# WiFi credentials
ssid = 'HanBeingHan'
password = 'Han19@@2005'
# Connect to WiFi
print("===== Connect to Wifi =====")
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
while not wlan.isconnected():
pass
print("Connected to Wif-Fi")
# Function to send data to Ubidots
def send_data(analog_value):
url = "https://industrial.api.ubidots.com/api/v1.6/devices/mq-2/"
headers = {"X-Auth-Token": API_TOKEN, "Content-Type": "application/json"}
payload = {"smoke_level": analog_value}
try:
response = urequests.post(url, headers=headers, json=payload)
response.close()
print("Data sent to Ubidots")
except Exception as e:
print("Failed to send data:", e)
while True:
analog_value = smoke_sensor.read_u16() # 0 to 65535
print("Smoke Level:", analog_value)
send_data(analog_value)
time.sleep(10)
if analog_value > threshold:
print("⚠️ Smoke Level HIGH - Taking Action!")
buzzer.value(1)
red.value(1)
green.value(0)
motor.value(1)
else:
print("✅ Smoke Level Normal")
buzzer.value(0)
green.value(1)
red.value(0)
motor.value(0)
time.sleep(1)