/
sdm120.py
33 lines (28 loc) · 871 Bytes
/
sdm120.py
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
#!/usr/bin/python3
import sys
import os
import time
import getopt
import socket
import struct
import binascii
import json
devicenumber = str(sys.argv[1])
seradd = str(sys.argv[2])
sdmid = int(sys.argv[3])
port = int(sys.argv[4])
from pymodbus.client.sync import ModbusTcpClient
client = ModbusTcpClient(seradd, port=port)
#KWH Total Import
resp = client.read_input_registers(0x0048,2, unit=sdmid)
vwh = struct.unpack('>f',struct.pack('>HH',*resp.registers))
vwh2 = float("%.3f" % vwh[0]) * int(1000)
vwh3 = str(vwh2)
#Aktueller Verbrauch
resp = client.read_input_registers(0x000C,2, unit=sdmid)
watti = struct.unpack('>f',struct.pack('>HH',*resp.registers))
watt = int(watti[0])
answer = '{"power":' + str(watt) + ',"powerc":' + str(vwh3) + '} '
f1 = open('/var/www/html/openWB/ramdisk/smarthome_device_ret' + str(devicenumber), 'w')
json.dump(answer,f1)
f1.close()