Commit 3
This commit is contained in:
parent
9e702af8c6
commit
2e2687d3f7
8 changed files with 252 additions and 57 deletions
|
|
@ -1,9 +1,11 @@
|
|||
import json
|
||||
#import fileinput
|
||||
import serial
|
||||
import datetime
|
||||
|
||||
import paho.mqtt.client as paho
|
||||
|
||||
|
||||
#input = '{"model":"Oregon-THGR810","id":226,"channel":10,"battery_ok":0,"temperature_C":19.7,"humidity":24,"protocol":"Oregon Scientific Weather Sensor"}'
|
||||
SKIP_KEYS = [ "type", "model", "subtype", "channel", "id", "mic", "mod", "freq", "sequence_num", "message_type", "exception", "raw_msg", "protocol", "duration", "sync", "flags", "status" ]
|
||||
TOPIC_KEYS = [ "type", "model", "subtype", "channel", "id" ]
|
||||
|
|
@ -11,10 +13,16 @@ TOPIC_KEYS = [ "type", "model", "subtype", "channel", "id" ]
|
|||
#default="devices[/type][/model][/subtype][/channel][/id]"
|
||||
prefix = "home/rtl_433"
|
||||
|
||||
broker = "192.168.67.1"
|
||||
broker = "127.0.0.1"
|
||||
port = 1883
|
||||
|
||||
altitude = 150
|
||||
#altitude = 150
|
||||
altitude = 210
|
||||
|
||||
rainref = -1
|
||||
rainlast = -1
|
||||
rainrefday = -1
|
||||
rainday = -1
|
||||
|
||||
serialdev = "/dev/ttyACM0"
|
||||
serialspeed = 115200
|
||||
|
|
@ -43,11 +51,15 @@ def publish(jsonin, prefix_device):
|
|||
def on_connect(client, userdata, flags, reason_code, properties):
|
||||
print(f"Connected to MQTT")
|
||||
client.subscribe(prefix+"/cmd")
|
||||
client.subscribe(prefix+"/rain_reset")
|
||||
|
||||
def on_message(client, userdata, msg):
|
||||
global rainref
|
||||
print(msg.topic+" "+str(msg.payload))
|
||||
if(str(msg.topic) == prefix+"/cmd"):
|
||||
ser.write(msg.payload)
|
||||
if(str(msg.topic) == prefix+"/rain_reset"):
|
||||
rainref = rainlast
|
||||
|
||||
mqtt=paho.Client(paho.CallbackAPIVersion.VERSION2)
|
||||
mqtt.on_connect = on_connect
|
||||
|
|
@ -73,6 +85,23 @@ while True:
|
|||
if 'pressure' in data:
|
||||
data['pressure'] = get_sealevel_pressure(data['pressure'], altitude, data['temperature_C'])
|
||||
data['pressure'] = round(data['pressure'], 1)
|
||||
if 'rain_mm' in data:
|
||||
if rainref == -1 or data['rain_mm'] < rainlast:
|
||||
rainref = data['rain_mm']
|
||||
rainrefday = data['rain_mm']
|
||||
rainlast = rainref
|
||||
rainday = datetime.datetime.now().day
|
||||
data['rain_local'] = 0
|
||||
data['rain_day'] = 0
|
||||
else:
|
||||
rainlast = data['rain_mm']
|
||||
data['rain_local'] = round(rainlast - rainref, 2)
|
||||
if datetime.datetime.now().day != rainday:
|
||||
rainday = datetime.datetime.now().day
|
||||
rainrefday = data['rain_mm']
|
||||
data['rain_day'] = 0
|
||||
else:
|
||||
data['rain_day'] = round(data['rain_mm'] - rainrefday,2)
|
||||
|
||||
mqtt.publish(prefix+'/events', json.dumps(data))
|
||||
prefix_device = generate_topic(data)
|
||||
|
|
|
|||
|
|
@ -71,34 +71,46 @@ def on_message(mqttc, obj, msg):
|
|||
tags[t] = str(jsonin[t])
|
||||
|
||||
measures = ""
|
||||
measuresnb = 0
|
||||
|
||||
if 'temperature_C' in jsonin:
|
||||
value = float(jsonin['temperature_C'])
|
||||
measures += create_measure("temperature", currentts, tags, value)
|
||||
if 'temperature' not in recorddb[ref] or recorddb[ref]['temperature'] != value:
|
||||
measures += create_measure("temperature", currentts, tags, value)
|
||||
measuresnb += 1
|
||||
recorddb[ref]['temperature'] = value
|
||||
if 'humidity' in jsonin:
|
||||
value = int(jsonin['humidity'])
|
||||
measures += create_measure("humidity", currentts, tags, value)
|
||||
if 'humidity' not in recorddb[ref] or recorddb[ref]['humidity'] != value:
|
||||
measures += create_measure("humidity", currentts, tags, value)
|
||||
measuresnb += 1
|
||||
recorddb[ref]['humidity'] = value
|
||||
if 'moisture' in jsonin:
|
||||
value = int(jsonin['moisture'])
|
||||
measures += create_measure("moisture", currentts, tags, value)
|
||||
if 'moisture' not in recorddb[ref] or recorddb[ref]['moisture'] != value:
|
||||
measures += create_measure("moisture", currentts, tags, value)
|
||||
measuresnb += 1
|
||||
recorddb[ref]['moisture'] = value
|
||||
if 'rain_mm' in jsonin:
|
||||
value = float(jsonin['rain_mm'])
|
||||
measures += create_measure("rain", currentts, tags, value)
|
||||
if 'rain' not in recorddb[ref] or recorddb[ref]['rain'] != value:
|
||||
measuresnb += 1
|
||||
recorddb[ref]['rain'] = value
|
||||
if 'pressure' in jsonin:
|
||||
value = float(jsonin['pressure'])
|
||||
measures += create_measure("pressure", currentts, tags, value)
|
||||
if 'pressure' not in recorddb[ref] or recorddb[ref]['pressure'] != value:
|
||||
measures += create_measure("pressure", currentts, tags, value)
|
||||
recorddb[ref]['humidity'] = value
|
||||
measuresnb += 1
|
||||
recorddb[ref]['pressure'] = value
|
||||
if 'battery_ok' in jsonin:
|
||||
value = int(jsonin['battery_ok'])
|
||||
measures += create_measure("battery_ok", currentts, tags, value)
|
||||
if 'battery_ok' not in recorddb[ref] or recorddb[ref]['battery_ok'] != value:
|
||||
measures += create_measure("battery_ok", currentts, tags, value)
|
||||
measuresnb += 1
|
||||
recorddb[ref]['battery_ok'] = value
|
||||
|
||||
if len(measures) > 0:
|
||||
if measuresnb > 0:
|
||||
recorddb[ref]["lastsend"] = currentts
|
||||
#print(measures)
|
||||
try:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue