add MQTT broker to monitor shellies
This commit is contained in:
parent
9cd3c3dc7f
commit
45a314cf9d
|
|
@ -67,6 +67,7 @@ see https://linuxize.com/post/how-to-add-swap-space-on-ubuntu-20-04/
|
|||
# ufw allow ssh
|
||||
# ufw allow http
|
||||
# ufw allow https
|
||||
# ufw allow 1883 # MQTT
|
||||
# ufw enable
|
||||
```
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,10 @@
|
|||
MQTT Broker for shelly, mqtt.nbit.ch, no TLS, Port 1883
|
||||
|
||||
Setup, see https://www.laub-home.de/wiki/Eclipse_Mosquitto_Secure_MQTT_Broker_Docker_Installation#Mosquitto_via_Docker_Compose
|
||||
|
||||
Create User:
|
||||
$ docker-compose exec mosquitto mosquitto_passwd -c /mosquitto/config/mosquitto.passwd mqtt
|
||||
|
||||
|
||||
Idea for Python Script:
|
||||
https://schroederdennis.de/monitoring/tasmota-mqtt-daten-mit-grafana-visualisieren-python-influxdb-script/
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
version: '3.7'
|
||||
|
||||
services:
|
||||
mosquitto:
|
||||
image: eclipse-mosquitto:latest
|
||||
restart: always
|
||||
volumes:
|
||||
- /home/joerg/nbit-mqtt/config:/mosquitto/config
|
||||
- /home/joerg/nbit-mqtt/data:/mosquitto/data
|
||||
- /home/joerg/nbit-mqtt/log:/mosquitto/log
|
||||
environment:
|
||||
TZ: Europe/Zurich
|
||||
network_mode: host
|
||||
tty: true
|
||||
|
||||
mqtt2log:
|
||||
build: mqtt2log-build
|
||||
tty: true
|
||||
volumes:
|
||||
- /home/joerg/nbit-mqtt/data-shellies:/data
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
FROM python:3.10
|
||||
ADD . /code
|
||||
WORKDIR /code
|
||||
RUN pip install --root-user-action ignore -r requirements.txt
|
||||
CMD ["python", "mqtt2log.py"]
|
||||
|
|
@ -0,0 +1,65 @@
|
|||
import re
|
||||
import json
|
||||
from typing import NamedTuple
|
||||
from datetime import datetime
|
||||
|
||||
import paho.mqtt.client as mqtt
|
||||
|
||||
MQTT_ADDRESS = 'mqtt.nbit.ch'
|
||||
MQTT_USER = 'mqtt'
|
||||
MQTT_PASSWORD = 'mqtt7355@'
|
||||
MQTT_TOPIC = 'shellies/events/rpc'
|
||||
MQTT_CLIENT_ID = 'MQTT_Logfile_Bridge'
|
||||
|
||||
def on_connect(client, userdata, flags, rc):
|
||||
""" The callback for when the client receives a CONNACK response from the server."""
|
||||
print('Connected with result code ' + str(rc))
|
||||
client.subscribe(MQTT_TOPIC)
|
||||
|
||||
def _parse_mqtt_message(topic, payload):
|
||||
print("_parse_mqtt_message")
|
||||
payload = json.loads(payload)
|
||||
src = payload.get('src','N/A')
|
||||
if 'params' in payload.keys():
|
||||
ts = payload['params'].get('ts',-1)
|
||||
if 'switch:0' in payload['params'].keys():
|
||||
if 'aenergy' in payload['params']['switch:0'].keys():
|
||||
if 'total' in payload['params']['switch:0']['aenergy'].keys():
|
||||
return "E,%.0f,%s,%.3f" % (ts,src,payload['params']['switch:0']['aenergy']['total'])
|
||||
if 'apower' in payload['params']['switch:0'].keys():
|
||||
return "P,%.0f,%s,%.1f" % (ts,src,payload['params']['switch:0']['apower'])
|
||||
else:
|
||||
return None
|
||||
|
||||
def write2file(msg):
|
||||
now = datetime.now() # current date and time
|
||||
filename = "/data/" + now.strftime("%Y%m%d") + ".log"
|
||||
f = open(filename, "a")
|
||||
f.write(msg + "\n")
|
||||
f.close()
|
||||
|
||||
def on_message(client, userdata, msg):
|
||||
"""The callback for when a PUBLISH message is received from the server."""
|
||||
#print(msg.topic + ' ' + str(msg.payload))
|
||||
result = _parse_mqtt_message(msg.topic, msg.payload.decode('utf-8'))
|
||||
if result is None:
|
||||
print("Couldn't parse sensor data!")
|
||||
print(msg.payload.decode('utf-8'))
|
||||
return
|
||||
else:
|
||||
write2file(result)
|
||||
return
|
||||
|
||||
def main():
|
||||
mqtt_client = mqtt.Client(MQTT_CLIENT_ID)
|
||||
mqtt_client.username_pw_set(MQTT_USER, MQTT_PASSWORD)
|
||||
mqtt_client.on_connect = on_connect
|
||||
mqtt_client.on_message = on_message
|
||||
|
||||
mqtt_client.connect(MQTT_ADDRESS, 1883)
|
||||
mqtt_client.loop_forever()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
print('MQTT to Logfile bridge')
|
||||
main()
|
||||
|
|
@ -0,0 +1 @@
|
|||
paho-mqtt
|
||||
Loading…
Reference in New Issue