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

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 | 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
        
        * 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
        
      2. sudo apt-get update && sudo apt-get install influxdb
        sudo service influxdb start 
      3. 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
      4. 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
      5.   sudo service influxdb restart
    2. Install grafana
      1. curl https://packagecloud.io/gpg.key | sudo apt-key add -
        echo "deb https://packagecloud.io/grafana/stable/debian/ stretch main" | sudo tee /etc/apt/sources.list.d/influxdb.list
      2. sudo apt-get update
        sudo apt-get install grafana
      3. sudo service grafana-server start 
      4. sudo update-rc.d grafana-server defaults
        
  4. Go to http://[your-server-ip]:3000
    The default username and password is : admin/admin
    !!!!!! CHANGE IT NOW !!!!!

    1. Create a database connection
  5. Create the python script on the raspberry
    1. sudo apt install python-pip
      
      pip install requests
    2. vim ~/update.py
      #!/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)
  6. Set the graph in grafana to show the data you want …
  7. Setting the code to run at boot
    crontab -e
    
    @reboot /usr/bin/python /home/pi/update.py