After connecting the DB18B20 sensor to the Raspberry Pi3B + and getting the temperature with a Python script, I got the temperature regularly with crontab.
The DB18B20 sensor is an inexpensive sensor that can acquire temperature without using an analog converter between the sensor and Raspberry Pi. If you want to measure the temperature (or humidity) in the air in terms of the amount of information and the price, the dht11 sensor or this upward compatible dht22 sensor can be said to be the best. However, the biggest merit of DB18B20 is that the waterproof type is on sale and the water temperature can be measured. By the way, is the author made in China called HiLetgo? I bought 5 items for about 1000 yen on Amazon.
devices: Raspberry Pi3B+ OS: Rasbian NOOBS_ver_3_5_1 Language: Python 3 series
Regarding the circuit, I referred to the following URL. http://aba.doorblog.jp/archives/55110922.html Thank you for posting the circuit. However, it is only via a pull-down resistor. The point is to connect the signal line (generally yellow) to GPIO4! You can change it, but this time I did it. In the case of the author, I had only 2kΩ at hand, so I used this, but no error was found in the measurement results. In addition, since the Raspberry Pi has a resistance on the main body, it seems that it is not necessary to go through this resistance.
This sensor seems to use a communication method that uses an interface called 1-wire. So turn this on. First, enter the following command in the terminal.
$sudo nano /boot/config.txt
The nano here is an editor specification, but of course you can use a different editor if you like. The editor will open, so add the following at the bottom.
$dtoverlay=w1-gpio
If you do this, restart Raspberry Pi with sudo reboot. If you turn on 1-wire in Raspberry Pi Desktop> Raspberry Pi Settings> Interface, all the above work will be completed with GUI (yes, no).
Next, also at the terminal
$lsmod
Enter. Here, check the next of the tenth line from the bottom.
w1_therm 24576 0
w1_gpio 16384 0
fixed 16384 0
wire 36864 2 w1_gpio,w1_therm
cn 16384 1 wire
uas 24576 0
uio_pdrv_genirq 16384 0
uio 20480 1 uio_pdrv_genirq
i2c_dev 20480 0
ip_tables 28672 0
x_tables 32768 1 ip_tables
ipv6 458752 46
To be honest, I don't really understand what this means, but if there is a description of w1_therm, w1_gpio, it seems OK. If this doesn't appear, make sure the 1-wire interface is properly turned on and the pull-down resistor is properly engaged. I flew for an hour because the circuit was wrong.
Then do the following in your terminal:
$sudo modprobe w1-gpio
$sudo modprobe w1-therm
Next, go to check the ID of the sensor. Enter the following path in the terminal to move the directory.
$ cd /sys/bus/w1/devices/
After moving, do the following:
$ ls -la
Then, the following information is obtained.
drwxr-xr-x 2 root root 0 December 30 10:20 .
drwxr-xr-x 4 root root 0 December 30 10:20 ..
lrwxrwxrwx 1 root root 0 December 30 10:26 28-3c01b5564493 -> ../../../devices/w1_bus_master1/28-3c[The number string displayed on your screen]
lrwxrwxrwx 1 root root 0 December 30 10:20 w1_bus_master1 -> ../../../devices/w1_bus_master1
Here, read the number at 28-3c ... Memo memo because I will use it later. If you don't know the number 28, go back to reconfirming whether the circuit diagram points to the signal line to GPIO4 or 1-wire is turned on.
Next is the operation test. Please enter the following path in the terminal.
$/sys/bus/w1/devices/28-[The number you took note of above]
At the destination, do the following
/sys/bus/w1/devices/28-3c01b5564493 $ cat w1_slave
Then, you will get something like this ↓.
a7 00 55 05 7f a5 a5 66 a5 : crc=a5 YES
a7 00 55 05 7f a5 a5 66 a5 t=10437
t = 10437 is the temperature. The value divided by 1000 is the temperature. 10.437 ° C. Yeah, my house is cold, lol.
This completes the preparation. If it gets stuck (1) Check the 1-wire settings. Did you forget to reboot? (2) Check the circuit. In this flow, it is necessary to select GPIO4 as the communication line. (3) The temperature sensor itself is dead. Since it is made in China, it remains as it is, so I will change each sensor.
Next, since the above preparations have been completed, we will create a program that measures the temperature with Python and records the measurement data in a csv file with the date and time. The created program is as follows.
import datetime
import os
#Please change the file pass here to your arbitrary user name or desired file storage location (pi → tarou)
fileName = '/home/pi/waterTemp/waterTemp.csv'
#It is written with with statement, but it does not have to be this separately
with open('/sys/bus/w1/devices/28-[Your sequence]/w1_slave') as f:
#print(type(f))
data = f.read()
waterTemp = float(data[data.index('t=')+2:])/1000
#print(waterTemp)
#If there is no file in the relevant part, create it
if not os.path.exists(fileName):
thData = open(fileName,'w')
thData.write('date_time,waterTemp\n')
thData.close()
#Processing to record temperature data over time
while True:
now = str(datetime.datetime.now())[0:16]
data = [now,waterTemp]
if waterTemp == 0:
continue
thData = open(fileName, 'a')
thData.write(','.join(map(str,data))+'\n')
thData.close()
print(','.join(map(str,data)))
break
This time, I wrote it in Thonny.
Finally, run the Python script created above with cron on a regular basis. It seems that cron is relatively easy, but I'm a beginner, so I'm surprised that I'm stuck, so I'd like to write a detailed article again. This time, I will briefly describe the flow.
Enter the following in the terminal.
crontab -e
Select your favorite editor (nano is the one for me. I think it's easy.) Write as follows at the bottom.
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
#Add the following items! !!
*/1 * * * * /usr/bin/python3 /home/pi/waterTemp/waterTemp.py
For the path, specify the location where the script is placed. Now press y with ctrl -x to finish. This time, the */1 * * * * part is acquired every minute, but please change it appropriately.
This time, I connected the DS18B20 sensor to the Raspberry Pi and did DIY to save the temperature in csv on a regular basis. With this, it seems to be fun to use for IoT work such as visualization of the water temperature of ornamental fish and the nutrient solution temperature of hydroponics. Heppoko is a groping work for science university students, so I would appreciate it if you could share any mistakes or if you think that this is smarter.
Thank you very much for the following sites and books. Thank you very much. https://www.taneyats.com/entry/raspi-smart-aquarium-2 https://dkpyn.com/blog/2344 https://kookye.com/ja/2017/06/01/desgin-a-temperature-detector-through-raspberry-pi-and-ds18b20-temperature-sensor/ You can understand from scratch! Raspberry Pi Introductory Guide to Electronic Work First Edition by Tatra Edit p189, p190
Recommended Posts