From 2af034cc865898b8d0c78b061362d09ce131ac5b Mon Sep 17 00:00:00 2001 From: Stef Date: Thu, 10 Apr 2025 16:34:53 +0200 Subject: [PATCH] Added Pico Files Added the files used on the raspberry pi pico to send and receive data --- pico_files/config.py | 5 +++++ pico_files/connection.py | 36 ++++++++++++++++++++++++++++++++ pico_files/main.py | 44 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 pico_files/config.py create mode 100644 pico_files/connection.py create mode 100644 pico_files/main.py diff --git a/pico_files/config.py b/pico_files/config.py new file mode 100644 index 0000000..fca380f --- /dev/null +++ b/pico_files/config.py @@ -0,0 +1,5 @@ +port = 2222 +server = "192.168.137.1" +data_path = "upload" +ssid = "STEF" +password = "TEST123123" diff --git a/pico_files/connection.py b/pico_files/connection.py new file mode 100644 index 0000000..4a4cee2 --- /dev/null +++ b/pico_files/connection.py @@ -0,0 +1,36 @@ +import config as c +import network +from time import sleep + +connection = network.WLAN(network.STA_IF) + + +def connect(): + # Check for established connection + if connection.isconnected(): + print("[connection.py]: Already connected") + return + + # Enable network interface + print("[connection.py]: Enabling network interface") + connection.active(True) + + # Connect to configurated SSID + print(f"[connection.py]: Connecting to SSID {c.ssid}") + connection.connect(c.ssid, c.password) + + retry = 0 + while not connection.isconnected(): + if retry == 10: + print("Could not establish connection, check your settings") + retry = 0 + retry += 1 + + sleep(1) + + # no exit, we have a connection! + print("[connection.py]: Connection established") + + +if __name__ == "__main__": + connect() diff --git a/pico_files/main.py b/pico_files/main.py new file mode 100644 index 0000000..6cb78b1 --- /dev/null +++ b/pico_files/main.py @@ -0,0 +1,44 @@ +import urequests as requests +import config +from connection import connection +from machine import Pin +from time import sleep + +# Path to the image +image_path = "car2.jpg" +pin = Pin(15, Pin.OUT) + +# Prepare the image for sending (open as binary) +with open(image_path, "rb") as image_file: + img_data = image_file.read() + +while connection.isconnected(): + # Send as multipart/form-data + boundary = "----WebKitFormBoundary7MA4YWxkTrZu0gW" + headers = {"Content-Type": "multipart/form-data; boundary=" + boundary} + + body = ( + ( + "--" + + boundary + + "\r\n" + + 'Content-Disposition: form-data; name="image"; filename="image.jpg"\r\n' + + "Content-Type: image/jpeg\r\n\r\n" + ).encode("utf-8") + + img_data + + ("\r\n--" + boundary + "--\r\n").encode("utf-8") + ) + + # Send the POST request with the raw image data as the body + url = f"http://{config.server}:{config.port}/{config.data_path}" + print(url) + response = requests.post(url, headers=headers, data=body) + print(response) + print(response.status_code) + print(response.text) + if response.json()["status"]: + pin.high() + sleep(0.5) + pin.low() + response.close() + sleep(2)