Raspberry pi, InfluxDB, Grafan & Digitalocean as an IOT architecture

Eran Goldman-Malka · April 24, 2018

My brother asked my help, He got some sensors connected to a raspberry pi and he needs to see the graph of the readings.

I chose a simple architecture based on a reporting unit, the raspberry pi, and aggregation server with influxdb and grafana.

The system takes about 15 minutes to install.

Just a small bonus : if you use my referal link you’ll get 10$ credit it’s 2 first months free, and I’ll get 25$ if you stay a customer more than 7 months

https://m.do.co/c/b8f7d3b94794

And for the installation :

  1. Start a droplet in DigitalOcean.
  2. Choose some options :
    1. Ubuntu 17.04
    2. Standard Droplets 1GB 5$/mo
    3. No extra storage
    4. The datacenter that is closest to you
    5. No additional options
    6. Create ssh key, If you don’t know how rtfm
    7. One droplet
    8. Give it a NameAnd press the create button
  3. ssh to your new droplet
    ssh -i [Your\_private\_key] root@[DropletIP]

Install influxdb

Ubutnu :

curl -sL https://repos.influxdata.com/influxdb.key | sudo apt-key add -
source /etc/lsb-release
echo"deb https://repos.influxdata.com/$DISTRIB_ID,$DISTRIB_CODENAME stable"
sudo tee /etc/apt/sources.list.d/influxdb.list

influx :

CREATE DATABASE logs
CREATE USER admin WITH PASSWORD '[TOP-SECRET-ADMIN-PASS]' WITH ALL PRIVILEGES
CREATE USER logs_user WITH PASSWORD '[EVEN-MORE-SECRET-PASS]'
CREATE USER grafana WITH PASSWORD '[THE-MOST-SECRET-PASS-EVER]'
GRANT ALL ON logs TO logs_user
GRANT READ ON logs TO grafana
exit
sudo vim /etc/influxdb/influxdb.conf
[http] 
enabled = true 
bind-address = ":8086" # change to a specific interface if needed
auth-enabled = true # will enforce authentication</pre>
sudo service influxdb restart

Install grafana

wget https://dl.grafana.com/oss/release/grafana_6.2.1_amd64.deb

  1. Go to http://[your-server-ip]:3000
    The default username and password is : admin/admin
    !!!!!! CHANGE IT NOW !!!!!

Create a database connection

  1. Create the python script on the raspberry
    sudo apt install python-pip
    pip install requests 
    vim ~/update.py 
    

    ```python #!/usr/bin/python

import json import math import requests import sys from time import sleep import RPi.GPIO as GPIO

Here you should update you own GPIO code

GPIO.setmode (GPIO.BOARD) GPIO.setup(18,GPIO.IN)

IP = “[your-server-ip]” # The IP of the machine hosting your influxdb instance DB = “logs” # The database to write to, has to exist USER = “logs_user” # The influxdb user to authenticate with PASSWORD = “[EVEN-MORE-SECRET-PASS]” # The password of that user
TIME = 5 # Delay in seconds between two consecutive updates
while True: v = ‘Button value=%s’ % GPIO.input(18)
r = requests.post(“http://%s:8086/write?db=%s” %(IP, DB), auth=(USER, PASSWORD), data=v)
if r.status_code != 204:
print (‘Failed to add point to influxdb (%d) - aborting.’ %r.status_code)
sys.exit(1)
sleep(TIME)</pre>


  6. Set the graph in grafana to show the data you want &#8230;  
    
  7. Setting the code to run at boot 
    <pre>crontab -e
```shell
@reboot /usr/bin/python /home/pi/update.py</pre>

Twitter, Facebook