Created
January 24, 2021 08:00
-
-
Save maxious/30f5bd8b191f048982c71cc67acb8356 to your computer and use it in GitHub Desktop.
Copy zigbee2mqtt weather sensor reading to Met Office/BOM WOW
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
from datetime import datetime | |
import requests | |
import paho.mqtt.client as mqtt | |
from datetime import datetime | |
import json | |
import traceback | |
last_weather = None | |
def on_log(client, userdata, level, buff): | |
print(datetime.now().isoformat(), client, userdata,level,buff) | |
def on_connect(client, userdata, flags, rc): # The callback for when the client connects to the broker | |
print("Connected with result code {0}".format(str(rc))) # Print result of connection attempt | |
#client.subscribe("zigbee2mqtt/0x00a5") | |
#client.subscribe("zigbee2mqtt/#") | |
def on_message(client, userdata, msg): # The callback for when a PUBLISH message is received from the server. | |
payload = str(msg.payload) | |
print("Message received -> " + msg.topic + " " + payload) # Print a received msg | |
try: | |
m_in=json.loads(msg.payload.decode('utf-8')) | |
print(m_in) | |
if type(m_in) == list: | |
print("no data in this message") | |
return | |
print(datetime.now().isoformat(), m_in.get("humidity"), m_in.get("temperature")) | |
temp = m_in.get("temperature") | |
humidity = m_in.get("humidity") | |
pressure = m_in.get("pressure") | |
if not temp and not humidity: | |
print("no weather data in this message") | |
return | |
if last_weather is not None and (datetime.now() - last_weather).seconds < 300: | |
print("too soon weather data", (datetime.now() - last_weather).seconds) | |
return | |
globals()['last_weather'] = datetime.now() | |
time_weather = datetime.now().strftime('%Y-%m-%d %H:%M:%S') | |
url = 'http://wow.metoffice.gov.uk/automaticreading?siteid=f2000c0-0000-e000-0000-000000000&siteAuthenticationKey=00000000' | |
url += '&dateutc='+datetime.utcnow().strftime("%Y-%m-%d+%H:%M:%S").replace(':','%3A') | |
url += '&softwaretype=custom_software' | |
url += '&humidity='+str(humidity) | |
url += '&tempf='+str((temp*1.8)+32) | |
url += '&baromin='+str(pressure*0.02953) | |
print(url) | |
r = requests.get(url) | |
print(r) | |
except: | |
traceback.print_exc() | |
client = mqtt.Client("weather") | |
client.on_connect = on_connect | |
client.on_message = on_message | |
client.on_log = on_log | |
client.username_pw_set("mqtt", "mqtt") | |
client.connect('192.168.1.1') | |
client.loop_forever() # Start networking daemon |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment