Last active
August 12, 2021 15:07
-
-
Save bphermansson/f9d6e7c8d91b62e362b4c75aa3cc5b0b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
''' | |
This code reads weather data from a Rflink (http://www.rflink.nl) and sends out data via Mqtt. | |
''' | |
import time | |
import serial | |
import paho.mqtt.client as mqtt | |
import json | |
MQTT_HOST = '192.168.1.79' | |
MQTT_PORT = 1883 | |
MQTT_USER = 'emonpi' | |
MQTT_PASS = 'emonpimqtt2016' | |
MQTT_TOPIC = 'Rflink' | |
ser = serial.Serial( | |
port='/dev/ttyACM0', | |
baudrate = 57600, | |
parity=serial.PARITY_NONE, | |
stopbits=serial.STOPBITS_ONE, | |
bytesize=serial.EIGHTBITS, | |
timeout=1 | |
) | |
while 1: | |
x=ser.readline() | |
if len(x) > 60: # Ignore start message from Rflink | |
# Extract data from Rflink and create a Mqtt message | |
x=x.rstrip() | |
print x | |
# Can look like: | |
# 20;01;DKW2012; ID=0022;TEMP=00a6;HUM=69;WINSP=0056;WINGS=00ab;RAIN=2871;WINDIR=0004;BAT=OK; | |
# 20;05;Alecto V2;ID=0069;TEMP=0113;HUM=36;WINSP=0000;WINGS=0000;RAIN=0000;BAT=OK; | |
inputdata = x.split(';') | |
name = inputdata[2] | |
id = inputdata[3].split('=') | |
id = id[1] | |
temp = inputdata[4].split('=') | |
temp = temp[1] | |
hum = inputdata[5].split('=') | |
hum = hum[1] | |
winsp = inputdata[6].split('=') | |
winsp = winsp[1] | |
wings = inputdata[7].split('=') | |
wings = wings[1] | |
rain = inputdata[8].split('=') | |
rain = rain[1] | |
# Add current time | |
ts = int(time.time()) | |
tz = time.timezone / -(60*60)+1 | |
if (name == "DKW2012"): | |
winddir = inputdata[9].split('=') | |
winddir = winddir[1] | |
batt = inputdata[10].split('=') | |
batt = batt[1] | |
else: | |
batt = inputdata[9].split('=') | |
batt = batt[1] | |
winddir = "NA" # This unit doesnt measure wind direction | |
# Convert temp from hex format to human readable | |
''' | |
http://www.rflink.nl/blog2/protref | |
TEMP=9999 => Temperature celcius (hexadecimal), high bit contains negative sign, needs division by 10 (0xC0 = 192 decimal = 19.2 degrees) | |
=> (example negative temperature value: 0x80DC, high bit indicates negative temperature 0xDC=220 decimal the client ''' | |
temphex = temp[1:] # Get last three chars | |
#print temphex | |
tempdec = int(temphex,16) # Convert to decimal | |
#print tempdec # Temp * 10 | |
tempf = tempdec / float(10) # Force tempf to be a float to preserve decimals | |
sign = temp[:1] # Highest bit is set when negative temperature | |
if (sign == '8'): | |
charsign = "-" | |
else: | |
charsign = "" | |
comptemp = charsign + str(tempf) | |
#print comptemp | |
data = {} | |
data['count'] = inputdata[1] | |
data['device'] = inputdata[2] | |
data['id'] = id | |
data['temp'] = comptemp | |
data['hum'] = hum | |
data['winsp'] = winsp | |
data['wings'] = wings | |
data['rain'] = rain | |
data['winddir'] = winddir | |
data['bat'] = batt | |
data['ts'] = ts | |
data['tz'] = tz | |
jsondata = json.dumps(data) | |
full_topic = MQTT_TOPIC + "/" + data['device'] | |
try: | |
mqtt_client = mqtt.Client() | |
mqtt_client.username_pw_set(MQTT_USER, MQTT_PASS) | |
mqtt_client.connect(MQTT_HOST, MQTT_PORT) | |
mqtt_client.publish(full_topic, jsondata) | |
except: | |
print "Unexpected error:", sys.exc_info()[0] |
vargatomy
commented
Aug 12, 2021
via email
Thanks! :)
…On 2021. 08. 12. 10:20, Patrik Hermansson wrote:
***@***.**** commented on this gist.
------------------------------------------------------------------------
Hi!
This is because the script is written for Python 2.x and you probably
are using 3.x. Just add parenthesis to the print statements to make it
work:
print (x)
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<https://gist.github.com/f9d6e7c8d91b62e362b4c75aa3cc5b0b#gistcomment-3857783>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AMC6CGO5P27JUEFYI7A2NX3T4N74VANCNFSM5B7PBM4A>.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&utm_campaign=notification-email>.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment