AllWize Library
serial2idb.py
Go to the documentation of this file.
1 #
2 #
3 # AllWize - WIZE 2 InfluxDB Bridge
4 #
5 # Listens to messages in the serial port and inserts them to an InfluxDB instance.
6 # Requires pyserial and requests packages: `pip install pyserial requests`.
7 # You might need to give permissions to the dialout group to the current user
8 # if on Linux /Raspberry Pi): `sudo adduser $USER dialout`
9 #
10 # Copyright (C) 2018-2019 by AllWize <github@allwize.io>
11 #
12 # This program is free software: you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation, either version 3 of the License, or
15 # (at your option) any later version.
16 #
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU Lesser General Public License for more details.
21 #
22 # You should have received a copy of the GNU Lesser General Public License
23 # along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #
25 
26 import serial
27 import requests
28 
29 # configuration
30 SERIAL_PORT = "/dev/ttyACM0"
31 SERIAL_BAUD = 115200
32 IDB_ENDPOINT = "http://localhost:8086/write?db=bridge"
33 
34 # parse message
35 def parse(data):
36 
37  parts = data.rstrip().split(",")
38  uid = parts[0]
39  rssi = parts[2]
40  fields = parts[3:]
41  data = "%s,uid=%s rssi=%s" % ("data", uid, rssi)
42  index = 0
43  for f in fields:
44  index = index + 1
45  data = "%s,field%d=%s" % (data, index, f)
46  print("[IDB] Inserting %s" % data)
47  requests.post(url = IDB_ENDPOINT, data = data)
48 
49 # connect to device
50 ser = serial.Serial(SERIAL_PORT, SERIAL_BAUD, timeout=0.5)
51 while True:
52  line = ser.readline()
53  if len(line) and line[0] != "#":
54  print("[SERIAL] Received %s" % line.rstrip())
55  parse(line)