Jun 042015

So I got a bit sidetracked from my tear down of the Ritmo CE-3590 NAS, and ended up building a temperature sensor with some DS18B20s and a Raspberry Pi.

Today’s post will detail how I configured the Pi to output the temperature values to the SNMP agent that I’m running on the Pi.

Plugging The Sensors In

Since I used the DS18B20s, they are polled over a single wire with a 4K7 resistor across the VCC and Data wires.
I ended up using a whole bunch of CD Audio cables like this one –
CD Audio Cable
to create the wiring for the sensors. If I had the pins to use between the cables, I could have just joined cables together, but as I didn’t, I cut and soldered multiple cables together. At the end of the wires that will plug into the Pi, I spliced in the 4K7 resistor and heatshrinked it so hopefully it won’t short anything out.
Temp Sensor Wires
It ended up very messy, but it seems to have worked.

I used the Red wire for 3.3V, the white wire as data, and the black as ground.
Red then gets plugged into the Pi header at Pin 1 which is 3.3V power
White gets plugged into Pin 7 which is GPIO 4
Black gets plugged into Pin 6, which is a ground. This could be any of the other grounds, but this is the closest one.

Configuring the Pi

I’m using Raspbian on my Pi, so any instructions are done with a Raspbian flavour. Most distros should have the same setup though.
The Pi needs to be configured to use the w1-gpio device tree overlay (dtoverlay).
This is done by adding the line dtoverlay=w1-gpio to the end of the config.txt file on the /boot partition.
Once this dtoverlay has been activated, you should be able to see your sensors under /sys/devices/w1_bus_master1/. They should appear as 28-0215**. On my Pi, the sensors appear like so –


Catting one of the w1_slave nodes under the sensor should output something like this –

57 01 55 00 7f ff 0c 10 05 : crc=05 YES
57 01 55 00 7f ff 0c 10 05 t=21437

The YES confirms the sensor is working, and the temperature is displayed after the t=
In this example, the temperature is 21.437 Degrees Celsius. The output needs to be divided by 1000 to get the temperature to the correct amount of decimal places.

Configuring the SNMP agent

With the sensors plugged in, it’s time to extend the SNMP agent to allow us to poll the temperature sensors over SNMP.
I’ve written a script that will output just the temperature to the SNMP agent, I’ve placed this script in /opt/scripts but you’re free to put it wherever you want. You’ll need to keep the location handy for when we extend the SNMP agent.
I’ve named this script gettemp.sh

# Usage: gettemp.sh
# e.g. gettemp.sh 28-0215012018ff
OUTPUT=$(/bin/cat $SLAVE | /usr/bin/awk -F 't=' ' { printf $2 } ')
echo $OUTPUT

With this script, you should be able to call the script from a command line, and it should output the temperature for that sensor.

# /opt/scripts/gettemp.sh 28-021500ce50ff

With this script operational, we’ll need to setup the SNMP agent to run this script to provide us the output of this script over SNMP.
To do this, we need to modify /etc/snmp/snmpd.conf. At the end of the file, put in this line –
Make sure you replace the sensor id with your own sensor id. If you want to monitor more than one sensor, add more extend lines, and give them differing names, like temp2, temp3, etc.

extend . temp1 /bin/bash /opt/scripts/gettemp.sh 28-021500ce50ff

After you modify the snmpd.conf file, make sure you restart SNMP agent with /etc/init.d/snmpd restart or service snmpd restart.

Once the SNMP agent is back up and running, you should be able to poll the OID we specified above (. and get the output of the command. I’ve run the snmpwalk on the Pi itself, polling itself.

# snmpwalk -v 2c -c public .
iso. = INTEGER: 1
iso. = STRING: "/bin/bash"
iso. = STRING: "/opt/scripts/gettemp.sh 28-021500ce50ff"
iso. = ""
iso. = INTEGER: 5
iso. = INTEGER: 1
iso. = INTEGER: 1
iso. = INTEGER: 4
iso. = INTEGER: 1
iso. = STRING: "21437"
iso. = STRING: "21437"
iso. = INTEGER: 1
iso. = INTEGER: 0
iso. = STRING: "21437"

As you can see from the example, the temperature is output in a couple of different places.
Polling the SNMP agent with one of those locations directly should yield only the temperature.

# snmpwalk -v 2c -c public .
iso. = STRING: "21437"

With that, we now have SNMP set up on the Pi, ready to be polled for the temperature.


  6 Responses to “Temperature Monitoring With A Raspberry Pi and Zabbix – Part 1”

  1. […] the previous post, I got the Raspberry Pi outputting the values from the temperature sensors via […]

  2. Im stuck after snmpd service restart
    When i try to snmpwalk get this message:
    iso. = No more variables left in this MIB View (It is past the end of the MIB tree)

    Is ISO number i use from the example correct or should i find one for my sensor which is defferent then the one you use…

    • Can you poll the sensor using `cat`-ing /sys/devices/w1_bus_master1/28-02150xxxx ?

      Have you extended the SNMP agent already ?

      The SNMP OID that you need to use is the one you specified when you extended the agent with the line extend . temp1 /bin/bash /opt/scripts/gettemp.sh 28-021500ce50ff

      • Thanks for your reply. I had to make a change in the “acces control” of the snmpd.conf file I added/included the . and now it works and also part 2 is working Thanks!
        It was not possible for me to fix this withouth your blog

        If possible i would like to know why you use the OID. dont know a lot of snmp so if you can give me more explanation or a website, which help me understand this please ..

  3. Wanted know do i can use any wire to build it??

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>