-
Notifications
You must be signed in to change notification settings - Fork 0
/
pollucom.py
81 lines (72 loc) · 2.95 KB
/
pollucom.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
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
#!/usr/bin/python
### original script by 93schlucko
### https://forum-raspberrypi.de/forum/thread/57389-sensus-pollucom-e-ueber-pymeterbus-auslesen/?postID=543096#post543096 ###
# -*- coding: utf-8 -*-
import serial, time
import meterbus
import requests
import paho.mqtt.client as paho
import os
import subprocess
#Get environment variables
#mandatory
esp_ip = os.environ['ESP_IP']
mqtt_broker = os.environ['MQTT_BROKER']
mqtt_topic = os.environ['MQTT_TOPIC']
#optional
if "MQTT_PORT" in os.environ:
mqtt_port = int(os.environ['MQTT_PORT'])
else:
mqtt_port = int(1883)
if "MQTT_CLIENT" in os.environ:
mqtt_client = os.environ['MQTT_CLIENT']
else:
mqtt_client = 'PolluComE'
if "MQTT_USER" in os.environ:
mqtt_user = os.environ['MQTT_USER']
if "MQTT_PWD" in os.environ:
mqtt_pwd = os.environ['MQTT_PWD']
#Serial Settings
serial_port = './ttyV666'
address = 0
#Check if socat is connected to esp-link, if not connect
if os.path.exists(serial_port):
print("socat is connected")
else:
print("socat not connected")
cmd_str = "socat pty,link={} tcp:{}:23 &".format(serial_port,esp_ip)
subprocess.run(cmd_str, shell=True)
time.sleep(0.5)
# start communication with 8N1, DTR enabled
url = 'http://{}/console/fmt?fmt=8N1'.format(esp_ip) #tell esp-link to use 8N1
requests.get(url)
ser = serial.Serial(serial_port, 2400, 8, 'N', 1, 0.5)
ser.dsrdtr=True
# send wake up sequence
ser.write(b"\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55")
time.sleep(0.130)
ser.read()
response = ser.readline()
# switch to 8E1
url = 'http://{}/console/fmt?fmt=8E1'.format(esp_ip) #tell esp-link to use 8E1
requests.get(url)
ser.parity = serial.PARITY_EVEN
# SND_NKE to reset the communication
meterbus.send_ping_frame(ser, address)
frame = meterbus.load(meterbus.recv_frame(ser, 1))
assert isinstance(frame, meterbus.TelegramACK)
# request data from meter
meterbus.send_request_frame(ser, address)
frame = meterbus.load(meterbus.recv_frame(ser, meterbus.FRAME_DATA_LENGTH))
assert isinstance(frame, meterbus.TelegramLong)
# print data
print(frame.to_JSON())
#publish via MQTT
msg = frame.to_JSON()
def on_publish(client,userdata,result): #create function for callback
print("data published \n")
pass
client1= paho.Client(mqtt_client) #create client object
client1.on_publish = on_publish #assign function to callback
client1.connect(mqtt_broker,mqtt_port) #establish connection
ret= client1.publish(mqtt_topic,msg) #publish