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

erangoldman@gmail.com, 24 April 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]  

    1. Install influxdb
      1. * For Ubutnu :
        curl -sL https://repos.influxdata.com/influxdb.key <span class="p">|</span> sudo apt-key add -
        <span class="nb">source</span> /etc/lsb-release
        <span class="nb">echo</span> <span class="s2">"deb https://repos.influxdata.com/</span><span class="si">${</span><span class="nv">DISTRIB_ID</span><span class="p">,,</span><span class="si">}</span> <span class="si">${</span><span class="nv">DISTRIB_CODENAME</span><span class="si">}</span><span class="s2"> stable"</span> <span class="p">|</span> sudo tee /etc/apt/sources.list.d/influxdb.list
        
        
  • For Debian / Raspberry curl -sL https://repos.influxdata.com/influxdb.key | sudo apt-key add -source /etc/os-release echo “deb https://repos.influxdata.com/debian jessie stable” | sudo tee /etc/apt/sources.list.d/influxdb.list </code></pre>

        2. <pre class="chroma"><code class="language-bash">sudo apt-get update &lt;span class="o">&&&lt;/span> sudo apt-get install influxdb sudo service influxdb start</code> <code class="language-bash"></code></pre>
        
        3. <pre class="chroma"><code class="language-bash">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</code></pre>

      4. <pre class="chroma">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>

      5. <pre>  sudo service influxdb restart</pre>

  2. Install grafana 
      1. <pre class="chroma"><span class="keyword">wget</span> <a href="https://dl.grafana.com/oss/release/grafana_6.2.1_amd64.deb">https://dl.grafana.com/oss/release/grafana_6.2.1_amd64.deb</a> <br /><span class="keyword">sudo dpkg</span> -i grafana_6.2.1_amd64.deb <code class=" language-bash">&lt;span class="token function">sudo&lt;/span> &lt;span class="token function">service&lt;/span> grafana-server start</code> <code class=" language-bash"></code></pre>
    
      2. <pre class=" language-bash"><code class=" language-bash">&lt;span class="token function">sudo&lt;/span> update-rc.d grafana-server defaults </code></pre>
  1. Go to http://[your-server-ip]:3000
    The default username and password is : admin/admin
    !!!!!! CHANGE IT NOW !!!!!  

    1. Create a database connection
  2. Create the python script on the raspberry

    1. sudo apt install python-pip
      
      

pip install requests</pre>

  2. vim ~/update.py 
    <pre>#!/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>

  1. Set the graph in grafana to show the data you want …
  2. Setting the code to run at boot <pre>crontab -e

@reboot /usr/bin/python /home/pi/update.py</pre>