Files
ProjectIOT/pico_files/main.py
2025-06-14 10:18:40 +02:00

77 lines
2.1 KiB
Python

import urequests
import config
from connection import connection
from machine import Pin
from time import sleep
import random
from machine import Pin, SPI, reset
from camera import *
import os
import gc
import usocket as socket
wait_pin = Pin(13, Pin.OUT)
good_pin = Pin(15, Pin.OUT)
bad_pin = Pin(14, Pin.OUT)
# Send the POST request with the raw image data as the body
url = f'http://{config.server}:{config.port}/{config.data_path}'
spi = SPI(0,sck=Pin(18), miso=Pin(16), mosi=Pin(19), baudrate=8000000) # Pins for the Raspberry Pi Pico
cs = Pin(17, Pin.OUT)
cam = Camera(spi, cs, debug_text_enabled=True)
headers = {"Content-Type": "image/jpeg"}
addr = socket.getaddrinfo(config.server,config.port)[0][-1]
def send_image(image_file_path):
cam.capture_jpg()
with open(image_file_path, 'wb') as f:
f.write(b"")
cam.save_JPG(image_file_path)
with open(image_file_path, 'rb') as f:
s = socket.socket()
s.connect(addr)
# Send HTTP headers
s.send(b"POST %s HTTP/1.1\r\n" % config.data_path.encode())
s.send(b"Host: %s\r\n" % config.server.encode())
s.send(b"Transfer-Encoding: chunked\r\n")
s.send(b"Content-Type: application/octet-stream\r\n")
s.send(b"\r\n")
chunk_size = 512
while True:
chunk = f.read(chunk_size)
if not chunk:
break
hex_len = hex(len(chunk))[2:].encode()
one = hex_len + b"\r\n"
two = chunk + b"\r\n"
print(one)
s.send(one)
print(two[-5:])
s.send(two)
gc.collect()
# Final chunk to end the stream
s.send(b"0\r\n\r\n")
s.close()
return
while connection.isconnected():
good_pin.low()
bad_pin.low()
wait_pin.high()
#if random.choice([True, False]):
# response = send_image("1234.jpg")
#else:
# response = send_image("car2.jpg")
response = send_image("image.jpg")
print(response)
wait_pin.low()
if response:
if response["status"]:
good_pin.high()
else:
bad_pin.high()
sleep(5)