The goal of this project was to publish a live, low bandwidth, photo feed of a chick and duckling pen using a Raspberry Pi.MaterialsRequired: Raspberry Pi and a Pi CameraI 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 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).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.MosquittoMosquitto (with two ts, 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.
We will also be using Mosquittos 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 InstructionsRaspberry 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 Terminalsudo apt-get install -y mosquitto mosquitto-clientsIn order to subscribe to and publish MQTT messages as a client in python, runpip install paho-mqttMacBook ProIf you have homebrew already installed, installing Mosquitto in OS is as easy asbrew install mosquittoand then the following will add the broker to be automatically run upon startupbrew services start mosquittoln -sfv /usr/local/opt/mosquitto/*.plist /Library/LaunchAgentslaunchctl load /Library/LaunchAgents/homebrew.
mxcl.mosquitto.plistmosquitto -c /usr/local/etc/mosquitto/mosquitto.
confAndroidI 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 Codechardorn/cambiaYou can't perform that action at this time. You signed in with another tab or window. You signed out in another tab 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.
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. pyThis 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_connectclient.on_message = on_messageclient.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 computerMobile ViewNo 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. comThe Nest Steps in this project will be:Publishing data over a local networkExploring IoT for permaculture with temperature/moisture sensors and solar panel data
Shenzhen TigerWong Technology Co.,Ltd
Tel: +86 0755-23773884
Add: Room 601-605, Building 6, 1980 Science and Technology Industrial Park, Longhua Street, Longhua District, Shenzhen