Example Using InfluxDB: Difference between revisions

From lechacal
Jump to navigation Jump to search
(34 intermediate revisions by the same user not shown)
Line 1: Line 1:




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


==Prerequisites==
==Prerequisites==


The RPICT series board must output as CSV for this work.
The RPICT series board must output using format=3 (default) for this to work.


Document edited using influxdb version 1.2 and python-influxdb version 4.0.
Document edited using influxdb version 1.2 and python-influxdb version 4.0.
We will assume here a setup having:
* Server Side. The unit running the influxdb service and waiting for input data.
* Raspbberypi Side. The unit connected to the RPICT and forwarding data to the server above.
We have covered the case where the Raspberrypi is both the Influxdb/Grafana server and also connected to the RPICT in this guide below.
[[Install an InfluxDB Grafana stack on a Raspberrypi]]


==Installation==
==Installation==


===Server side hosting InfluxDB===
===Server side hosting InfluxDB===
Install InfluxDB and Chronograf as recommended. [https://portal.influxdata.com/downloads#influxdb This link] provides compiled executables.
Install InfluxDB as recommended. [https://portal.influxdata.com/downloads#influxdb This link] provides compiled executables.


Github link is [https://github.com/influxdata/influxdb here].
Github link is [https://github.com/influxdata/influxdb here].


We will call this server hostname '''myserver''' for the rest of our example below.
We will call this server hostname '''myserver''' for our example below.
 
For our example we need to create a database within influxdb.
From a terminal on the influxdb hosting server issue the command below:
 
$ curl -i -XPOST http://localhost:8086/query --data-urlencode "q=CREATE DATABASE db01"
 
Replace the db01 in the above with whatever you please.


===Raspberrypi side===
===Raspberrypi side===
Install the python biding to influxdb. Available from [https://github.com/influxdata/influxdb-python github here].
We are proposing a basic script template which can be installed as below.
 
sudo apt-get install python-requests
wget lechacal.com/RPICT/tools/lcl-gateway.py.zip
unzip lcl-gateway.py.zip
wget lechacal.com/RPICT/tools/gateway.conf
sudo mv lcl-gateway.py /usr/local/bin/
sudo mv gateway.conf /etc/
Edit the gateway.conf file to reflect your own setting. Content will be self explanatory.
 
sudo nano /etc/gateway.conf
 
Insert the RPICT and run
 
lcl-gateway.py -d


Do not be tempted by using apt-get install! This installs an outdated version. Use pip or download the package from github and install using
===Run the Gateway script in background===
sudo setup.py install


Check version using
You might need to keep the previous gateway program running in the background.
python
import influxdb
print influxdb.__version__


Also install python serial if not done already.
Install Screen.
  sudo apt-get install python-serial
  sudo apt-get install screen


===Testing===
Then run
Use some of the example from [https://docs.influxdata.com/influxdb/v1.2/guides/writing_data/ influxdb documentation] to test the correct communication between the server and raspberrypi.
screen -d -m lcl-gateway.py


Also make sure you can access Chronograf by going to
To stop it just run screen (provided you are only running one screen at a time).
  http://myserver:8888
  screen -r


==Python Script==
Press ctrl ^C to kill the program.<br>
Press Ctrl ^A then D to let it run and detach from the screen session.


[[File:Screenshot_2017-03-22_13-27-36.png | 300px | right]]
===Run the Gateway script at boot time===


The python script below should be a good starting point to forward any RPICT series data to influxdb.  
Edit the rc.local file.


Copy the content into a file on the raspberrypi and make it executable:
  sudo nano /etc/rc.local
  chmod 755 InfluxDB_forward.py


Make sure you modify the head parameters first.
Then just before the exit 0 line add the following line:
screen -d -m /usr/local/bin/lcl-gateway.py


#!/usr/bin/python
Once the Raspberrypi rebooted you can access the session using
import time
  sudo screen -r
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[0])
                                        }
                                }
                                points.append(point)
                        client.write_points(points)
  except KeyboardInterrupt:
        ser.close()

Revision as of 10:24, 11 March 2020


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

Prerequisites

The RPICT series board must output using format=3 (default) for this to work.

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

We will assume here a setup having:

  • Server Side. The unit running the influxdb service and waiting for input data.
  • Raspbberypi Side. The unit connected to the RPICT and forwarding data to the server above.

We have covered the case where the Raspberrypi is both the Influxdb/Grafana server and also connected to the RPICT in this guide below. Install an InfluxDB Grafana stack on a Raspberrypi

Installation

Server side hosting InfluxDB

Install InfluxDB as recommended. This link provides compiled executables.

Github link is here.

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

For our example we need to create a database within influxdb. From a terminal on the influxdb hosting server issue the command below:

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

Replace the db01 in the above with whatever you please.

Raspberrypi side

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

sudo apt-get install python-requests
wget lechacal.com/RPICT/tools/lcl-gateway.py.zip
unzip lcl-gateway.py.zip
wget lechacal.com/RPICT/tools/gateway.conf
sudo mv lcl-gateway.py /usr/local/bin/
sudo mv gateway.conf /etc/

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

sudo nano /etc/gateway.conf

Insert the RPICT and run

lcl-gateway.py -d

Run the Gateway script in background

You might need to keep the previous gateway program running in the background.

Install Screen.

sudo apt-get install screen

Then run

screen -d -m lcl-gateway.py

To stop it just run screen (provided you are only running one screen at a time).

screen -r

Press ctrl ^C to kill the program.
Press Ctrl ^A then D to let it run and detach from the screen session.

Run the Gateway script at boot time

Edit the rc.local file.

sudo nano /etc/rc.local

Then just before the exit 0 line add the following line:

screen -d -m /usr/local/bin/lcl-gateway.py

Once the Raspberrypi rebooted you can access the session using

sudo screen -r