Example Using InfluxDB: Difference between revisions

From lechacal
Jump to navigation Jump to search
No edit summary
 
(25 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 OUTDATED USE THE LINK BELOW INSTEAD=
 
 
[[Forward to Influxdb from RPICT]]
 
 
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 to 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].
Line 18: Line 33:
We will call this server hostname '''myserver''' for our example below.
We will call this server hostname '''myserver''' for our example below.


Also make sure you can access Chronograf going to  
For our example we need to create a database within influxdb.
  http://myserver:8888
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===
We are proposing a basic script template which can be installed as below.
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
  wget lechacal.com/RPICT/tools/lcl-gateway.py.zip
  unzip lcl-gateway.py.zip
  unzip lcl-gateway.py.zip
  wget lechacal.com/RPICT/tools/gateway.conf
  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.
Edit the gateway.conf file to reflect your own setting. Content will be self explanatory.


  nano gateway.conf
  sudo nano /etc/gateway.conf


Insert the RPICT and run
Insert the RPICT and run


  ./lcl-gateway.py
  lcl-gateway.py -d


===Testing===
===Run the Gateway script in background===
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.


===Create Database===
You might need to keep the previous gateway program running in the background.
For our example we need to create a database within influxdb. We propose two way to perform this.


'''with curl'''<br>
Install Screen.
From a terminal on the influxdb hosting server one can issue the command below:
  sudo apt-get install screen
  $ curl -i -XPOST http://localhost:8086/query --data-urlencode "q=CREATE DATABASE mydb"


'''with python'''<br>
Then run
Alternatively from the raspberrypi itself the database can be created as follow
  screen -d -m lcl-gateway.py
$ python
>>> from influxdb import InfluxDBClient
>>> client = InfluxDBClient('myserver', 8086, 'root', 'root', 'mydb')
  >>> client.create_database('mydb')


==Python Script==
To stop it just run screen (provided you are only running one screen at a time).
screen -r


[[File:Screenshot_2017-03-22_13-27-36.png | 300px | right]]
Press ctrl ^C to kill the program.<br>
Press Ctrl ^A then D to let it run and detach from the screen session.


The python script below should be a good starting point to forward any RPICT series data to influxdb.
===Run the Gateway script at boot time===


Copy the content into a file on the raspberrypi and make it executable:
Edit the rc.local file.
$ chmod 755 InfluxDB_forward.py


Make sure you modify the head parameters first.
sudo nano /etc/rc.local


  #!/usr/bin/python
Then just before the exit 0 line add the following line:
import time
  screen -d -m /usr/local/bin/lcl-gateway.py
import datetime
 
Once the Raspberrypi rebooted you can access the session using
  sudo screen -r
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()

Latest revision as of 19:51, 12 August 2021



THIS IS OUTDATED USE THE LINK BELOW INSTEAD

Forward to Influxdb from RPICT


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