Duckling and Chick Camera with MQTT/Mosquitto and Raspberry Pi

Charlotte Dorn
5 min readMar 31, 2020

--

The goal of this project was to publish a live, low bandwidth, photo feed of a chick and duckling pen using a Raspberry Pi.

note: weird coloration due to heat lamp

Materials

Required: Raspberry Pi and a Pi Camera

I also used my 2014 Macbook Pro for hosting and an Android cell phone for mobile viewing. We have no internet connection here, but a cellular hotspot worked fine to connect devices and publish to the web.

Background:

So you want to send data from a small wifi-capable microelectronic device? It might sound kind of niche, but this is a fundamental part of IoT. In this project, I will be using a data message protocol called Message Queuing Telemetry Transport or MQTT. There are many methods of data transmission that would work for this project, but I will just be using this one.

MQTT is “great for collecting data from large networks of small devices into a single location” according to IoTforall.com.

There are two basic parts of MQTT: the broker and the client.

As the names suggest, in a given system, there are multiple clients that will request data from (subscribe) and send data to (publish) one singular broker. This data is organized into topics. The clients can subscribe or publish to any number of these topics.

The broker can either be hosted locally on a computer or on a server, like the one we will be using. Our broker will be test.mosquitto.org and our clients will be the devices we access the photo data from (Macbook and Android phone).

The server/broker and client establish a TCP/IP connection to connect, publish, and subscribe to messages all of the same structure.

“The MQTT packet or message format consists of a 2 byte fixed header (always present) + Variable-header (not always present)+ payload (not always present).”

from www.steves-internet-guide.com/

Luckily, we have the benefit of importing libraries that do complicated work for us. Later you will see how easy it is to implement code to send MQTT messages.

Mosquitto

Mosquitto (with two t’s, like MQTT) is an open-source MQTT broker developed by Eclipse. It allows you to start an MQTT Broker and clients and to publish and subscribe to topics.

Eclipse Paho is an MQTT implementation. Paho is available on various platforms and programming languages (Java, C#, C, Python, and JavaScript), but we will only be using it for Python (paho-mqtt).

Here is a comparison of different MQTT brokers and implementation software.

We will also be using Mosquitto’s public broker, test.mosquitto.org, as the domain to which we will publish and receive messages. Other public servers can be found here.

Software Install Instructions

Raspberry Pi:

If you have not flashed your raspberry pi and connected it to the internet, those things must be done first.

First I downloaded Mosquitto. This will allow you to run a broker or client from the pi. Installation is as simple as running the following in Terminal

sudo apt-get install -y mosquitto mosquitto-clients

In order to subscribe to and publish MQTT messages as a client in python, run

pip install paho-mqtt

MacBook Pro

If you have homebrew already installed, installing Mosquitto in OS is as easy as

brew install mosquitto

and then the following will add the broker to be automatically run upon startup

brew services start mosquitto
ln -sfv /usr/local/opt/mosquitto/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mosquitto.plist
mosquitto -c /usr/local/etc/mosquitto/mosquitto.conf

Android

I downloaded the app MQTT Dash from the Google Play store and it works perfectly for connecting to the broker.

Unfortunately, there is not a good alternative for iOS! None of the MQTT applications include image data conversion capabilities.

The Code

Loop_pub.py

In this code, we are taking a picture every 15 seconds on the Raspberry Pi and publishing it to the test.mosquitto.org.

We set up a client using paho.mqtt.client and connect it to the specified address and topic.

client = mqtt.Client()
client.connect(ip_address, 1883)
client.subscribe(topic)

A picture is taken using picamera:

camera.start_preview()
sleep(1)
camera.capture(‘image_test.jpg’, resize=(500,281))
camera.stop_preview()

The image itself is published by opening the image with the ability to read in binary mode, “rb”, and then is converted into an array of bytes and published over the desired topic.

f=open("image_test.jpg", "rb")
fileContent = f.read()
byteArr = bytearray(fileContent)
client.publish(topic, byteArr)

Subscribe.py

This program as run on my Macbook laptop to subscribe to the appropriate topic, and then save and preview any published images.

First, a client is created and the functions on_connect and on_message are established and will run at the time of their respective self-explanatory triggers.

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(ip_address, 1883, 60)

The following code opens an image with writing abilities in binary mode (“wb”), and then rites the incoming message to that file. The subprocess call opens the image to appear on the screen.

f = open('output.jpg', "wb")
f.write(msg.payload)
print("Image Received")
f.close()
openimg = subprocess.call(["open", "output.jpg"])

To run these program from the cambia repository,

python loop_pub.py #on the raspberry pi 

python subscribe.py #on another computer

Mobile View

No programming is needed. In the MQTT Dash application, use test.mosquitto.org as the server/ip address and the topic name that matches what is declared in your loop_pub.py code.

That should explain everything to get this system setup. Feel free to contact me if there are any holes in my explanations or if you have questions:

charleydorn@gmail.com

The Nest Steps in this project will be:

  1. Publishing data over a local network
  2. Exploring IoT for permaculture with temperature/moisture sensors and solar panel data

--

--