Saturday, December 29, 2012

sensor monitoring with arduino and raspberry pi

Thought I would change my experiment a little bit. Previously I had grabbed the DHT22 from Adafruit and hooked it up to my raspberry pi. This worked fairly well and with some modifications to their driver code I was getting fairly reasonable stats albeit running in to a minor driver issue ( getting stuck in an unbounded loop )

In the mean time I've been thinking about how I would want to handle my remote temp/humidity sensors project. The main idea so far is to put a sensor in every room and aggregate the data in to graphite ( via collectd ). The cost of the sensor plus the heft of the full linux box for a simple little sensor started bugging me - the sensor side of things is actually better off running on something like an Arduino - its very low power, commonly available and flexible enough to do what I want in the long run. Sadly, for the price/power ( cpu/memory ) the difference in price is kinda off but the platforms really are rather different.

I probably went overkill on the arduino side - picked up an R3 Mega - an Uno or maybe even the Micro may be better suited but I grabbed the Mega as I have other projects in mind for the future and thought I'd get this instead.

Adafruit has another tutorial for the same sensor but on an Arduino including a driver ( and looking at the source, I think their pi version was based off of this code ). Setting it up on the arduino was surprisingly very very easy!

The idea is to read the sensor metrics via serial ( eventually switching to XBee for RF/wireless <=> serial. For now, I'm just using jumper wires from the Mega in to the serial port of the pi ( Serial1 on the Mega )

I found a few interesting things:

  1. The Serial.read does not block as I thought it would/should
    • Idea was to control the collectd Interval from the exec/c code - can't do that ( at least not yet )
  2. Even though I don't do a Serial.setup() trying to write to Serial1 blocks unless I have a usb connection from the hub ( which is powering the Mega ) to some system. Not sure what that is about
The driver code is fairly simple. Basically just a very slightly modified print statement to only print the temp/humidity over the serial port. On the pi I have a little ruby script exec'd by collectd which is in a read loop and ( admittedly rather blindly ) spits out the values:


while 1
  stats = sp.gets.chomp.split()
  if stats.size > 1
    temp_c   = stats[1]
    temp_f   = ( temp_c.to_f * 9 / 5 ) + 32
    humidity = stats[0]

    puts "PUTVAL \"#{hostname}/sensor-basement/gauge-tempc\" interval=10 N:#{temp_c}"
    puts "PUTVAL \"#{hostname}/sensor-basement/gauge-tempf\" interval=10 N:#{temp_f}"
    puts "PUTVAL \"#{hostname}/sensor-basement/gauge-humidity\" interval=10 N:#{humidity}"
end

So fairly simple and like I said, its rather blind but I control the input to some degree and collectd will whine if the data is not normalized

Next step is to see if I can find/assemble the right sized arduino/case and find a way to get a connector for the sensor so I can swap it out ( and not get funky/false readings ) and get some of the XBee chips to play with.

No comments: