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