import network
import time
from config import *
from sensors import *
from actuators import *
from cloud import *
from machine import *
# WiFi connect
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect(WIFI_SSID, WIFI_PASSWORD)
while not wifi.isconnected():
time.sleep(1)
print("WiFi Connected:", wifi.ifconfig())
presence = False
while True:
pir_state = read_pir()
# STEP 1: PIR detection
if pir_state == 1 and not presence:
presence = True
print("Person detected")
if pir_state == 0 and presence:
presence = False
relay_off()
display_off()
print("Person left")
# STEP 2: LDR + Relay
if presence:
ldr_val = read_ldr()
if ldr_val < 1500:
relay_on()
else:
relay_off()
# STEP 3: Touch Display ON
display_on()
"""adc = ADC(Pin(34))
adc.atten(ADC.ATTN_11DB) # 0–3.3V
adc.width(ADC.WIDTH_12BIT)
print("🔊 Noise Level Monitoring")
while True:
value = adc.read()
print("Sound Level:", value)
if value > 5000:
print("🚨 Loud Noise Detected")
time.sleep(0.3)
"""
# STEP 4: Read DHT
temp, hum = read_dht()
# STEP 5: Upload ENV data
send_thingspeak(
THINGSPEAK_URL,
THINGSPEAK_API_KEY_ENV,
{
"field1": temp,
"field2": hum,
}
)
time.sleep(20)