Prechádzať zdrojové kódy

+ publishing script for RPi, which reads temperatures of Dallas DS18x20 sensors and store values to PostgreSQL /A

InstallAndUse 2 rokov pred
rodič
commit
a544108c4a

+ 72 - 0
read_temperature_from_ds18x20_sensors/README.md

@@ -0,0 +1,72 @@
+#
+# 2022 03 18  + published on https://github.com/InstallAndUse/RPi /A
+#
+
+
+# INSTALLATION:
+
+ sudo su
+# raspi-config, 5. interfaceing, P7 1-wire, enable, yes. reboot
+ ls -la /sys/bus/w1/devices/*
+     lrwxrwxrwx 1 root root 0 Jun 12 12:18 /sys/bus/w1/devices/00-200000000000 -> ../../../devices/w1_bus_master1/00-200000000000
+     lrwxrwxrwx 1 root root 0 Jun 12 12:18 /sys/bus/w1/devices/00-a00000000000 -> ../../../devices/w1_bus_master1/00-a00000000000
+     lrwxrwxrwx 1 root root 0 Jun 12 12:13 /sys/bus/w1/devices/w1_bus_master1 -> ../../../devices/w1_bus_master1#
+
+
+# when sensors connected:
+# (connect VCC to 5V, GND to GND, DATA to TXD PIN, remember to put resistor 4.7 kOhm between VCC and DATA)
+ ls -la /sys/bus/w1/devices/*
+     lrwxrwxrwx 1 root root 0 Jun 12 12:20 /sys/bus/w1/devices/00-600000000000 -> ../../../devices/w1_bus_master1/00-600000000000
+     lrwxrwxrwx 1 root root 0 Jun 12 12:20 /sys/bus/w1/devices/00-e00000000000 -> ../../../devices/w1_bus_master1/00-e00000000000
+     lrwxrwxrwx 1 root root 0 Jun 12 12:20 /sys/bus/w1/devices/10-000800e489b3 -> ../../../devices/w1_bus_master1/10-000800e489b3   <-- this is first sensor
+     lrwxrwxrwx 1 root root 0 Jun 12 12:20 /sys/bus/w1/devices/10-00080280a759 -> ../../../devices/w1_bus_master1/10-00080280a759   <-- this is second sensor
+     lrwxrwxrwx 1 root root 0 Jun 12 12:18 /sys/bus/w1/devices/w1_bus_master1 -> ../../../devices/w1_bus_master1#
+
+
+# save this script (read_ds18x20.py) and bash script (read_ds18x20.psh) to /home/pi/meter
+ chmod +x /home/pi/meter/*
+ ls -la /home/pi/meter/*
+
+
+# check you have python installed (probabaly on RPi it is)
+ which python
+     /usr/bin/python
+ root@(host):/home/pi/meter# ./read_ds18x20.py
+     /sys/bus/w1/devices/10-000800e489b3;27000
+     /sys/bus/w1/devices/10-00080280a759;29500#
+
+
+ change DB credentials in shell script (user) and (pass)
+ nano ./read_ds18x20.sh
+ pi@(host):~/meter $ ./read_ds18x20.py | ./read_ds18x20.sh
+     INSERT 0 1
+     INSERT 0 1
+
+
+# set peiodic check with cron (good idea to setup cron for root, because future checks will be consolidated in one place)
+ sudo
+ crontab -e
+     */1 * * * * /home/pi/meter/read_ds18x20.py | /home/pi/meter/read_ds18x20.sh
+
+
+# check that cron is working:
+ tail -f /var/log/syslog | grep CRON
+     Jun 12 12:47:01 lab5rp41 CRON[1507]: (root) CMD (/home/pi/meter/read_ds18x20.py | /home/pi/meter/read_ds18x20.sh)
+     Jun 12 12:47:04 lab5rp41 CRON[1503]: (CRON) info (No MTA installed, discarding output)#
+
+
+# check that entries actually written to db
+ psql -h (host_ip) -U (user2) -d (db) -c "SELECT * FROM telemetry_host;"
+     Password for user (host):
+                timestamp           | value |                              sensor
+     -------------------------------+-------+------------------------------------------------------------------
+      2020-06-12 08:58:36.732033+01 | 12345 | test_sensor
+      2020-06-12 10:38:12.519066+01 | 27500 | /sys/bus/w1/devices/10-000800e489b3
+      2020-06-12 10:38:12.68798+01  | 29000 | /sys/bus/w1/devices/10-00080280a759
+      2020-06-12 10:38:47.060764+01 | 27000 | /sys/bus/w1/devices/10-000800e489b3
+      2020-06-12 10:38:47.222548+01 | 28937 | /sys/bus/w1/devices/10-00080280a759
+      2020-06-12 10:46:38.363432+01 | 27062 | /sys/bus/w1/devices/10-000800e489b3
+      2020-06-12 10:46:38.552815+01 | 28812 | /sys/bus/w1/devices/10-00080280a759
+      2020-06-12 10:47:03.880082+01 | 27687 | /sys/bus/w1/devices/10-000800e489b3
+      2020-06-12 10:47:04.031554+01 | 28750 | /sys/bus/w1/devices/10-00080280a759
+     (9 rows)

+ 32 - 0
read_temperature_from_ds18x20_sensors/read_ds18x20.py

@@ -0,0 +1,32 @@
+#!/usr/bin/python
+#
+# read DS18b20 or DS1820 sensors
+#
+# 2020 05 07  * initial code for DS18B20 /A
+# 2020 05 08  + multi sensor support /A
+#             + DS1820 support /A
+#             * return in millidegrees (get rid of float)
+# 2020 06 12  + install doc added /A
+# 2022 03 18  + published on https://github.com/InstallAndUse/RPi /A
+#             * moved doc to separate README.md file /A
+#
+
+
+#
+# TODO:
+#
+#
+
+
+
+import glob
+
+base_dir = '/sys/bus/w1/devices/'
+sensors = glob.glob(base_dir + '[28|10]*')
+for sensor in sensors:
+    f = open(sensor + '/w1_slave', 'r')
+    lines = f.readlines()
+    f.close()
+    if lines[0].strip()[-3:] == 'YES':
+        value = lines[1][lines[1].find('t=')+2:]
+    print("%s;%s" %(sensor, value))

+ 19 - 0
read_temperature_from_ds18x20_sensors/read_ds18x20.sh

@@ -0,0 +1,19 @@
+#!/bin/bash
+#
+# history and doc, refer to read_ds18x20.py /A
+#
+# 2022 03 18  + published on https://github.com/InstallAndUse/RPi /A
+
+host="(host)"
+user="(user)"
+export PGPASSWORD="(dbpass)";
+db="(dbname)"
+table="(dbtable)"
+
+while read line                      # read values for each sensor
+    do
+        values=(${line//;/ })        # split by ";" and build array
+        if [ -n "$values" ]; then    # if array is not empty (empty line) write to DB
+            echo "INSERT INTO ${table} (timestamp, sensor, value) VALUES (NOW(),'${values[0]}','${values[1]}')" | psql -h ${host} -U ${user} -d ${db}
+        fi
+    done < "${1:-/dev/stdin}"        # read stdin from python script (read sensors)