Node-RED – Voorbeeld – Data via MQTT visualiseren
Op deze pagina vind je een voorbeeld met Node-RED hoe je data dat binnenkomt met MQTT koppelt aan een meter/grafiek.
Wat heb je nodig?
- MQTT (client)
- Python MQTT client (paho)
- Node-RED Dashboard
Stap 1
Ik heb op mijn P1 poort een Raspberry Pi aangesloten en daarop een python script lopen dat zodra een telegram binnenkomt, deze ontrafeld wordt en het vermogen (W) via MQTT wordt doorgegeven naar de broker (waar ook Node-RED op draait).
Configureer:
- Server
- Publish naam
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
#!/usr/bin/env python # Python script om P1 telegram weer te geven import re import serial import paho.mqtt.client as paho broker="raspberrypi.local" port=1883 def on_publish(client,userdata,result): #create function for callback print("data published \n") pass client1=paho.Client("control1") #create client object client1.on_publish = on_publish #assign function to callback client1.connect(broker,port) #establish connection # Seriele poort confguratie ser = serial.Serial() # DSMR 2.2 > 9600 7E1: ser.baudrate = 9600 ser.bytesize = serial.SEVENBITS ser.parity = serial.PARITY_EVEN ser.stopbits = serial.STOPBITS_ONE # DSMR 4.0/4.2 > 115200 8N1: #ser.baudrate = 115200 #ser.bytesize = serial.EIGHTBITS #ser.parity = serial.PARITY_NONE #ser.stopbits = serial.STOPBITS_ONE ser.xonxoff = 0 ser.rtscts = 0 ser.timeout = 12 ser.port = "/dev/ttyUSB0" ser.close() while True: ser.open() checksum_found = False while not checksum_found: telegram_line = ser.readline() # Lees een seriele lijn in. telegram_line = telegram_line.decode('ascii').strip() # Strip spaties en blanke regels #print (telegram_line) #debug if re.match(b'(?=1-0:1.7.0)', telegram_line): #1-0:1.7.0 = Actueel verbruik in kW # 1-0:1.7.0(0000.54*kW) kw = telegram_line[10:-4] # Knip het kW gedeelte eruit (0000.54) watt = float(kw) * 1000 # vermengvuldig met 1000 voor conversie naar Watt (540.0) watt = int(watt) # rond float af naar heel getal (540) print watt # Check wanneer het uitroepteken ontavangen wordt (einde telegram) if re.match(b'(?=!)', telegram_line): checksum_found = True ser.close() ret = client1.publish("verbruik",watt) #publish |
Stap 2
Voeg in Node-RED een MQTT ingang (subscribe) toe, en “abonneer” je op het topic “verbruik”, configureer de MQTT server als localhost (potloodje)
Stap 3
Koppel aan de uitgang van de MQTT topic een meter:
Stap 4
Koppel aan de uitgang van de MQTT topic een grafiek:
Op het dashboard kan je dan je actueel verbruik zien:
Project in Node-RED clipboard:
1 |
[{"id":"af3b914f.da09c","type":"mqtt in","z":"8816a623.365d38","name":"verbruik","topic":"verbruik","qos":"2","broker":"3719830a.30f96c","x":890,"y":240,"wires":[["c96eb704.eff048","501fac51.dabe44"]]},{"id":"c96eb704.eff048","type":"ui_gauge","z":"8816a623.365d38","name":"Vermogen","group":"ecd6bf51.31fa5","order":0,"width":0,"height":0,"gtype":"gage","title":"VermogenMeter","label":"W","format":"{{value}}","min":0,"max":"4000","colors":["#00b500","#e6e600","#ca3838"],"seg1":"1500","seg2":"2500","x":1050,"y":220,"wires":[]},{"id":"501fac51.dabe44","type":"ui_chart","z":"8816a623.365d38","name":"VermogenTabel","group":"ecd6bf51.31fa5","order":0,"width":0,"height":0,"label":"","chartType":"line","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"0","ymax":"4000","removeOlder":"4","removeOlderPoints":"","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"useOldStyle":false,"x":1060,"y":260,"wires":[[],[]]},{"id":"3719830a.30f96c","type":"mqtt-broker","z":"","name":"Local MQTT","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""},{"id":"ecd6bf51.31fa5","type":"ui_group","z":"","name":"GWL","tab":"c1507dbd.860c1","order":1,"disp":true,"width":"6","collapse":false},{"id":"c1507dbd.860c1","type":"ui_tab","z":"","name":"Home","icon":"dashboard","order":1}] |