Infinite Playground Logo

Analog Pixel

This month in the AdaBox , I got the MEMENTO . These are the experiments I did with it:

Install Circuit Python

before you do anything, you need to get python on the camera:

  • https://learn.adafruit.com/adafruit-memento-camera-board/install-circuitpython
  • download the newest version of the uf2
  • slowly double click the reset button
  • when the camera drive appears, drag the uf2 file to the drive.

Sending an image to a server

First off, let's turn this into a cheap security camera. We'll have it connect to the wifi
and send an image to a server every minute:

Client

ssid="ssid"
passwd="password"

import socketpool
import wifi
import adafruit_pycamera
import adafruit_requests
import time
import ssl

for network in wifi.radio.start_scanning_networks():
    print(network, network.ssid, network.channel)
wifi.radio.stop_scanning_networks()

print("joining network...")
print(wifi.radio.connect(ssid=ssid,password=passwd))

pycam = adafruit_pycamera.PyCamera()
pycam.resolution = "240x240"

pool = socketpool.SocketPool(wifi.radio)
ssl_context = ssl.create_default_context()
request = adafruit_requests.Session(pool, ssl.create_default_context())

while True:
    data = pycam.capture_into_jpeg()
    headers = {"Content-Type": "image/jpeg"}
    response = request.post("http://192.168.0.70:5050/jpg", data=data, headers=headers)
    print("Response: ", response.text)
    time.sleep(60)

Server

#!/usr/bin/env python

from flask import Flask, request, jsonify
import os

app = Flask(__name__)

@app.route('/jpg', methods=['POST'])
def upload_image():
    open("image.jpg", "wb").write(request.data)
    return jsonify({'message': '10 4 big buddy'}), 400

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=5050)

Hosting Images over Wifi

The following code will expose an endpoint at http://camera.local:5000/capture that will return a jpeg image from the camera.

  • mdns is used to create the local multicast dns entry for the camera
  • adafruit_pycamera module is used to control the camera
  • adafruit_httpserver is used to create the http server
ssid="SSID"
passwd="PASSWORD"

import mdns
import socketpool
import wifi
import adafruit_pycamera

from adafruit_httpserver import Server, Request, FileResponse, Response

for network in wifi.radio.start_scanning_networks():
    print(network, network.ssid, network.channel)
wifi.radio.stop_scanning_networks()

print("joining network...")
print(wifi.radio.connect(ssid=ssid,password=passwd))

mdns_server = mdns.Server(wifi.radio)
mdns_server.hostname = "camera"
mdns_server.advertise_service(service_type="_http", protocol="_tcp", port=5000)

pool = socketpool.SocketPool(wifi.radio)
server = Server(pool, "/static", debug=True)

# initialize the camera
pycam = adafruit_pycamera.PyCamera()

@server.route("/capture")
def capture(request: Request):
    """
    Capture an image
    """
    pycam.resolution = "240x240"
    return Response(request, body=pycam.capture_into_jpeg(), content_type="image/jpeg")

server.serve_forever(str(wifi.radio.ipv4_address))

Links



Home