Friday, February 6, 2015

Pi-Temperature

Raspberry Pi's are a great little tool for learning. Software people can learn a little hardware and hardware people can learn a little software. I recently got a Raspberry Pi B+ kit and have been messing around with it lately to learn a little hardware.

My goal was to attach temperature sensors and have the Pi report back the temperature to me.

So, I went out and bought the Vktech DS18b20 sensors and attached them to a GPIO port. Once, the sensors have been connected, you need to login and run the following 2 commands to have Linux setup and probe them:

 sudo modprobe w1-gpio  
 sudo modprobe w1-therm  

Now, once those commands are ran, the temperatures will show up in following files:

 /sys/bus/w1/devices/w1_bus_master1/<serial id>/w1_slave  

where <serial id> is the serial id of the sensor itself. An example of one of these files is:

 4d 01 4b 46 7f ff 03 10 d8 : crc=d8 YES  
 4d 01 4b 46 7f ff 03 10 d8 t=20812  

Now, in that file, the Celsius temperature is actually the last little bit, 20812 - it just needs to be divided by 1000 and you have your Celsius temperature of 20.8.

Great, it's now working. Linux is probing the sensor and reporting it to a file which I can read and see. It works, but not that practical. At this point I wanted to see if I could automate this process so I didn't have to manually read the file each time I want to know the temperature.

Meet pi-temperature, a Java library I wrote to solve this exact problem. It probes all attached sensors for me at a given interval (defaults to 1 min) and can report the temperatures of each sensor back to the user. It utilizes spring boot to startup a local Tomcat instance for a web server and has several REST urls attached:

 /sensors/list  
 /alerts/list  
 /alerts/setOn/<name>  
 /alerts/setOff/<name>  
 /alerts/update/<name>  

The /sensors/list is the primary endpoint which reads all of the sensors and reports back the temperatures in both Celsius and Fahrenheit. The /alerts endpoints are currently a work in progress and another post will be coming soon with their features.

Specific instructions on how to build, run, and more thorough instructions can be found on the pi-temperature project's README.