P1 poort slimme meter – Data naar Node-Red (MQTT)
Dit is een python voorbeeldscript om een P1 poort uit te lezen met een raspberry pi en serial <> USB interface en data via MQTT te verzenden naar Node-Red.
Zet onderstaand script in de Raspberry Pi home folder en maak een @reboot commando aan in de CRON
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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
#!/usr/bin/env python # Python script om P1 telegram weer te geven import datetime import re import serial import paho.mqtt.client as paho broker="controller.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() kwhtot = 0 kwhoud = 0 kwhverschil = 0 gastot = 0 gasoud = 0 gasverschil = 0 # Telegram # # /KMP5 KA6U001660740912 # # 0-0:96.1.1(204B413655303031363630373430393132) # 1-0:1.8.1(13629.373*kWh) # 1-0:1.8.2(14700.866*kWh) # 1-0:2.8.1(00000.000*kWh) # 1-0:2.8.2(00000.000*kWh) # 0-0:96.14.0(0001) # 1-0:1.7.0(0000.64*kW) # 1-0:2.7.0(0000.00*kW) # 0-0:96.13.1() # 0-0:96.13.0() # 0-1:24.1.0(3) # 0-1:96.1.0(3238313031353431303037343732343132) # 0-1:24.3.0(180428140000)(08)(60)(1)(0-1:24.2.1)(m3) # (03862.650) # ! while True: ser.open() checksum_found = False gasflag = 0 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) if re.match(b'(?=1-0:1.8.1)', telegram_line): #1-0:1.8.1 - Hoog tarief / 1-0:1.8.1(13579.595*kWh) kwh1 = telegram_line[10:-5] # Knip het kWh gedeelte eruit (13579.595) if re.match(b'(?=1-0:1.8.2)', telegram_line): #1-0:1.8.2 - Laag tarief / 1-0:1.8.2(14655.223*kWh) kwh2 = telegram_line[10:-5] # Knip het kWh gedeelte eruit (14655.223) if gasflag == 1: gas = telegram_line[1:-1] gasflag = 0 if re.match(b'(?=0-1:24.3.0)', telegram_line): #0-1:24.3.0 - Gasverbruik gasflag = 1 # Check wanneer het uitroepteken ontavangen wordt (einde telegram) if re.match(b'(?=!)', telegram_line): checksum_found = True ser.close() ####################################### if kwhoud < 1: #Script eerste keer opgestart, sla waarde op kwhoud = kwhtot kwhtot = float(kwh1) + float(kwh2) kwhverschil = round(float(kwhtot) - float(kwhoud), 3) if gasoud < 1: #Script eerste keer opgestart, sla waarde op gasoud = gas gasouduur = gas gasverschil = round(float(gas) - float(gasoud), 3) gasverschiluur = round(float(gas) - float(gasouduur), 3) # Reset tellers tijd = str(datetime.datetime.now().time())[0:-10] tijdmin = str(datetime.datetime.now().time())[3:-10] if tijd == "00:00": # Reset de counter van de dag kwhoud = kwhtot gasoud = gas if tijdmin == "00": # Reset de counter van het uur gasouduur = gas ###################################### # MQTT PUBLISH ###################################### client1.publish("elektra\w", watt) client1.publish("elektra\kwh", kwhverschil) client1.publish("gas", gasverschil) client1.publish("gas\uur", gasverschiluur) |
Node-Red
In Node-Red maak je dan vervolgens MQTT nodes aan om de data weer uit te lezen:
In de functie kan je omrekenen naar verbruik in euro:
1 2 3 4 |
kwhprijs = 0.199 //19.90 cent calc = parseFloat(msg.payload * kwhprijs).toFixed(2); var msg1 = {payload:calc}; return [msg1]; |
Node-Red project in clipboard:
1 |
[{"id":"f0d0157e.2d2e98","type":"mqtt in","z":"87f0e90a.ec6a88","name":"Verbruik Elektra W","topic":"elektra\\w","qos":"2","broker":"3feb27f3.671af8","x":130,"y":60,"wires":[["b41bb16c.eebf4","ca84983.3ad3c68"]]},{"id":"b41bb16c.eebf4","type":"ui_gauge","z":"87f0e90a.ec6a88","name":"VerbruikMeter","group":"230fa75d.ba47d8","order":1,"width":0,"height":0,"gtype":"gage","title":"Verbruik live","label":"W","format":"{{value}}","min":0,"max":"4000","colors":["#00b500","#e6e600","#ca3838"],"seg1":"1500","seg2":"2500","x":360,"y":60,"wires":[]},{"id":"b0e9c4d.b6a4d38","type":"mqtt in","z":"87f0e90a.ec6a88","name":"Verbruik Elektra KWh","topic":"elektra\\kwh","qos":"2","broker":"3feb27f3.671af8","x":140,"y":180,"wires":[["d8b151bf.60439","a430392.1f5b7c8"]]},{"id":"d8b151bf.60439","type":"ui_text","z":"87f0e90a.ec6a88","group":"230fa75d.ba47d8","order":3,"width":0,"height":0,"name":"Verbruik KWh","label":"Verbr. vandaag","format":"{{msg.payload}} KWh","layout":"row-spread","x":360,"y":180,"wires":[]},{"id":"a430392.1f5b7c8","type":"function","z":"87f0e90a.ec6a88","name":"KWh naar Euro","func":"kwhprijs = 0.199 //19.90 cent\ncalc = parseFloat(msg.payload * kwhprijs).toFixed(2);\nvar msg1 = {payload:calc};\nreturn [msg1];","outputs":1,"noerr":0,"x":360,"y":240,"wires":[["7d288c0a.a707b4"]]},{"id":"7d288c0a.a707b4","type":"ui_text","z":"87f0e90a.ec6a88","group":"230fa75d.ba47d8","order":4,"width":0,"height":0,"name":"Verbruik Euro","label":"Kosten vandaag","format":"{{msg.payload}} Euro","layout":"row-spread","x":560,"y":240,"wires":[]},{"id":"5ddf1550.4435bc","type":"mqtt in","z":"87f0e90a.ec6a88","name":"Verbruik Gas","topic":"gas\\uur","qos":"2","broker":"3feb27f3.671af8","x":110,"y":320,"wires":[["ebcbf689.1191f8"]]},{"id":"ebcbf689.1191f8","type":"ui_gauge","z":"87f0e90a.ec6a88","name":"VerbruikMeter","group":"2c849d60.92a9d2","order":1,"width":0,"height":0,"gtype":"gage","title":"Verbruik laatste uur","label":"m3","format":"{{value}}","min":0,"max":"2","colors":["#00b500","#e6e600","#ca3838"],"seg1":"1500","seg2":"2500","x":360,"y":320,"wires":[]},{"id":"781aafd2.4fe4f","type":"ui_chart","z":"87f0e90a.ec6a88","name":"VerbruikGrafiek","group":"2c849d60.92a9d2","order":2,"width":0,"height":0,"label":"","chartType":"line","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"0","ymax":"3","removeOlder":"4","removeOlderPoints":"","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"useOldStyle":false,"x":360,"y":420,"wires":[[],[]]},{"id":"4e4d1d32.66e4d4","type":"mqtt in","z":"87f0e90a.ec6a88","name":"Verbruik Gas","topic":"gas","qos":"2","broker":"3feb27f3.671af8","x":110,"y":480,"wires":[["e9101190.84488","3961d12.60bcd2e","781aafd2.4fe4f"]]},{"id":"e9101190.84488","type":"ui_text","z":"87f0e90a.ec6a88","group":"2c849d60.92a9d2","order":0,"width":0,"height":0,"name":"Verbruik Gas m3","label":"Verbr. vandaag","format":"{{msg.payload}} m3","layout":"row-spread","x":370,"y":480,"wires":[]},{"id":"3961d12.60bcd2e","type":"function","z":"87f0e90a.ec6a88","name":"m3 naar Euro","func":"gasprijs = 0.658 //0.66 cent\ncalc = parseFloat(msg.payload * gasprijs).toFixed(2);\nvar msg1 = {payload:calc};\nreturn [msg1];","outputs":1,"noerr":0,"x":360,"y":540,"wires":[["ce278487.2b33b8"]]},{"id":"ce278487.2b33b8","type":"ui_text","z":"87f0e90a.ec6a88","group":"2c849d60.92a9d2","order":0,"width":0,"height":0,"name":"Verbruik Euro","label":"Kosten vandaag","format":"{{msg.payload}} Euro","layout":"row-spread","x":560,"y":540,"wires":[]},{"id":"ca84983.3ad3c68","type":"ui_chart","z":"87f0e90a.ec6a88","name":"VerbruikGrafiek","group":"230fa75d.ba47d8","order":2,"width":0,"height":0,"label":"","chartType":"line","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"","ymax":"","removeOlder":"4","removeOlderPoints":"","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"useOldStyle":false,"x":360,"y":120,"wires":[[],[]]},{"id":"3feb27f3.671af8","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":"230fa75d.ba47d8","type":"ui_group","z":"","name":"Elektra","tab":"84c7a07.a96616","order":8,"disp":true,"width":"6","collapse":false},{"id":"2c849d60.92a9d2","type":"ui_group","z":"","name":"Gas","tab":"84c7a07.a96616","order":9,"disp":true,"width":"6","collapse":false},{"id":"84c7a07.a96616","type":"ui_tab","z":"","name":"Verlichting","icon":"dashboard","order":1}] |
Dash view: