Add control over MQTT
This commit is contained in:
parent
38b9ed20cd
commit
43ba1bc18a
53
code/main.py
53
code/main.py
@ -101,7 +101,7 @@ def now(clock):
|
||||
return "{0:04d}/{1:02d}/{2:02d}_{4:02d}:{5:02d}:{6:02d}".format(*clock.datetime())
|
||||
|
||||
|
||||
def MqttPublish(topic, message, retain=False, qos=0, sleep=0):
|
||||
def mqtt_publish(topic, message, retain=False, qos=0, sleep=0):
|
||||
"""MQTT publish helper."""
|
||||
global CLIENT
|
||||
|
||||
@ -114,10 +114,35 @@ def MqttPublish(topic, message, retain=False, qos=0, sleep=0):
|
||||
time.sleep_ms(sleep)
|
||||
|
||||
|
||||
def MqttMultiPublish(payload, sleep=0):
|
||||
def mqtt_multipub(payload, sleep=0):
|
||||
"""Send multiple data"""
|
||||
for pl in payload:
|
||||
MqttPublish(pl[0], pl[1], retain=pl[2], qos=pl[3], sleep=sleep)
|
||||
mqtt_publish(pl[0], pl[1], retain=pl[2], qos=pl[3], sleep=sleep)
|
||||
|
||||
|
||||
def mqtt_callback(topic, msg):
|
||||
"""Callback en cas de changement de mode"""
|
||||
global CURRENT_MODE
|
||||
global BRIGHTN
|
||||
global PWR
|
||||
# print(topic, msg)
|
||||
t = topic.split(b'/')
|
||||
l = len(t)
|
||||
if t[l-1] == b'value' and msg.isdigit():
|
||||
msg = int(msg)
|
||||
# print(t[l-2], msg)
|
||||
if t[l-2] == b'mode' and msg in range(0, len(light_modes.MODES_LST)):
|
||||
# print("Nouveau mode: ", int(msg))
|
||||
CURRENT_MODE = msg
|
||||
if t[l-2] == b'brightness' and msg in range(0, 101):
|
||||
# print("Modif. luminosité :", int(msg))
|
||||
BRIGHTN = msg
|
||||
ENCODER.reset(BRIGHTN)
|
||||
if t[l-2] == b'power' and msg in (0, 1):
|
||||
PWR = bool(msg)
|
||||
else:
|
||||
print("Pas une valeur. Ignore...")
|
||||
|
||||
|
||||
|
||||
def power_cycle():
|
||||
@ -143,8 +168,8 @@ def power_cycle():
|
||||
time.sleep_ms(10)
|
||||
|
||||
# Envoi de la nouvelle info au MQTT
|
||||
MqttMultiPublish([['power/date', now(rtc), True, 1],
|
||||
['power/value', "{}".format(PWR), True, 0]])
|
||||
mqtt_multipub([['power/date', now(rtc), True, 1],
|
||||
['power/value', "{:d}".format(PWR), True, 0]])
|
||||
|
||||
|
||||
def update_button_c(button, button_state, pwr, mode):
|
||||
@ -174,7 +199,7 @@ def update_button_c(button, button_state, pwr, mode):
|
||||
if mode >= len(light_modes.MODES_LST):
|
||||
mode = 0
|
||||
|
||||
MqttMultiPublish([['mode/date', now(rtc), True, 1],
|
||||
mqtt_multipub([['mode/date', now(rtc), True, 1],
|
||||
['mode/value', "{}".format(mode), True, 1]])
|
||||
|
||||
return mode
|
||||
@ -202,10 +227,18 @@ data = [["last_boot", now(rtc), True, 1],
|
||||
['brightness/date', now(rtc), True, 1],
|
||||
["brightness/value", "{}".format(BRIGHTN), True, 1],
|
||||
['power/date', now(rtc), True, 1],
|
||||
["power/value", "{}".format(PWR), True, 1],
|
||||
["power/value", "{:d}".format(PWR), True, 1],
|
||||
]
|
||||
MqttMultiPublish(data)
|
||||
mqtt_multipub(data)
|
||||
|
||||
# MQTT subscription
|
||||
print("Set callback...")
|
||||
CLIENT.set_callback(mqtt_callback)
|
||||
print("OK")
|
||||
print("Subcribe...")
|
||||
CLIENT.subscribe(b"{location}/{device}/#".format(location=config.LOCATION,
|
||||
device=CLIENT.client_id))
|
||||
print("OK")
|
||||
time.sleep(1)
|
||||
|
||||
BRIGHTN_DATA = None
|
||||
@ -236,9 +269,11 @@ while True:
|
||||
|
||||
update_conn_led()
|
||||
|
||||
CLIENT.check_msg()
|
||||
|
||||
# On n'envoie les données de luminosité qu'une fois stabilisée (MQTT_DELAY)
|
||||
if BRIGHTN_DATA is not None and time.ticks_diff(time.ticks_ms(), MQTT_TIMER) >= 0:
|
||||
MqttMultiPublish(BRIGHTN_DATA)
|
||||
mqtt_multipub(BRIGHTN_DATA)
|
||||
BRIGHTN_DATA = None
|
||||
|
||||
time_to_test = rtc.datetime()
|
||||
|
Loading…
Reference in New Issue
Block a user