print("SMART CLASSROOM SYSTEM - SCADA EDITION")
print("BY: SOUKAINA MALAK HAFIDA")
import network
from machine import Pin, PWM
import dht
from utime import sleep, ticks_ms
from umqtt.simple import MQTTClient
import ujson
# ================= MQTT CONFIG =================
MQTT_BROKER = "broker.hivemq.com"
MQTT_CLIENT_ID = "ESP32_SmartClass_SOUKAINA"
MQTT_TOPIC = "hackathon/team:SOUKAINA/salle1/data"
MQTT_STATUS = "hackathon/team:SOUKAINA/salle1/status"
MQTT_CONTROL = "hackathon/team:SOUKAINA/salle1/control"
# ================= HARDWARE =================
PIR_sensor = Pin(13, Pin.IN)
Green_led = Pin(4, Pin.OUT)
Red_led = Pin(2, Pin.OUT)
Buzzer = PWM(Pin(18))
dht_sensor = dht.DHT22(Pin(26))
# ================= TEMPERATURE LIMITS =================
TEMP_MAX = 30 # °C
TEMP_MIN = 15 # °C
# ================= WIFI =================
def connect_wifi():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect("Wokwi-GUEST", "")
while not wlan.isconnected():
print(".", end="")
sleep(0.5)
print("\nWiFi Connected!")
# ================= MQTT CALLBACK =================
def sub_cb(topic, msg):
message = msg.decode()
print("Message from SCADA:", message)
if message == "OFF":
Buzzer.duty(0)
elif message == "ON":
Buzzer.freq(1000)
Buzzer.duty(512)
# ================= MQTT CONNECT =================
def connect_mqtt():
client = MQTTClient(MQTT_CLIENT_ID, MQTT_BROKER, port=1883)
client.set_callback(sub_cb)
client.connect()
client.subscribe(MQTT_CONTROL)
client.publish(MQTT_STATUS, "online", retain=True)
print("MQTT Connected & Subscribed!")
return client
# ================= START =================
connect_wifi()
client = connect_mqtt()
# ================= VARIABLES =================
Number_of_people = 0
Total_student = 10
last_motion_time = 0
motion_delay = 3000 # debounce 3 seconds
# ================= MAIN LOOP =================
while True:
client.check_msg()
# -------- DHT READ --------
try:
dht_sensor.measure()
temp = dht_sensor.temperature()
hum = dht_sensor.humidity()
except:
temp, hum = None, None
# -------- TEMPERATURE ALERT --------
if temp is not None and (temp > TEMP_MAX or temp < TEMP_MIN):
alert = 1
Red_led.on()
Green_led.off()
Buzzer.freq(1000)
Buzzer.duty(512)
else:
alert = 0
Red_led.off()
Green_led.on()
Buzzer.duty(0)
# -------- PRESENCE DETECTION (room_status always Empty/Occupied) --------
motion_status = PIR_sensor.value()
current_time = ticks_ms()
if motion_status == 0 and (current_time - last_motion_time) > motion_delay:
Number_of_people = min(Number_of_people + 1, Total_student)
last_motion_time = current_time
if Number_of_people > 0:
status_msg = "Occupied"
else:
status_msg = "Empty"
absent = Total_student - Number_of_people
# -------- MQTT PAYLOAD --------
payload = ujson.dumps({
"temperature": temp,
"humidity": hum,
"presence": 1 if Number_of_people > 0 else 0,
"students_count": Number_of_people,
"absent_count": absent,
"room_status": status_msg,
"alert": alert
})
try:
client.publish(MQTT_TOPIC, payload)
print("Data sent to SCADA:", payload)
except:
print("MQTT Reconnecting...")
client = connect_mqtt()
sleep(5)