Wednesday, 15 May 2019

Revisited: Raspberry Pi as a temperature logger

I revisited this project to get rid of 1-wire USB adapter and connect Dallas sensors directly to Raspberry Pi's GPIO pins. At the same time I converted scripts from python to bash.

DS1820 sensors are connected to Raspberry with following setup:
-Sensor Vcc pin connected to GND (parasite power)
-Sensor GND connected to Raspberry GND
-Raspberry GPIO 4 connected to sensor data pin
-Sensor data pin connected to Raspberry 3v3 pin via pull-up resistor (300R)

Raspberry Pi OS version is Raspbian Stretch Lite, Version: April 2019 (2019-04-08).

Enable 1-Wire in Raspberry


1-Wire GPIO support can be activated in Raspberry by adding overlay 'w1-gpio' to the device tree configuration file (/boot/config.txt).

Sensors will be powered via parasitic power which requires also 'pullup=on' configuration.
pi@raspberrypi:~ $ sudo nano /boot/config.txt
...

dtoverlay=w1-gpio,pullup=on

Reboot device to activate changes.
pi@raspberrypi:~ $ reboot

Detected temperature sensors are listed in /sys/devices/w1_bus_master1/-folder. Sensor address is same as folder name (e.g. 28-0000063d6ca4).
pi@raspberrypi:~ $ ls -og /sys/devices/w1_bus_master1/
total 0
drwxr-xr-x 4    0 May 15 16:11 28-0000063d6ca4
drwxr-xr-x 4    0 May 15 16:11 28-0000063d9593
drwxr-xr-x 4    0 May 15 16:11 28-0000067a8041
drwxr-xr-x 4    0 May 15 16:11 28-0000067cd19e
drwxr-xr-x 4    0 May 15 16:11 28-0000067cefe8
drwxr-xr-x 4    0 May 15 16:11 28-0000067d01e8
lrwxrwxrwx 1    0 May 15 17:04 driver -> ../../bus/w1/drivers/w1_master_driver
drwxr-xr-x 2    0 May 15 17:04 power
lrwxrwxrwx 1    0 May 15 17:04 subsystem -> ../../bus/w1
-rw-r--r-- 1 4096 May 15 17:04 uevent
...
...
pi@raspberrypi:~ $

Sensor functionality can be verified by reading a raw measurement value from w1_slave entry.
pi@raspberrypi:~ $ cat /sys/devices/w1_bus_master1/28-0000063d6ca4/w1_slave
7d 01 4b 46 7f ff 03 10 24 : crc=24 YES
7d 01 4b 46 7f ff 03 10 24 t=23812
pi@raspberrypi:~ $
Temperature is presented in uC, i.e. 23812 = +23,8C

Data logger SW setup for 10 sensors


Install rrdtool:
pi@raspberrypi:~ $ sudo apt install rrdtool

Create round robin database for 10 temperature sensors. Measurements will be made once in a minute. Database configuration will be
  • 10 data sources. Named as t_01 -> t_10
  • temperature of past 90 days, resolution 1 minute
  • minumum and maximum temperatures of past 90 days, resolution 1 minute
  • average day temperature for past 1 year, resolution 1 day
  • minimum and maximum day temperatures for past 1 year, resolution 1 day

rrdtool create temperature.rrd \
--step 60 \
DS:t_01:GAUGE:120:U:U \
DS:t_02:GAUGE:120:U:U \
DS:t_03:GAUGE:120:U:U \
DS:t_04:GAUGE:120:U:U \
DS:t_05:GAUGE:120:U:U \
DS:t_06:GAUGE:120:U:U \
DS:t_07:GAUGE:120:U:U \
DS:t_08:GAUGE:120:U:U \
DS:t_09:GAUGE:120:U:U \
DS:t_10:GAUGE:120:U:U \
RRA:AVERAGE:0.5:1:129600 \
RRA:MIN:0.5:1:129600 \
RRA:MAX:0.5:1:129600 \
RRA:AVERAGE:0.5:1440:365 \
RRA:MIN:0.5:1440:365 \
RRA:MAX:0.5:1440:365

Configuration file


Sensor configuration is defined in ds18b20_config.txt which should be created to home folder.

Each line in the file contains information for single sensor:

source1_name, print_name1, sensor1_address
source2_name, print_name2, sensor2_address
etc...
  • source_name: Data source name in database. Must match with the name used in rrdtool create command above.
  • print_name: Temperature sensor name used in graph legends.
  • sensor_address: Sensor 1-wire address.

example file content:
t_01,temp_01,28-0000063d6ca4
t_02,temp_02,28-0000063d9593
t_03,temp_03,28-0000067a8041
t_04,temp_04,28-0000067cd19e
t_05,temp_05,28-0000067cefe8
t_06,temp_06,28-0000067d01e8
t_07,temp_07,28-0000063d6ca4
t_08,temp_08,28-0000063d9593
t_09,temp_09,28-0000067a8041
t_10,temp_10,28-0000067cd19e

Temperature measurement


Copy store_measurement.sh-script to home folder.

Mark the script executable.
pi@raspberrypi:~ $ chmod +x store_measurement.sh

And verify it's working properly.
pi@raspberrypi:~ $ ./store_measurement.sh
Sensor configuration:
sensor: t_01 address: 28-0000063d6ca4
sensor: t_02 address: 28-0000063d9593
sensor: t_03 address: 28-0000067a8041
sensor: t_04 address: 28-0000067cd19e
sensor: t_05 address: 28-0000067cefe8
sensor: t_06 address: 28-0000067d01e8
sensor: t_07 address: 28-0000063d6ca4
sensor: t_08 address: 28-0000063d9593
sensor: t_09 address: 28-0000067a8041
sensor: t_10 address: 28-0000067cd19e

Measurements:
t_01: 24687
t_02: 24312
t_03: 24750
t_04: 24687
t_05: 24500
t_06: 24687
t_07: 24750
t_08: 24312
t_09: 24687
t_10: 24687
N:24687:24312:24750:24687:24500:24687:24750:24312:24687:24687
pi@raspberrypi:~ $

Schedule script to run on every minute by adding it to cron:
pi@raspberrypi:~ $ crontab -e
...
...
# Edit this file to introduce tasks to be run by cron.
#
...
...
# m h  dom mon dow   command
  * *  *   *   *     /home/pi/store_measurement.sh

Create measument graphs


Copy script print_png.sh to home folder.

Script will print 4 graphs with different measurement periods: 1 day, 1 week, 1 month  and 1 year.

Mark the script executable.
pi@raspberrypi:~ $ chmod +x print_png.sh

And verify it's working properly.
pi@raspberrypi:~ $ ./print_png.sh
sensor: t_01 (temp_01) address: 28-0000063d6ca4
sensor: t_02 (temp_02) address: 28-0000063d9593
sensor: t_03 (temp_03) address: 28-0000067a8041
sensor: t_04 (temp_04) address: 28-0000067cd19e
sensor: t_05 (temp_05) address: 28-0000067cefe8
sensor: t_06 (temp_06) address: 28-0000067d01e8
sensor: t_07 (temp_07) address: 28-0000063d6ca4
sensor: t_08 (temp_08) address: 28-0000063d9593
sensor: t_09 (temp_09) address: 28-0000067a8041
sensor: t_10 (temp_10) address: 28-0000067cd19e
1day (03.06.2019 16.49)981x1139
1week (03.06.2019 16.49)981x1139
1month (03.06.2019 16.49)981x1139
1year (03.06.2019 16.49)981x1139
pi@raspberrypi:~ $

Now there should be 4 png-files in home folder.
pi@raspberrypi:~ $ ls -og
total 30760
-rw-r--r-- 1      290 May 15 17:23 ds18b20_config.txt
-rw-r--r-- 1    67555 Jun  3 16:49 meas_1day.png
-rw-r--r-- 1    62295 Jun  3 16:49 meas_1month.png
-rw-r--r-- 1    81048 Jun  3 16:49 meas_1week.png
-rw-r--r-- 1    65524 Jun  3 16:49 meas_1year.png
-rwxr-xr-x 1     2366 Jun  3 16:49 print_png.sh
-rwxr-xr-x 1      991 May 27 15:34 store_measurement.sh
-rw-r--r-- 1 31199544 Jun  3 17:04 temperature.rrd
pi@raspberrypi:~ $

Measurement graphs should be generated before upload script is run. Here's example cron configuration for updating measurement graphs and uploading those to web page in 10 minute interval.
pi@raspberrypi:~ $ crontab -e
...
...
# Edit this file to introduce tasks to be run by cron.
...
# m h  dom mon dow   command
  * *  *   *   *     /home/pi/store_measurement.sh

  1,11,21,31,41,51 * * * * /home/pi/print_png.sh
  3,13,23,33,43,53 * * * * /home/pi/upload_graphs.sh

No comments:

Post a Comment