# Raspberry pi, InfluxDB, Grafan &amp; Digitalocean as an IOT architecture

Date: 2018-04-24

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.

<!--more-->

Just a small bonus : if you use my referal link you&#8217;ll get 10$ credit it&#8217;s 2 first months free, and I&#8217;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](http://digitalocean.com/).
  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&#8217;t know how [rtfm](https://www.digitalocean.com/community/tutorials/how-to-set-up-ssh-keys-on-ubuntu-1604)
      7. One droplet
      8. Give it a NameAnd press the create button  
        [<img class="alignnone wp-image-374 size-thumbnail" src="http://goldmanmalka.com/wp-content/uploads/sites/10/2018/04/2-150x150.png" alt="" width="150" height="150" />](http://goldmanmalka.com/wp-content/uploads/sites/10/2018/04/2.png)
  3. ssh to your new droplet  
    ```ssh -i [Your\_private\_key] root@[DropletIP]``` 
     
    
## Install influxdb 
Ubutnu :
```shell
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 :
```mysql
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
```
```shell
sudo vim /etc/influxdb/influxdb.conf
```        
```shell
[http] 
enabled = true 
bind-address = ":8086" # change to a specific interface if needed
auth-enabled = true # will enforce authentication</pre>
```
 
        
```shell
sudo service influxdb restart
```
    
Install grafana 
```shell
wget https://dl.grafana.com/oss/release/grafana_6.2.1_amd64.deb

```



  4. Go to http://[your-server-ip]:3000  
    The default username and password is : admin/admin  
    !!!!!! CHANGE IT NOW !!!!! 
     
    
## Create a database connection  
        
  5. Create the python script on the raspberry 
   ```shell
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 = "[<code class="language-bash">EVEN-MORE-SECRET-PASS</code>]" # The password of that user <br />TIME = 5 # Delay in seconds between two consecutive updates <br />while True: v = 'Button value=%s' % GPIO.input(18) <br />r = requests.post("http://%s:8086/write?db=%s" %(IP, DB), auth=(USER, PASSWORD), data=v) <br />if r.status_code != 204: <br />print ('Failed to add point to influxdb (%d) - aborting.' %r.status_code) <br />sys.exit(1) <br />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>
```
