Main Content

Write Image to ThingSpeak from Raspberry Pi with Python

This example shows how to write an image to a ThingSpeak® image channel from a Raspberry Pi™ board using Python.

This example uses a Raspberry Pi to capture an image from a camera and write a file locally. Then the image file is sent to ThingSpeak each time a new image is taken. You can use this example for a Raspberry Pi connected to a web cam or a Pi Camera, depending on the lines selected in the provided code.

For a Pi camera, you can use the raspistill command. To use this code with a webcam, use fswebcam, which you can get using

sudo apt-get install fswebcam

Your Pi must have internet access to run this example.

python-image-ex.jpg

Setup

1) Create a new ThingSpeak image channel as described in Create an Image Channel.

2) Create a new ThingSpeak data channel as described in Collect Data in a New Channel.

3) Add an Image Display widget to the view of your data channel, as described in Image Display.

Code

1) Include the following libraries in your Python code.

#!/usr/bin/python

from time import sleep
import os
import requests
import sys

2) Define the variables to specify your Thingspeak channels, credentials, and image file location.

# Update this section with your ThingSpeak Image Channel ID and Image Channel API Key
thingspeakImageChannelID = 'YOUR_THINGSPEAK_IMAGE_CHANNEL_ID'
thingSpeakImageChannelAPIKey = 'YOUR_THINGSPEAK_IMAGE_CHANNEL_API_KEY'
localFileName = '/home/pi/snapshot.jpg'
thingSpeakURL = 'https://data.thingspeak.com/channels/' + thingspeakImageChannelID + '/images'

3) Save a snapshot from your Raspberry Pi camera board.

def getSnapshotBytes():
   # Use FSWEBCAM to save a screenshot from a webcam. This requires the FSWEBCAM package.
   # Use raspistill to save a screenshot from a Pi Cam.
   # Alternatively, you can use OpenCV to directly get the bytestream from the camera.
   imageByteStream = None
   returnCode = os.system('fswebcam -r 1024x768 -S 1 -q ' + localFileName) # Use this line for webcam.
   # returnCode = os.system(‘raspistill -o ‘ + localFileName) # Use this line for a pi cam.
   if returnCode == 0:
      fh = open(localFileName, 'rb')
      imageByteStream = fh.read()
      fh.close()
   return imageByteStream

4) Run a loop to capture a snapshot and write it to your ThingSpeak image channel.

def main():
   # Loop infinitely
   while True:
      # Get image bytes
      imData = getSnapshotBytes()
      # POST image to ThingSpeak if there is a valid image
      if imData is not None:
         x = requests.post(url = thingSpeakURL, data = imData,
             headers = {'Content-Type': 'image/jpeg',
             'thingspeak-image-channel-api-key': thingSpeakImageChannelAPIKey,
             'Content-Length' : str(sys.getsizeof(imData))})
         print(x)
         # Sleep so we do not get locked out of ThingSpeak for posting too fast
         sleep(30)

if __name__=="__main__":
   main()

Write the Image

Run the code while monitoring the Image Display widget in your page view.

stapler_image_widget.jpg