Temperature monitoring solution based on Raspberry Pi

My house is heated by an ancient ground source heat pump that every now and then has had various problems. Being a 40-year-old piece of equipment, it doesn’t have any of the electronics that all the new heat pumps come with. Since getting data about the operation of the pump is always useful when trying to figure out what’s wrong this time, I decided to make a monitoring device myself.

I started with a Raspberry Pi Zero with wifi, and four ds18b20 temperature sensors. Here’s a good resource on the basics on how to get the Raspi to talk to the sensors: https://www.circuitbasics.com/raspberry-pi-ds18b20-temperature-sensor-tutorial/. I followed the instructions, but still had trouble connecting to the sensors. A quick look on the internet revealed that other people had had similar problems, and the solution was to replace the 4.7kohm resistor with a 10kohm one.

With the hardware side sorted out, the next step was to write some software. I wanted a web server that would show me the temperature data in graph form that I could access from the house’s wifi network. Being most comfortable with Node.js, I installed it on the Raspi, and wrote a script that reads the sensor readings to a log file. A once-a-minute cron job finished this part of the system.

The last part needed was a server application that would read the log file and display the graph. I chose https://www.chartjs.org as the frontend, and then wrote a simple web server that serves the html page and the JSON file needed for the temperature data. Here’s a screenshot of the final product:

Screenshot of temperature monitor
Screenshot of temperature monitor

How-to

Rough steps on how to make something similar:

Raspberry Pi setup

  • Connect the sensors to the Raspberry Pi Zero according to https://www.circuitbasics.com/raspberry-pi-ds18b20-temperature-sensor-tutorial/
    • Except use a 10kohm resistor instead of a 4.7kohm one.
  • Write the basic Raspberry Pi OS image on an SD card.
  • Connect power, display, mouse and a keyboard to the Raspi and start it up.
  • Go through the initial setup:
    • Set region/language
    • Create a user account called “monitor”
    • Set up wifi network
    • Reboot
  • Start a terminal and run raspi-config
    • System options -> Boot / auto login -> Select non-auto-login CLI
    • Interface options: Enable SSH
  • Reboot again

Install Node.js

Instructions from  https://pimylifeup.com/raspberry-pi-nodejs/

sudo apt install -y ca-certificates curl gnupg
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /usr/share/keyrings/nodesource.gpg
NODE_MAJOR=20
echo "deb [signed-by=/usr/share/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list
sudo apt update
sudo apt install nodejs

Copy the script files

See end of this post for the files. Copy the files to:

/home/monitor/monitor/server.js
/home/monitor/monitor/reader.js
/home/monitor/monitor/enable.sh

And then:

chmod +x /home/monitor/monitor/enable.sh

Go to /sys/bus/w1/devices/ and run ls. You should see your temperature sensors as directories named 28-xxxxxxxxxxxx. Edit reader.js, and replace the “files” array values with the ids of your sensors.

Add cron jobs

Add cronjobs in /etc/cron.d/monitor:

@reboot /home/monitor/monitor/enable.sh # runs required setup commands on boot
0 5 * * * monitor find /home/monitor/monitor/logs/ -type f -mtime +90 -delete # Cleans up old log files, change the number of days "90" to what you want
* * * * * monitor /usr/bin/node /home/monitor/monitor/reader.js # Reads the sensors once a minute and saves data to log file
@reboot /usr/bin/node /home/monitor/monitor/server.js & # Runs the server on startup

Test

Reboot the Raspberry Pi. Check its IP address, and try to access the monitor at for example http://192.168.0.10:8080

Files

See Github: https://github.com/xird/raspberry-pi-temperature-monitor

Leave a comment

Your email address will not be published. Required fields are marked *