Begin to add binary support for swisscom receiver

This commit is contained in:
Nico Schottelius 2016-11-12 16:09:33 +01:00
commit acf5f05b81
7 changed files with 84 additions and 18 deletions

71
python/adeunisrf.py Normal file
View file

@ -0,0 +1,71 @@
#!/usr/bin/env python3
# Send lora packet to node-red when being triggered by postgresql
# Nico Schottelius <nico.schottelius -at- ungleich.ch>
# 2016-11-02
# GPLv3+
# Bit 6 = 1 : accelerometer was triggered
#
# Bit 5 = 0 : BTN1 was NOT triggered
#
# Bit 4 = 1 : GPS info is present
#
# Bit 3 = 1 : MAC Down Counter is present
#
# Bit 2 = 1 : MAC up Counter is present
#
# Bit 1 = 1 : Battery voltage information is present
#
# Bit 0 = 0 : RSSI + SNR information is NOT present
#
# * Byte 02 : 1E = 0001 1110 (Temperature in °C, signed in twos complement) = 30 °C
#
# * Byte 03 : 47 = GPS Latitude, degrees
#
# * Byte 04 : 22 = GPS Latitude, minutes
#
# * Byte 05 : 49 = GPS Latitude, minutes
#
# * Byte 06 : 60 = GPS Latitude, minutes + Hemisphere (0 = North)
#
# * Byte 07 : 00 = GPS Longitude, minutes
#
# * Byte 08 : 83 = GPS Longitude, minutes
#
# * Byte 09 : 18 = GPS Longitude, minutes
#
# * Byte 10 : 70 = GPS Longitude, minutes + Hemisphere (0 = East)
#
# * Byte 11 : none (because Byte 12 is starting with a 0 and not 1, 2 or 3)
#
# * Byte 12 : 08 = Uplink frame counter
#
# * Byte 13 : 08 = Downlink frame counter
#
# * Byte 14 : 0E = MSB Battery voltage )
#
# * Byte 15 : FD = LSB Battery voltage ) è 3.837 V
#
# * Byte 16 : none
#
# * Byte 17 : none
# should be: 46.969943, 9.038999
import binascii
def decode_pkg(pkg):
return binascii.a2b_hex(s)
def has_gps(pkg):
return pkg[0] & 0x10
def gpsdata(pkg):
if not has_gps(pkg):
return (0,0)
if __name__ == '__main__':
decode_pkg()

40
python/lorautil.py Normal file
View file

@ -0,0 +1,40 @@
# Helper functions for various Lora receivers
# Nico Schottelius <nico.schottelius -at- ungleich.ch>
# 2016-11-02
# GPLv3+
import psycopg2
import json
import logging
log = logging.getLogger("lorautil")
log.setLevel(logging.DEBUG)
dbname="lorawan"
def db_notify(provider, payload='', deveui=''):
notify="{}:{}".format(deveui, payload)
log.debug("Notify: {} {}".format(provider, notify))
try:
conn = psycopg2.connect("dbname={}".format(dbname))
cursor = conn.cursor()
cursor.execute("select pg_notify (%s, %s)", (provider, notify))
cursor.connection.commit()
except Exception as e:
log.error("DB Notify failed: %s" % e)
def db_insert_json(provider, data, payload='', deveui=''):
try:
conn = psycopg2.connect("dbname={}".format(dbname))
cursor = conn.cursor()
cursor.execute("insert into packets values (DEFAULT, DEFAULT, %s, %s, %s, %s)", (provider, data, payload, deveui))
cursor.connection.commit()
conn.close()
except Exception as e:
log.error("DB Insert failed: %s" % e)
def jsonToDict(data):
return json.loads(data)

43
python/loriot-receiver.py Normal file
View file

@ -0,0 +1,43 @@
#!/usr/bin/env python3
import ssl
import websocket
import json
import os
import psycopg2
import lorautil
class Loriot():
def __init__(self):
ws = websocket.WebSocket()
def connect(self):
tokenurl = os.environ['LORIOT_URL']
self.ws = websocket.create_connection(tokenurl, sslopt={"cert_reqs": ssl.CERT_NONE})
def listen(self):
while True:
result = self.ws.recv()
print(result)
jdata = lorautil.jsonToDict(result)
eui = self.devEUI(jdata)
try:
payload = get_payload(jdata)
except Exception:
payload = ""
lorautil.db_insert_json("loriot", result, payload=payload, deveui=eui)
lorautil.db_notify("loriot", payload=payload, deveui=eui)
def devEUI(self, data):
return data['EUI']
def get_payload(self, data):
return bytes.fromhex(data['payload']).decode('utf-8')
if __name__ == '__main__':
l = Loriot()
l.connect()
l.listen()

48
python/notify.py Normal file
View file

@ -0,0 +1,48 @@
#!/usr/bin/env python3
# Send lora packet to node-red when being triggered by postgresql
# Nico Schottelius <nico.schottelius -at- ungleich.ch>
# 2016-11-02
# GPLv3+
import select
import psycopg2
import psycopg2.extensions
import websocket
import adeunisrf
channels = [ "loriot", "swisscom", "ttn" ]
def to_nodered(provider, data):
ws = websocket.create_connection("ws://localhost:1880/{}".format(provider))
ws.send("%s" % data)
ws.close()
def wait_for_pkg(conns):
readable, writable, exceptional = select.select(conns,[],[])
for conn in readable:
conn.poll()
while conn.notifies:
notify = conn.notifies.pop(0)
print("Got NOTIFY: {} {} {}".format(notify.pid, notify.channel, notify.payload))
to_nodered(notify.channel, notify.payload)
if __name__ == '__main__':
conns = []
for channel in channels:
conn = psycopg2.connect("dbname=lorawan")
conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
curs = conn.cursor()
curs.execute("LISTEN {};".format(channel))
conns.append(conn)
print("Waiting for notifications on channel {}".format(channel))
while True:
wait_for_pkg(conns)

View file

@ -0,0 +1 @@
create table packets ( id SERIAL, received_dt timestamp default now(), provider varchar(128), packet json, payload varchar(128), deveui varchar(128) );

View file

@ -0,0 +1,50 @@
#!/usr/bin/env python3
import urllib
import psycopg2
import websocket
from http.server import BaseHTTPRequestHandler, HTTPServer
import re
import json
import pprint
import lorautil
# HTTPRequestHandler class
class testHTTPServer_RequestHandler(BaseHTTPRequestHandler):
def do_POST(self):
length = int(self.headers['Content-Length'])
post_data = self.rfile.read(length).decode('utf-8')
payload = self.payload_hex(post_data)
deveui = self.get_deveui(post_data)
# Try to decode to unicode
try:
payload = self.data_to_unicode(payload)
except UnicodeDecodeError:
pass
print("deveui/payload: {}:{}".format(deveui, payload))
# And insert into the db
lorautil.db_insert_json("swisscom", post_data, payload, deveui)
lorautil.db_notify("swisscom", payload, deveui)
def payload_hex(self, data):
mydict = lorautil.jsonToDict(data)
return mydict['DevEUI_uplink']['payload_hex']
def data_to_unicode(self, myhex):
return bytes.fromhex(myhex).decode('utf-8')
def get_deveui(self, data):
mydict = lorautil.jsonToDict(data)
eui = mydict['DevEUI_uplink']['DevEUI']
return eui
if __name__ == '__main__':
server_address = ('0.0.0.0', 8000)
httpd = HTTPServer(server_address, testHTTPServer_RequestHandler)
print('running server...')
httpd.serve_forever()

66
python/ttn-receiver.py Normal file
View file

@ -0,0 +1,66 @@
# node.py
# Simple Python client to show node activity from ttn MQTT brooker with credentials
# Author: R.Schimmel
# www.schimmel-bisolutions.nl
# first install paho.mqtt.client: pip install paho-mqtt
#
# Enhanced for lorawan in Digital Glarus by Nico Schottelius (nico.schottelius -at- ungleich.ch)
#
import paho.mqtt.client as mqtt
import psycopg2
import json
import base64
import os
import logging
import lorautil
log = logging.getLogger("ttn")
log.setLevel(logging.DEBUG)
#Call back functions
# gives connection message
def on_connect(client,userdata,rc):
print("Connected with result code:"+str(rc))
# subscribe for all devices of user
client.subscribe('+/devices/+/up')
# gives message from device
def on_message(client,userdata,msg):
myjson = msg.payload.decode('utf-8')
mydict = json.loads(myjson)
deveui = mydict['dev_eui']
payload = base64.b64decode(mydict['payload']).decode('utf-8')
log.info("Message received: {}: {}".format(deveui, payload))
lorautil.db_insert_json("ttn", myjson, payload, deveui)
lorautil.db_notify("ttn", payload, deveui)
def on_log(client,userdata,level,buf):
log.debug("message:" + msg)
log.debug("userdata:" + str(userdata))
def on_disconnect(client, userdata, rc):
log.debug("reconnecting...")
client.reconnect()
if __name__ == '__main__':
mqttc= mqtt.Client()
mqttc.on_connect=on_connect
mqttc.on_message=on_message
mqttc.on_disconnect=on_disconnect
print("Connecting to ttn")
username=os.environ['ttn_user']
password=os.environ['ttn_password']
mqttc.username_pw_set(username, password)
mqttc.connect("staging.thethingsnetwork.org",1883,10)
# and listen to server
print("Listening via mqtt")
run = True
while run:
mqttc.loop()