hanze/muizenval

added 5g test-passthrough, 5g-sensors etc (d58c1a861f91a9ba3090305198baada78c2bd26a)
Repositories

commit d58c1a861f91a9ba3090305198baada78c2bd26a
parent 89a5e754117a593e4d66db5a958a1a41cf8edd36
Author: Friedel Schön <[email protected]>
Date:   Fri, 17 Jun 2022 02:56:48 +0200

added 5g test-passthrough, 5g-sensors etc

Diffstat:
M.gitignore5+++--
M.vscode/arduino.json5+++--
M.vscode/c_cpp_properties.json4++++
M.vscode/settings.json7+++++++
D5g-client/5g-board.ino107-------------------------------------------------------------------------------
A5g-client/5g-client.ino161+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
D5g-client/command.h21---------------------
D5g-client/command.ino102-------------------------------------------------------------------------------
D5g-client/config.h33---------------------------------
A5g-client/include/config.h28++++++++++++++++++++++++++++
A5g-client/include/modem.h29+++++++++++++++++++++++++++++
A5g-client/include/passthrough.h18++++++++++++++++++
A5g-client/modem.ino130+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A5g-client/passthrough.ino69+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Apassthough.py50++++++++++++++++++++++++++++++++++++++++++++++++++
Mreadme.md12++++++++----
16 files changed, 510 insertions(+), 271 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1,2 +1,3 @@ __pycache__ -.DS_Store -\ No newline at end of file +.DS_Store +build/ +\ No newline at end of file diff --git a/.vscode/arduino.json b/.vscode/arduino.json @@ -1,5 +1,6 @@ { - "sketch": "5g-client/5g-board.ino", + "sketch": "5g-client/5g-client.ino", "board": "SODAQ:samd:sodaq_sara", - "port": "/dev/tty.usbmodem1D11101" + "port": "/dev/tty.usbmodem14101", + "output": "build" } \ No newline at end of file diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json @@ -23,6 +23,10 @@ "/Users/friedel/Library/Arduino15/packages/arduino/tools/CMSIS-Atmel/1.2.0/CMSIS/Device/ATMEL/", "/Users/friedel/Library/Arduino15/packages/SODAQ/hardware/samd/1.8.9/cores/arduino", "/Users/friedel/Library/Arduino15/packages/SODAQ/hardware/samd/1.8.9/variants/sodaq_sara", + "/Users/friedel/Documents/Arduino/libraries/Arduino_JSON/src", + "/Users/friedel/Documents/Arduino/libraries/Sodaq_LSM303AGR/src", + "/Users/friedel/Library/Arduino15/packages/SODAQ/hardware/samd/1.8.9/libraries/Wire", + "/Users/friedel/Documents/Arduino/libraries/Sodaq_UBlox_GPS/src", "/Users/friedel/Library/Arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/arm-none-eabi/include/c++/7.2.1", "/Users/friedel/Library/Arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/arm-none-eabi/include/c++/7.2.1/arm-none-eabi", "/Users/friedel/Library/Arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/arm-none-eabi/include/c++/7.2.1/backward", diff --git a/.vscode/settings.json b/.vscode/settings.json @@ -1,3 +1,10 @@ { "C_Cpp.intelliSenseEngine": "Default", + "files.associations": { + ".clang-format": "json", + ".clangd": "yaml", + ".yabairc": "shellscript", + "v.mod": "plaintext", + "limits": "cpp" + }, } \ No newline at end of file diff --git a/5g-client/5g-board.ino b/5g-client/5g-board.ino @@ -1,107 +0,0 @@ -#include "command.h" -#include "config.h" - -void setup() { - // -*- hardware initiation -*- - pinMode(powerPin, OUTPUT); // Put voltage on the nb-iot module - pinMode(voltagePin, OUTPUT); // Switch module voltage - pinMode(enablePin, OUTPUT); // Set state to active - - digitalWrite(powerPin, HIGH); - digitalWrite(voltagePin, LOW); - digitalWrite(enablePin, HIGH); - - usbSerial.begin(baud); - while (usbWait && !usbSerial) - ; - - modemSerial.begin(baud); - while (!modemSerial) - ; - - - // -*- module initialization -*- - usbSerial.print(prefixInfo "waiting for module to start up"); - for (;;) { - usbSerial.print('.'); - modemSerial.write("AT\r\n"); - delay(1000); - if (modemSerial.available()) - break; - } - while (modemSerial.available()) // clear cache - modemSerial.read(); - usbSerial.println(); - - sendCommand("ATE0"); // disable command-echo - - // if (sendCommand("AT+CPIN=\"" SIM_PIN "\"") == COMMAND_ERROR) { - // usbSerial.println("[EROR] sim can't be unlocked, wrong PIN"); - // return; - // } - usbSerial.println(prefixInfo "sim successful unlocked"); - - sendCommand("AT+CPSMS=0"); // Disable Power Saving Mode - sendCommand("AT+CEDRXS=0"); // Disable eDRX - usbSerial.println(prefixInfo "disabled power safe"); - - // -*- internet initialization -*- - char info[100]; - - sendCommand("AT+CFUN=15", COMMAND_BLOCK); // Reset the module - sendCommand("AT+UMNOPROF=1", COMMAND_BLOCK); // Set MNO profile (1=automatic,100=standard europe) - sendCommand("AT+URAT?", info); - usbSerial.print(prefixInfo "urat: "); - usbSerial.println(info); - sendCommand("AT+URAT=8", COMMAND_IGNORE); // Set URAT to LTE-M/NB-IOT - sendCommand("AT+CEREG=3", COMMAND_IGNORE); // Enable URCs - sendCommand("AT+CGDCONT=1,\"IP\",\"" simAPN "\"", COMMAND_BLOCK); // Set the APN - sendCommand("AT+CFUN=1"); // enable radio - sendCommand("AT+COPS=0,2", COMMAND_BLOCK); // Autoselect the operator - - usbSerial.print(prefixInfo "waiting for connection"); - - char response[100]; - - // Check Siganl strenght, repeat till you have a valid CSQ (99,99 means no signal) - while (sendCommand("AT+CSQ", response, COMMAND_SILENT) == COMMAND_OK && !strcmp(response, "+CSQ: 99,99")) { - delay(1000); - usbSerial.print("."); - } - - // Wait for attach, 1 = attached - while (sendCommand("AT+CGATT?", response, COMMAND_SILENT) == COMMAND_OK && strcmp(response, "+CGATT: 1")) { - delay(1000); - usbSerial.print("."); - } - usbSerial.println(); - - usbSerial.println(prefixInfo "connected!"); - - // -*- server connection -*- - - /* -AT+UHTTP=0,0,"86.92.67.21" - -AT+UHTTP=0,5,80 - -AT+UHTTPC=0,5,"/api/search_connect","","TEST!",1 - - */ - - sendCommand("AT+UHTTP=0,0,\"86.92.67.21\""); - sendCommand("AT+UHTTP=0,5,80"); - sendCommand("AT+UHTTPC=0,5,\"/api/search_connect\",\"\",\"TEST!\",1"); - - - usbSerial.println(prefixInfo "initiation completed, starting passthrough:"); -} - -void loop() { - // -*- passthrough for custom commands -*- - while (usbSerial.available()) - modemSerial.write(usbSerial.read()); - - while (modemSerial.available()) - usbSerial.write(modemSerial.read()); -} diff --git a/5g-client/5g-client.ino b/5g-client/5g-client.ino @@ -0,0 +1,161 @@ +#include "include/config.h" +#include "include/modem.h" +#include "include/passthrough.h" + +#include <Sodaq_LSM303AGR.h> +#include <Sodaq_UBlox_GPS.h> + +#define ADC_AREF 3.3f +#define BATVOLT_R1 4.7f +#define BATVOLT_R2 10.0f +#define BATVOLT_PIN BAT_VOLT + +#define batteryFactor 0.978 / ADC_AREF*(BATVOLT_R1 / BATVOLT_R2 + 1) + +// sara_modem modem; +Sodaq_LSM303AGR accel; +passthrough pass; + +void setup() { + // -*- hardware initiation -*- + + usbSerial.begin(baud); + while (usbWait && !usbSerial) + ; + + pinMode(BATVOLT_PIN, INPUT); + + // modem.init(); + pass.init(); + + Wire.begin(); + delay(1000); + sodaq_gps.init(GPS_ENABLE); + + accel.rebootAccelerometer(); + delay(1000); + + // Enable the Accelerometer + accel.enableAccelerometer(); + + pass.connect("muizenval.tk", 80); + + // modem.send("ATE0"); // disable command-echo + + // if (modem.send("AT+CPIN=\"" SIM_PIN "\"") == sara_modem::COMMAND_ERROR) { + // // usbSerial.println("[EROR] sim can't be unlocked, wrong PIN"); + // return; + // } + // usbSerial.println(prefixInfo "sim successful unlocked"); + /* + + modem.send("AT+CPSMS=0"); // Disable Power Saving Mode + modem.send("AT+CEDRXS=0"); // Disable eDRX + // usbSerial.println(prefixInfo "disabled power safe"); + + + // -*- internet initialization -*- + char info[100]; + + modem.send("AT+CFUN=15", sara_modem::COMMAND_BLOCK); // Reset the module + modem.send("AT+UMNOPROF=1", sara_modem::COMMAND_BLOCK); // Set MNO profile (1=automatic,100=standard europe) + modem.send("AT+URAT?", info); + // usbSerial.print(prefixInfo "urat: "); + // usbSerial.println(info); + modem.send("AT+URAT=8", sara_modem::COMMAND_IGNORE); // Set URAT to LTE-M/NB-IOT + modem.send("AT+CEREG=3", sara_modem::COMMAND_IGNORE); // Enable URCs + modem.send("AT+CGDCONT=1,\"IP\",\"" simAPN "\"", sara_modem::COMMAND_BLOCK); // Set the APN + modem.send("AT+CFUN=1"); // enable radio + modem.send("AT+COPS=0,2", sara_modem::COMMAND_BLOCK); // Autoselect the operator + + // usbSerial.print(prefixInfo "waiting for connection"); + + char response[100]; + + // Check Siganl strenght, repeat till you have a valid CSQ (99,99 means no signal) + while (modem.send("AT+CSQ", response, sara_modem::COMMAND_SILENT) == sara_modem::COMMAND_OK && !strcmp(response, "+CSQ: 99,99")) { + delay(1000); + // usbSerial.print("."); + } + + // Wait for attach, 1 = attached + while (modem.send("AT+CGATT?", response, sara_modem::COMMAND_SILENT) == sara_modem::COMMAND_OK && strcmp(response, "+CGATT: 1")) { + delay(1000); + // usbSerial.print("."); + } + // usbSerial.println(); + + // usbSerial.println(prefixInfo "connected!"); + + // -*- server connection -*- + + + //AT+UHTTP=0,0,"86.92.67.21" + //AT+UHTTP=0,5,80 + //AT+UHTTPC=0,5,"/api/search_connect","","TEST!",1 + + modem.send("AT+UHTTP=0,0,\"86.92.67.21\""); + modem.send("AT+UHTTP=0,5,80"); + modem.send("AT+UHTTPC=0,5,\"/api/search_connect\",\"\",\"TEST!\",1");*/ + + + // usbSerial.println(prefixInfo "initiation completed, starting passthrough:"); +} + + +void loop() { + /* // -*- passthrough for custom commands -*- + while (usbSerial.available()) + modemSerial.write(usbSerial.read()); + + while (modemSerial.available()) + usbSerial.write(modemSerial.read()); + + char buffer[512]; + gps.read(buffer); + + if (buffer[0] != '\0') { + // usbSerial.print("gps | "); + // usbSerial.println(buffer); + }*/ + + static double lat = 0, lon = 0, accuracy = 0; + + if (sodaq_gps.scan(true, 10000)) { + lat = sodaq_gps.getLat(); + lon = sodaq_gps.getLon(); + accuracy = 1.0 / sodaq_gps.getHDOP() * 100; + // -> 100% the best, 0% the worst + // usbSerial.print(sodaq_gps.getLat(), 13); + // usbSerial.print(" - "); + // usbSerial.print(sodaq_gps.getLon(), 13); + // usbSerial.print(" ~ accuracy "); + // usbSerial.print(1.0 / sodaq_gps.getHDOP() * 100, 1); + // usbSerial.println("%"); + } + + passthrough::http_packet req, res; + req.method = "POST"; + req.endpoint = "/api/update"; + req.body["latitude"] = lat; + req.body["longitude"] = lon; + req.body["accuracy"] = accuracy; + req.body["battery"] = batteryVoltage(); + req.body["temperature"] = temperature(); + + pass.send(req); + + // usbSerial.print(batteryVoltage()); + // usbSerial.println("V battery"); + + // usbSerial.print(temperature()); + // usbSerial.println(" deg celsius"); +} + +int temperature() { + return accel.getTemperature(); +} + +int batteryVoltage() { + return batteryFactor * (float) analogRead(BATVOLT_PIN); +} diff --git a/5g-client/command.h b/5g-client/command.h @@ -1,20 +0,0 @@ -#pragma once - -// -*- enums and structs -*- -enum command_status { - COMMAND_OK = 0, // command succeed - COMMAND_ERROR = 1, // command returned an error - COMMAND_TIMEOUT = 2 // command timed out -}; - -enum command_flags { - COMMAND_NONE, // none of them underneath - COMMAND_SILENT = 1 << 0, // no debug messages (for looped commands) - COMMAND_BLOCK = 1 << 1, // no time-out (for waiting commands) - COMMAND_IGNORE = 1 << 2, // don't wait for response, just wait $ignoreDelay secounds - COMMAND_EVENT = 1 << 3, // handle '+'-responses as event -}; - -// -*- declarations -*- -command_status sendCommand(const char* request, char* response, command_flags flags = COMMAND_NONE); -command_status sendCommand(const char* request, command_flags flags = COMMAND_NONE); -\ No newline at end of file diff --git a/5g-client/command.ino b/5g-client/command.ino @@ -1,102 +0,0 @@ -#include "command.h" -#include "config.h" - -// -*- helper functions -*- -command_status sendCommand(const char* request, char* response, command_flags flags) { - char line[lineBuffer]; - size_t lineLen; - char buf; - - bool silent = flags & COMMAND_SILENT, - block = flags & COMMAND_BLOCK, - ignore = flags & COMMAND_IGNORE, - event_handle = flags & COMMAND_EVENT; - - if (response) - response[0] = '\0'; - - unsigned long start = millis(), - now; - - modemSerial.write(request); - modemSerial.write("\r\n"); - modemSerial.flush(); - - if (blockDebug && block && !silent) { - usbSerial.print(prefixDebug "command '"); - usbSerial.print(request); - usbSerial.println("' is blocking"); - } - - for (;;) { - lineLen = 0; - for (;;) { - while (!modemSerial.available()) { - now = millis(); - if (ignore && now - start > ignoreDelay * 1000) { - if (commandDebug && !silent) { - usbSerial.print(prefixDebug "command '"); - usbSerial.print(request); - usbSerial.println("' succeed (ignoring response)"); - } - return COMMAND_TIMEOUT; - } else if (!ignore && !block && now - start > commandTimeout * 1000) { - if (commandDebug && !silent) { - usbSerial.print(prefixWarn "command '"); - usbSerial.print(request); - usbSerial.println("' timed out"); - } - return COMMAND_TIMEOUT; - } - } - buf = modemSerial.read(); - if (buf == '\r') - continue; - if (buf == '\n') - break; - line[lineLen++] = buf; - } - line[lineLen] = '\0'; - - if (String(line) == "OK") { - if (commandDebug && !silent) { - usbSerial.print(prefixDebug "command '"); - usbSerial.print(request); - usbSerial.println("' succeed"); - } - return COMMAND_OK; - } else if (strstr(line, "ERROR")) { - if (commandDebug && !silent) { - usbSerial.print(prefixError "command '"); - usbSerial.print(request); - usbSerial.println("' failed"); - } - return COMMAND_ERROR; - } else if (event_handle && line[0] == '+') { - if (eventDebug && !silent) { - usbSerial.print(prefixEvent "event '"); - usbSerial.print(line); - usbSerial.println(" caused'"); - } - } else if (line[0] != '\0' && strcmp(request, line)) { - if (lineDebug && !silent) { - usbSerial.print(prefixLine); - usbSerial.print(request); - usbSerial.print(" -> '"); - usbSerial.print(line); - usbSerial.println("'"); - } - - if (response) { - if (response[0] != '\0') // check if not empty string - strcat(response, "\n"); - strcat(response, line); - } - } - } - delay(commandDelay * 1000); // wait 0.1 sec -} - -command_status sendCommand(const char* request, command_flags flags) { - return sendCommand(request, NULL, flags); -} diff --git a/5g-client/config.h b/5g-client/config.h @@ -1,32 +0,0 @@ -#pragma once - -// -*- prefixes -*- -#define prefixInfo "info | " -#define prefixDebug "debug | " -#define prefixError "error | " -#define prefixLine "line | " -#define prefixWarn "warn | " -#define prefixEvent "event | " - -// -*- hardware stuff -*- -#define usbSerial SerialUSB -#define modemSerial Serial1 -#define powerPin SARA_ENABLE -#define enablePin SARA_TX_ENABLE -#define voltagePin SARA_R4XX_TOGGLE - -// -*- 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 - -// -*- sim settings -*- -#define simPin "0000" // PIN of the sim -#define simAPN "lpwa.vodafone.iot" // APN-network of the sim -\ No newline at end of file diff --git a/5g-client/include/config.h b/5g-client/include/config.h @@ -0,0 +1,28 @@ +#pragma once + +// -*- hardware stuff -*- +#define usbSerial SerialUSB + +// -*- 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 + +// -*- sim settings -*- +#define simPin "0000" // PIN of the sim +#define simAPN "lpwa.vodafone.iot" // APN-network of the sim + +// -*- prefixes -*- +#define prefixInfo "info | " +#define prefixDebug "debug | " +#define prefixError "error | " +#define prefixLine "line | " +#define prefixWarn "warn | " +#define prefixEvent "event | " diff --git a/5g-client/include/modem.h b/5g-client/include/modem.h @@ -0,0 +1,29 @@ +#pragma once + +#define modemSerial Serial1 +#define modemPowerPin SARA_ENABLE +#define modemEnablePin SARA_TX_ENABLE +#define modemVoltagePin SARA_R4XX_TOGGLE + + +struct sara_modem { + // -*- enums and structs -*- + enum command_status { + COMMAND_OK = 0, // command succeed + COMMAND_ERROR = 1, // command returned an error + COMMAND_TIMEOUT = 2 // command timed out + }; + + enum command_flags { + COMMAND_NONE, // none of them underneath + COMMAND_SILENT = 1 << 0, // no debug messages (for looped commands) + COMMAND_BLOCK = 1 << 1, // no time-out (for waiting commands) + COMMAND_IGNORE = 1 << 2, // don't wait for response, just wait $ignoreDelay secounds + COMMAND_EVENT = 1 << 3, // handle '+'-responses as event + }; + + // -*- declarations -*- + void init(); + command_status send(const char* request, char* response, command_flags flags = COMMAND_NONE); + command_status send(const char* request, command_flags flags = COMMAND_NONE); +}; diff --git a/5g-client/include/passthrough.h b/5g-client/include/passthrough.h @@ -0,0 +1,17 @@ +#pragma once + +#include <Arduino_JSON.h> + +struct passthrough { + struct http_packet { + const char* method; + const char* endpoint; + JSONVar headers; + JSONVar body; + }; + + void init(); + void connect(const char* host, int port); + const char* send(http_packet request, http_packet& response); + const char* send(http_packet request); +}; +\ No newline at end of file diff --git a/5g-client/modem.ino b/5g-client/modem.ino @@ -0,0 +1,130 @@ +#include "include/config.h" +#include "include/modem.h" + + +void sara_modem::init() { + pinMode(modemPowerPin, OUTPUT); // Put voltage on the nb-iot module + pinMode(modemVoltagePin, OUTPUT); // Switch module voltage + pinMode(modemEnablePin, OUTPUT); // Set state to active + + digitalWrite(modemPowerPin, HIGH); + digitalWrite(modemVoltagePin, LOW); + digitalWrite(modemEnablePin, HIGH); + modemSerial.begin(baud); + + while (!modemSerial) + ; + + // -*- module initialization -*- + // usbSerial.print(prefixInfo "waiting for module to start up"); + for (;;) { + // usbSerial.print('.'); + modemSerial.write("AT\r\n"); + delay(1000); + if (modemSerial.available()) + break; + } + + while (modemSerial.available()) // clear cache + modemSerial.read(); + // usbSerial.println(); +} + +sara_modem::command_status sara_modem::send(const char* request, char* response, command_flags flags) { + char line[lineBuffer]; + size_t lineLen; + char buf; + + bool silent = flags & COMMAND_SILENT, + block = flags & COMMAND_BLOCK, + ignore = flags & COMMAND_IGNORE, + event_handle = flags & COMMAND_EVENT; + + if (response) + response[0] = '\0'; + + unsigned long start = millis(), + now; + + modemSerial.write(request); + modemSerial.write("\r\n"); + modemSerial.flush(); + + if (blockDebug && block && !silent) { + // usbSerial.print(prefixDebug "command '"); + // usbSerial.print(request); + // usbSerial.println("' is blocking"); + } + + for (;;) { + lineLen = 0; + for (;;) { + while (!modemSerial.available()) { + now = millis(); + if (ignore && now - start > ignoreDelay * 1000) { + if (commandDebug && !silent) { + // usbSerial.print(prefixDebug "command '"); + // usbSerial.print(request); + // usbSerial.println("' succeed (ignoring response)"); + } + return COMMAND_TIMEOUT; + } else if (!ignore && !block && now - start > commandTimeout * 1000) { + if (commandDebug && !silent) { + // usbSerial.print(prefixWarn "command '"); + // usbSerial.print(request); + // usbSerial.println("' timed out"); + } + return COMMAND_TIMEOUT; + } + } + buf = modemSerial.read(); + if (buf == '\r') + continue; + if (buf == '\n') + break; + line[lineLen++] = buf; + } + line[lineLen] = '\0'; + + if (String(line) == "OK") { + if (commandDebug && !silent) { + // usbSerial.print(prefixDebug "command '"); + // usbSerial.print(request); + // usbSerial.println("' succeed"); + } + return COMMAND_OK; + } else if (strstr(line, "ERROR")) { + if (commandDebug && !silent) { + // usbSerial.print(prefixError "command '"); + // usbSerial.print(request); + // usbSerial.println("' failed"); + } + return COMMAND_ERROR; + } else if (event_handle && line[0] == '+') { + if (eventDebug && !silent) { + // usbSerial.print(prefixEvent "event '"); + // usbSerial.print(line); + // usbSerial.println(" caused'"); + } + } else if (line[0] != '\0' && strcmp(request, line)) { + if (lineDebug && !silent) { + // usbSerial.print(prefixLine); + // usbSerial.print(request); + // usbSerial.print(" -> '"); + // usbSerial.print(line); + // usbSerial.println("'"); + } + + if (response) { + if (response[0] != '\0') // check if not empty string + strcat(response, "\n"); + strcat(response, line); + } + } + } + delay(commandDelay * 1000); // wait 0.1 sec +} + +sara_modem::command_status sara_modem::send(const char* request, command_flags flags) { + return send(request, NULL, flags); +} diff --git a/5g-client/passthrough.ino b/5g-client/passthrough.ino @@ -0,0 +1,69 @@ +#include "include/config.h" +#include "include/passthrough.h" + +JSONVar readJSON() { + char line[lineBuffer]; + char buf; + int i = 0; + for (;;) { + while (!usbSerial.available()) + ; + buf = usbSerial.read(); + if (buf == '\r') + continue; + if (buf == '\n') + break; + line[i++] = buf; + } + line[i++] = '\0'; + + return JSON.parse(line); +} + +void passthrough::init() { + usbSerial.println("{\"command\":\"hello\"}"); + JSONVar res_json = readJSON(); + if (res_json["error"] != nullptr) { + // :( + } +} + +void passthrough::connect(const char* host, int port) { + JSONVar body; + body["command"] = "connect"; + body["host"] = host; + body["port"] = port; + + usbSerial.println(body); +} + +const char* passthrough::send(http_packet request, http_packet& response) { + JSONVar body; + body["command"] = "send"; + body["method"] = request.method; + body["endpoint"] = request.endpoint; + body["headers"] = request.headers; + body["body"] = request.body; + usbSerial.println(body); + + JSONVar res_json = readJSON(); + response.body = res_json["body"]; + response.headers = res_json["headers"]; + + return res_json["error"]; +} + + +const char* passthrough::send(http_packet request) { + JSONVar body; + body["command"] = "send"; + body["method"] = request.method; + body["endpoint"] = request.endpoint; + body["headers"] = request.headers; + body["body"] = request.body; + usbSerial.println(body); + + JSONVar res_json = readJSON(); + + return res_json["error"]; +} diff --git a/passthough.py b/passthough.py @@ -0,0 +1,50 @@ +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>') + +serial_port = serial.Serial(port=sys.argv[1], baudrate=115200) + +client: Optional[HTTPConnection] = None + +def handle(req): + global client + + if 'command' not in req: + return 'command ommitted' + elif req['command'] == 'hello': + return None + elif req['command'] == 'connect': + client = HTTPConnection(req['host'], req['port']) + elif req['command'] == 'send': + if client is None: + return 'not connected' + client.request(req['method'], req['endpoint'], json.dumps(req['body']), req['headers'] or {}) + res = client.getresponse() + return { 'code': res.status, 'headers': dict(res.headers), 'body': json.load(res) } + else: + return 'unknown command' + +while serial_port.is_open: + req = json.loads(serial_port.readline()) + + print('-> ' + repr(req)) + res = handle(req) + + if type(res) == str: + res = { "error": res } + elif res is None: + res = { "error": None } + elif type(res) == dict: + if 'error' not in res: + res['error'] = None + else: + res = { "error": None, "value": res } + print('<- ' + repr(res)) + + serial_port.write((json.dumps(res) + '\n').encode()) diff --git a/readme.md b/readme.md @@ -2,7 +2,10 @@ > Hamdi Hassan, Loes Hoogstra, Gerco van Woudenberg, Friedel Schon -## De server runnen +# :warning: Disclaimer +**Op Anaconda-python werkt niks (flask zou gewoon ophangen), dus gebruik de officiële Python-versie** + +## Server runnen Dit is een dev-server, dus run je met `debug=True`-flag! @@ -37,6 +40,7 @@ $ python3 run-server.py $ python3 test-client.py --help ``` -## Known issues +## Third-party libraries (Arduino Library Manager) -- op Anaconda-python werkt niks (flask zou gewoon ophangen), dus gebruik de officiële Python-versie -\ No newline at end of file +- https://github.com/SodaqMoja/Sodaq_LSM303AGR +- https://github.com/SodaqMoja/Sodaq_UBlox_GPS +\ No newline at end of file