Hi,
These are the notes I made when I first started using 1-wire sensors on the Pi.
(replace <username> with whichever user you are logged on as)
(the notes include a script that runs every 5 minutes to log the results from all sensors - it's not required to make the 1-wire sensors work)
Load the modules:
sudo su
modprobe wire
modprobe w1-gpio
modprobe w1-therm
Make modules load at boot:
Add these three lines to the file /etc/modules:
wire
w1-gpio
w1-therm
Examine the system:
ls /sys/bus/w1/devices/w1_bus_master1/
This shows these 'functions':
driver, subsystem, power, uevent
w1_master_add, w1_master_attempts, w1_master_max_slave_count, w1_master_name, w1_master_pointer
w1_master_pullup, w1_master_remove, w1_master_search, w1_master_slave_count, w1_master_slaves & w1_master_timeout
When a 1-wire device is added it should show up here as well
Connect a 1-wire temperature sensor:
Connect VCC to 3.3V,
ground to ground and
Data to GPIO #4.
Then connect a 4.7K resistor (yellow, violet, red) from Data to VCC (3.3v).
Repeat the 'examine the system' steps and the temperature sensor should show up.
Here is a temperature measurement:
enter this command:
cat /sys/bus/w1/devices/w1_bus_master1/28-0000018a4be8/w1_slave
this returns:
49 01 4b 46 7f ff 07 10 f6 : crc=f6 YES
49 01 4b 46 7f ff 07 10 f6 t=20562
giving a temperature of 20.6 C (to 1 decimal place)
Log the data:
Start by creating a folder to hold the result files
sudo mkdir /var/1w_files
cd /var/1w_files
make a file for each device using its unique id
e.g. touch 28-0000018a4be8
Move back to the folder before:
cd ..
change ownership & permissions of all your new device log files:
sudo chown -R <username> 1w_files
sudo chmod -R 0760 1w_files
Create a logging script:
# gets all temperature sensor names from directory /sys/bus/w1/devices/w1_bus_master1/
# calls the conversion routine w1_slave for each device
# grep returns the line containing the temperature
# and sed gives us the end of the line following t=
#
# a formatted date & time is added to the result
# and the result is output to files with the device names in folder /var/1w_files
#
# save this file in your home directory - matching the path you used in crontab -e
#
# The script has been enhanced to catch known error conditions such as no temperature conversion
# by the sensor and CRC failures.
# A second read is attempted if there is one of these failures - but a repeat failure results in the
# sensor being ignored for the current read cycle so that a faulty sensor doesn't stop the whole
# process. A log is kept of errors, including date/time and sensor id.
#
The actual script is in the home/<username>/ folder and is named:
1w_logging.sh
Code: Select all
#!/bin/bash
# a script to read and save temperature readings from all the group 28 1-wire temperature sensors
#
# get all devices in the '28' family
FILES=`ls /sys/bus/w1/devices/w1_bus_master1/ | grep '^28'`
# iterate through all the devices
for file in $FILES
do
# get the 2 lines of the response from the device
GETDATA=`cat /sys/bus/w1/devices/w1_bus_master1/$file/w1_slave`
GETDATA1=`echo "$GETDATA" | grep crc`
GETDATA2=`echo "$GETDATA" | grep t=`
# get temperature
TEMP=`echo $GETDATA2 | sed -n 's/.*t=//;p'`
#
# test if crc is 'YES' and temperature is not -62 or +85
if [ `echo $GETDATA1 | sed 's/^.*\(...\)$/\1/'` == "YES" -a $TEMP != "-62" -a $TEMP != "85000" ]
then
# crc is 'YES' and temperature is not -62 or +85 - so save result
echo `date +"%d-%m-%Y %H:%M:%S "; echo $GETDATA2 | sed -n 's/.*t=//;p'` >> /var/1w_files/$file
else
# there was an error (crc not 'yes' or invalid temperature)
# try again after waiting 1 second
sleep 1
# get the 2 lines of the response from the device again
GETDATA=`cat /sys/bus/w1/devices/w1_bus_master1/$file/w1_slave`
GETDATA1=`echo "$GETDATA" | grep crc`
GETDATA2=`echo "$GETDATA" | grep t=`
# get the temperature from the new response
TEMP=`echo $GETDATA2 | sed -n 's/.*t=//;p'`
if [ `echo $GETDATA1 | sed 's/^.*\(...\)$/\1/'` == "YES" -a $TEMP != "-62" -a $TEMP != "85000" ]
then
# save result if crc is 'YES' and temperature is not -62 or +85 - if not, just miss it and move on
echo `date +"%d-%m-%Y %H:%M:%S "; echo $GETDATA2 | sed -n 's/.*t=//;p'` >> /var/1w_files/$file
fi
# this is a retry so log the failure - record date/time & device ID
echo `date +"%d-%m-%Y %H:%M:%S"`" - ""$file" >> /var/1w_files/err.log
fi
done
exit 0
Note the back ticks ` in the script - these are NOT replaceable by single quotes '.
The line starting 'FILES' gets all the temperature sensor device names from the folder 'w1_bus_master1/' (family code 28)
The lines starting GETDATA= get the two lines of data for each device then GETDATA1 gets the line with the crc and GETDATA2 gets the line with the temperature.
Make the 1w_logging script accessible
sudo chown <username> 1w_logging.sh
sudo chmod 0750 1w_logging.sh
Create a cron job to run the logging script at appropriate intervals
enter this command: (if crontab has not been used before it will ask you to select which text editor to use)
crontab -e
add this line
*/5 * * * * /home/<username>/1w_logging.sh
write/save & close the file - depending on text editor being used
That's it
I hope this helps
Regards
anita2R