Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Publishing Raspberry Pi image data to Adafruit IO from Python
#!/usr/bin/env python
# based on
import io
import time
import os
# Camera setup guide:
import picamera
import base64
import sys
# Installing the Python Imaging Library on a Raspberry Pi 3
# $ sudo apt-get install libtiff4-dev libjpeg8-dev zlib1g-dev libfreetype6-dev liblcms2-dev libwebp-dev tcl8.5-dev tk8.5-dev python-tk
# $ sudo pip install pillow
from PIL import Image
# Import Adafruit IO MQTT client. Install with:
# $ sudo pip install adafruit-io
from Adafruit_IO import MQTTClient
IO_KEY = os.environ['IO_KEY']
IO_URL = os.environ.get('IO_URL', '')
camera = picamera.PiCamera()
camera.resolution = (640, 480)
# Start a preview and let the camera warm up for 2 seconds
def connected(client):
Connected function will be called when the client is connected to
Adafruit IO. This is a good place to subscribe to feed changes. The
client parameter passed to this function is the Adafruit IO MQTT client
so you can make calls against it easily.
print 'Connected to Adafruit IO! Listening for /click changes...'
client.publish('actions', 'i am alive ' + time.strftime('%c'))
# Subscribe to changes on a feed with key, 'click'. This will tell the
# camera when to snap a pic
def disconnected(client):
Disconnected function will be called when the client disconnects.
print 'Disconnected from Adafruit IO!'
def message(client, feed_id, payload):
Message function will be called when a subscribed feed has a new value.
The feed_id parameter identifies the feed, and the payload parameter has
the new value.
print 'Feed {0} received new value: {1}'.format(feed_id, payload)
if payload == '1':
send_image(camera, client)
def send_image(camera, client):
Take a photo, optimize it with PIL, and publish it to Adafruit IO.
stream = io.BytesIO()
print('> capturing')
# Tweak point 1: if your data size is too big, try reducing the original
# dimensions here.
camera.capture(stream, format='jpeg', resize=(800, 600))
print('> optimizing')
image =, 'r')
optim_stream = io.BytesIO()
# Tweak point 2: if your data size is too big, try reducing the quality
# here. 85 is a good middle ground, but lower will still produce
# recognizable images., format='jpeg', quality=70, optimize=True)
print('> converting')
value = base64.b64encode(
if len(value) > 102400:
print "image is too big!"
print " got %i bytes" % len(value)
client.publish('actions', 'image too big')
print 'Publishing {0}... {1} bytes to image-stream.'.format(value[0:32], len(value))
# the actual moment of publishing, this is where the image feed key is set
client.publish('image-stream', value)
# Publish to a second feed with notification that the image has been sent.
# Not necessary, but can be helpful for debugging.
client.publish('actions', 'image sent, {} bytes'.format(len(value)))
def start():
# Create an MQTT client instance.
print "connect with {0}:{1}".format(IO_USERNAME, IO_KEY)
client = MQTTClient(IO_USERNAME, IO_KEY, service_host=IO_URL)
# Setup the callback functions defined above.
client.on_connect = connected
client.on_disconnect = disconnected
client.on_message = message
# Connect to the Adafruit IO server.
# Now the program needs to use a client loop function to ensure messages
# are sent and received. This command will block foreground processing and
# let the MQTT library do its work.
if __name__ == '__main__':
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment