read_ds18x20.sh 697 B

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