The code used here is largely based on DS18B20 Temperature Sensing by Simon Monk.
3V3 - Red wire
Ground - Black wire
GPIO 4 - White wire (with 4.7K resistor between this and 3V3).
The 3V3, Ground, GPIO wire can be connected to any 3.3V, Ground, GPIO pin accordingly. Raspberry Pi pinout diagrams can be found here
Soldered example of our sensor:
Edit /boot/config.txt and add the configuration line dtoverlay=w1-gpio, then reboot the Raspberry Pi.
sudo nano /boot/config.txt
*add dtoverlay=w1-gpio and save*
sudo reboot
To see if it worked out, change directory (cd) /sys/bus/w1/devices. list directory contents (ls), and you should see a 28-xxxxxxx directory, one directory for each connected sensor. cd into that directory, then concatenate (cat) w1_slave.
sudo modprobe w1-therm
cd /sys/bus/w1/devices
ls
cd 28-xxxxxxx
cat w1_slave
What makes this programm different is it automatically writes temperature measurements in text file.
You also might have noticed both temp.py and temp2.py in this repository. temp.py contains endless while True loop, thus making it useful for continuous measurements. However, temp2.py runs only once after each execution, therefore it can be used with cron.
To see, which dependencies are required, check __init__.py
file.
Some packages can be found in the Raspbian archives. In that case use good old apt-get
:
sudo apt-get install python3-packagename
Some packages might need to be installed from the Python Package Index (PiPI). To install pip for python3:
sudo apt-get install python3-pip
To install, for example, ntplib package:
pip3 install ntplib
In case you want to only execute the python programm at specific times.
Installation
sudo apt-get install cron
Edit cron jobs
crontab -e
Basic format for a crontab is minute hour day_of_month month day_of_week command
.
This converts crontab to human readable form for the sake of testing if it is set to exactly what one expects.
Here it is used as
*/5 * * * * python3 /home/pi/tempSensorDS18B20/temp2.py >/dev/null 2>&1
every 5 minutes | execute temp2.py | disable email notification
To observe cron working:
tail -f /var/sys/syslog
Example of the output received:
Jul 26 11:40:01 raspberrypi CRON[3000]: (pi) CMD (python3 /home/pi/tempSensorDS18B20/temp2.py >/dev/null 2>&1)
Jul 26 11:45:01 raspberrypi CRON[3062]: (pi) CMD (python3 /home/pi/tempSensorDS18B20/temp2.py >/dev/null 2>&1)
Jul 26 11:50:01 raspberrypi CRON[3159]: (pi) CMD (python3 /home/pi/tempSensorDS18B20/temp2.py >/dev/null 2>&1)
Jul 26 11:55:01 raspberrypi CRON[3214]: (pi) CMD (python3 /home/pi/tempSensorDS18B20/temp2.py >/dev/null 2>&1)