ESP32 Camera MicroPython IoT Device for Trading BOT Automation and Analysis

ESP32 Camera MicroPython IoT Device for Trading BOT Automation and Analysis

ESP32 Camera USB COM Connection, Putty Configuration and Running MicroPython Programme:

For Instructions Watch The Video Here:

Programmes Contained in the ESP32 Camera Board:

boot.py

# This file is executed on every boot (including wake-boot from deepsleep)
#import esp
#esp.osdebug(None)
#import webrepl
#webrepl.start()

main.py

from machine import Pin
from neopixel import NeoPixel
import time

pin = Pin(48, Pin.OUT)
np = NeoPixel(pin, 1)
np[0] = (10,0,0)
np.write()
r, g, b = np[0]

print("RGB Demo")

while True:
    print("50%R")
    np[0] = (127, 0, 0)
    np.write()
    time.sleep_ms(1000)

    print("50%G")
    np[0] = (0, 127, 0)
    np.write()
    time.sleep_ms(1000)

    print("50%B")
    np[0] = (0, 0, 127)
    np.write()
    time.sleep_ms(1000)

Blinking RGB LED Output:

Open CMD and Type this to Erase Flash:

C:\Users\VIKASH\Desktop\arduino\esp32_camera> python -m esptool --port COM7 erase-flash

Download Firmware:

Download Firmware such as firmware.bin from this link as per ESP32 Camera Board: micropython-camera-API Releases

Open CMD and Type this to Write Flash and upload Firmware in ESP32 S3 Camera Board:

C:\Users\VIKASH\Desktop\arduino\esp32_camera> python -m esptool --port COM7 --baud 460800 write-flash -z 0x0 firmware.bin

For Instructions Watch The Video Here:

Camera Capture Python Programme:

import os, time, struct, gc
import micropython
from machine import Pin
from camera import Camera, PixelFormat, FrameSize

W, H = 320, 240
SWAP = False    # byte order: if colors are noisy/wrong, flip this to True
VFLIP = True    # flip image upside-down
HMIRROR = False # set True to mirror left/right

try:
    import neopixel
    led = neopixel.NeoPixel(Pin(48), 1)
except Exception:
    led = None

def flash(color, ms=300):
    if led:
        led[0] = color
        led.write()
        time.sleep_ms(ms)
        led[0] = (0, 0, 0)
        led.write()

def open_camera():
    # try a few clock speeds; some boards/sensors only work at one of them
    for xclk in (20000000, 30000000, 10000000):
        try:
            gc.collect()
            cam = Camera(
                data_pins=[11, 9, 8, 10, 12, 18, 17, 16],
                vsync_pin=6, href_pin=7, pclk_pin=13,
                xclk_pin=15, sda_pin=4, scl_pin=5,
                powerdown_pin=-1, reset_pin=-1,
                xclk_freq=xclk,
                pixel_format=PixelFormat.RGB565,
                frame_size=FrameSize.QVGA,
                fb_count=1,
            )
            print("Camera OK at", xclk // 1000000, "MHz")
            return cam
        except OSError as e:
            print("Failed at", xclk // 1000000, "MHz:", e)
    raise OSError("Camera would not start - check wiring/pins")

@micropython.viper
def convert(src: ptr8, dst: ptr8, n: int, swap: int):
    j = 0
    for i in range(0, n * 2, 2):
        if swap:
            v = (int(src[i + 1]) << 8) | int(src[i])
        else:
            v = (int(src[i]) << 8) | int(src[i + 1])
        b = v & 0x1F
        g = (v >> 5) & 0x3F
        r = (v >> 11) & 0x1F
        dst[j] = (b << 3) | (b >> 2)
        dst[j + 1] = (g << 2) | (g >> 4)
        dst[j + 2] = (r << 3) | (r >> 2)
        j += 3

def next_name():
    files = os.listdir()
    n = 1
    while "selfie_%d.bmp" % n in files:
        n += 1
    return "selfie_%d.bmp" % n

def save_bmp(buf, path):
    out = bytearray(W * H * 3)
    convert(buf, out, W * H, 1 if SWAP else 0)
    header = struct.pack('<2sIHHI', b'BM', 54 + len(out), 0, 0, 54)
    header += struct.pack('<IiiHHIIiiII', 40, W, -H, 1, 24, 0, len(out), 2835, 2835, 0, 0)
    with open(path, "wb") as f:
        f.write(header)
        f.write(out)

cam = open_camera()
cam.set_vflip(1 if VFLIP else 0)
cam.set_hmirror(1 if HMIRROR else 0)

try:
    flash((40, 40, 0), 200)          # yellow blink = get ready
    for _ in range(15):              # let auto-exposure settle
        cam.capture()
        time.sleep_ms(100)
    cam.capture()                    # throw away a stale frame
    buf = cam.capture()
    name = next_name()
    save_bmp(buf, name)
    print("Saved", name)
    flash((0, 40, 0))                # green blink = saved
finally:
    cam.deinit()