From e9dc6c38ee3c99277c3bbd84b89c7fb5b52475e7 Mon Sep 17 00:00:00 2001 From: nmascrie Date: Wed, 5 Mar 2025 14:53:46 +0100 Subject: [PATCH] Preparing for server sending --- .gitignore | 0 ArduinoCube/ArduinoCube.ino | 21 +++++++------ ArduinoCube/arduino_secrets.h | 5 +++ server.py | 57 +++++++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 9 deletions(-) create mode 100644 .gitignore create mode 100644 ArduinoCube/arduino_secrets.h create mode 100644 server.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/ArduinoCube/ArduinoCube.ino b/ArduinoCube/ArduinoCube.ino index 69965a9..54ebada 100644 --- a/ArduinoCube/ArduinoCube.ino +++ b/ArduinoCube/ArduinoCube.ino @@ -42,8 +42,8 @@ int status = STATUS_SAFE; int ledState = LOW; //please enter your sensitive data in the Secret tab -char ssid[] = SECRET_SSID; // your network SSID (name) -char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) +//char ssid[] = SECRET_SSID; // your network SSID (name) +//char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) unsigned long previousMillis = 0; @@ -56,6 +56,9 @@ const long intervalShort = 20; const int trigger = 12; const int echo = 11; +//std::string sides[6]; +std::string sides[6] = {"Bottom", "Top", "Right", "Front", "Left", "Back"}; + int count = 0; long duration = 0; @@ -95,19 +98,19 @@ void setup() { * * @return calculated distance. */ -long fire_sensor(int trigger, int echo) +int fire_sensor(int trigger, int echo) { - long dist; + int dist; long dur; digitalWrite(trigger, LOW); - delayMicroseconds(2); + delayMicroseconds(100); digitalWrite(trigger, HIGH); - delayMicroseconds(10); + delayMicroseconds(500); digitalWrite(trigger, LOW); dur = pulseIn(echo, HIGH); dist = dur * 0.034 / 2; - delay(50); + delay(100); return (dist); } @@ -126,7 +129,7 @@ void loop() { for (int i = 0; i < 6; i++) { Serial.print("Sensor {"); - Serial.print(i); + Serial.print(sides[i].c_str()); Serial.print("} : "); Serial.print(distances[i]); Serial.println("cm"); @@ -134,6 +137,6 @@ void loop() { //update_status(distance); // wait a bit before reading again - delayMicroseconds(1000000); + delayMicroseconds(10000); count++; } diff --git a/ArduinoCube/arduino_secrets.h b/ArduinoCube/arduino_secrets.h new file mode 100644 index 0000000..3768ab5 --- /dev/null +++ b/ArduinoCube/arduino_secrets.h @@ -0,0 +1,5 @@ +#define SSID "Non" +#define PASS "Non" +#define SERV_ADDR "Non" +#define SERV_PORT "Non" +#define AI_KEY "Non" \ No newline at end of file diff --git a/server.py b/server.py new file mode 100644 index 0000000..75cfc8b --- /dev/null +++ b/server.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python3 +""" +License: MIT License +Copyright (c) 2023 Miel Donkers + +Very simple HTTP server in python for logging requests +Usage:: + ./server.py [] +""" +from http.server import BaseHTTPRequestHandler, HTTPServer +import logging +import json + +class S(BaseHTTPRequestHandler): + def _set_response(self): + self.send_response(200) + self.send_header('Content-type', 'text/html') + self.end_headers() + + def do_GET(self): + logging.info("GET request,\nPath: %s\nHeaders:\n%s\n", str(self.path), str(self.headers)) + self._set_response() + self.wfile.write("GET request for {}".format(self.path).encode('utf-8')) + + def do_POST(self): + content_length = int(self.headers['Content-Length']) + post_data = self.rfile.read(content_length) + + # Parse JSON + try: + data = json.loads(post_data.decode('utf-8')) + logging.info("Received data: %s", data) + except json.JSONDecodeError: + logging.error("Failed to decode JSON") + + self._set_response() + self.wfile.write("Received JSON data".encode('utf-8')) + +def run(server_class=HTTPServer, handler_class=S, port=8080): + logging.basicConfig(level=logging.INFO) + server_address = ('', port) + httpd = server_class(server_address, handler_class) + logging.info('Starting httpd...\n') + try: + httpd.serve_forever() + except KeyboardInterrupt: + pass + httpd.server_close() + logging.info('Stopping httpd...\n') + +if __name__ == '__main__': + from sys import argv + + if len(argv) == 2: + run(port=int(argv[1])) + else: + run() \ No newline at end of file