Skip to content

Commit da00252

Browse files
authored
Merge pull request #74 from adafruit/io-camera-example
adding pi camera example to library
2 parents 62db168 + f9ba5cd commit da00252

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

examples/basics/pi_camera.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
"""
2+
'pi_camera.py'
3+
=======================================
4+
Example for sending pictures taken by
5+
a Raspberry Pi camera to an
6+
Adafruit IO feed.
7+
"""
8+
# import standard python modules
9+
import time
10+
import base64
11+
import os
12+
13+
# import Adafruit IO REST client
14+
from Adafruit_IO import Client, Feed, RequestError
15+
16+
# import raspberry pi camera module
17+
import picamera
18+
19+
# camera capture interval, in seconds
20+
CAMERA_INTERVAL = 3
21+
22+
# Set to your Adafruit IO key.
23+
# Remember, your key is a secret,
24+
# so make sure not to publish it when you publish this code!
25+
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
26+
27+
# Set to your Adafruit IO username.
28+
# (go to https://accounts.adafruit.com to find your username)
29+
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
30+
31+
# Create an instance of the REST client
32+
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
33+
34+
# Adafruit IO Pi Camera Feed
35+
# note: this feed requires its history to be
36+
# turned off from the adafruit io feed page
37+
picam_feed = aio.feeds('picam')
38+
39+
# set up picamera
40+
camera = picamera.PiCamera()
41+
42+
# set the resolution of the camera's captures
43+
# note: Adafruit IO feeds with history turned
44+
# OFF only support images < 1kb
45+
camera.resolution = (200, 200)
46+
47+
print('Adafruit IO: Raspberry Pi Camera Example')
48+
49+
while True:
50+
camera.capture('image.jpg')
51+
print('Camera: SNAP!')
52+
with open("image.jpg", "rb") as imageFile:
53+
image = base64.b64encode(imageFile.read())
54+
# encode the b64 bytearray as a string for adafruit-io
55+
image_string = image.decode("utf-8")
56+
try:
57+
aio.send(picam_feed.key, image_string)
58+
print('Picture sent to Adafruit IO')
59+
except:
60+
print('Sending to Adafruit IO Failed...')
61+
62+
time.sleep(CAMERA_INTERVAL)

0 commit comments

Comments
 (0)