diff --git a/main.py b/main.py index 959a7d3..09b147b 100644 --- a/main.py +++ b/main.py @@ -1,8 +1,11 @@ -import wifi_server -from machine import Pin -from neopixel import NeoPixel -np = NeoPixel(Pin(8), 1) -np[0] = (0, 0, 0) -np.write() +try: + from machine import Pin + from neopixel import NeoPixel + np = NeoPixel(Pin(8), 1) + np[0] = (0, 0, 0) + np.write() +except Exception as e: + print("rgb off failed", e) +import wifi_server wifi_server.main() diff --git a/wifi_server.py b/wifi_server.py index 85a25ce..0108a5f 100644 --- a/wifi_server.py +++ b/wifi_server.py @@ -53,6 +53,9 @@ def delete_code(name, filename=CODES_FILE): def capture_and_save(name, timeout_ms=5000, idle_us=8000, recv_pin=None): from capture import capture_raw, RECV_PIN pin = recv_pin if recv_pin is not None else RECV_PIN + + time.sleep_ms(200) + timings = capture_raw(pin_num=pin, timeout_ms=timeout_ms, idle_us=idle_us) if not timings: return False, 0 @@ -142,8 +145,10 @@ def handle_request(path, headers, api_key): ok, count = capture_and_save(name, timeout_ms=timeout_ms) if ok: return 200, {"status": "ok", "saved": name, "pulses": count} - else: + elif count == 0: return 408, {"error": "сигнал не обнаружен (таймаут)"} + else: + return 422, {"error": f"сигнал похож на помеху ({count} импульсов, ожидается от 6)"} except Exception as e: return 500, {"error": str(e)} @@ -209,7 +214,7 @@ def run_server(ip, api_key=None): body_bytes = body_json.encode("utf-8") status_text = {200: "OK", 400: "Bad Request", 401: "Unauthorized", 404: "Not Found", - 408: "Request Timeout", 500: "Internal Server Error"}.get(status, "OK") + 408: "Request Timeout", 422: "Unprocessable Entity", 500: "Internal Server Error"}.get(status, "OK") response_headers = ( f"HTTP/1.1 {status} {status_text}\r\n"