My Pi Description

My Experiences With the Raspberry Pi -- Tracking My Learning -- My Pi Projects

Monday, June 24, 2013

A Raspberry Pi Thermometer - Interpreting the Data

The W1-gpio and W1-therm modules create the following directory structure: /sys/bus/w1/devices followed by a directory tree for each 1- Wire device on the bus. These directory names are ids of the devices in hex. The DS18B20's ids start with 28-, so the two directories of my devices have the names 28-00000400d39d and 28-000004986cbb. Each of these directories contain a file called w1_slave. When queried, the DS18B20 returns 9 bytes of data which is placed into the w1_slave file. We get our temperature by reading this file.
w1_slave is not a file like a text or python file. Successive reads can contain different data, but the modified date/time of the file does not change. It's modify date/time reflects the date/time that the W1-gpio and W1-therm modules were loaded into the kernel.
Let's take a look at the contents of the w1_slave file. The following consists of two successive readings of that file using the cat command:
Notice the top line, everything before the "crc=". This is the output from the device in hex. The first two bytes represents the temperature in LSB followed by the MSB. Here, the temperature is 0166h. Convert that to decimal and multiply that number by 62.5 and you get 22375 which you see on the second line after the "t=". Divide this number by 1000 to get the temperature, 22.375 in degrees centigrade. The last byte returned is the CRC (cyclic redundancy check) which is calculated by the device based on the other eight bytes. Following the "crc=" you have the CRC calculated by the software. If that matches the CRC returned by the device, the software says "YES", otherwise "NO".
Note the second reading of w1_slave. Here the file says "NO". Note that the CRC returned by the device is 2Dh as before, but the software calculates ECh. Also note that the seventh byte returned by the device is 2Ah not 0Ah. The sixth, seventh, and eighth bytes are called "reserved" in the DS18B20's documentation. No further details are given except that they can not be overwritten. The sixth byte is always FFh and the eighth byte is always 10h. It does not say what the seventh byte should be. But, I have noted that, usually, if there is a "NO" at the end of the first line, the seventh byte is 2Ah, if "YES" it is always 0Ah. I say "usually" because, once in a while, we get a really lousy return when reading w1_slave. It looks like this:
I indicated in my previous post that this reading of w1_slave "almost works", and I'm not talking about whether there is a "YES" or a "NO" in the file, but something worse. I can make 10,000, or 20,000, successive measurements by reading w1_slave, and the next attempt will fail. Python says the file does not exist, but when you look, the file is still there. A very frustrating problem. I "think" I have solved that problem by unloading and reloading the W1-gpio and W1-therm modules, within my programs, when this failure is detected.
Enough of this. The next post will reveal the python program I wrote to use these temperature sensors.

No comments:

Post a Comment