Skip to content

Temperature Overlay

Calin Crisan edited this page Mar 9, 2020 · 1 revision

About

It is possible to show a text overlay with the actual temperature on the screen. This temperature can be either the system temperature or the ambient temperature using a DS18B20 temperature sensor.

Show System Temperature

Create the script /usr/local/bin/overlaytemperature with the following contents, for instance by typing sudo nano and pasting the below content:


CAM_CONFIG_FILE="/etc/motioneye/camera-1.conf"
CAM_NAME=''
if [ -f $CAM_CONFIG_FILE ]; then
	CAM_NAME=`grep text_left $CAM_CONFIG_FILE | cut -f2 -d' '`
fi
TEMP=`vcgencmd measure_temp | cut -f2 -d'='`
curl "http://localhost:7999/1/config/set?text_left=$CAM_NAME\%20$TEMP"

Make this script executable by entering chmod +x /usr/local/bin/overlaytemperature.

Add a cron script /etc/cron.d/temperature that updates the overlay temperature at boot and after a specified interval:

*/10 * * * * pi /usr/local/bin/overlaytemperature

This script will set the temperature 2 minutes after boot and update it every 10 minutes. You can change these values if you want it to be updated more often or less often.

Show Ambient Temperature

To show the ambient temperature you need to buy some cheap electronics. The main parts that you need are the DS18B20 temperature sensor and a 4.7K Ohm resistor. For testing purposes, you can use a breadboard and breadboard wiring. If you want to make the final temperature sensor, you need some basic soldering experience as well.

Instructions for the wring can be found at https://www.circuitbasics.com/raspberry-pi-ds18b20-temperature-sensor-tutorial/

At the software side, you need to load tyhe 1-Wire module:

To make sure this module is loaded at each boot, add the following lines to /etc/modules:

w1-therm

To avoid issues, you also need to add the following lines to /boot/config.txt:

dtoverlay=w1-gpio

Keep all the settings as configured to show the system temperature, but change the file /usr/local/bin/overlaytemperature:


CAM_CONFIG_FILE="/etc/motioneye/camera-1.conf"
CAM_NAME=''
if [ -f $CAM_CONFIG_FILE ]; then
        CAM_NAME=`grep text_left $CAM_CONFIG_FILE | cut -f2 -d' '`
fi
TEMP=`/usr/local/bin/temperature.py | cut -f2 -d'='`
/usr/bin/curl "http://localhost:7999/1/config/set?text_left=$CAM_NAME\%20$TEMP"

As can be seen, you also need to creature a file /usr/local/bin/temperature.py and make it executable by chmod +x:

# Import system libraries
import sys
# Define an array (temp)
temp = {}

sensorids = ["28-03159779916b"]
# loop net zo lang alles sensors af dat in het array hieboven staan.
for sensor in range(len(sensorids)):
  #tfile = open("/sys/bus/w1/devices/"+ sensorids[sensor] +"/w1_slave") #RPi 1,2 with older kernel.
  tfile = open("/sys/bus/w1/devices/"+ sensorids[sensor] +"/w1_slave") #RPi 2,3 with newer kernel.
  # Read all data and put in variable.
  text = tfile.read()
  # Close after reading.
  tfile.close()
  # Split text per new line (\n)
  # and select the 2nd line [1] (1st line = [0])
  secondline = text.split("\n")[1]
  # Split the line in words.
  # Select the 10th verb [9] (counting from 0)
  temperaturedata = secondline.split(" ")[9]
  # The first 2 charachters are "t=", we need to remove those.
  # we make an integer from the string (number).
  temperature = float(temperaturedata[2:])
  # Divide the temperature value by 1000 to get the correct value.
  temp[sensor] = temperature / 1000
  # print date to console
  print "sensor", sensor, "=", temp[sensor], "degrees"

In this file you need to change the sensorids value to your situation. You can find the value for your sensor with the following Python script:

 
import glob
 
for sensor in glob.glob("/sys/bus/w1/devices/*/w1_slave"):
  id = sensor.split("/")[5]
 
print id