muizenval

Observe mouse traps remotely
Log | Files | Refs

commit fef3e0497445427eb3c300e59a48fa8ee0a30fc0
parent 249a22b1a3f3f3e044a82dbf18e6138780bb7000
Author: Friedel Schön <[email protected]>
Date:   Sat, 18 Jun 2022 13:34:23 +0200

adding 5g-remote

Diffstat:
M5g-client/5g-client.ino | 18+++++++++---------
M5g-client/include/config.h | 33++++++++++++++++++++-------------
M5g-client/include/modem.h | 6+-----
M5g-client/include/remote.h | 5+++--
M5g-client/remote.ino | 14+++++++++-----
Aremote.md | 51+++++++++++++++++++++++++++++++++++++++++++++++++++
Mremote.py | 11++++++-----
7 files changed, 99 insertions(+), 39 deletions(-)

diff --git a/5g-client/5g-client.ino b/5g-client/5g-client.ino @@ -12,23 +12,23 @@ #define statusDelay 5 // seconds -#define batteryFactor 0.978 / ADC_AREF*(BATVOLT_R1 / BATVOLT_R2 + 1) +#define batteryFactor (0.978 * (BATVOLT_R1 / BATVOLT_R2 + 1) / ADC_AREF) -// sara_modem modem; +sara_modem modem; Sodaq_LSM303AGR accel; -remote pass; +serial_remote remote; void setup() { // -*- hardware initiation -*- usbSerial.begin(baud); - while (usbWait && !usbSerial) - ; + // while (usbWait && !usbSerial) + // ; pinMode(BATVOLT_PIN, INPUT); // modem.init(); - pass.init(); + remote.begin(); Wire.begin(); delay(1000); @@ -40,7 +40,7 @@ void setup() { // Enable the Accelerometer accel.enableAccelerometer(); - pass.connect("127.0.0.1", 5000); + remote.connect("127.0.0.1", 5000); // modem.send("ATE0"); // disable command-echo @@ -140,7 +140,7 @@ void loop() { // usbSerial.println("%"); } - remote::http_packet req, res; + serial_remote::http_packet req, res; req.method = "POST"; req.endpoint = "/api/update"; req.body["latitude"] = lat; @@ -149,7 +149,7 @@ void loop() { req.body["battery"] = batteryVoltage(); req.body["temperature"] = temperature(); - pass.send(req); + remote.send(req); last = now; } diff --git a/5g-client/include/config.h b/5g-client/include/config.h @@ -1,23 +1,30 @@ #pragma once // -*- hardware stuff -*- -#define usbSerial SerialUSB +#define usbSerial SerialUSB // usb serial port +#define modemSerial Serial1 // modem serial port +#define modemPowerPin SARA_ENABLE // modem power pin +#define modemEnablePin SARA_TX_ENABLE // modem enable pin +#define modemVoltagePin SARA_R4XX_TOGGLE // modem voltage pin // -*- behaviour settings -*- -#define baud 115200 // baut-rate of modem-/usb-serial -#define lineBuffer 512 // buffer-size (bytes) to use to store lines -#define commandTimeout 10.0 // seconds to cancel a command -#define commandDelay 0.1 // delay after every command -#define ignoreDelay 2 // seconds to wait if command is run with COMMAND_IGNORE -#define commandDebug true // send debug information about command requests -#define eventDebug true // print '+'-events -#define lineDebug false // print each line to debug -#define blockDebug true // print if command is blocking -#define usbWait true // wait for a usb-connection +#define remoteBaut 115200 // baut-rate of modem-/usb-serial +#define remoteFirstTimeout 5.0 // seconds to wait for the first timeout +#define remoteTimeout 1.0 // seconds to wait for remote to timeout +#define lineBuffer 512 // buffer-size (bytes) to use to store lines +#define commandTimeout 10.0 // seconds to cancel a command +#define commandDelay 0.1 // delay after every command +#define ignoreDelay 2.0 // seconds to wait if command is run with COMMAND_IGNORE +#define commandDebug true // send debug information about command requests +#define eventDebug true // print '+'-events +#define lineDebug false // print each line to debug +#define blockDebug true // print if command is blocking +#define usbWait true // wait for a usb-connection // -*- sim settings -*- -#define simPin "0000" // PIN of the sim -#define simAPN "lpwa.vodafone.iot" // APN-network of the sim +#define macAddress "CAFEBABE01234567" // the boards mac-address +#define simPin "0000" // PIN of the sim +#define simAPN "lpwa.vodafone.iot" // APN-network of the sim // -*- prefixes -*- #define prefixInfo "info | " diff --git a/5g-client/include/modem.h b/5g-client/include/modem.h @@ -1,10 +1,6 @@ #pragma once -#define modemSerial Serial1 -#define modemPowerPin SARA_ENABLE -#define modemEnablePin SARA_TX_ENABLE -#define modemVoltagePin SARA_R4XX_TOGGLE - +#include "config.h" struct sara_modem { // -*- enums and structs -*- diff --git a/5g-client/include/remote.h b/5g-client/include/remote.h @@ -2,7 +2,7 @@ #include <Arduino_JSON.h> -struct remote { +struct serial_remote { struct http_packet { const char* method; const char* endpoint; @@ -10,7 +10,8 @@ struct remote { JSONVar body; }; - void init(); + void begin(); + bool available(); void connect(const char* host, int port); const char* send(http_packet request, http_packet& response); const char* send(http_packet request); diff --git a/5g-client/remote.ino b/5g-client/remote.ino @@ -1,7 +1,7 @@ #include "include/config.h" #include "include/remote.h" -JSONVar readJSON() { +static JSONVar readJSON() { char line[lineBuffer]; char buf; int i = 0; @@ -20,7 +20,7 @@ JSONVar readJSON() { return JSON.parse(line); } -void remote::init() { +void serial_remote::begin() { usbSerial.println("{\"command\":\"hello\"}"); JSONVar res_json = readJSON(); if (res_json["error"] != nullptr) { @@ -28,7 +28,11 @@ void remote::init() { } } -void remote::connect(const char* host, int port) { +bool serial_remote::available() { + return usbSerial; +} + +void serial_remote::connect(const char* host, int port) { JSONVar body; body["command"] = "connect"; body["host"] = host; @@ -37,7 +41,7 @@ void remote::connect(const char* host, int port) { usbSerial.println(body); } -const char* remote::send(http_packet request, http_packet& response) { +const char* serial_remote::send(http_packet request, http_packet& response) { JSONVar body; body["command"] = "send"; body["method"] = request.method; @@ -54,7 +58,7 @@ const char* remote::send(http_packet request, http_packet& response) { } -const char* remote::send(http_packet request) { +const char* serial_remote::send(http_packet request) { JSONVar body; body["command"] = "send"; body["method"] = request.method; diff --git a/remote.md b/remote.md @@ -0,0 +1,50 @@ +# PROTOCOL REMOTE CLIENT + +> Serial USB met 115200 baud! + +**Stuurt JSON-objecten met een newline als terminator** + +- **een request bevat altijd de string `'command'` ([see commands](#commands))** +- **een response bevat altijd de string `'error'` ([see errors](#errors))** + +## Commands + +### `hello` + +> initialiseert de connectie + +``` +→ { "command": "hello" } +← { "error": null } +``` +--- + +### `connect host=<string> port=<int>` + +> opend een remote http-client ipv. de 5g-module + +``` +→ { "command": "connect", "host": "muizenval.tk", "port": 8080 } +← { "error": null } +``` +--- + +### `send method=<string> endpoint=<string> headers?=<object> body?=<any>` + +&rarr; `code=<int> headers=<object> body=<any>` +> stuurt een http-request naar de server + +``` +→ { "command": "send", "method": "POST", "endpoint": "/api/update", "headers": {}, "body": { "latitude": 53.2414306640625, "longitude": 6.533237711588542, "accuracy": 58.479530859171305, "battery": 394, "temperature": 25 } } +← { "error": null, "code": 200, "headers": { "Content-Type": "application/json" }, "body": {} } +``` +--- + +## Errors + +| code | description | +| ----------------- | --------------------------------------------- | +| `null` | succes | +| `'invalid-json'` | request is verkeerd geformateerd | +| `'not-available'` | geen connectie naar de server mogelijk | +| `'not-connected'` | request verstuurd zonder geconnect te zijn | +\ No newline at end of file diff --git a/remote.py b/remote.py @@ -1,16 +1,16 @@ from http.client import HTTPConnection -from typing import Optional import serial import sys import json if len(sys.argv) < 2: - print(f'{sys.argv[0]} <port>') + print(f'{sys.argv[0]} <serial>') -serial_port = serial.Serial(port=sys.argv[1], baudrate=115200) +server_address = '' +serial_port = serial.Serial(port=sys.argv[2], baudrate=115200) -client: Optional[HTTPConnection] = None +client = None def handle(req): global client @@ -35,10 +35,11 @@ def handle(req): while serial_port.is_open: try: req = json.loads(serial_port.readline()) - print('-> ' + repr(req)) res = handle(req) except Exception as e: + req = '<error>' res = { 'error': 'internal', 'description': str(e) } + print('-> ' + repr(req)) if type(res) == str: res = { "error": res }