Sunday, January 10, 2016

Adding a sensor zone to Sphaera

My Sphaera project is  nice and useful when used alone, but I designed it to be expandable quite easily by adding more Raspberry Pis to the LAN

Also, with the new extremely low cost Raspberry Pi Zero You can add several expansions at an affordable price without sacrifying system performance, as this new board has the same performances of an A+ model.

In this article I will show you just one example of configuration, but adding a new Raspberry Pi would be almost the same for any number of them.

It will be a short and surely not complete description, but could give you a starting point for building something similar. Anyway if you have some question about it, just ask.

Before starting, just few words about my Sphaera configuration...

Sphaera project has a bunch of software installed:
  • SOX and PicoTTS for speech
  • Shairport for audio AirPlay
  • Driver for Neopixels
  • Mosquitto as MQTT server
  • paho-mqtt as MQTT python client
  • Apache and PHP5 for webserver
  • mjpg-streamer for the PiCamera

I programmed a small web interface useful for both small and big screens and few simple python script for the more complex tasks and that's all.

The web interface can manage a number of "zones". Every zone is nothing more than a Raspberry Pi with its sensors. For example I will have the following zones:
  • Restroom
  • Bedroom
  • Porch
  • Outside

Every zone has an id number to identify it and of course a name. These zones are hard coded inside the web interface, so if one day I will decide to add a different zone I will need to reconfigure it, but this is not a big problem for me.

To make a new RPi communicate with Sphaera, I use the MQTT protocol. As Sphaera is a MQTT server, all of the data will stay inside my network and only selected info will be accessible on the internet.


Let's take a look at the porch zone (zone number 4 for me).

This RPi uses the following sensors:
  • PiCamera
  • Humidity and temperature (Adafruit HDC1008)
  • Digital barometer (Adafruit MPL115A2)

The PiCamera is needed because my cats stays in the porch when I'm not at home, so I can take a look on them when away. I added a wide angle lens to see the whole room just like I did with Sphaera.

Humidity and temperature are used just to check the ambient parameters in the room. I will put them in every zone (room) of my house.

The barometer will be used for simple weather conditions and forecast (the porch is closed, but has a couple of vents, so I can measure the real outside pressure here).

The two sensors use I2C, so you just need 4 wires to connect to the RPi: +3.3, gnd, sda and scl. The signals for both the boards goes to the same pins in the RPi GPIO header.
First of all I needed a case for it. I decided to place it on a side wall quite high, so my cats cannot reach it and the camera can see almost everything. Just take a look at some photos:


The blue box is my case. I created it with my 3D printer (one of the most useful "gadgets" I've ever bought!). Now let's see some detail:


On the left you can see one of the hinges that keep the box closed. The other two photos show the inside. You can clearly see the RPi (model B+) with a wi-fi dongle (actually I could have used an A+ for this). on the top of the middle photo there is the power adaptor (I used a 12V power plug, so I needed to lower if for the RPi).

In the center of the last two photos there is the PiCamera and the wide angle lens. Finally, on the left you can see the two sensor boards, placed against two holes.


Now the software part...

This Raspberry has two daemons started at boot: the mjpg-streamer and the following python script:

import time, os, sys, subprocess
import paho.mqtt.publish as publish
from humidity import GetHumidity
from pressure import GetPressure
from common import *

zone=Zone4

if __name__ == '__main__':
    while (True):
        # Temp and humidity
        rh,t=GetHumidity()
        publish.single(topic, zone+","+temp+","+t.strip(), hostname=MQTT)
        time.sleep(.1)
        publish.single(topic, zone+","+hum+","+rh.strip(), hostname=MQTT)
        # Pressure
        p=GetPressure()
        publish.single(topic, zone+","+press+","+p.strip(), hostname=MQTT)
        # CPU temp
        try:
            s=subprocess.check_output(["/opt/vc/bin/vcgencmd","measure_temp"])
            tc=s.split('=')[1][:-3]
        except:
            tc='-'
        publish.single(topic, zone+","+cpu+","+tc.strip(), hostname=MQTT)
        # Waits one minute
        time.sleep(60)

As you can see, the script is quite simple. I created some script that can be imported by every RPi that needs them, but they are also short and simple (umidity, pressure and common).

This script just reads the sensors values and publish them to the MQTT server. One more value used, but not read by sensors is the CPU temperature, as I wish to keep this parameter under control.
I read the values every minute as more frequent reading is really not necessary.

Sphaera then reads the published values and make them available for the web interface.

Finally let's take a look at the web interface. Please just note that I'm a Star Trek fan...

As this is an access to my home, the whole interface is password protected. To make it even more secure, there are several passwords and every time one is chosen at random, giving a criptic clue to me so that I can give the correct answer.

Here is the login screen and the error one if the password is wrong:



The main screen is different for large and small screens. With large ones I can use a map of the house:


The webcam interface is almost the same for both types of screens. Just the view size is different, of course:


Note that I have 3 webcams available: Zone 0 (Sphaera), Zone 1 (outside) and Zone 4 (the porch).

Now the sensors page. This page is the only one not in "Star Trek" style, but I will convert it soon:


Only Zone 4 has values, because it's the only active zone for now. Other zones will be activated in the next few days.

Using MQTT is a simple but powerful way to make communication with several RPis, and it can be quite useful also inside a single Raspberry (I'm using this method to exchange data between python scripts and web interface.

Maybe the result it's not a real professional system, but it's easy to build and setup and can be fully customized and expanded in few steps.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.