Files
python-scripts/mqtt-print-time.py
2020-10-30 19:21:36 +01:00

82 lines
2.5 KiB
Python
Executable File

#!/usr/bin/python3.8
import paho.mqtt.client as mqtt #import the client1
from mqtt_print import get_message as M
from slovak_calendar import slovak_name_calendar as names
from datetime import datetime
import time
from pyowm import OWM
from covid import get_covid_stats
def on_message(client, userdata, message):
print("message received " ,str(message.payload.decode("utf-8")))
print("message topic=",message.topic)
print("message qos=",message.qos)
print("message retain flag=",message.retain)
client.publish("home/scroller",M("ZVONI",color=0));
def print_time(format='%H:%M',color=1):
t = time.strftime(format)
print("format time = ",format," ",M(t,color=color))
client.publish("home/scroller",M(t,color=color));
def print_weather(color=1):
owm = OWM('d15c61931a053026e98769dd9fc46ba3')
mgr = owm.weather_manager()
observation = mgr.weather_at_place('Trnava,SK')
weather = observation.weather
s = weather.status + " " + str(weather.temperature('celsius')['temp']) + "C " + str(weather.humidity) + "%"
print("Weather:",s)
print("Humidity:",weather.humidity)
client.publish("home/scroller",M(s,color=color,speed=100));
def print_name(color=0):
dt = datetime.today()
for name in names:
(m,d,n) = name
if m == dt.month and d == dt.day:
print("Name:",M(n,color=color,speed=100))
client.publish("home/scroller",M(n,color=color,speed=100));
def print_covid(color=0):
d = get_covid_stats()
msg = "Umrt {:d} {:d}".format(d["umrti"]["last"],d["umrti"]["delta"])
print(msg)
client.publish("home/scroller",M(msg,color=color,speed=100))
time.sleep(10)
msg = "Test {:d} {:d}".format(d["testovany"]["last"],d["testovany"]["delta"])
print(msg)
client.publish("home/scroller",M(msg,color=color,speed=100))
msg = "Pozi {:d} {:d}".format(d["vysetreni"]["last"],d["vysetreni"]["delta"])
print(msg)
client.publish("home/scroller",M(msg,color=color,speed=100))
time.sleep(10)
broker_address="172.16.1.254"
#broker_address="iot.eclipse.org"
print("creating new instance")
client = mqtt.Client("NUC-client-home") #create new instance
print("connecting to broker")
client.connect(broker_address) #connect to broker
print("Subscribing to topic","home/bell/ring")
client.subscribe("home/bell/ring")
client.on_message=on_message
client.loop_start()
print_time()
time.sleep(5)
print_time("%d.%m",color=2)
time.sleep(5)
print_weather(color=1)
time.sleep(12)
print_name()
time.sleep(7)
print_covid()
time.sleep(10)
client.loop_stop()