- Install Raspbian Lite on an SD card
- Set up InfluxDB
- Create a logIt script to log data to Influxdb
- Set up 1wire bit-banging (GPIO 4)
- Create a getTemperature script to read temperatures from a connected sensor
- Create a looping wrapper that fetches temprature from sonsors, then logging it to the database
This small writeup is meant to show that it is quite simple to write a data logger by relying on "unix style" programs that do one thing only.
- getTemperature => only fetches the temprature once from one sensor
- logIt => only logs one entry to the influxdb
The beauty of breaking out the functionality this way, is that you can scale your solution, and you can reuse components easily for later use.
For example, the logIt script does not care if you log a temperature. It just takes a sensor name and a value. And the getTemperature script does not log the data in any way. It simply just fetches the temperature and prints it to STDOUT.
- Some goodies
sudo apt-get -y install jq git
This is a very condensed installation of InfluxDB on a freshly installed Raspberry Pi with Raspbian Stretch Lite. For a more verbose installation, have a look here: https://github.com/maglub/rpi-sous-vide/blob/master/setup-influxdb.sh
curl -sL https://repos.influxdata.com/influxdb.key | sudo apt-key add -
. /etc/os-release
echo "deb https://repos.influxdata.com/debian stretch stable" | sudo tee /etc/apt/sources.list.d/influxdb.list
sudo apt-get update
sudo apt-get -y install influxdb
sudo systemctl start influxd
The default port for influx is 8086, and I am running the influxdb on the same raspberry pi as I am connected to.
- Create a new database (called: smoker)
#--- 127.0.0.1 is the localhost
myIp=127.0.0.1
curl -X POST -G http://$myIp:8086/query --data-urlencode "q=CREATE DATABASE smoker"
curl -X POST -G http://$myIp:8086/query --data-urlencode "q=show databases"
- Send some data to the database
curl -i -XPOST "http://${myIp}:8086/write?db=smoker" --data-binary "sensor_data,sensor=minSensor1 value=55"
- Read data from the database
curl -X POST -G http://$myIp:8086/query --data-urlencode "db=smoker" --data-urlencode "q=select value from sensor_data where sensor='minSensor1'"
This is a very simple logging script:
#!/bin/bash
#----------------------------
# influx
#----------------------------
#--- set these variables in conf/app.conf - default below works in my (Magnus) environment
influxHost=localhost
influxPort=8086
influxDb=smoker
influxSenderId=this-smoker
#--- get sensor name and metric from the command line
PILOGGER_SENSOR=$1
PILOGGER_DATA=$2
curl -i -XPOST "http://${influxHost}:${influxPort}/write?db=${influxDb}" --data-binary "sensor_data,sensor=${PILOGGER_SENSOR} value=${PILOGGER_DATA}" > /dev/null 2>&1
Example:
chmod 755 ./logIt
./logIt minSensor1 33
./logIt minSensor1 44
./logIt minSensor1 55
./logIt minSensor1 66
Read the data:
curl --silent -X POST -G http://$myIp:8086/query --data-urlencode "db=smoker" --data-urlencode "q=select value from sensor_data where sensor='minSensor1'" | jq "."
- Connect the DS18B20 to your RPI (not explained here, +3.3V, GPIO4, GND, and a 4.7kOhm resistor)
- Confiure the Raspberry Pi
echo "dtoverlay=w1-gpio" | sudo tee -a /boot/config.txt
echo w1-gpio | sudo tee -a /etc/modules
echo w1-therm | sudo tee -a /etc/modules
sudo shutdown -r now
- Check that the sensor works
ls /sys/bus/w1/devices | grep "^2"
28-0000054e56f8
- Create a script to fetch the temperature (called getTemperature)
#!/bin/bash
cat /sys/bus/w1/devices/$1/w1_slave | awk '$NF~/t=/ { gsub(".*=","", $NF); temperature=$NF/1000; printf "%.2f", temperature;}'
- Make it executable, then run it with your sensor's ID as parameter
chmod 755 ./getTemperature
./getTemperature 28-0000054e56f8
22.56
- I call this script "logger"
#!/bin/bash
#--- more than one sensor, add to the string, separated by space
mySensors="28-0000054e56f8"
sleepTime=10
while true
do
for sensor in $mySensors
do
temperature=$(./getTemperature $sensor)
echo "Sensor: $sensor Temperatur: $temperature"
./logIt $sensor $temperature
sleep $sleepTime
done
done