[last updated: 2017-09-29]
go to: GMC UI project
-----
- Setup:
- I started with Adafruit tutorial to "Add OneWire support"
I could not make it work. it seemed a little sketchy/incomplete/ambiguous in places
- Did another google search and found:
(link to:) modmypi.com
it says that GPIO#4 is the dedicated pin on the rPi for one-wire sensing. Swell. That critical detail was not mentioned (at least that I found...) in Adafruit docs.
Had to re-wire the power shield since I'd used that pin for the heartbeat LED
- Starting over with modmypi instructions:
- wired DS18B20 to 3.3v, gnd, and GPIO#4, with 4.7k pullup
power-up the rPi
- enable i2c:
open LXT and execute:
sudo raspi-config
scroll down to interfacing options, press enter
scroll to i2C, press enter
tab to "yes", press enter
"OK" is highlighted, press enter
tab to Finish, press enter
- enable 1-wire library:
sudo nano /boot/config.txt
scroll to bottom of file and add:
dtoverlay=w1-gpio
exit nano with: ctrl-x, Y, press enter
reboot
- this is how the sensor works with the rPi:
- Once set up this way, when you plug in a new sensor, (it magically happens that...) the 64-bit unique ID of the sensor is read, and a directory is created named with that id. The sensor directories are located in: /sys/bus/w1/devices/ on your rPi.
The sensor directories will be named: "28-" followed by 12 hex characters.
This will happen (magically, like I said...) without any python program instruction or initiation.
Further, if you unplug the sensor, then its directory will be removed (after a time-out or at the next reboot).
- The process of the sensor reading the temperature and sending that data back to the rPi via the one-wire protocol is also automatic and free-running,
that is, no python program instruction or initiation is necessary. Sensor data is written to a file automatically,
and the python program just accesses/reads that file, then parses the data to extract the numerical temperature recorded.
- When the sensor takes a reading, it creates two text lines in a file named "w1_slave" located in the sensor directory. If the reading is a valid one (as determined by matching the crc error detection code), then the last 3 characters of the first line will be "YES".
- At the end of the second line will be "t=" followed by an integer that is the actual temperature in degC multiplied by 1000.
So eg. if the end of the second line reads: "t=20125", the temperature measured was 20.125 degC
- The python program reads those two lines, and if "YES" is present at the end of the first line, it extracts the number following "t=" and calculates the temperature read.
- However: the modmypi code uses an infinite while-loop, waiting for valid data. That is, if eg a sensor fails or is unplugged, your whole program locks up waiting for data that will never arrive. Accordingly I rewrote it.
- here's how the python program works to extract the actual temperature from the sensor data:
- The modmypi program (significant details of which at least...) is here: [or will be at some point...]
- to start, the path and filenames where the python program will look for the data are defined.
Remember, we'll be looking for files named like this:
/sys/bus/w1/devices/ [15-hex-char-sensor-ID directory name] /w1_slave
- The modmypi program finds the sensor-ID directory name using the "glob" method.
- The glob.glob statement returns a list of files that match the filter criteria ('28*' in this case)
In the original modmypi program, only the first element (index [0]) was used. This is therefore assuming that you had a single-sensor system.
However I am using multiple sensors, so have edited the program accordingly.
- [link to my program - coming soon]
.
.
.
eof