Example Using InfluxDB

From lechacal
Jump to navigation Jump to search


This is a basic example to forward data to an InfluxDB database. Note this only covers a skeleton to start building your own application.

Prerequisites

The RPICT series board must output as CSV for this to work.

Document edited using influxdb version 1.2 and python-influxdb version 4.0.

Installation

Server side hosting InfluxDB

Install InfluxDB and Chronograf as recommended. This link provides compiled executables.

Github link is here.

We will call this server hostname myserver for our example below.

Also make sure you can access Chronograf going to

http://myserver:8888

Raspberrypi side

We are proposing a basic script template which can be installed as below.

wget lechacal.com/RPICT/tools/lcl-gateway.py.zip
unzip lcl-gateway.py.zip
wget lechacal.com/RPICT/tools/gateway.conf

Edit the gateway.conf file to reflect your own setting. Content will be self explanatory.

nano gateway.conf

Insert the RPICT and run

./lcl-gateway.py

Testing

Use some of the example from influxdb documentation to test the correct communication between the server and raspberrypi.

Create Database

For our example we need to create a database within influxdb. We propose two way to perform this.

with curl
From a terminal on the influxdb hosting server one can issue the command below:

$ curl -i -XPOST http://localhost:8086/query --data-urlencode "q=CREATE DATABASE mydb"

with python
Alternatively from the raspberrypi itself the database can be created as follow

$ python
>>> from influxdb import InfluxDBClient
>>> client = InfluxDBClient('myserver', 8086, 'root', 'root', 'mydb')
>>> client.create_database('mydb')

Python Script

The python script below should be a good starting point to forward any RPICT series data to influxdb.

Copy the content into a file on the raspberrypi and make it executable:

$ chmod 755 InfluxDB_forward.py

Make sure you modify the head parameters first.

#!/usr/bin/python
import time
import datetime


serial_port = "/dev/ttyAMA0" # or "/dev/ttyS0"
baud = 38400

host = 'myserver'
port = 8086
DBNAME = 'mydb'
USER = 'root'
PASSWORD = 'root'

import serial
ser = serial.Serial(serial_port, baud, timeout=1)

from influxdb import InfluxDBClient
client = InfluxDBClient(host, port, USER, PASSWORD, DBNAME)

try:
       while 1:
                response = ser.readline()
                Z = response.split(",")
                if len(Z)>=2:
                        now = datetime.datetime.today()
                        i = 0
                        points = []
                        for z in Z:
                                i += 1
                                point = {
                                        "measurement":'ch%02d'%i,
                                        "time": 1000000000*int(now.strftime('%s')),
                                        "fields": {
                                                "value":float(z)
                                        }
                                }
                                points.append(point)

                        client.write_points(points)

except KeyboardInterrupt:
       ser.close()