"""
==========================================================
Smart Energy Management System
Main Program
==========================================================
"""
from umqtt import MQTTClient
print("MQTT Library OK")
import time
from config import (
AUTO_MODE,
MANUAL_MODE,
PUBLISH_INTERVAL,
COMMAND
)
from wifi import WiFiManager
from mqtt_client import MQTTManager
from discovery import Discovery
from system_state import SystemState
from energy_manager import EnergyManager
from automation import Automation
from hardware import (
read_dht,
read_ldr,
read_gas,
read_solar,
motion_detected,
light_on,
light_off,
ac_on,
ac_off,
heater_on,
heater_off,
open_curtains,
close_curtains
)
# ==========================================================
# Objects
# ==========================================================
wifi = WiFiManager()
mqtt = MQTTManager()
state = SystemState()
energy = EnergyManager(state)
automation = Automation(state, energy)
discovery = Discovery(mqtt)
# ==========================================================
# MQTT Callback
# ==========================================================
def on_mqtt_message(topic, msg):
topic = topic.decode()
msg = msg.decode()
# ------------------------------------------------------
if topic == COMMAND["mode"]:
if msg in (AUTO_MODE, MANUAL_MODE):
state.mode = msg
mqtt.publish_sensor("mode", state.mode)
return
# ------------------------------------------------------
if state.mode != MANUAL_MODE:
return
# ------------------------------------------------------
if topic == COMMAND["light"]:
if msg == "ON":
light_on()
state.light = True
else:
light_off()
state.light = False
# ------------------------------------------------------
elif topic == COMMAND["ac"]:
if msg == "ON":
ac_on()
state.ac = True
else:
ac_off()
state.ac = False
# ------------------------------------------------------
elif topic == COMMAND["heater"]:
if msg == "ON":
heater_on()
state.heater = True
else:
heater_off()
state.heater = False
# ------------------------------------------------------
elif topic == COMMAND["curtain"]:
if msg == "OPEN":
open_curtains()
state.curtain = True
state.curtain_led = True
elif msg == "CLOSE":
close_curtains()
state.curtain = False
state.curtain_led = False
# ==========================================================
# Startup
# ==========================================================
wifi.connect()
mqtt.connect()
mqtt.set_callback(on_mqtt_message)
mqtt.subscribe()
time.sleep(5)
discovery.publish()
print("System Started Successfully")
# ==========================================================
# Main Loop
# ==========================================================
while True:
# ---------------------------
# Read Sensors
# ---------------------------
dht = read_dht()
state.temperature = dht["temperature"]
state.humidity = dht["humidity"]
state.ldr = read_ldr()
state.gas = read_gas()
state.solar = read_solar()
state.motion = motion_detected()
# ---------------------------
# Energy
# ---------------------------
energy.update()
# ---------------------------
# Automation
# ---------------------------
automation.update()
# ---------------------------
# Publish Sensors
# ---------------------------
mqtt.publish_sensor("temperature", state.temperature)
time.sleep_ms(50) # <--- أضف هذا السطر هنا وكل سطرين بعد الآن
mqtt.publish_sensor("humidity", state.humidity)
time.sleep_ms(50)
mqtt.publish_sensor("ldr", state.ldr)
time.sleep_ms(50)
mqtt.publish_sensor("gas", state.gas)
time.sleep_ms(50)
mqtt.publish_sensor(
"motion",
"ON" if state.motion else "OFF"
)
time.sleep_ms(50)
mqtt.publish_sensor("battery", round(state.battery, 1))
time.sleep_ms(50)
mqtt.publish_sensor("solar", state.solar)
time.sleep_ms(50)
mqtt.publish_sensor(
"solar_power",
round(state.solar_power, 1)
)
time.sleep_ms(50)
mqtt.publish_sensor(
"load_power",
round(state.load_power, 1)
)
time.sleep_ms(50)
mqtt.publish_sensor(
"surplus_power",
round(state.surplus_power, 1)
)
time.sleep_ms(50)
mqtt.publish_sensor(
"light",
"ON" if state.light else "OFF"
)
time.sleep_ms(50)
mqtt.publish_sensor(
"ac",
"ON" if state.ac else "OFF"
)
time.sleep_ms(50)
mqtt.publish_sensor(
"heater",
"ON" if state.heater else "OFF"
)
time.sleep_ms(50)
mqtt.publish_sensor(
"curtain",
"OPEN" if state.curtain else "CLOSED"
)
time.sleep_ms(50)
mqtt.publish_sensor(
"status",
state.system_status
)
time.sleep_ms(50)
mqtt.publish_sensor(
"mode",
state.mode
)
# ---------------------------
# Receive MQTT Commands
# ---------------------------
mqtt.check()
# ---------------------------
# Debug
# ---------------------------
state.print_status()
# ---------------------------
# Delay
# ---------------------------
time.sleep(PUBLISH_INTERVAL)LEDs
Heater
cooling
solar