Skip to content

Instantly share code, notes, and snippets.

@rgee0
Last active December 23, 2017 20:53
Show Gist options
  • Save rgee0/549b8343463bf9218230e0792f462687 to your computer and use it in GitHub Desktop.
Save rgee0/549b8343463bf9218230e0792f462687 to your computer and use it in GitHub Desktop.

Based off https://www.circuits.dk/install-grafana-influxdb-raspberry/ & http://docs.grafana.org/installation/debian/

Grafana

sudo apt-get install apt-transport-https curl  && curl https://bintray.com/user/downloadSubjectPublicKey?username=bintray | sudo apt-key add -

echo "deb https://dl.bintray.com/fg2it/deb-rpi-1b jessie main" | sudo tee -a /etc/apt/sources.list.d/grafana.list

echo "deb https://dl.bintray.com/fg2it/deb jessie main" | sudo tee -a /etc/apt/sources.list.d/grafana.list

sudo apt-get update && sudo apt-get install -y grafana

sudo systemctl enable grafana-server.service && sudo service grafana-server start

Influxdb

curl -sL https://repos.influxdata.com/influxdb.key | sudo apt-key add -source /etc/os-release

test $VERSION_ID="8" && echo "deb https://repos.influxdata.com/debian jessie stable" | sudo tee /etc/apt/sources.list.d/influxdb.list

sudo apt-get update && sudo apt-get install -y influxdb && sudo service influxdb start

influx

CREATE DATABASE officeTemps

Python

sudo pip install influxdb

Script

import time
import sys
import datetime
from influxdb import InfluxDBClient
from envirophat import weather
 
# Set this variables, influxDB should be localhost on Pi
host = "localhost"
port = 8086
user = "root"
password = "root"
dbname = "office"

client = InfluxDBClient(host, port, user, password, dbname)

# Sample period (s)
interval = 60
 
# Allow user to set session and runno via args otherwise auto-generate
if len(sys.argv) > 1:
    if (len(sys.argv) < 2):
        print "Must provide a desk number"
        sys.exit()
    else:
        deskNo = sys.argv[1]
 else:
    deskNo = "3"
    now = datetime.datetime.now()
 
# Run until keyboard out
try:
    while True:

        temp = weather.temperature()
        press = weather.pressure()
        iso = time.ctime()
 
        json_body = [
        {
          "measurement": temperatures,
              "tags": {
                  "desk": deskNo,
                  },
              "time": iso,
              "fields": {
                  "Degrees" : temp
                  "Pressure" : press
              }
          }
        ]
 
        # Write JSON to InfluxDB
        client.write_points(json_body)
        # Wait for next sample
        time.sleep(interval)
 
 
except KeyboardInterrupt:
     client.close()

Cron

select-editor
crontab -e
@reboot python /home/pi/code/envirophat/temperature_logger.py &

Test

sudo reboot now
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment