mirror of
https://github.com/StefBuwalda/ProjectIOT.git
synced 2025-10-30 03:09:58 +00:00
BUNCH OF TRASH
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
port = 2222
|
||||
server = "192.168.137.1"
|
||||
data_path = "upload"
|
||||
ssid = "STEF"
|
||||
password = "TEST123123"
|
||||
data_path = "api"
|
||||
ssid = "stef"
|
||||
password = "test123123"
|
||||
|
||||
@@ -1,44 +1,38 @@
|
||||
import urequests as requests
|
||||
import urequests
|
||||
import config
|
||||
from connection import connection
|
||||
from machine import Pin
|
||||
from time import sleep
|
||||
import random
|
||||
|
||||
# Path to the image
|
||||
image_path = "car2.jpg"
|
||||
pin = Pin(15, Pin.OUT)
|
||||
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}"
|
||||
|
||||
|
||||
def send_image(image_file_path):
|
||||
with open(image_file_path, "rb") as f:
|
||||
image_data = f.read()
|
||||
headers = {"Content-Type": "image/jpeg"}
|
||||
response = urequests.post(url, headers=headers, data=image_data)
|
||||
return response.json()
|
||||
|
||||
# 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)
|
||||
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")
|
||||
print(response)
|
||||
print(response.status_code)
|
||||
print(response.text)
|
||||
if response.json()["status"]:
|
||||
pin.high()
|
||||
sleep(0.5)
|
||||
pin.low()
|
||||
response.close()
|
||||
sleep(2)
|
||||
wait_pin.low()
|
||||
if response["status"]:
|
||||
good_pin.high()
|
||||
else:
|
||||
bad_pin.high()
|
||||
sleep(1)
|
||||
|
||||
Reference in New Issue
Block a user