nrocco / smeterd

Read P1 smart meter packets in Python
MIT License
31 stars 19 forks source link

How to modify script to read a network socket instead of serial port? #9

Closed ThinkPadNL closed 7 years ago

ThinkPadNL commented 7 years ago

Hi,

Great code, works fine with a USB-Serial cable. However, i have moved my server out of my metercabinet, so i can't use a USB-Serial adapter anymore because of the distance.

I found this: http://romix.macuser.nl/ and made it with a ESP8266 i had lying around. When i use putty (with 'telnet' or 'raw' setting) i can connect to the ESP8266 and every 10s i receive a new P1-telegram. Nice!

Unfortunately my Python skills are equal to zero, would it be possible to create a variant of the script that can listen to a socket instead of a tty? See also here: https://gathering.tweakers.net/forum/list_message/48850001#48850001

nrocco commented 7 years ago

Hi @ThinkPadNL,

I read this thread https://gathering.tweakers.net/forum/list_message/48850001#48850001 And the easiest way for now is to use smeterd as a python module instead.

import socket
from smeterd.meter import P1Packet

# creates socket object
s = socket.socket(socket.AF_INET,
                  socket.SOCK_STREAM)

host = '192.168.4.17'
port = 8088 

s.connect((host, port))

raw_telegram = s.recv(1024) # msg can only be 1024 bytes long
telegram = P1Packet(raw_telegram)

print(packet['gas']['total'])
ThinkPadNL commented 7 years ago

Thanks, i will try something with that when i have some time. However the thing i posted there was just some fooling around by me (with no Python skills). The actual script i am using is this one which posts the data to InfluxDB like you can see here.

My goal is to change that script so it reads from a socket instead of the serial port.

Haringstad commented 7 years ago

Using this option, I do try to crreate an interface between my HomeWizard and Domoticz. Yup, I was stupid enough to invest in a HW, and not in something that could be easy used with a Raspi.

The Homewizard produces a telegram like this:

/KFM5KAIFA-METER

1-3:0.2.8(42)
0-0:1.0.0(--modified-by-me)
0-0:96.1.1(------modified-by-me)
1-0:1.8.1(003615.113*kWh)
1-0:1.8.2(002968.771*kWh)
1-0:2.8.1(000000.000*kWh)
1-0:2.8.2(000000.000*kWh)
0-0:96.14.0(0002)
1-0:1.7.0(00.217*kW)
1-0:2.7.0(00.000*kW)
0-0:96.7.21(00006)
0-0:96.7.9(00004)
1-0:99.97.0(2)(0-0:-----)(------)(-----)(--------)(------)
1-0:32.32.0(00000)
1-0:32.36.0(00000)
0-0:96.13.1()
0-0:96.13.0()
1-0:31.7.0(001*A)
1-0:21.7.0(00.217*kW)
1-0:22.7.0(00.000*kW)
0-1:24.1.0(003)
0-1:96.1.0(-----------------------------modified by me ------------------------)
0-1:24.2.1(number of meter)(01605.229*m3)
!AAA9

And I use this script:

#!/usr/bin/env python
import sys, os, time, json, requests
from smeterd.meter import P1Packet

r = requests.get('http://homewizard/password/el/get/0/telegram')
raw_telegram = r.text
strip_telegram = raw_telegram.strip()
telegram = P1Packet(strip_telegram)
print raw_telegram
print(telegram['kwh']['current_consumed'])

And I only get "None" or a 0 (zero) as output...... Can't seem to see where I go wrong....

nrocco commented 7 years ago

Hi @Haringstad,

It is difficult to tell what exactly goes wrong. In the meantime I have uploaded a new version of smeterd to pypi, version 2.6.1. Could you give that one a try?

The P1Packet class expects the raw packet from the serial port (as binary data) including the original line endings.

So I am not surprised if any of that is lost along the way from your homewizard, over http to the above snippet.

Haringstad commented 7 years ago

Hi @nrocco,

Thanks for the update! And I agree upon your suspicion that the binary is lost the moment that I get it from the homewizard. Hm.. interesting... Let me see if I can actually change that? And I will update! Will keep you posted! And again, thanks for your input!

Haringstad commented 7 years ago

Hi @nrocco The output in my original post, is exactly what I do receive from the homewizard. So, I have to transform that back to binary. Question remains: Which type? The real old-skool 00000001 type, or the base64? Would be fun if I could work this out, so no code-change to smeterd is needed, and still hw owners can use Domoticz :+1:

ThinkPadNL commented 7 years ago

I managed to get it working. Not with your library, but with help from the author of dsmr-reader. See https://github.com/dennissiemensma/dsmr-reader/issues/187 for code and such.

The code posted there can be converted to serve a different purpose quite easily by someone with Python-skills i think. Because the question i asked in this repository is not needed anymore for me, i will close this ticket. Feel free to use the code posted in the other GitHub project.