Sender

Hardware setup

I've just used an old plastic food carton and cut holes in the side and front for the cabling and camera. As this box is outside I've kept the number of holes to a minimum. It's worth having long enough jumper cables so the temperature sensor can hang outside the box. I was getting slightly high readings when I had it in the box with the lid on due to the small amount of heat produced by the Pi.

Some duct tape holds the camera in place behind the hole.

Four female to female jumper cables are all you need to wire up the MCP9808. This guide has a nice diagram explaining the connections: MCP9808 temperature sensor raspberry pi

Sending snapshots to Adafruit.io

First create your adafruit.io account, here's the guide if you're stuck: Adafruit.io getting started

Now follow this guide, Motion setup but don't edit any of the settings in motion.conf we're going to use a different set of configuration in this project to simply send a snapshot every 5 minutes. When you get more familiar with Motion you can change the configuraiton to do motion detection to upload videos to the cloud and all sorts as well as the 5 minute snapshot if you wish.

You will likely also want to have Motion running on boot up so follow this section of the guide too.

Next we need to install the uploader code for this project. First, install the required packages:

cd ~
sudo pip install adafruit-io
sudo apt-get install -y libjpeg-dev python-dev
sudo pip install Pillow --upgrade

Now get the project code. The adaiot project includes all the code for the sender and the receiver but at this stage we're interested in the image uploader script.

[email protected]:~ $ git clone https://github.com/jerbly/adaiot.git
Cloning into 'adaiot'...
remote: Counting objects: 13, done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 13 (delta 0), reused 0 (delta 0), pack-reused 8
Unpacking objects: 100% (13/13), done.
Checking connectivity... done.

Get your secret AIO key and user name from Adafruit.io and edit the code in adaiot-uploader.py. Replace the word SECRET with your secret AIO key.

#!/usr/bin/python

from Adafruit_IO import Client
from PIL import Image
import base64

aio = Client('SECRET')
BASE_DIR='/var/lib/motion/'
SRC_FILE=BASE_DIR+'lastsnap.jpg'
DST_FILE=BASE_DIR+'lastsmall.jpg'

fd_img = open(SRC_FILE, 'r')
img = Image.open(fd_img)
size = 320, 240
img.thumbnail(size)
img.save(DST_FILE, img.format)
fd_img.close()
 
with open(DST_FILE, "rb") as imageFile:
    str = base64.b64encode(imageFile.read())
  
aio.send('pic', str )

The script is fairly simple. First of all it logs into adafruit.io using the REST client. We don't need MQTT here because we're simply sending a value in to a feed. We then look for the lastsnap.jpg file and use the python image library (PIL from the Pillow package) to create a smaller image lastsmall.jpg. In the next part of this project I'm using a 320x240 screen to display the feed so that's why we're resizing in this script. Of course, we could set Motion to 320x240 and then we wouldn't need the resize but, as I said earlier, you're likely going to want to use Motion for a few different purposes and 320x240 is pretty small! Finally the code converts the output jpeg to base64 and sends it to the pic feed.

Now we need to set up Motion to take a snapshot every 5 minutes and call this script when it does. So change some settings in /etc/motion/motion.conf

sudo pico /etc/motion/motion.conf
width 1024
height 768
output_pictures off
snapshot_interval 300
snapshot_filename lastsnap
on_picture_save /home/pi/adaiot/adaiot-uploader.py

Restart the motion service so the changes take effect:

sudo service motion restart

Now we can set up the adafruit.io dashboard to display the feed:

  1. Log in and select "Your Dashboards"
  2. Click "Create Dashboard"
  3. Give it a name and create it
  4. Click on the + icon to add a block
  5. Choose the image block
  6. Choose the "pic" feed

You should now see the snapshots from the camera sent every 5 minutes. At the time of writing the image block cycles through all the images received during the browser session, don't be alarmed!

OK, next we need to send the temperature too...

Sending temperature readings

This guide, MCP9808 Temperature sensor python library, has instructions on how to set up the python library but it boils down to these few commands below:

cd ~
sudo apt-get install -y python-smbus
git clone https://github.com/adafruit/Adafruit_Python_MCP9808.git
cd Adafruit_Python_MCP9808/
sudo python setup.py install

Edit ~/adaiot/adaiot-temp.py and put your AIO key instead of SECRET.

import time
import Adafruit_MCP9808.MCP9808 as MCP9808
from Adafruit_IO import Client

aio = Client('SECRET')
  
sensor = MCP9808.MCP9808()
sensor.begin()

def get_temperature():
        temp = sensor.readTempC()
        return '{0:0.3F}'.format(temp)

while True:
    try:
        aio.send('deck-temp', get_temperature() )
    except:
        print "Failed to send"
    time.sleep(30)

This script uses the REST client again as we're only sending. In a forever loop it simply reads the current temperature from the MCP9808 and formats it to 3 decimal places. The value is then sent to the deck-temp feed. You can change this feed name if you wish. I have this sensor out on my deck.

To set the temperature sender to run automatically when you reboot the Pi edit the /etc/rc.local file like so:

sudo pico /etc/rc.local
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

python /home/pi/adaiot/adaiot-temp.py &

exit 0

Now we'll go back to the dashboard and add a gauge block. Choose the deck-temp feed. (Or the feed name you changed it to). Set the min and max values appropriately. The code above sends the temperature in celcius. I live in Canada near Toronto so I need a pretty wide range between min and max.

Hit create and you should see the new block on the dashboard next to your image block.

Last updated on 2016-04-09 at 01.40.21 PM Published on 2016-04-12 at 01.12.03 PM