Finally, after visiting the end of the internet and talking with countless people about hacking code up, I think I have put together a decent little system for polling temp readings off of a digital DS18B20 probe. Ill attempt to map out everything I have done from start to finish. Here goes:
Current Setup:
I got the Wheezy build from
here and followed their instructions on how to put it on the SD card in the paragraphs above the download section. Depending on what OS you are using, there are obviously different programs to use.
I hooked the Pi up to a screen, keyboard, mouse and network, stuck the SD card in and, after a little initial setup, i was off and running. I gave it a static by adding these lines to the /etc/network/interfaces file:
iface eth0 inet static
address IP ADDRESS
netmask NETMASK
gateway GW
dns-nameservers DNS
I commented out the line that said "iface eth0 inet dhcp" because we don't need it for a static. Either one or the other. Now, either restart the Pi or restart the daemon for the network settings to take effect. I've had more luck rebooting....
Following directions from
here,
Step One: Updating the Kernel
- The first step is to change where our Pi updates from, by editing a text file. We need to open up the file /etc/apt/sources.list.d/raspi.list as root, so type:
sudo leafpad /etc/apt/sources.list.d/raspi.list
- Now change the line in this file so that it reads "deb http://archive.raspberrypi.org/debian/ wheezy main untested", then save and close the file.
- Next, do the following commands:
apt-get update
apt-get upgrade
Next comes wiring the probe and connecting it to the Pi. Using the directions
here, I wired it to look similar to this pic:
Obviously, it can be set up any way but it must be connected correctly. I'm talking about using different pins on the breadboard and placement and such.
After that is done and the ribbon cable is connected, I powered the device on. You should feel the probe during this initial power-up. If it gets hot, which it will within seconds, turn it off and check your wiring.
Running these two commands:
Sudo modprobe w1-gpio
sudo modprobe w1-therm
will fire up the probe. These will need to be put in a start-up file upon boot if ever the device is restarted. After those two commands complete, it will be dumping data into a file located at
/sys/bus/w1/devices/SERIAL OF PROBE/w1_slave
The serial of the probe can be found by doing an "ls" inside the /sys/bus/w1/devices folder. In the pic at the top, I found what that number was and made a yellow label for it since that serial will never change. Easier to label them rather than trying to remember! :)
Running "cat w1_slave" inside that serial folder will produce two lines of output. the very last few digits will be the temperature in Celsius. Its at the thousands place so the python code further down that links page will tell us how to divide by 1k.
The following code is taken from that page with a little tweak at the end to convert it to Fahrenheit:
# Open the file that we viewed earlier so that python can see what is in it. Replace the serial number as before.
tfile = open("/sys/bus/w1/devices/28-00000474e0b1/w1_slave")
# Read all of the text in the file.
text = tfile.read()
# Close the file now that the text has been read.
tfile.close()
# Split the text with new lines (\n) and select the second line.
secondline = text.split("\n")[1]
# Split the line into words, referring to the spaces, and select the 10th word (counting from 0).
temperaturedata = secondline.split(" ")[9]
# The first two characters are "t=", so get rid of those and convert the temperature from a string to a number.
temperature = float(temperaturedata[2:])
# Put the decimal point in the right place and display it.
temperature = temperature / 1000
# Convert Temperature to Fahrenheit
temperature = (temperature * 9) / 5 + 32
# Print Temperature
print int(temperature)
The 28-00000474e0b1 part in that first line is the serial of MY probe. That will be different for you, of course. Copy and paste that, using your serial number, into a file called temp.py and place it wherever. to run it, type:
python temp.py
and it should return a rounded-like (because of the int() statement) integer that is the Celsius degrees converted to Fahrenheit. The probe works and the temp is being converted. That step is done.
Once we have all that done, we can install SMNPD and get it to respond to queries from a remote machine. This proved to be the most difficult for me because, up until this point, I always piggybacked on an existing SNMP install with the OIDs already there. Building this from scratch was definitely tricky for an SNMP nub like me.
So, taking directions from a lot of places, Ill post links to the most helpful places here.
Following directions from
here, I installed SNMPD by running:
sudo apt-get update
sudo apt-get install snmpd
sudo apt-get install snmp
Im not 100% sure if i needed the client portion, but I installed it anyway during troubleshooting steps.
Please be aware that this is for a totally internal setup with no outward facing access therefore its pretty wide open. You'll want to check up on securing your SNMP install if you choose to have it facing the internet.
You'll need to edit your snmpd.conf file by typing:
sudo nano /etc/snmp/snmpd.conf
Here is where it got tricky. I had a line in there that read:
agentAddress udp:127.0.0.1:161
which i commented out and added this line below it:
agentAddress udp:161
which I'm assuming from what I read allows for any machine with explicit access to get queries. Then, further down in that config, I found the rocommunity lines. I added these lines:
rocommunity public IP OF REMOTE MACHINE
rocommunity public localhost
not sure if that last one was there or not to begin with. Then commented out:
rocommunity public default -V systemonly
just for good measure! :) it looked like it might have caused a problem. Not sure if it was needed but, again, SNMP newb here. Make sure you restart the daemon to make your changes stick:
sudo /etc/init.d/snmpd restart
once those are done, from the local machine, you should be able to type:
snmpwalk -v 1 -c public localhost
and it will throw a ton of stuff at you from the console. you can also try that same command from a remote machine instead putting in the IP of the Pi rather than localhost and it should do the same thing.
From this moment on it gets a touch more tricky. Getting an OID to match to the temp probe output. remember the temp probe? yeah its been a while....
Continuing to follow the instructions
here, you must do a few things which I will list:
- make a script to call the python file we created in the first steps above
- config a new OID inside the snmpd.conf to point to this new script
Easy. Kinda. If you originally knew what you were doing....
I grabbed the executable from that link above and changed it a bit:
#!/bin/bash
if [ "$1" = "-g" ]
then
echo .1.3.6.1.2.1.25.1.8.0
echo gauge
python /usr/local/bin/temp.py
fi
exit 0
copy and paste that above into a file called snmp-temp. Then run this:
sudo chmod 755 snmp-temp
which will make it executable and try running it:
./snmp-temp -g
Please notice that I changed the directories where these files are located which you will need to do as well for this to work. If they are all in the same place calling the same files, you should be ok. SNMP likes them to be in that /usr/local/bin directory. i was NOT able to run them out of my home directory where i was originally building these files. i moved both the python scripts and the shell scripts to that location and everything is working fine.
once you run that file, you should get output in three lines:
1.3.6.1.2.1.25.1.8.0
gauge
OUTPUT TEMP FROM PROBE
Now that that is working, we need to add a line to our snmpd.conf file to join that script with that OID. So... Edit the snmpd.conf:
sudo nano /etc/snmp/snmpd.conf
and add this line down where the other "PASS" lines are:
pass .1.3.6.1.2.1.25.1.8.0 /bin/sh /usr/local/bin/snmp-temp -g
Again, please remember that I moved files around to that /usr/local/bin directory.
So.... Now if we run this (either locally or from the remote machine - ill use localhost for this example):
snmpget -v 1 -c public localhost .1.3.6.1.2.1.25.1.8.0
it should return:
iso.3.6.1.2.1.25.1.8.0 = Gauge32: CURRENT TEMP FROM PROBE
So for understanding, this is what happens:
- snmpget goes out via version 1 looking for the public community on localhost for the OID labeled .1.3.6.1.2.1.25.1.8.0
- Once it finds that OID in the snmpd.conf, it runs the associated script called snmp-temp inside /usr/local/bin
- That script calls the python script that actually does all the work; grabbing the output of the probe, parsing all the gibberish and converting it to Fahrenheit.
- snmp-get returns the results of the temp.py into a nicely formatted snmp query that can then be put into any program.
I use nagios to poll a ton of other devices so this was an easy setup inside nagios once the output was generated and the ability to query against it was done.