import time
import network
from umqtt.simple import MQTTClient
import ujson
from machine import Pin
import random
WIFI_SSID = "nyhome5G"
WIFI_PASS = "bm@1574d"
THINGSBOARD_HOST = "192.168.1.1"
MQTT_PORT = 1884 # ou 1883
ACCESS_TOKEN = "81qyBt6mbpldVTBfzjcj"
TOPIC_TELEMETRY = b"v1/devices/me/telemetry"
TOPIC_ATTRIBUTES = b"v1/devices/me/attributes"
TOPIC_RPC_SUB = b"v1/devices/me/rpc/request/+"
# ---------- GPIO RELAIS ----------
PIN_FAN = 2 # Ventilateur
PIN_AC = 4 # Climatisation
# Si ton relais est "actif bas", mets RELAY_ACTIVE_LOW = True
RELAY_ACTIVE_LOW = False
relay_fan = Pin(PIN_FAN, Pin.OUT)
relay_ac = Pin(PIN_AC, Pin.OUT)
etat_fan = "OFF"
etat_ac = "OFF"
def apply_relay(pin, on: bool):
if RELAY_ACTIVE_LOW:
pin.value(0 if on else 1)
else:
pin.value(1 if on else 0)
def publish_states():
# Etat persistant (dashboard lit ça)
client.publish(TOPIC_ATTRIBUTES, ujson.dumps({
"ventilateur": etat_fan,
"climatisation": etat_ac
}))
def connecter_wifi():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
wlan.connect(WIFI_SSID, WIFI_PASS)
while not wlan.isconnected():
time.sleep(0.5)
print("Wi-Fi OK:", wlan.ifconfig())
return wlan
def on_rpc(topic, msg):
global etat_fan, etat_ac, client
try:
t = topic.decode() if isinstance(topic, bytes) else topic
req_id = t.split('/')[-1] # requestId
data = ujson.loads(msg)
method = data.get("method")
params = data.get("params")
def to_on(p):
return p in (True, 1, "ON", "on", "1")
if method == "setFan":
on = to_on(params)
etat_fan = "ON" if on else "OFF"
apply_relay(relay_fan, on)
publish_states()
client.publish("v1/devices/me/rpc/response/{}".format(req_id),
ujson.dumps({"success": True, "ventilateur": etat_fan}))
print("RPC setFan -> Ventilateur:", etat_fan)
elif method == "setAC":
on = to_on(params)
etat_ac = "ON" if on else "OFF"
apply_relay(relay_ac, on)
publish_states()
client.publish("v1/devices/me/rpc/response/{}".format(req_id),
ujson.dumps({"success": True, "climatisation": etat_ac}))
print("RPC setAC -> Climatisation:", etat_ac)
elif method == "read_state":
client.publish("v1/devices/me/rpc/response/{}".format(req_id),
ujson.dumps({
"success": True,
"ventilateur": etat_fan,
"climatisation": etat_ac
}))
print("RPC read_state -> Ventilateur:", etat_fan, "| Climatisation:", etat_ac)
else:
client.publish("v1/devices/me/rpc/response/{}".format(req_id),
ujson.dumps({"success": False, "error": "unknown method"}))
except Exception as e:
print("RPC error:", e)
def connecter_mqtt():
c = MQTTClient(
client_id=b"esp32_weatherst_01",
server=THINGSBOARD_HOST,
port=MQTT_PORT,
user=ACCESS_TOKEN,
password="",
keepalive=30
)
c.set_callback(on_rpc)
c.connect()
c.subscribe(TOPIC_RPC_SUB)
print("MQTT OK + RPC SUB")
return c
# ---------- MAIN ----------
connecter_wifi()
client = connecter_mqtt()
# état initial OFF
apply_relay(relay_fan, False)
apply_relay(relay_ac, False)
publish_states()
t_last = 0
while True:
client.check_msg()
# telemetry capteurs (optionnel, tu peux garder ton code capteurs ici)
if time.time() - t_last >= 5:
t_last = time.time()
data = {
"temperature": round(20 + random.random() * 10, 2),
"humidite": round(40 + random.random() * 30, 2),
"qualite_air": int(350 + random.random() * 300)
}
client.publish(TOPIC_TELEMETRY, ujson.dumps(data))
print("Telemetry:", data)
time.sleep(0.1)