add mqtt
This commit is contained in:
parent
96b94af981
commit
b5ed9807b8
|
@ -0,0 +1,15 @@
|
|||
WIFI_SSID="ssid"
|
||||
WIFI_PSK="secret"
|
||||
|
||||
HOUR_COLOR=(255,0,0,0)
|
||||
MINUTE_COLOR=(0,0,255,0)
|
||||
|
||||
TIMEZONE_OFFSET = 3600
|
||||
TIMEZONE_OFFSET_DAYLIGHT = 7200
|
||||
|
||||
MQTT_SERVER = "ip_address"
|
||||
MQTT_PORT = 1883
|
||||
MQTT_ID = "ledclock00"
|
||||
MQTT_TOPIC = "home/livingroom/ledclock"
|
||||
MQTT_USER = "user"
|
||||
MQTT_PASSWORD = "secret"
|
65
main.py
65
main.py
|
@ -3,14 +3,20 @@ import neopixel
|
|||
import config
|
||||
import ntptime
|
||||
import time
|
||||
from umqtt.simple import MQTTClient
|
||||
|
||||
hour_color = config.HOUR_COLOR
|
||||
minute_color = config.MINUTE_COLOR
|
||||
brightness = 1.0
|
||||
switch_state = True
|
||||
|
||||
def set_time(hour, minute):
|
||||
np_hour = neopixel.NeoPixel(machine.Pin(23), 24, bpp=4)
|
||||
np_hour[(hour % 12) * 2 + int(minute / 30)] = config.HOUR_COLOR
|
||||
np_hour[(hour % 12) * 2 + int(minute / 30)] = hour_color if switch_state == True else (0,0,0,0)
|
||||
np_hour.write()
|
||||
|
||||
np_minute = neopixel.NeoPixel(machine.Pin(32), 30, bpp=4)
|
||||
np_minute[int(minute / 2)] = config.MINUTE_COLOR
|
||||
np_minute[int(minute / 2)] = minute_color if switch_state == True else (0,0,0,0)
|
||||
np_minute.write()
|
||||
|
||||
def sync_ntptime():
|
||||
|
@ -43,6 +49,44 @@ def check_wifi_connection():
|
|||
sta_if = network.WLAN(network.STA_IF)
|
||||
return sta_if.isconnected
|
||||
|
||||
def mqtt_send_availability(mqtt_client):
|
||||
mqtt_client.publish(config.MQTT_TOPIC + "/available", "online")
|
||||
|
||||
def mqtt_send_status(mqtt_client):
|
||||
global brightness
|
||||
global switch_state
|
||||
mqtt_client.publish(config.MQTT_TOPIC + "/brightness", str(int(brightness * 100)))
|
||||
|
||||
if switch_state == True:
|
||||
mqtt_client.publish(config.MQTT_TOPIC + "/switch", 'ON')
|
||||
else:
|
||||
mqtt_client.publish(config.MQTT_TOPIC + "/switch", 'OFF')
|
||||
|
||||
def mqtt_receive_message(topic, msg):
|
||||
global hour_color
|
||||
global minute_color
|
||||
global brightness
|
||||
global switch_state
|
||||
if topic.decode('utf-8') == config.MQTT_TOPIC + "/brightness/set":
|
||||
brightness = float(msg) / 100.0
|
||||
hour_color = (
|
||||
int(float(config.HOUR_COLOR[0]) * brightness),
|
||||
int(float(config.HOUR_COLOR[1]) * brightness),
|
||||
int(float(config.HOUR_COLOR[2]) * brightness),
|
||||
int(float(config.HOUR_COLOR[3]) * brightness),
|
||||
)
|
||||
minute_color = (
|
||||
int(float(config.MINUTE_COLOR[0]) * brightness),
|
||||
int(float(config.MINUTE_COLOR[1]) * brightness),
|
||||
int(float(config.MINUTE_COLOR[2]) * brightness),
|
||||
int(float(config.MINUTE_COLOR[3]) * brightness),
|
||||
)
|
||||
elif topic.decode('utf-8') == config.MQTT_TOPIC + "/switch/set":
|
||||
if msg.decode('utf-8').strip() == "ON":
|
||||
switch_state = True
|
||||
else:
|
||||
switch_state = False
|
||||
|
||||
# Test LEDs before startup
|
||||
for hour in range(0, 11):
|
||||
set_time(hour, 0)
|
||||
|
@ -52,6 +96,16 @@ for minute in range(0, 59):
|
|||
time.sleep(0.05)
|
||||
set_time(0, 0)
|
||||
|
||||
if not check_wifi_connection():
|
||||
machine.reset()
|
||||
|
||||
# MQTT Setup
|
||||
mqtt_client = MQTTClient(config.MQTT_ID, config.MQTT_SERVER, config.MQTT_PORT, config.MQTT_USER, config.MQTT_PASSWORD)
|
||||
mqtt_client.set_callback(mqtt_receive_message)
|
||||
mqtt_client.connect()
|
||||
mqtt_client.subscribe(config.MQTT_TOPIC + "/brightness/set")
|
||||
mqtt_client.subscribe(config.MQTT_TOPIC + "/switch/set")
|
||||
|
||||
# Main Loop
|
||||
while True:
|
||||
|
||||
|
@ -68,7 +122,12 @@ while True:
|
|||
if check_daylight_saving(current_time):
|
||||
current_time = time.localtime(time.mktime(current_time) + config.TIMEZONE_OFFSET_DAYLIGHT)
|
||||
else:
|
||||
current_time = time.localtime(time.mktime(current_time) + config.TIMEZONE_OFFSET)
|
||||
current_time = time.localtime(time.mktime(current_time) + config.TIMEZONE_OFFSET)#
|
||||
|
||||
# Call MQTT
|
||||
mqtt_client.check_msg()
|
||||
mqtt_send_availability(mqtt_client)
|
||||
mqtt_send_status(mqtt_client)
|
||||
|
||||
print("Set Time:", current_time)
|
||||
set_time(current_time[3], current_time[4])
|
||||
|
|
Loading…
Reference in New Issue