Main Content

Cette page a été traduite par traduction automatique. Cliquez ici pour voir la dernière version en anglais.

Écrire une image sur ThingSpeak à partir de Raspberry Pi avec Python

Cet exemple montre comment écrire une image sur un canal d'image ThingSpeak ® à partir d'une carte Raspberry Pi™ à l'aide de Python.

Cet exemple utilise un Raspberry Pi pour capturer une image à partir d'une caméra et écrire un fichier localement. Ensuite, le fichier image est envoyé à ThingSpeak chaque fois qu'une nouvelle image est prise. Vous pouvez utiliser cet exemple pour un Raspberry Pi connecté à une webcam ou une caméra Pi, selon les lignes sélectionnées dans le code fourni.

Pour une caméra Pi, vous pouvez utiliser la commande raspistill . Pour utiliser ce code avec une webcam, utilisez fswebcam, que vous pouvez obtenir en utilisant

sudo apt-get install fswebcam

Votre Pi doit avoir accès à Internet pour exécuter cet exemple.

python-image-ex.jpg

Installation

1) Créez un nouveau canal d'image ThingSpeak comme décrit dans Créer un canal d'images.

2) Créez un nouveau canal de données ThingSpeak comme décrit dans Collecter des données dans un nouveau canal.

3) Ajoutez un widget d'affichage d'image à la vue de votre canal de données, comme décrit dans Affichage des images.

Code

1) Incluez les bibliothèques suivantes dans votre code Python.

#!/usr/bin/python

from time import sleep
import os
import requests
import sys

2) Définissez les variables pour spécifier vos canaux Thingspeak, vos informations d'identification et l'emplacement du fichier image.

# 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) Enregistrez un instantané de votre carte caméra Raspberry Pi.

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) Exécutez une boucle pour capturer un instantané et l'écrire sur votre canal d'image ThingSpeak .

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()

Ecrire l'image

Exécutez le code tout en surveillant le widget Image Display dans votre vue de page.

stapler_image_widget.jpg