diff --git a/tinyGS/src/ConfigManager/ConfigManager.cpp b/tinyGS/src/ConfigManager/ConfigManager.cpp index 717e3b87..1c2c52bf 100644 --- a/tinyGS/src/ConfigManager/ConfigManager.cpp +++ b/tinyGS/src/ConfigManager/ConfigManager.cpp @@ -27,42 +27,39 @@ #endif ConfigManager::ConfigManager() -: IotWebConf2(thingName, &dnsServer, &server, initialApPassword, configVersion) -, server(80) -, gsConfigHtmlFormatProvider(*this) -, boards({ - //OLED_add, OLED_SDA, OLED_SCL, OLED_RST, PROG_BUTTON, BOARD_LED, L_SX127X?, L_NSS, L_DI00, L_DI01, L_BUSSY, L_RST, L_MISO, L_MOSI, L_SCK, L_TCXO_V, BOARD - { 0x3c, 4, 15, 16, 0, 25, 1, 18, 26, 12, 0, 14, 19, 27, 5, 0.0f, "433Mhz HELTEC WiFi LoRA 32 V1" }, // @4m1g0 - { 0x3c, 4, 15, 16, 0, 25, 1, 18, 26, 12, 0, 14, 19, 27, 5, 0.0f, "863-928Mhz HELTEC WiFi LoRA 32 V1" }, - { 0x3c, 4, 15, 16, 0, 25, 1, 18, 26, 35, 0, 14, 19, 27, 5, 0.0f, "433Mhz HELTEC WiFi LoRA 32 V2" }, // @4m1g0 - { 0x3c, 4, 15, 16, 0, 25, 1, 18, 26, 35, 0, 14, 19, 27, 5, 0.0f, "863-928Mhz HELTEC WiFi LoRA 32 V2" }, - { 0x3c, 4, 15, 16, 0, 2, 1, 18, 26, 0, 0, 14, 19, 27, 5, 0.0f, "433Mhz TTGO LoRa 32 v1" }, // @g4lile0 - { 0x3c, 4, 15, 16, 0, 2, 1, 18, 26, 0, 0, 14, 19, 27, 5, 0.0f, "868-915Mhz TTGO LoRa 32 v1" }, // - { 0x3c, 21, 22, 16, 0, 22, 1, 18, 26, 33, 0, 14, 19, 27, 5, 0.0f, "433 Mhz TTGO LoRA 32 v2" }, // @TCRobotics - { 0x3c, 21, 22, 16, 0, 22, 1, 18, 26, 33, 0, 14, 19, 27, 5, 0.0f, "868-915Mhz TTGO LoRA 32 v2" }, // - { 0x3c, 21, 22, 16, 39, 22, 1, 18, 26, 33, 32, 14, 19, 27, 5, 0.0f, "433Mhz T-BEAM + OLED" }, - { 0x3c, 21, 22, 16, 39, 22, 1, 18, 26, 33, 32, 14, 19, 27, 5, 0.0f, "868-915Mhz T-BEAM + OLED" }, - { 0x3c, 21, 22, 16, 0, 25, 0, 5, 0, 27, 26, 14, 19, 23, 18, 0.0f, "Custom ESP32 Wroom + SX126x (Crystal)" }, // @4m1g0, @lillefyr - { 0x3c, 21, 22, 16, 0, 25, 0, 18, 0, 33, 32, 14, 19, 27, 5, 0.0f, "TTGO LoRa 32 V2 Modified with module SX126x (crystal)" },// @TCRobotics - { 0x3c, 21, 22, 16, 0, 25, 0, 5, 0, 2, 13, 26, 19, 23, 18, 1.6f, "Custom ESP32 Wroom + SX126x DRF1268T (TCX0) (5, 2, 26, 13)" }, // @sdey76 - { 0x3c, 21, 22, 16, 0, 25, 0, 5, 0, 26, 12, 14, 19, 23, 18, 1.6f, "Custom ESP32 Wroom + SX126x DRF1268T (TCX0) (5, 26, 14, 12)" }, // @imants - { 0x3c, 21, 22, 16, 38, 22, 1, 18, 26, 33, 0, 14, 19, 27, 5, 0.0f, "T-BEAM V1.0 + OLED" }, // @fafu - { 0x3c, 21, 22, 16, 0, 2, 0, 5, 0, 34, 32, 14, 19, 27, 18, 1.6f, "433Mhz FOSSA 1W Ground Station" }, // @jgromes - { 0x3c, 21, 22, 16, 0, 2, 0, 5, 0, 34, 32, 14, 19, 27, 18, 1.6f, "868-915Mhz FOSSA 1W Ground Station" }, // @jgromes - }) + : IotWebConf2(thingName, &dnsServer, &server, initialApPassword, configVersion), server(80), gsConfigHtmlFormatProvider(*this), boards({ + //OLED_add, OLED_SDA, OLED_SCL, OLED_RST, PROG_BUTTON, BOARD_LED, L_SX127X?, L_NSS, L_DI00, L_DI01, L_BUSSY, L_RST, L_MISO, L_MOSI, L_SCK, L_TCXO_V, BOARD + {0x3c, 4, 15, 16, 0, 25, 1, 18, 26, 12, 0, 14, 19, 27, 5, 0.0f, "433Mhz HELTEC WiFi LoRA 32 V1"}, // @4m1g0 + {0x3c, 4, 15, 16, 0, 25, 1, 18, 26, 12, 0, 14, 19, 27, 5, 0.0f, "863-928Mhz HELTEC WiFi LoRA 32 V1"}, + {0x3c, 4, 15, 16, 0, 25, 1, 18, 26, 35, 0, 14, 19, 27, 5, 0.0f, "433Mhz HELTEC WiFi LoRA 32 V2"}, // @4m1g0 + {0x3c, 4, 15, 16, 0, 25, 1, 18, 26, 35, 0, 14, 19, 27, 5, 0.0f, "863-928Mhz HELTEC WiFi LoRA 32 V2"}, + {0x3c, 4, 15, 16, 0, 2, 1, 18, 26, 0, 0, 14, 19, 27, 5, 0.0f, "433Mhz TTGO LoRa 32 v1"}, // @g4lile0 + {0x3c, 4, 15, 16, 0, 2, 1, 18, 26, 0, 0, 14, 19, 27, 5, 0.0f, "868-915Mhz TTGO LoRa 32 v1"}, // + {0x3c, 21, 22, 16, 0, 22, 1, 18, 26, 33, 0, 14, 19, 27, 5, 0.0f, "433 Mhz TTGO LoRA 32 v2"}, // @TCRobotics + {0x3c, 21, 22, 16, 0, 22, 1, 18, 26, 33, 0, 14, 19, 27, 5, 0.0f, "868-915Mhz TTGO LoRA 32 v2"}, // + {0x3c, 21, 22, 16, 39, 22, 1, 18, 26, 33, 32, 14, 19, 27, 5, 0.0f, "433Mhz T-BEAM + OLED"}, + {0x3c, 21, 22, 16, 39, 22, 1, 18, 26, 33, 32, 14, 19, 27, 5, 0.0f, "868-915Mhz T-BEAM + OLED"}, + {0x3c, 21, 22, 16, 0, 25, 0, 5, 0, 27, 26, 14, 19, 23, 18, 0.0f, "Custom ESP32 Wroom + SX126x (Crystal)"}, // @4m1g0, @lillefyr + {0x3c, 21, 22, 16, 0, 25, 0, 18, 0, 33, 32, 14, 19, 27, 5, 0.0f, "TTGO LoRa 32 V2 Modified with module SX126x (crystal)"}, // @TCRobotics + {0x3c, 21, 22, 16, 0, 25, 0, 5, 0, 2, 13, 26, 19, 23, 18, 1.6f, "Custom ESP32 Wroom + SX126x DRF1268T (TCX0) (5, 2, 26, 13)"}, // @sdey76 + {0x3c, 21, 22, 16, 0, 25, 0, 5, 0, 26, 12, 14, 19, 23, 18, 1.6f, "Custom ESP32 Wroom + SX126x DRF1268T (TCX0) (5, 26, 14, 12)"}, // @imants + {0x3c, 21, 22, 16, 38, 22, 1, 18, 26, 33, 0, 14, 19, 27, 5, 0.0f, "T-BEAM V1.0 + OLED"}, // @fafu + {0x3c, 21, 22, 16, 0, 2, 0, 5, 0, 34, 32, 14, 19, 27, 18, 1.6f, "433Mhz FOSSA 1W Ground Station"}, // @jgromes + {0x3c, 21, 22, 16, 0, 2, 0, 5, 0, 34, 32, 14, 19, 27, 18, 1.6f, "868-915Mhz FOSSA 1W Ground Station"}, // @jgromes + }) { - server.on(ROOT_URL, [this]{ handleRoot(); }); - server.on(CONFIG_URL, [this]{ handleConfig(); }); - server.on(DASHBOARD_URL, [this]{ handleDashboard(); }); - server.on(RESTART_URL, [this]{ handleRestart(); }); - server.on(REFRESH_CONSOLE_URL, [this]{ handleRefreshConsole(); }); + server.on(ROOT_URL, [this] { handleRoot(); }); + server.on(CONFIG_URL, [this] { handleConfig(); }); + server.on(DASHBOARD_URL, [this] { handleDashboard(); }); + server.on(RESTART_URL, [this] { handleRestart(); }); + server.on(REFRESH_CONSOLE_URL, [this] { handleRefreshConsole(); }); setupUpdateServer( - [this](const char* updatePath) { httpUpdater.setup(&server, updatePath); }, - [this](const char* userName, char* password) { httpUpdater.updateCredentials(userName, password); }); + [this](const char *updatePath) { httpUpdater.setup(&server, updatePath); }, + [this](const char *userName, char *password) { httpUpdater.updateCredentials(userName, password); }); setHtmlFormatProvider(&gsConfigHtmlFormatProvider); formValidatorStd = std::bind(&ConfigManager::formValidator, this, std::placeholders::_1); setFormValidator(formValidatorStd); - setConfigSavedCallback([this]{ configSavedCallback(); }); + setConfigSavedCallback([this] { configSavedCallback(); }); skipApStartup(); // Customize own parameters @@ -144,10 +141,10 @@ void ConfigManager::handleDashboard() s += F("

Groundstation Status

"); s += ""; s += ""; - s += ""; - s += ""; - s += ""; - s += ""; + s += ""; + s += ""; + s += ""; + s += ""; //s += ""; s += F("
Name " + String(getThingName()) + "
Version " + String(status.version) + "
MQTT Server " + String(status.mqtt_connected?"CONNECTED":"NOT CONNECTED") + "
WiFi " + String(WiFi.isConnected()?"CONNECTED":"NOT CONNECTED") + "
Radio " + String(Radio::getInstance().isReady()?"READY":"NOT READY") + "
Test Mode " + String(getTestMode()?"ENABLED":"DISABLED") + "
MQTT Server " + String(status.mqtt_connected ? "CONNECTED" : "NOT CONNECTED") + "
WiFi " + String(WiFi.isConnected() ? "CONNECTED" : "NOT CONNECTED") + "
Radio " + String(Radio::getInstance().isReady() ? "READY" : "NOT READY") + "
Test Mode " + String(getTestMode() ? "ENABLED" : "DISABLED") + "
Uptime " + // process and update in js + "
"); s += F("

Modem Configuration

"); @@ -171,7 +168,7 @@ void ConfigManager::handleDashboard() s += ""; s += ""; s += ""; - s += ""; + s += ""; s += F("
Signal RSSI " + String(status.lastPacketInfo.rssi) + "
Signal SNR " + String(status.lastPacketInfo.snr) + "
Frequency error " + String(status.lastPacketInfo.frequencyerror) + "
" + String(status.lastPacketInfo.crc_error?"CRC ERROR!":"") + "
" + String(status.lastPacketInfo.crc_error ? "CRC ERROR!" : "") + "
"); s += FPSTR(IOTWEBCONF_CONSOLE_BODY_INNER); s += "


"; @@ -196,44 +193,48 @@ void ConfigManager::handleRefreshConsole() } } - uint32_t counter = 0; + uint32_t counter = 0; String svalue = server.arg("c1"); - if (svalue.length()) { + if (svalue.length()) + { Log::console(PSTR("COMMAND: %s"), svalue.c_str()); - if (strcmp(svalue.c_str(), "p") == 0) { + if (strcmp(svalue.c_str(), "p") == 0) + { if (!getAllowTx()) { Log::console(PSTR("Radio transmission is not allowed by config! Check your config on the web panel and make sure transmission is allowed by local regulations")); } - else + else { static long lastTestPacketTime = 0; - if (millis() - lastTestPacketTime < 20*1000) + if (millis() - lastTestPacketTime < 20 * 1000) { Log::console(PSTR("Please wait a few seconds to send another test packet.")); - } + } else { - Radio& radio = Radio::getInstance(); + Radio &radio = Radio::getInstance(); radio.sendTestPacket(); lastTestPacketTime = millis(); Log::console(PSTR("Sending test packet to nearby stations!")); } } } - else + else { Log::console(PSTR("%s"), F("Command still not supported in web serial console!")); } } - char stmp[8]; String s = server.arg("c2"); strlcpy(stmp, s.c_str(), sizeof(stmp)); - if (strlen(stmp)) { counter = atoi(stmp); } + if (strlen(stmp)) + { + counter = atoi(stmp); + } server.client().flush(); server.sendHeader(F("Cache-Control"), F("no-cache, no-store, must-revalidate")); server.sendHeader(F("Pragma"), F("no-cache")); @@ -241,24 +242,31 @@ void ConfigManager::handleRefreshConsole() server.setContentLength(CONTENT_LENGTH_UNKNOWN); server.send(200, F("text/plain"), ""); server.sendContent(String((uint8_t)Log::getLogIdx()) + "\n"); - if (counter != Log::getLogIdx()) { - if (!counter) { + if (counter != Log::getLogIdx()) + { + if (!counter) + { counter = Log::getLogIdx(); } - do { - char* tmp; + do + { + char *tmp; size_t len; Log::getLog(counter, &tmp, &len); - if (len) { - char stemp[len +1]; + if (len) + { + char stemp[len + 1]; memcpy(stemp, tmp, len); - stemp[len-1] = '\n'; + stemp[len - 1] = '\n'; stemp[len] = '\0'; server.sendContent(stemp); } counter++; counter &= 0xFF; - if (!counter) { counter++; } // Skip log index 0 as it is not allowed + if (!counter) + { + counter++; + } // Skip log index 0 as it is not allowed } while (counter != Log::getLogIdx()); } @@ -279,7 +287,6 @@ void ConfigManager::handleRestart() } } - String s = String(FPSTR(IOTWEBCONF_HTML_HEAD)); s += ""; s += ""; @@ -297,10 +304,10 @@ void ConfigManager::handleRestart() ESP.restart(); } -bool ConfigManager::formValidator(iotwebconf2::WebRequestWrapper* webRequestWrapper) +bool ConfigManager::formValidator(iotwebconf2::WebRequestWrapper *webRequestWrapper) { String name = webRequestWrapper->arg(this->getThingNameParameter()->getId()); - + if (name.length() < 3) { this->getThingNameParameter()->errorMessage = "Your ground station name must have more then 3 characters and less then 25 and should be unique."; @@ -313,7 +320,7 @@ bool ConfigManager::formValidator(iotwebconf2::WebRequestWrapper* webRequestWrap return false; } - for (const char* c = name.c_str(); *c != '\0'; c++) + for (const char *c = name.c_str(); *c != '\0'; c++) { if (!((*c >= '0' && *c <= '9') || (*c >= 'A' && *c <= 'Z') || *c == '_' || (*c >= 'a' && *c <= 'z'))) { @@ -347,17 +354,17 @@ void ConfigManager::resetAllConfig() strncpy(mqttServerParam.valueBuffer, MQTT_DEFAULT_SERVER, MQTT_SERVER_LENGTH); mqttUserParam.valueBuffer[0] = '\0'; mqttPassParam.valueBuffer[0] = '\0'; - latitude[0] = '\0'; - longitude[0] = '\0'; - oledBright[0] = '\0'; - allowTx[0] = '\0'; - remoteTune[0] = '\0'; - telemetry3rd[0] = '\0'; - testMode[0] = '\0'; - autoUpdate[0] = '\0'; - boardTemplate[0] = '\0'; - modemStartup[0] = '\0'; - advancedConfig[0] = '\0'; + latitude[0] = '\0'; + longitude[0] = '\0'; + oledBright[0] = '\0'; + allowTx[0] = '\0'; + remoteTune[0] = '\0'; + telemetry3rd[0] = '\0'; + testMode[0] = '\0'; + autoUpdate[0] = '\0'; + boardTemplate[0] = '\0'; + modemStartup[0] = '\0'; + advancedConfig[0] = '\0'; saveConfig(); } @@ -378,13 +385,16 @@ boolean ConfigManager::init() setApTimeoutMs(atoi(AP_TIMEOUT_MS)); // no board selected - if (!strcmp(board, "")){ + if (!strcmp(board, "")) + { boardDetection(); } if (strlen(advancedConfig)) parseAdvancedConf(); + parseModemStartup(); + strcpy(savedThingName, this->getThingName()); return validConfig; } @@ -414,37 +424,37 @@ void ConfigManager::boardDetection() if (boards[ite].L_BUSSY) {Serial.print(F(" BUSSY:")); Serial.print(boards[ite].L_BUSSY);} Serial.println(""); }*/ - + // test OLED configuration Log::error(PSTR("Automatic board detection running... ")); - for (uint8_t ite=0; ite<((sizeof(boards)/sizeof(boards[0])));ite++) + for (uint8_t ite = 0; ite < ((sizeof(boards) / sizeof(boards[0]))); ite++) { Serial.print(boards[ite].BOARD); - pinMode(boards[ite].OLED__RST,OUTPUT); - digitalWrite(boards[ite].OLED__RST, LOW); + pinMode(boards[ite].OLED__RST, OUTPUT); + digitalWrite(boards[ite].OLED__RST, LOW); delay(50); digitalWrite(boards[ite].OLED__RST, HIGH); - Wire.begin (boards[ite].OLED__SDA, boards[ite].OLED__SCL); + Wire.begin(boards[ite].OLED__SDA, boards[ite].OLED__SCL); Wire.beginTransmission(boards[ite].OLED__address); if (!Wire.endTransmission()) - { - Log::error(PSTR("Compatible OLED FOUND")); + { + Log::error(PSTR("Compatible OLED FOUND")); itoa(ite, board, 10); break; } - else + else { Log::error(PSTR("Not Compatible board found, please select it manually on the web config panel")); - } + } } } void ConfigManager::printConfig() { Log::debug(PSTR("MQTT Port: %u\nMQTT Server: %s\nMQTT Pass: %s\nLatitude: %f\nLongitude: %f"), getMqttPort(), getMqttServer(), getMqttPass(), getLatitude(), getLongitude()); - Log::debug(PSTR("tz: %s\nboard: %u --> %s\nOLED Bright: %u\nTX %s"),getTZ(), getBoard(), boards[getBoard()].BOARD.c_str(), getOledBright(), getAllowTx() ? "Enable" : "Disable"); - Log::debug(PSTR("Remote Tune %\nSend telemetry to third party %s"), getRemoteTune() ? "Allowed" : "Blocked", getTelemetry3rd() ? "Allowed" : "Blocked"); - Log::debug(PSTR("Test mode %s\nAuto Update %s"), getTestMode() ? "Enable" : "Disable", getAutoUpdate() ? "Enable" : "Disable"); + Log::debug(PSTR("tz: %s\nboard: %u --> %s\nOLED Bright: %u\nTX %s"), getTZ(), getBoard(), boards[getBoard()].BOARD.c_str(), getOledBright(), getAllowTx() ? "Enable" : "Disable"); + Log::debug(PSTR("Remote Tune %\nSend telemetry to third party %s"), getRemoteTune() ? "Allowed" : "Blocked", getTelemetry3rd() ? "Allowed" : "Blocked"); + Log::debug(PSTR("Test mode %s\nAuto Update %s"), getTestMode() ? "Enable" : "Disable", getAutoUpdate() ? "Enable" : "Disable"); } void ConfigManager::configSavedCallback() @@ -455,13 +465,16 @@ void ConfigManager::configSavedCallback() ESP.restart(); } - forceApMode(false); + if (!remoteSave) + { + forceApMode(false); + parseModemStartup(); + MQTT_Client::getInstance().scheduleRestart(); + } parseAdvancedConf(); - MQTT_Client::getInstance().scheduleRestart(); - - if (Radio::getInstance().isReady()) - Radio::getInstance().begin(); + + remoteSave = false; } void ConfigManager::parseAdvancedConf() @@ -471,7 +484,7 @@ void ConfigManager::parseAdvancedConf() size_t size = 512; DynamicJsonDocument doc(size); - deserializeJson(doc, (const char*)advancedConfig); + deserializeJson(doc, (const char *)advancedConfig); if (doc.containsKey(F("dmode"))) { @@ -493,3 +506,67 @@ void ConfigManager::parseAdvancedConf() advancedConf.lowPower = doc["lowPower"]; } } + +void ConfigManager::parseModemStartup() +{ + size_t size = JSON_ARRAY_SIZE(10) + 10 * JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(16) + JSON_ARRAY_SIZE(8) + JSON_ARRAY_SIZE(8) + 64; + DynamicJsonDocument doc(size); + DeserializationError error = deserializeJson(doc, (const char *)modemStartup); + + if (error.code() != DeserializationError::Ok || !doc.containsKey("mode")) + { + Log::console(PSTR("ERROR: Your modem config is invalid. Resetting to default")); + resetModemConfig(); + return; + } + + ModemInfo &m = status.modeminfo; + m.modem_mode = doc["mode"].as(); + strcpy(m.satellite, doc["sat"].as()); + m.NORAD = doc["NORAD"]; + + if (m.modem_mode == "LoRa") + { + m.frequency = doc["freq"]; + m.bw = doc["bw"]; + m.sf = doc["sf"]; + m.cr = doc["cr"]; + m.sw = doc["sw"]; + m.power = doc["pwr"]; + m.preambleLength = doc["pl"]; + m.gain = doc["gain"]; + m.crc = doc["crc"]; + m.fldro = doc["fldro"]; + } + else + { + m.frequency = doc["freq"]; + m.bw = doc["bw"]; + m.bitrate = doc["br"]; + m.freqDev = doc["fd"]; + m.power = doc["pwr"]; + m.preambleLength = doc["pl"]; + m.OOK = doc["ook"]; + m.swSize = doc["fsw"].size(); + for (int i = 0; i < 8; i++) + { + if (i < m.swSize) + m.fsw[i] = doc["fsw"][i]; + else + m.fsw[i] = 0; + } + } + + // packets Filter + uint8_t filterSize = doc["filter"].size(); + for (int i = 0; i < 8; i++) + { + if (i < filterSize) + status.modeminfo.filter[i] = doc["filter"][i]; + else + status.modeminfo.filter[i] = 0; + } + + if (Radio::getInstance().isReady()) + Radio::getInstance().begin(); +} diff --git a/tinyGS/src/ConfigManager/ConfigManager.h b/tinyGS/src/ConfigManager/ConfigManager.h index 9a7fb552..33c36f84 100644 --- a/tinyGS/src/ConfigManager/ConfigManager.h +++ b/tinyGS/src/ConfigManager/ConfigManager.h @@ -20,7 +20,7 @@ #define ConfigManager_h #include "IotWebConf2.h" -#if IOTWEBCONF_DEBUG_DISABLED == 0 && !PLATFORMIO +#if IOTWEBCONF_DEBUG_DISABLED == 0 && !PLATFORMIO #error "Using Arduino IDE is not recommended, please follow this guide https://github.com/G4lile0/tinyGS/wiki/Arduino-IDE or edit /IotWebCong2/src/IotWebConf2Settings.h and add this line at the beggining of the file #define IOTWEBCONF_DEBUG_DISABLED 1" #endif #include "logos.h" @@ -28,9 +28,9 @@ #include "html.h" #ifdef ESP8266 - #include "ESP8266HTTPUpdateServer.h" +#include "ESP8266HTTPUpdateServer.h" #elif defined(ESP32) - #include "IotWebConf2ESP32HTTPUpdateServer.h" +#include "IotWebConf2ESP32HTTPUpdateServer.h" #endif constexpr auto STATION_NAME_LENGTH = 21; @@ -46,7 +46,6 @@ constexpr auto MODEM_LEN = 256; constexpr auto ADVANCED_LEN = 256; constexpr auto CB_SELECTED_STR = "selected"; - constexpr auto ROOT_URL = "/"; constexpr auto CONFIG_URL = "/config"; constexpr auto DASHBOARD_URL = "/dashboard"; @@ -56,18 +55,18 @@ constexpr auto REFRESH_CONSOLE_URL = "/cs"; const char TITLE_TEXT[] PROGMEM = "TinyGS Configuration"; - constexpr auto thingName = "My TinyGS"; constexpr auto initialApPassword = ""; constexpr auto configVersion = "0.05"; //max 4 chars #define MQTT_DEFAULT_SERVER "mqtt.tinygs.com" -#define MQTT_DEFAULT_PORT "8883" +#define MQTT_DEFAULT_PORT "8883" #define MODEM_DEFAULT "{\"mode\":\"LoRa\",\"freq\":436.703,\"bw\":250.0,\"sf\":10,\"cr\":5,\"sw\":18,\"pwr\":5,\"cl\":120,\"pl\":8,\"gain\":0,\"crc\":true,\"fldro\":1,\"sat\":\"Norbi\",\"NORAD\":46494}" constexpr auto AP_TIMEOUT_MS = "300000"; -enum boardNum { +enum boardNum +{ HELTEC_V1_LF = 0, HELTEC_V1_HF, HELTEC_V2_LF, @@ -89,37 +88,38 @@ enum boardNum { NUM_BOARDS //this line always has to be the last one }; -typedef struct { - uint8_t OLED__address; - uint8_t OLED__SDA; - uint8_t OLED__SCL; - uint8_t OLED__RST; - uint8_t PROG__BUTTON; - uint8_t BOARD_LED; - uint8_t L_SX127X; // 0 SX1262 1 SX1278 - uint8_t L_NSS; // CS - uint8_t L_DI00; - uint8_t L_DI01; - uint8_t L_BUSSY; - uint8_t L_RST; - uint8_t L_MISO; - uint8_t L_MOSI; - uint8_t L_SCK; - float L_TCXO_V; - String BOARD; +typedef struct +{ + uint8_t OLED__address; + uint8_t OLED__SDA; + uint8_t OLED__SCL; + uint8_t OLED__RST; + uint8_t PROG__BUTTON; + uint8_t BOARD_LED; + uint8_t L_SX127X; // 0 SX1262 1 SX1278 + uint8_t L_NSS; // CS + uint8_t L_DI00; + uint8_t L_DI01; + uint8_t L_BUSSY; + uint8_t L_RST; + uint8_t L_MISO; + uint8_t L_MOSI; + uint8_t L_SCK; + float L_TCXO_V; + String BOARD; } board_type; -typedef struct { +typedef struct +{ bool flipOled = true; bool dnOled = true; bool lowPower = false; } AdvancedConfig; - class ConfigManager : public IotWebConf2 { public: - static ConfigManager& getInstance() + static ConfigManager &getInstance() { static ConfigManager instance; return instance; @@ -130,13 +130,13 @@ class ConfigManager : public IotWebConf2 boolean init(); void printConfig(); - uint16_t getMqttPort() { return (uint16_t) atoi(mqttPort); } - const char* getMqttServer() { return mqttServer; } - const char* getMqttUser() { return mqttUser; } - const char* getMqttPass() { return mqttPass; } + uint16_t getMqttPort() { return (uint16_t)atoi(mqttPort); } + const char *getMqttServer() { return mqttServer; } + const char *getMqttUser() { return mqttUser; } + const char *getMqttPass() { return mqttPass; } float getLatitude() { return atof(latitude); } float getLongitude() { return atof(longitude); } - const char* getTZ() { return tz + 3; } // +3 removes the first 3 digits used for time zone deduplication + const char *getTZ() { return tz + 3; } // +3 removes the first 3 digits used for time zone deduplication uint8_t getBoard() { return atoi(board); } uint8_t getOledBright() { return atoi(oledBright); } bool getAllowTx() { return !strcmp(allowTx, CB_SELECTED_STR); } @@ -144,48 +144,98 @@ class ConfigManager : public IotWebConf2 bool getTelemetry3rd() { return !strcmp(telemetry3rd, CB_SELECTED_STR); } bool getTestMode() { return !strcmp(testMode, CB_SELECTED_STR); } bool getAutoUpdate() { return !strcmp(autoUpdate, CB_SELECTED_STR); } - void setAllowTx(bool status) { if (status) strcpy(allowTx, CB_SELECTED_STR); else allowTx[0] = '\0'; this->saveConfig(); } - void setRemoteTune(bool status) { if (status) strcpy(remoteTune, CB_SELECTED_STR); else remoteTune[0] = '\0'; this->saveConfig(); } - void setTelemetry3rd(bool status) { if (status) strcpy(telemetry3rd, CB_SELECTED_STR); else telemetry3rd[0] = '\0'; this->saveConfig(); } - void setTestMode(bool status) { if (status) strcpy(testMode, CB_SELECTED_STR); else testMode[0] = '\0'; this->saveConfig(); } - void setAutoUpdate(bool status) { if (status) strcpy(autoUpdate, CB_SELECTED_STR); else autoUpdate[0] = '\0'; this->saveConfig(); } - const char* getModemStartup() { return modemStartup; } - void setModemStartup(const char* modemStr) { strcpy(modemStartup, modemStr); this->saveConfig(); } - const char* getAvancedConfig() { return advancedConfig; } - void setAvancedConfig(const char* adv_prmStr) { strcpy(advancedConfig, adv_prmStr); this->saveConfig(); } - const char* getBoardTemplate() { return boardTemplate; } - void setBoardTemplate(const char* boardTemplateStr) { strcpy(boardTemplate, boardTemplateStr); this->saveConfig(); } + void setAllowTx(bool status) + { + if (status) + strcpy(allowTx, CB_SELECTED_STR); + else + allowTx[0] = '\0'; + this->saveConfig(); + } + void setRemoteTune(bool status) + { + if (status) + strcpy(remoteTune, CB_SELECTED_STR); + else + remoteTune[0] = '\0'; + this->saveConfig(); + } + void setTelemetry3rd(bool status) + { + if (status) + strcpy(telemetry3rd, CB_SELECTED_STR); + else + telemetry3rd[0] = '\0'; + this->saveConfig(); + } + void setTestMode(bool status) + { + if (status) + strcpy(testMode, CB_SELECTED_STR); + else + testMode[0] = '\0'; + this->saveConfig(); + } + void setAutoUpdate(bool status) + { + if (status) + strcpy(autoUpdate, CB_SELECTED_STR); + else + autoUpdate[0] = '\0'; + this->saveConfig(); + } + const char *getModemStartup() { return modemStartup; } + void setModemStartup(const char *modemStr) + { + strcpy(modemStartup, modemStr); + this->saveConfig(); + parseModemStartup(); + } + const char *getAvancedConfig() { return advancedConfig; } + void setAvancedConfig(const char *adv_prmStr) + { + strcpy(advancedConfig, adv_prmStr); + this->saveConfig(); + } + const char *getBoardTemplate() { return boardTemplate; } + void setBoardTemplate(const char *boardTemplateStr) + { + strcpy(boardTemplate, boardTemplateStr); + this->saveConfig(); + } - const char* getWiFiSSID() { return getWifiSsidParameter()->valueBuffer; } + const char *getWiFiSSID() { return getWifiSsidParameter()->valueBuffer; } bool isConnected() { return getState() == IOTWEBCONF_STATE_ONLINE; }; bool isApMode() { return (getState() != IOTWEBCONF_STATE_CONNECTING && getState() != IOTWEBCONF_STATE_ONLINE); } - board_type getBoardConfig(){ return boards[getBoard()]; } - bool getFlipOled(){ return advancedConf.flipOled; } - bool getDayNightOled(){ return advancedConf.dnOled; } - bool getLowPower(){ return advancedConf.lowPower; } - + board_type getBoardConfig() { return boards[getBoard()]; } + bool getFlipOled() { return advancedConf.flipOled; } + bool getDayNightOled() { return advancedConf.dnOled; } + bool getLowPower() { return advancedConf.lowPower; } + void saveConfig() + { + remoteSave = true; + IotWebConf2::saveConfig(); + }; private: - class GSConfigHtmlFormatProvider : public iotwebconf2::HtmlFormatProvider { public: - GSConfigHtmlFormatProvider(ConfigManager& x) : configManager(x) { } + GSConfigHtmlFormatProvider(ConfigManager &x) : configManager(x) {} + protected: String getScriptInner() override { - return - iotwebconf2::HtmlFormatProvider::getScriptInner(); - //String(FPSTR(CUSTOMHTML_SCRIPT_INNER)); + return iotwebconf2::HtmlFormatProvider::getScriptInner(); + //String(FPSTR(CUSTOMHTML_SCRIPT_INNER)); } String getBodyInner() override { - return - String(FPSTR(LOGO)) + - iotwebconf2::HtmlFormatProvider::getBodyInner(); + return String(FPSTR(LOGO)) + + iotwebconf2::HtmlFormatProvider::getBodyInner(); } - ConfigManager& configManager; + ConfigManager &configManager; }; ConfigManager(); @@ -193,12 +243,13 @@ class ConfigManager : public IotWebConf2 void handleDashboard(); void handleRefreshConsole(); void handleRestart(); - bool formValidator(iotwebconf2::WebRequestWrapper* webRequestWrapper); + bool formValidator(iotwebconf2::WebRequestWrapper *webRequestWrapper); void boardDetection(); void configSavedCallback(); void parseAdvancedConf(); - - std::function formValidatorStd; + void parseModemStartup(); + + std::function formValidatorStd; DNSServer dnsServer; WebServer server; #ifdef ESP8266 @@ -207,9 +258,10 @@ class ConfigManager : public IotWebConf2 HTTPUpdateServer httpUpdater; #endif GSConfigHtmlFormatProvider gsConfigHtmlFormatProvider; - board_type boards[NUM_BOARDS]; + board_type boards[NUM_BOARDS]; AdvancedConfig advancedConf; char savedThingName[IOTWEBCONF_WORD_LEN] = ""; + bool remoteSave = false; char latitude[COORDINATE_LENGTH] = ""; char longitude[COORDINATE_LENGTH] = ""; @@ -231,24 +283,24 @@ class ConfigManager : public IotWebConf2 iotwebconf2::NumberParameter latitudeParam = iotwebconf2::NumberParameter("Latitude (3 decimals, will be public)", "lat", latitude, COORDINATE_LENGTH, NULL, "0.000", "required min='-180' max='180' step='0.001'"); iotwebconf2::NumberParameter longitudeParam = iotwebconf2::NumberParameter("Longitude (3 decimals, will be public)", "lng", longitude, COORDINATE_LENGTH, NULL, "-0.000", "required min='-180' max='180' step='0.001'"); - iotwebconf2::SelectParameter tzParam = iotwebconf2::SelectParameter("Time Zone", "tz", tz, TZ_LENGTH, (char*)TZ_VALUES, (char*)TZ_NAMES, sizeof(TZ_VALUES) / TZ_LENGTH, TZ_NAME_LENGTH); + iotwebconf2::SelectParameter tzParam = iotwebconf2::SelectParameter("Time Zone", "tz", tz, TZ_LENGTH, (char *)TZ_VALUES, (char *)TZ_NAMES, sizeof(TZ_VALUES) / TZ_LENGTH, TZ_NAME_LENGTH); - iotwebconf2::ParameterGroup groupMqtt = iotwebconf2::ParameterGroup("MQTT credentials" , "MQTT credentials (get them here)"); + iotwebconf2::ParameterGroup groupMqtt = iotwebconf2::ParameterGroup("MQTT credentials", "MQTT credentials (get them here)"); iotwebconf2::TextParameter mqttServerParam = iotwebconf2::TextParameter("Server address", "mqtt_server", mqttServer, MQTT_SERVER_LENGTH, MQTT_DEFAULT_SERVER, MQTT_DEFAULT_SERVER, "required type=\"text\" maxlength=30"); iotwebconf2::NumberParameter mqttPortParam = iotwebconf2::NumberParameter("Server Port", "mqtt_port", mqttPort, MQTT_PORT_LENGTH, MQTT_DEFAULT_PORT, MQTT_DEFAULT_PORT, "required min=\"0\" max=\"65536\" step=\"1\""); iotwebconf2::TextParameter mqttUserParam = iotwebconf2::TextParameter("MQTT Username", "mqtt_user", mqttUser, MQTT_USER_LENGTH, NULL, NULL, "required type=\"text\" maxlength=30"); iotwebconf2::TextParameter mqttPassParam = iotwebconf2::TextParameter("MQTT Password", "mqtt_pass", mqttPass, MQTT_PASS_LENGTH, NULL, NULL, "required type=\"text\" maxlength=30"); - iotwebconf2::ParameterGroup groupBoardConfig = iotwebconf2::ParameterGroup("Board config" , "Board config"); - iotwebconf2::SelectParameter boardParam = iotwebconf2::SelectParameter("Board type", "board", board, BOARD_LENGTH, (char*)BOARD_VALUES, (char*)BOARD_NAMES, sizeof(BOARD_VALUES) / BOARD_LENGTH, BOARD_NAME_LENGTH); + iotwebconf2::ParameterGroup groupBoardConfig = iotwebconf2::ParameterGroup("Board config", "Board config"); + iotwebconf2::SelectParameter boardParam = iotwebconf2::SelectParameter("Board type", "board", board, BOARD_LENGTH, (char *)BOARD_VALUES, (char *)BOARD_NAMES, sizeof(BOARD_VALUES) / BOARD_LENGTH, BOARD_NAME_LENGTH); iotwebconf2::NumberParameter oledBrightParam = iotwebconf2::NumberParameter("OLED Bright", "oled_bright", oledBright, NUMBER_LEN, "100", "0..100", "min='0' max='100' step='1'"); - iotwebconf2::CheckboxParameter AllowTxParam = iotwebconf2::CheckboxParameter("Enable TX (HAM licence/ no preamp)", "tx", allowTx, CHECKBOX_LENGTH, true); - iotwebconf2::CheckboxParameter remoteTuneParam = iotwebconf2::CheckboxParameter("Allow Automatic Tuning","remote_tune",remoteTune, CHECKBOX_LENGTH, true); - iotwebconf2::CheckboxParameter telemetry3rdParam = iotwebconf2::CheckboxParameter("Allow sending telemetry to third party","telemetry3rd",telemetry3rd, CHECKBOX_LENGTH, true); - iotwebconf2::CheckboxParameter testParam = iotwebconf2::CheckboxParameter("Test mode","test",testMode, CHECKBOX_LENGTH, false); - iotwebconf2::CheckboxParameter autoUpdateParam = iotwebconf2::CheckboxParameter("Automatic Firmware Update","auto_update",autoUpdate, CHECKBOX_LENGTH, true); + iotwebconf2::CheckboxParameter AllowTxParam = iotwebconf2::CheckboxParameter("Enable TX (HAM licence/ no preamp)", "tx", allowTx, CHECKBOX_LENGTH, true); + iotwebconf2::CheckboxParameter remoteTuneParam = iotwebconf2::CheckboxParameter("Allow Automatic Tuning", "remote_tune", remoteTune, CHECKBOX_LENGTH, true); + iotwebconf2::CheckboxParameter telemetry3rdParam = iotwebconf2::CheckboxParameter("Allow sending telemetry to third party", "telemetry3rd", telemetry3rd, CHECKBOX_LENGTH, true); + iotwebconf2::CheckboxParameter testParam = iotwebconf2::CheckboxParameter("Test mode", "test", testMode, CHECKBOX_LENGTH, false); + iotwebconf2::CheckboxParameter autoUpdateParam = iotwebconf2::CheckboxParameter("Automatic Firmware Update", "auto_update", autoUpdate, CHECKBOX_LENGTH, true); - iotwebconf2::ParameterGroup groupAdvanced = iotwebconf2::ParameterGroup("Advanced config" , "Advanced Config (do not modify unless you know what you are doing)"); + iotwebconf2::ParameterGroup groupAdvanced = iotwebconf2::ParameterGroup("Advanced config", "Advanced Config (do not modify unless you know what you are doing)"); iotwebconf2::TextParameter boardTemplateParam = iotwebconf2::TextParameter("Board Template (requires manual restart)", "board_template", boardTemplate, TEMPLATE_LEN, NULL, NULL, "type=\"text\" maxlength=255"); iotwebconf2::TextParameter modemParam = iotwebconf2::TextParameter("Modem startup", "modem_startup", modemStartup, MODEM_LEN, MODEM_DEFAULT, MODEM_DEFAULT, "type=\"text\" maxlength=255"); iotwebconf2::TextParameter advancedConfigParam = iotwebconf2::TextParameter("Advanced parameters", "advanced_config", advancedConfig, ADVANCED_LEN, NULL, NULL, "type=\"text\" maxlength=255"); diff --git a/tinyGS/src/Mqtt/MQTT_Client.cpp b/tinyGS/src/Mqtt/MQTT_Client.cpp index d7c777bc..ea283bd2 100644 --- a/tinyGS/src/Mqtt/MQTT_Client.cpp +++ b/tinyGS/src/Mqtt/MQTT_Client.cpp @@ -19,22 +19,23 @@ #include "MQTT_Client.h" #include "ArduinoJson.h" -#if ARDUINOJSON_USE_LONG_LONG == 0 && !PLATFORMIO +#if ARDUINOJSON_USE_LONG_LONG == 0 && !PLATFORMIO #error "Using Arduino IDE is not recommended, please follow this guide https://github.com/G4lile0/tinyGS/wiki/Arduino-IDE or edit /ArduinoJson/src/ArduinoJson/Configuration.hpp and amend to #define ARDUINOJSON_USE_LONG_LONG 1 around line 68" #endif #include "../Radio/Radio.h" #include "../OTA/OTA.h" #include "../Logger/Logger.h" -MQTT_Client::MQTT_Client() -: PubSubClient(espClient) +MQTT_Client::MQTT_Client() + : PubSubClient(espClient) { #ifdef SECURE_MQTT - espClient.setCACert(usingNewCert ? newRoot_CA : DSTroot_CA); + espClient.setCACert(usingNewCert ? newRoot_CA : DSTroot_CA); #endif } -void MQTT_Client::loop() { +void MQTT_Client::loop() +{ if (!connected()) { status.mqtt_connected = false; @@ -71,9 +72,10 @@ void MQTT_Client::loop() { { int totalVbat = 0; int averageVbat = 0; - for(int i = 0; i < 20; i++){ - totalVbat += analogRead(36); - } + for (int i = 0; i < 20; i++) + { + totalVbat += analogRead(36); + } averageVbat = totalVbat / 20; StaticJsonDocument<128> doc; doc["Vbat"] = averageVbat; @@ -88,21 +90,23 @@ void MQTT_Client::loop() { void MQTT_Client::reconnect() { - ConfigManager& configManager = ConfigManager::getInstance(); + ConfigManager &configManager = ConfigManager::getInstance(); uint64_t chipId = ESP.getEfuseMac(); char clientId[13]; - sprintf(clientId, "%04X%08X",(uint16_t)(chipId>>32), (uint32_t)chipId); + sprintf(clientId, "%04X%08X", (uint16_t)(chipId >> 32), (uint32_t)chipId); Log::console(PSTR("Attempting MQTT connection...")); Log::console(PSTR("If this is taking more than expected, connect to the config panel on the ip: %s to review the MQTT connection credentials."), WiFi.localIP().toString().c_str()); - if (connect(clientId, configManager.getMqttUser(), configManager.getMqttPass(), buildTopic(teleTopic, topicStatus).c_str(), 2, false, "0")) { + if (connect(clientId, configManager.getMqttUser(), configManager.getMqttPass(), buildTopic(teleTopic, topicStatus).c_str(), 2, false, "0")) + { yield(); Log::console(PSTR("Connected to MQTT!")); status.mqtt_connected = true; subscribeToAll(); sendWelcome(); } - else { + else + { status.mqtt_connected = false; if (state() == -2) // first attempt @@ -120,9 +124,9 @@ void MQTT_Client::reconnect() } } -String MQTT_Client::buildTopic(const char* baseTopic, const char* cmnd) +String MQTT_Client::buildTopic(const char *baseTopic, const char *cmnd) { - ConfigManager& configManager = ConfigManager::getInstance(); + ConfigManager &configManager = ConfigManager::getInstance(); String topic = baseTopic; topic.replace("%user%", configManager.getMqttUser()); topic.replace("%station%", configManager.getThingName()); @@ -131,7 +135,8 @@ String MQTT_Client::buildTopic(const char* baseTopic, const char* cmnd) return topic; } -void MQTT_Client::subscribeToAll() { +void MQTT_Client::subscribeToAll() +{ subscribe(buildTopic(globalTopic, "#").c_str()); subscribe(buildTopic(cmndTopic, "#").c_str()); } @@ -139,44 +144,45 @@ void MQTT_Client::subscribeToAll() { void MQTT_Client::sendWelcome() { scheduledRestart = false; - ConfigManager& configManager = ConfigManager::getInstance(); + ConfigManager &configManager = ConfigManager::getInstance(); time_t now; time(&now); uint64_t chipId = ESP.getEfuseMac(); char clientId[13]; - sprintf(clientId, "%04X%08X",(uint16_t)(chipId>>32), (uint32_t)chipId); + sprintf(clientId, "%04X%08X", (uint16_t)(chipId >> 32), (uint32_t)chipId); - - const size_t capacity = JSON_ARRAY_SIZE(2) + JSON_OBJECT_SIZE(15) + 22 + 20 + 20; + const size_t capacity = JSON_ARRAY_SIZE(2) + JSON_OBJECT_SIZE(17) + 22 + 20 + 20; DynamicJsonDocument doc(capacity); JsonArray station_location = doc.createNestedArray("station_location"); station_location.add(configManager.getLatitude()); station_location.add(configManager.getLongitude()); - doc["version"] = status.version; - doc["git_version"] = status.git_version; - doc["board"] = configManager.getBoard(); - doc["mac"] = clientId; doc["tx"] = configManager.getAllowTx(); - doc["remoteTune"] = configManager.getRemoteTune(); - doc["telemetry3d"] = configManager.getTelemetry3rd(); doc["test"] = configManager.getTestMode(); - doc["unix_GS_time"] = now; + doc["tele3d"] = configManager.getTelemetry3rd(); + doc["time"] = now; + doc["version"] = status.version; + doc["git_version"] = status.git_version; + doc["sat"] = status.modeminfo.satellite; doc["autoUpdate"] = configManager.getAutoUpdate(); - doc["local_ip"]= WiFi.localIP().toString(); - doc["modem_conf"].set(configManager.getModemStartup()); - doc["boardTemplate"].set(configManager.getBoardTemplate()); + doc["remoteTune"] = configManager.getRemoteTune(); + doc["ip"] = WiFi.localIP().toString(); if (configManager.getLowPower()) doc["lp"].set(configManager.getLowPower()); + doc["modem_conf"].set(configManager.getModemStartup()); + doc["boardTemplate"].set(configManager.getBoardTemplate()); + doc["Mem"] = ESP.getFreeHeap(); + doc["board"] = configManager.getBoard(); + doc["mac"] = clientId; char buffer[1048]; serializeJson(doc, buffer); publish(buildTopic(teleTopic, topicWelcome).c_str(), buffer, false); } -void MQTT_Client::sendRx(String packet, bool noisy) +void MQTT_Client::sendRx(String packet, bool noisy) { - ConfigManager& configManager = ConfigManager::getInstance(); + ConfigManager &configManager = ConfigManager::getInstance(); time_t now; time(&now); struct timeval tv; @@ -189,10 +195,10 @@ void MQTT_Client::sendRx(String packet, bool noisy) station_location.add(configManager.getLongitude()); doc["mode"] = status.modeminfo.modem_mode; doc["frequency"] = status.modeminfo.frequency; - doc["frequency_offset"] = status.modeminfo.freqOffset; + doc["frequency_offset"] = status.modeminfo.freqOffset; doc["satellite"] = status.modeminfo.satellite; - - if (String(status.modeminfo.modem_mode)=="LoRa") + + if (String(status.modeminfo.modem_mode) == "LoRa") { doc["sf"] = status.modeminfo.sf; doc["cr"] = status.modeminfo.cr; @@ -223,9 +229,9 @@ void MQTT_Client::sendRx(String packet, bool noisy) publish(buildTopic(teleTopic, topicRx).c_str(), buffer, false); } -void MQTT_Client::sendStatus() +void MQTT_Client::sendStatus() { - ConfigManager& configManager = ConfigManager::getInstance(); + ConfigManager &configManager = ConfigManager::getInstance(); time_t now; time(&now); struct timeval tv; @@ -245,11 +251,11 @@ void MQTT_Client::sendStatus() doc["mode"] = status.modeminfo.modem_mode; doc["frequency"] = status.modeminfo.frequency; - doc["frequency_offset"] = status.modeminfo.freqOffset; + doc["frequency_offset"] = status.modeminfo.freqOffset; doc["satellite"] = status.modeminfo.satellite; doc["NORAD"] = status.modeminfo.NORAD; - - if (String(status.modeminfo.modem_mode)=="LoRa") + + if (String(status.modeminfo.modem_mode) == "LoRa") { doc["sf"] = status.modeminfo.sf; doc["cr"] = status.modeminfo.cr; @@ -274,7 +280,7 @@ void MQTT_Client::sendStatus() doc["unix_GS_time"] = now; doc["usec_time"] = (int64_t)tv.tv_usec + tv.tv_sec * 1000000ll; doc["time_offset"] = status.time_offset; - + char buffer[1024]; serializeJson(doc, buffer); publish(buildTopic(statTopic, topicStatus).c_str(), buffer, false); @@ -282,23 +288,22 @@ void MQTT_Client::sendStatus() void MQTT_Client::sendAdvParameters() { - ConfigManager& configManager = ConfigManager::getInstance(); + ConfigManager &configManager = ConfigManager::getInstance(); StaticJsonDocument<512> doc; doc["adv_prm"].set(configManager.getAvancedConfig()); char buffer[512]; serializeJson(doc, buffer); Log::debug(PSTR("%s"), buffer); publish(buildTopic(teleTopic, topicGet_adv_prm).c_str(), buffer, false); - } void MQTT_Client::manageMQTTData(char *topic, uint8_t *payload, unsigned int length) { - Radio& radio = Radio::getInstance(); + Radio &radio = Radio::getInstance(); bool global = true; - char* command; - strtok(topic, "/"); // tinygs + char *command; + strtok(topic, "/"); // tinygs if (strcmp(strtok(NULL, "/"), "global")) // user { global = false; @@ -308,23 +313,26 @@ void MQTT_Client::manageMQTTData(char *topic, uint8_t *payload, unsigned int len command = strtok(NULL, "/"); uint16_t result = 0xFF; - if (!strcmp(command, commandSatPos)) { - manageSatPosOled((char*)payload, length); + if (!strcmp(command, commandSatPos)) + { + manageSatPosOled((char *)payload, length); return; // no ack } if (!strcmp(command, commandReset)) ESP.restart(); - if (!strcmp(command, commandUpdate)) { + if (!strcmp(command, commandUpdate)) + { OTA::update(); return; // no ack } if (!strcmp(command, commandTest)) { - if (length < 1) return; - ConfigManager& configManager = ConfigManager::getInstance(); + if (length < 1) + return; + ConfigManager &configManager = ConfigManager::getInstance(); bool test = payload[0] - '0'; Log::console(PSTR("Set Test Mode to %s"), test ? F("ON") : F("OFF")); configManager.setTestMode(test); @@ -333,8 +341,9 @@ void MQTT_Client::manageMQTTData(char *topic, uint8_t *payload, unsigned int len if (!strcmp(command, commandRemoteTune)) { - if (length < 1) return; - ConfigManager& configManager = ConfigManager::getInstance(); + if (length < 1) + return; + ConfigManager &configManager = ConfigManager::getInstance(); bool tune = payload[0] - '0'; Log::console(PSTR("Set Remote Tune to %s"), tune ? F("ON") : F("OFF")); configManager.setRemoteTune(tune); @@ -343,8 +352,9 @@ void MQTT_Client::manageMQTTData(char *topic, uint8_t *payload, unsigned int len if (!strcmp(command, commandRemotetelemetry3rd)) { - if (length < 1) return; - ConfigManager& configManager = ConfigManager::getInstance(); + if (length < 1) + return; + ConfigManager &configManager = ConfigManager::getInstance(); bool telemetry3rd = payload[0] - '0'; Log::console(PSTR("Send rx to third parties %s"), telemetry3rd ? F("ON") : F("OFF")); configManager.setTelemetry3rd(telemetry3rd); @@ -358,22 +368,22 @@ void MQTT_Client::manageMQTTData(char *topic, uint8_t *payload, unsigned int len deserializeJson(doc, payload, length); status.remoteTextFrameLength[frameNumber] = doc.size(); Log::debug(PSTR("Received frame: %u"), status.remoteTextFrameLength[frameNumber]); - - for (uint8_t n=0; n %s"), n, - status.remoteTextFrame[frameNumber][n].text_font, - status.remoteTextFrame[frameNumber][n].text_alignment, - status.remoteTextFrame[frameNumber][n].text_pos_x, - status.remoteTextFrame[frameNumber][n].text_pos_y, - status.remoteTextFrame[frameNumber][n].text.c_str()); + status.remoteTextFrame[frameNumber][n].text = text; + + Log::debug(PSTR("Text: %u Font: %u Alig: %u Pos x: %u Pos y: %u -> %s"), n, + status.remoteTextFrame[frameNumber][n].text_font, + status.remoteTextFrame[frameNumber][n].text_alignment, + status.remoteTextFrame[frameNumber][n].text_pos_x, + status.remoteTextFrame[frameNumber][n].text_pos_y, + status.remoteTextFrame[frameNumber][n].text.c_str()); } result = 0; @@ -382,7 +392,7 @@ void MQTT_Client::manageMQTTData(char *topic, uint8_t *payload, unsigned int len if (!strcmp(command, commandStatus)) { uint8_t mode = payload[0] - '0'; - Log::debug(PSTR("Remote status requested: %u"), mode); // right now just one mode + Log::debug(PSTR("Remote status requested: %u"), mode); // right now just one mode sendStatus(); return; } @@ -407,102 +417,167 @@ void MQTT_Client::manageMQTTData(char *topic, uint8_t *payload, unsigned int len // ###################################################### if (ConfigManager::getInstance().getRemoteTune() && global) return; - - if (!strcmp(command, commandBegin)) + + if (!strcmp(command, commandBeginp)) { - char buff[length+1]; + char buff[length + 1]; memcpy(buff, payload, length); buff[length] = '\0'; Log::debug(PSTR("%s"), buff); ConfigManager::getInstance().setModemStartup(buff); } + if (!strcmp(command, commandBegine)) + { + size_t size = JSON_ARRAY_SIZE(10) + 10 * JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(16) + JSON_ARRAY_SIZE(8) + JSON_ARRAY_SIZE(8) + 64; + DynamicJsonDocument doc(size); + DeserializationError error = deserializeJson(doc, payload, length); + + if (error.code() != DeserializationError::Ok || !doc.containsKey("mode")) + { + Log::console(PSTR("ERROR: Your modem config is invalid. Resetting to default")); + return; + } + + ModemInfo &m = status.modeminfo; + m.modem_mode = doc["mode"].as(); + strcpy(m.satellite, doc["sat"].as()); + m.NORAD = doc["NORAD"]; + + if (m.modem_mode == "LoRa") + { + m.frequency = doc["freq"]; + m.bw = doc["bw"]; + m.sf = doc["sf"]; + m.cr = doc["cr"]; + m.sw = doc["sw"]; + m.power = doc["pwr"]; + m.preambleLength = doc["pl"]; + m.gain = doc["gain"]; + m.crc = doc["crc"]; + m.fldro = doc["fldro"]; + } + else + { + m.frequency = doc["freq"]; + m.bw = doc["bw"]; + m.bitrate = doc["br"]; + m.freqDev = doc["fd"]; + m.power = doc["pwr"]; + m.preambleLength = doc["pl"]; + m.OOK = doc["ook"]; + m.swSize = doc["fsw"].size(); + for (int i = 0; i < 8; i++) + { + if (i < m.swSize) + m.fsw[i] = doc["fsw"][i]; + else + m.fsw[i] = 0; + } + } + + // packets Filter + uint8_t filterSize = doc["filter"].size(); + for (int i = 0; i < 8; i++) + { + if (i < filterSize) + status.modeminfo.filter[i] = doc["filter"][i]; + else + status.modeminfo.filter[i] = 0; + } + + radio.begin(); + result = 0; + } + // Remote_Begin_Lora [437.7,125.0,11,8,18,11,120,8,0] if (!strcmp(command, commandBeginLora)) - result = radio.remote_begin_lora((char*)payload, length); + result = radio.remote_begin_lora((char *)payload, length); // Remote_Begin_FSK [433.5,100.0,10.0,250.0,10,100,16,0,0] if (!strcmp(command, commandBeginFSK)) - result = radio.remote_begin_fsk((char*)payload, length); + result = radio.remote_begin_fsk((char *)payload, length); if (!strcmp(command, commandFreq)) - result = radio.remote_freq((char*)payload, length); + result = radio.remote_freq((char *)payload, length); if (!strcmp(command, commandBw)) - result = radio.remote_bw((char*)payload, length); + result = radio.remote_bw((char *)payload, length); if (!strcmp(command, commandSf)) - result = radio.remote_sf((char*)payload, length); + result = radio.remote_sf((char *)payload, length); if (!strcmp(command, commandCr)) - result = radio.remote_cr((char*)payload, length); + result = radio.remote_cr((char *)payload, length); if (!strcmp(command, commandCrc)) - result = radio.remote_crc((char*)payload, length); + result = radio.remote_crc((char *)payload, length); // Remote_LoRa_syncword [8,1,2,3,4,5,6,7,8,9] if (!strcmp(command, commandCrc)) - result = radio.remote_lsw((char*)payload, length); + result = radio.remote_lsw((char *)payload, length); if (!strcmp(command, commandFldro)) - result = radio.remote_fldro((char*)payload, length); + result = radio.remote_fldro((char *)payload, length); if (!strcmp(command, commandAldro)) - result = radio.remote_aldro((char*)payload, length); + result = radio.remote_aldro((char *)payload, length); if (!strcmp(command, commandPl)) - result = radio.remote_pl((char*)payload, length); + result = radio.remote_pl((char *)payload, length); if (!strcmp(command, commandBr)) - result = radio.remote_br((char*)payload, length); + result = radio.remote_br((char *)payload, length); if (!strcmp(command, commandFd)) - result = radio.remote_fd((char*)payload, length); + result = radio.remote_fd((char *)payload, length); if (!strcmp(command, commandFbw)) - result = radio.remote_fbw((char*)payload, length); + result = radio.remote_fbw((char *)payload, length); // Remote_FSK_syncword [8,1,2,3,4,5,6,7,8,9] if (!strcmp(command, commandFsw)) - result = radio.remote_fsw((char*)payload, length); + result = radio.remote_fsw((char *)payload, length); // Remote_FSK_Set_OOK + DataShapingOOK(only sx1278) [1,2] if (!strcmp(command, commandFook)) - result = radio.remote_fook((char*)payload, length); + result = radio.remote_fook((char *)payload, length); // Remote_Satellite_Name [\"FossaSat-3\" , 46494 ] if (!strcmp(command, commandSat)) { - remoteSatCmnd((char*)payload, length); + remoteSatCmnd((char *)payload, length); result = 0; } // Satellite_Filter [1,0,51] (lenght,position,byte1,byte2,byte3,byte4) if (!strcmp(command, commandSatFilter)) { - remoteSatFilter((char*)payload, length); + remoteSatFilter((char *)payload, length); result = 0; } - // Send station to lightsleep x seconds + // Send station to lightsleep x seconds if (!strcmp(command, commandGoToSleep)) { - if (length < 1) return; - remoteGoToSleep((char*)payload, length); + if (length < 1) + return; + remoteGoToSleep((char *)payload, length); result = 0; } - // Set frequency offset + // Set frequency offset if (!strcmp(command, commandGoToSleep)) { - if (length < 1) return; - remoteSetFreqOffset((char*)payload, length); + if (length < 1) + return; + remoteSetFreqOffset((char *)payload, length); result = 0; } if (!strcmp(command, commandSetAdvParameters)) { - char buff[length+1]; + char buff[length + 1]; memcpy(buff, payload, length); buff[length] = '\0'; Log::debug(PSTR("%s"), buff); @@ -518,34 +593,34 @@ void MQTT_Client::manageMQTTData(char *topic, uint8_t *payload, unsigned int len // GOD MODE With great power comes great responsibility! // SPIsetRegValue (only sx1278) [1,2,3,4,5] if (!strcmp(command, commandSPIsetRegValue)) - result = radio.remote_SPIsetRegValue((char*)payload, length); + result = radio.remote_SPIsetRegValue((char *)payload, length); // SPIwriteRegister (only sx1278) [1,2] if (!strcmp(command, commandSPIwriteRegister)) { - radio.remote_SPIwriteRegister((char*)payload, length); + radio.remote_SPIwriteRegister((char *)payload, length); result = 0; } if (!strcmp(command, commandSPIreadRegister) && !global) - result = radio.remote_SPIreadRegister((char*)payload, length); + result = radio.remote_SPIreadRegister((char *)payload, length); if (!strcmp(command, commandBatchConf)) { Log::debug(PSTR("BatchConfig")); - DynamicJsonDocument doc(2048); + DynamicJsonDocument doc(2048); deserializeJson(doc, payload, length); JsonObject root = doc.as(); result = 0; for (JsonPair kv : root) { - const char* key = kv.key().c_str(); - char* value = (char*)kv.value().as(); + const char *key = kv.key().c_str(); + char *value = (char *)kv.value().as(); size_t len = strlen(value); Log::debug(PSTR("%s %s %u"), key, value, len); - if (!strcmp(key, commandCrc)) + if (!strcmp(key, commandCrc)) result = radio.remote_crc(value, len); else if (!strcmp(key, commandFreq)) @@ -601,7 +676,7 @@ void MQTT_Client::manageMQTTData(char *topic, uint8_t *payload, unsigned int len else if (!strcmp(key, commandSPIwriteRegister)) radio.remote_SPIwriteRegister(value, len); - + if (result) // there was an error { Log::debug(PSTR("Error ocurred during batch config!!")); @@ -611,10 +686,10 @@ void MQTT_Client::manageMQTTData(char *topic, uint8_t *payload, unsigned int len } if (!global) - publish(buildTopic(statTopic, command).c_str(), (uint8_t*)&result, 2U, false); + publish(buildTopic(statTopic, command).c_str(), (uint8_t *)&result, 2U, false); } -void MQTT_Client::manageSatPosOled(char* payload, size_t payload_len) +void MQTT_Client::manageSatPosOled(char *payload, size_t payload_len) { DynamicJsonDocument doc(60); deserializeJson(doc, payload, payload_len); @@ -622,7 +697,7 @@ void MQTT_Client::manageSatPosOled(char* payload, size_t payload_len) status.satPos[1] = doc[1]; } -void MQTT_Client::remoteSatCmnd(char* payload, size_t payload_len) +void MQTT_Client::remoteSatCmnd(char *payload, size_t payload_len) { DynamicJsonDocument doc(256); deserializeJson(doc, payload, payload_len); @@ -633,39 +708,39 @@ void MQTT_Client::remoteSatCmnd(char* payload, size_t payload_len) Log::debug(PSTR("Listening Satellite: %s NORAD: %u"), status.modeminfo.satellite, NORAD); } -void MQTT_Client::remoteSatFilter(char* payload, size_t payload_len) +void MQTT_Client::remoteSatFilter(char *payload, size_t payload_len) { DynamicJsonDocument doc(256); deserializeJson(doc, payload, payload_len); uint8_t filter_size = doc.size(); - - status.modeminfo.filter[0]=doc[0]; - status.modeminfo.filter[1]=doc[1]; - Log::debug(PSTR("Set Sat Filter Size %d"),status.modeminfo.filter[0]); - Log::debug(PSTR("Set Sat Filter POS %d"),status.modeminfo.filter[1]); - Log::debug(PSTR("-> ")); - for (uint8_t filter_pos=2; filter_pos ")); + for (uint8_t filter_pos = 2; filter_pos < filter_size; filter_pos++) { - status.modeminfo.filter[filter_pos]=doc[filter_pos]; - Log::debug(PSTR(" 0x%x ,"),status.modeminfo.filter[filter_pos]); - } + status.modeminfo.filter[filter_pos] = doc[filter_pos]; + Log::debug(PSTR(" 0x%x ,"), status.modeminfo.filter[filter_pos]); + } Log::debug(PSTR("Sat packets Filter enabled")); } -void MQTT_Client::remoteGoToSleep(char* payload, size_t payload_len) +void MQTT_Client::remoteGoToSleep(char *payload, size_t payload_len) { DynamicJsonDocument doc(60); deserializeJson(doc, payload, payload_len); uint16_t sleep_seconds = doc[0]; //uint8_t int_pin = doc [1]; // 99 no int pin - + Log::debug(PSTR("light_sleep_enter")); - esp_sleep_enable_timer_wakeup(sleep_seconds*1000000); //30 seconds + esp_sleep_enable_timer_wakeup(sleep_seconds * 1000000); //30 seconds //esp_sleep_enable_ext0_wakeup(int_pin,0); delay(100); - Serial.flush(); + Serial.flush(); WiFi.disconnect(true); delay(100); int ret = esp_light_sleep_start(); @@ -676,20 +751,31 @@ void MQTT_Client::remoteGoToSleep(char* payload, size_t payload_len) delay(500); esp_sleep_wakeup_cause_t wakeup_reason; wakeup_reason = esp_sleep_get_wakeup_cause(); - - switch(wakeup_reason) - { - case ESP_SLEEP_WAKEUP_EXT0 : Log::debug(PSTR("Wakeup caused by external signal using RTC_IO")); break; - case ESP_SLEEP_WAKEUP_EXT1 : Log::debug(PSTR("Wakeup caused by external signal using RTC_CNTL")); break; - case ESP_SLEEP_WAKEUP_TIMER : Log::debug(PSTR("Wakeup caused by timer")); break; - case ESP_SLEEP_WAKEUP_TOUCHPAD : Log::debug(PSTR("Wakeup caused by touchpad")); break; - case ESP_SLEEP_WAKEUP_ULP : Log::debug(PSTR("Wakeup caused by ULP program")); break; - default : Log::debug(PSTR("Wakeup was not caused by deep sleep: %d\n"),wakeup_reason); break; - } -} + switch (wakeup_reason) + { + case ESP_SLEEP_WAKEUP_EXT0: + Log::debug(PSTR("Wakeup caused by external signal using RTC_IO")); + break; + case ESP_SLEEP_WAKEUP_EXT1: + Log::debug(PSTR("Wakeup caused by external signal using RTC_CNTL")); + break; + case ESP_SLEEP_WAKEUP_TIMER: + Log::debug(PSTR("Wakeup caused by timer")); + break; + case ESP_SLEEP_WAKEUP_TOUCHPAD: + Log::debug(PSTR("Wakeup caused by touchpad")); + break; + case ESP_SLEEP_WAKEUP_ULP: + Log::debug(PSTR("Wakeup caused by ULP program")); + break; + default: + Log::debug(PSTR("Wakeup was not caused by deep sleep: %d\n"), wakeup_reason); + break; + } +} -void MQTT_Client::remoteSetFreqOffset(char* payload, size_t payload_len) +void MQTT_Client::remoteSetFreqOffset(char *payload, size_t payload_len) { DynamicJsonDocument doc(60); deserializeJson(doc, payload, payload_len); @@ -706,7 +792,7 @@ void manageMQTTDataCallback(char *topic, uint8_t *payload, unsigned int length) void MQTT_Client::begin() { - ConfigManager& configManager = ConfigManager::getInstance(); + ConfigManager &configManager = ConfigManager::getInstance(); setServer(configManager.getMqttServer(), configManager.getMqttPort()); setCallback(manageMQTTDataCallback); } diff --git a/tinyGS/src/Mqtt/MQTT_Client.h b/tinyGS/src/Mqtt/MQTT_Client.h index 6195912d..96589ac4 100644 --- a/tinyGS/src/Mqtt/MQTT_Client.h +++ b/tinyGS/src/Mqtt/MQTT_Client.h @@ -108,7 +108,8 @@ class MQTT_Client : public PubSubClient { const char* commandFldro PROGMEM= "fldro"; const char* commandAldro PROGMEM= "aldro"; const char* commandPl PROGMEM= "pl"; - const char* commandBegin PROGMEM= "begin"; + const char* commandBegine PROGMEM= "begine"; + const char* commandBeginp PROGMEM= "beginp"; const char* commandBeginLora PROGMEM= "begin_lora"; const char* commandBeginFSK PROGMEM= "begin_fsk"; const char* commandBr PROGMEM= "br"; diff --git a/tinyGS/src/Radio/Radio.cpp b/tinyGS/src/Radio/Radio.cpp index 2028cc99..78cbb393 100644 --- a/tinyGS/src/Radio/Radio.cpp +++ b/tinyGS/src/Radio/Radio.cpp @@ -19,7 +19,7 @@ #include "Radio.h" #include "ArduinoJson.h" -#if ARDUINOJSON_USE_LONG_LONG == 0 && !PLATFORMIO +#if ARDUINOJSON_USE_LONG_LONG == 0 && !PLATFORMIO #error "Using Arduino IDE is not recommended, please follow this guide https://github.com/G4lile0/tinyGS/wiki/Arduino-IDE or /ArduinoJson/src/ArduinoJson/Configuration.hpp and amend to #define ARDUINOJSON_USE_LONG_LONG 1 around line 68" #endif #include @@ -29,16 +29,15 @@ bool eInterrupt = true; bool noisyInterrupt = false; Radio::Radio() -: spi(VSPI) + : spi(VSPI) { - } void Radio::init() { Log::console(PSTR("[SX12xx] Initializing ... ")); board_type board; - + if (strlen(ConfigManager::getInstance().getBoardTemplate()) > 0) { size_t size = 512; @@ -72,7 +71,7 @@ void Radio::init() { board = ConfigManager::getInstance().getBoardConfig(); } - + spi.begin(board.L_SCK, board.L_MISO, board.L_MOSI, board.L_NSS); if (board.L_SX127X) @@ -91,96 +90,54 @@ int16_t Radio::begin() { status.radio_ready = false; board_type board = ConfigManager::getInstance().getBoardConfig(); - const char* modemConfig = ConfigManager::getInstance().getModemStartup(); - size_t size = JSON_ARRAY_SIZE(10) + 10*JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(16) + JSON_ARRAY_SIZE(8) + JSON_ARRAY_SIZE(8) +64; - DynamicJsonDocument doc(size); - DeserializationError error = deserializeJson(doc, modemConfig); - - if (error.code() != DeserializationError::Ok || !doc.containsKey("mode")) - { - Log::console(PSTR("ERROR: Your modem config is invalid. Resetting to default")); - ConfigManager::getInstance().resetModemConfig(); - return -1; - } - - ModemInfo& m = status.modeminfo; - m.modem_mode = doc["mode"].as(); - strcpy(m.satellite, doc["sat"].as()); - m.NORAD = doc["NORAD"]; - + ModemInfo &m = status.modeminfo; int16_t state = 0; if (m.modem_mode == "LoRa") { - m.frequency = doc["freq"]; - m.bw = doc["bw"]; - m.sf = doc["sf"]; - m.cr = doc["cr"]; - m.sw = doc["sw"]; - m.power = doc["pwr"]; - m.preambleLength = doc["pl"]; - m.gain = doc["gain"]; - m.crc = doc["crc"]; - m.fldro = doc["fldro"]; - if (board.L_SX127X) { - state = ((SX1278*)lora)->begin(m.frequency+status.modeminfo.freqOffset, m.bw, m.sf, m.cr, m.sw, m.power,m.preambleLength,m.gain); + state = ((SX1278 *)lora)->begin(m.frequency + status.modeminfo.freqOffset, m.bw, m.sf, m.cr, m.sw, m.power, m.preambleLength, m.gain); if (m.fldro == 2) - ((SX1278*)lora)->autoLDRO(); + ((SX1278 *)lora)->autoLDRO(); else - ((SX1278*)lora)->forceLDRO(m.fldro); + ((SX1278 *)lora)->forceLDRO(m.fldro); - ((SX1278*)lora)->setCRC(m.crc); + ((SX1278 *)lora)->setCRC(m.crc); } else { - state = ((SX1268*)lora)->begin(m.frequency+status.modeminfo.freqOffset, m.bw, m.sf, m.cr, m.sw, m.power, m.preambleLength, board.L_TCXO_V); + state = ((SX1268 *)lora)->begin(m.frequency + status.modeminfo.freqOffset, m.bw, m.sf, m.cr, m.sw, m.power, m.preambleLength, board.L_TCXO_V); if (m.fldro == 2) - ((SX1268*)lora)->autoLDRO(); + ((SX1268 *)lora)->autoLDRO(); else - ((SX1268*)lora)->forceLDRO(m.fldro); + ((SX1268 *)lora)->forceLDRO(m.fldro); - ((SX1268*)lora)->setCRC(m.crc); + ((SX1268 *)lora)->setCRC(m.crc); } } - else - { - m.frequency = doc["freq"]; - m.bw = doc["bw"]; - m.bitrate = doc["br"]; - m.freqDev = doc["fd"]; - m.power = doc["pwr"]; - m.preambleLength = doc["pl"]; - m.OOK = doc["ook"]; - uint8_t swSize = doc["fsw"].size(); - for (int i=0; i<8; i++) + else + { + if (ConfigManager::getInstance().getBoardConfig().L_SX127X) { - if (i < swSize) - m.fsw[i] = doc["fsw"][i]; - else - m.fsw[i] = 0; + state = ((SX1278 *)lora)->beginFSK(m.frequency + status.modeminfo.freqOffset, m.bitrate, m.freqDev, m.bw, m.power, m.preambleLength, (m.OOK != 255)); + ((SX1278 *)lora)->setDataShaping(m.OOK); + ((SX1278 *)lora)->startReceive(); + ((SX1278 *)lora)->setDio0Action(setFlag); + ((SX1278 *)lora)->setSyncWord(m.fsw, m.swSize); } - - - if (ConfigManager::getInstance().getBoardConfig().L_SX127X) { - state = ((SX1278*)lora)->beginFSK(m.frequency+status.modeminfo.freqOffset, m.bitrate, m.freqDev, m.bw, m.power, m.preambleLength, (m.OOK != 255)); - ((SX1278*)lora)->setDataShaping(m.OOK); - ((SX1278*)lora)->startReceive(); - ((SX1278*)lora)->setDio0Action(setFlag); - ((SX1278*)lora)->setSyncWord(m.fsw, swSize); - - } else { - state = ((SX1268*)lora)->beginFSK(m.frequency+status.modeminfo.freqOffset, m.bitrate, m.freqDev, m.bw, m.power, m.preambleLength, ConfigManager::getInstance().getBoardConfig().L_TCXO_V); - ((SX1268*)lora)->setDataShaping(m.OOK); - ((SX1268*)lora)->startReceive(); - ((SX1268*)lora)->setDio1Action(setFlag); - state = ((SX1268*)lora)->setSyncWord(m.fsw, swSize); + else + { + state = ((SX1268 *)lora)->beginFSK(m.frequency + status.modeminfo.freqOffset, m.bitrate, m.freqDev, m.bw, m.power, m.preambleLength, ConfigManager::getInstance().getBoardConfig().L_TCXO_V); + ((SX1268 *)lora)->setDataShaping(m.OOK); + ((SX1268 *)lora)->startReceive(); + ((SX1268 *)lora)->setDio1Action(setFlag); + state = ((SX1268 *)lora)->setSyncWord(m.fsw, m.swSize); } } // registers - JsonArray regs = doc.as(); + /*JsonArray regs = doc.as(); for (int i=0; i_mod->SPIsetRegValue((regValue>>8)&0x0F, regValue&0x0F, (regMask>>16)&0x0F, (regMask>>8)&0x0F, regMask&0x0F); // else // state = ((SX1268*)lora)->_mod->SPIsetRegValue((regValue>>8)&0x0F, regValue&0x0F, (regMask>>16)&0x0F, (regMask>>8)&0x0F, regMask&0x0F); - } - - - // packets Filter - uint8_t filterSize = doc["filter"].size(); - for (int i=0; i<8; i++) - { - if (i < filterSize) - status.modeminfo.filter[i] = doc["filter"][i]; - else - status.modeminfo.filter[i] = 0; - } - + }*/ if (state == ERR_NONE) { - Log::console(PSTR("success!")); - } + //Log::console(PSTR("success!")); + } else { Log::console(PSTR("failed, code %d"), state); @@ -219,24 +164,24 @@ int16_t Radio::begin() // attach the ISR to radio interrupt if (board.L_SX127X) { - ((SX1278*)lora)->setDio0Action(setFlag); + ((SX1278 *)lora)->setDio0Action(setFlag); } else { - ((SX1268*)lora)->setDio1Action(setFlag); + ((SX1268 *)lora)->setDio1Action(setFlag); } // start listening for LoRa packets Log::console(PSTR("[SX12x8] Starting to listen to %s"), m.satellite); if (board.L_SX127X) - state = ((SX1278*)lora)->startReceive(); + state = ((SX1278 *)lora)->startReceive(); else - state = ((SX1268*)lora)->startReceive(); + state = ((SX1268 *)lora)->startReceive(); if (state == ERR_NONE) { - Log::console(PSTR("success!")); - } + //Log::console(PSTR("success!")); + } else { Log::console(PSTR("failed, code %d\nGo to the config panel (%s) and check if the board selected matches your hardware."), state, WiFi.localIP().toString()); @@ -270,23 +215,23 @@ void Radio::disableInterrupt() void Radio::startRx() { - // put module back to listen mode + // put module back to listen mode if (ConfigManager::getInstance().getBoardConfig().L_SX127X) - ((SX1278*)lora)->startReceive(); + ((SX1278 *)lora)->startReceive(); else - ((SX1268*)lora)->startReceive(); - + ((SX1268 *)lora)->startReceive(); + // we're ready to receive more packets, // enable interrupt service routine enableInterrupt(); } -int16_t Radio::sendTx(uint8_t* data, size_t length) +int16_t Radio::sendTx(uint8_t *data, size_t length) { if (!ConfigManager::getInstance().getAllowTx()) { - Log::error(PSTR("TX disabled by config")); - return -1; + Log::error(PSTR("TX disabled by config")); + return -1; } disableInterrupt(); @@ -294,14 +239,14 @@ int16_t Radio::sendTx(uint8_t* data, size_t length) int16_t state = 0; if (ConfigManager::getInstance().getBoardConfig().L_SX127X) { - SX1278* l = (SX1278*)lora; + SX1278 *l = (SX1278 *)lora; state = l->transmit(data, length); l->setDio0Action(setFlag); l->startReceive(); } else { - SX1268* l = (SX1268*)lora; + SX1268 *l = (SX1268 *)lora; state = l->transmit(data, length); l->setDio1Action(setFlag); l->startReceive(); @@ -313,13 +258,13 @@ int16_t Radio::sendTx(uint8_t* data, size_t length) int16_t Radio::sendTestPacket() { - return sendTx((uint8_t*)TEST_STRING, strlen(TEST_STRING)); + return sendTx((uint8_t *)TEST_STRING, strlen(TEST_STRING)); } uint8_t Radio::listen() { // check if the flag is set (received interruption) - if(!received) + if (!received) return 1; // disable the interrupt service routine while @@ -330,7 +275,7 @@ uint8_t Radio::listen() received = false; size_t respLen = 0; - uint8_t* respFrame = 0; + uint8_t *respFrame = 0; int16_t state = 0; PacketInfo newPacketInfo; @@ -338,7 +283,7 @@ uint8_t Radio::listen() // read received data if (ConfigManager::getInstance().getBoardConfig().L_SX127X) { - SX1278* l = (SX1278*)lora; + SX1278 *l = (SX1278 *)lora; respLen = l->getPacketLength(); respFrame = new uint8_t[respLen]; state = l->readData(respFrame, respLen); @@ -348,7 +293,7 @@ uint8_t Radio::listen() } else { - SX1268* l = (SX1268*)lora; + SX1268 *l = (SX1268 *)lora; respLen = l->getPacketLength(); respFrame = new uint8_t[respLen]; state = l->readData(respFrame, respLen); @@ -371,74 +316,79 @@ uint8_t Radio::listen() status.lastPacketInfo.snr = newPacketInfo.snr; status.lastPacketInfo.frequencyerror = newPacketInfo.frequencyerror; - // print RSSI (Received Signal Strength Indicator) + // print RSSI (Received Signal Strength Indicator) Log::console(PSTR("[SX12x8] RSSI:\t\t%f dBm\n[SX12x8] SNR:\t\t%f dB\n[SX12x8] Frequency error:\t%f Hz"), status.lastPacketInfo.rssi, status.lastPacketInfo.snr, status.lastPacketInfo.frequencyerror); if (state == ERR_NONE && respLen > 0) { // read optional data Log::console(PSTR("Packet (%u bytes):"), respLen); - uint16_t buffSize = respLen*2+1; - if (buffSize > 255) buffSize = 255; - char* byteStr = new char[buffSize]; - for(int i = 0; i < respLen; i++) + uint16_t buffSize = respLen * 2 + 1; + if (buffSize > 255) + buffSize = 255; + char *byteStr = new char[buffSize]; + for (int i = 0; i < respLen; i++) { - sprintf(byteStr + i*2 % (buffSize-1), "%02x", respFrame[i]); - if (i*2 % buffSize == buffSize-3 || i == respLen-1) + sprintf(byteStr + i * 2 % (buffSize - 1), "%02x", respFrame[i]); + if (i * 2 % buffSize == buffSize - 3 || i == respLen - 1) Log::console(PSTR("%s"), byteStr); // print before the buffer is going to loop back } delete[] byteStr; - // if Filter enabled filter the received packet - if (status.modeminfo.filter[0]!=0) - { + // if Filter enabled filter the received packet + if (status.modeminfo.filter[0] != 0) + { bool filter_flag = false; uint8_t filter_size = status.modeminfo.filter[0]; uint8_t filter_ini = status.modeminfo.filter[1]; - - for (uint8_t filter_pos=0; filter_postm_hour < 10){ thisTime=thisTime + " ";} // add leading space if required - thisTime = String (timeinfo->tm_hour) + ":"; - if (timeinfo->tm_min < 10) { thisTime = thisTime + "0"; } // add leading zero if required - thisTime = thisTime + String (timeinfo->tm_min) + ":"; - if (timeinfo->tm_sec < 10) { thisTime = thisTime + "0"; } // add leading zero if required - thisTime = thisTime + String (timeinfo->tm_sec); - + if (timeinfo->tm_hour < 10) + { + thisTime = thisTime + " "; + } // add leading space if required + thisTime = String(timeinfo->tm_hour) + ":"; + if (timeinfo->tm_min < 10) + { + thisTime = thisTime + "0"; + } // add leading zero if required + thisTime = thisTime + String(timeinfo->tm_min) + ":"; + if (timeinfo->tm_sec < 10) + { + thisTime = thisTime + "0"; + } // add leading zero if required + thisTime = thisTime + String(timeinfo->tm_sec); + status.lastPacketInfo.time = thisTime; } - noisyInterrupt = false; - // put module back to listen mode + // put module back to listen mode startRx(); if (state == ERR_NONE) @@ -493,7 +451,7 @@ void Radio::readState(int state) if (state == ERR_NONE) { Log::error(PSTR("success!")); - } + } else { Log::error(PSTR("failed, code %d"), state); @@ -502,33 +460,33 @@ void Radio::readState(int state) } // remote -int16_t Radio::remote_freq(char* payload, size_t payload_len) +int16_t Radio::remote_freq(char *payload, size_t payload_len) { float frequency = _atof(payload, payload_len); Log::console(PSTR("Set Frequency: %.3f MHz"), frequency); int16_t state = 0; - if (ConfigManager::getInstance().getBoardConfig().L_SX127X) + if (ConfigManager::getInstance().getBoardConfig().L_SX127X) { - ((SX1278*)lora)->sleep(); // sleep mandatory if FastHop isn't ON. - state = ((SX1278*)lora)->setFrequency(frequency+status.modeminfo.freqOffset); - ((SX1278*)lora)->startReceive(); - } - else + ((SX1278 *)lora)->sleep(); // sleep mandatory if FastHop isn't ON. + state = ((SX1278 *)lora)->setFrequency(frequency + status.modeminfo.freqOffset); + ((SX1278 *)lora)->startReceive(); + } + else { - ((SX1268*)lora)->sleep(); - state = ((SX1268*)lora)->setFrequency(frequency+status.modeminfo.freqOffset); - ((SX1268*)lora)->startReceive(); + ((SX1268 *)lora)->sleep(); + state = ((SX1268 *)lora)->setFrequency(frequency + status.modeminfo.freqOffset); + ((SX1268 *)lora)->startReceive(); } - + readState(state); if (state == ERR_NONE) - status.modeminfo.frequency = frequency; - + status.modeminfo.frequency = frequency; + return state; } -int16_t Radio::remote_bw(char* payload, size_t payload_len) +int16_t Radio::remote_bw(char *payload, size_t payload_len) { float bw = _atof(payload, payload_len); Log::console(PSTR("Set bandwidth: %.3f MHz"), bw); @@ -536,25 +494,25 @@ int16_t Radio::remote_bw(char* payload, size_t payload_len) int16_t state = 0; if (ConfigManager::getInstance().getBoardConfig().L_SX127X) { - state = ((SX1278*)lora)->setBandwidth(bw); - ((SX1278*)lora)->startReceive(); - ((SX1278*)lora)->setDio0Action(setFlag); + state = ((SX1278 *)lora)->setBandwidth(bw); + ((SX1278 *)lora)->startReceive(); + ((SX1278 *)lora)->setDio0Action(setFlag); } else { - state = ((SX1268*)lora)->setBandwidth(bw); - ((SX1268*)lora)->startReceive(); - ((SX1268*)lora)->setDio1Action(setFlag); + state = ((SX1268 *)lora)->setBandwidth(bw); + ((SX1268 *)lora)->startReceive(); + ((SX1268 *)lora)->setDio1Action(setFlag); } readState(state); if (state == ERR_NONE) status.modeminfo.bw = bw; - + return state; } -int16_t Radio::remote_sf(char* payload, size_t payload_len) +int16_t Radio::remote_sf(char *payload, size_t payload_len) { uint8_t sf = _atof(payload, payload_len); Log::console(PSTR("Set spreading factor: %u"), sf); @@ -562,15 +520,15 @@ int16_t Radio::remote_sf(char* payload, size_t payload_len) int16_t state = 0; if (ConfigManager::getInstance().getBoardConfig().L_SX127X) { - state = ((SX1278*)lora)->setSpreadingFactor(sf); - ((SX1278*)lora)->startReceive(); - ((SX1278*)lora)->setDio0Action(setFlag); - } + state = ((SX1278 *)lora)->setSpreadingFactor(sf); + ((SX1278 *)lora)->startReceive(); + ((SX1278 *)lora)->setDio0Action(setFlag); + } else { - state = ((SX1268*)lora)->setSpreadingFactor(sf); - ((SX1268*)lora)->startReceive(); - ((SX1268*)lora)->setDio1Action(setFlag); + state = ((SX1268 *)lora)->setSpreadingFactor(sf); + ((SX1268 *)lora)->startReceive(); + ((SX1268 *)lora)->setDio1Action(setFlag); } readState(state); @@ -581,25 +539,25 @@ int16_t Radio::remote_sf(char* payload, size_t payload_len) return state; } -int16_t Radio::remote_cr(char* payload, size_t payload_len) +int16_t Radio::remote_cr(char *payload, size_t payload_len) { uint8_t cr = _atoi(payload, payload_len); Log::console(PSTR("Set coding rate: %u"), cr); - + int16_t state = 0; if (ConfigManager::getInstance().getBoardConfig().L_SX127X) { - state = ((SX1278*)lora)->setCodingRate(cr); - ((SX1278*)lora)->startReceive(); - ((SX1278*)lora)->setDio0Action(setFlag); + state = ((SX1278 *)lora)->setCodingRate(cr); + ((SX1278 *)lora)->startReceive(); + ((SX1278 *)lora)->setDio0Action(setFlag); } else { - state = ((SX1268*)lora)->setCodingRate(cr); - ((SX1268*)lora)->startReceive(); - ((SX1268*)lora)->setDio1Action(setFlag); + state = ((SX1268 *)lora)->setCodingRate(cr); + ((SX1268 *)lora)->startReceive(); + ((SX1268 *)lora)->setDio1Action(setFlag); } - + readState(state); if (state == ERR_NONE) @@ -608,7 +566,7 @@ int16_t Radio::remote_cr(char* payload, size_t payload_len) return state; } -int16_t Radio::remote_crc(char* payload, size_t payload_len) +int16_t Radio::remote_crc(char *payload, size_t payload_len) { bool crc = _atoi(payload, payload_len); Log::console(PSTR("Set CRC: %s"), crc ? F("ON") : F("OFF")); @@ -616,39 +574,39 @@ int16_t Radio::remote_crc(char* payload, size_t payload_len) if (ConfigManager::getInstance().getBoardConfig().L_SX127X) { - state = ((SX1278*)lora)->setCRC(crc); - ((SX1278*)lora)->startReceive(); - ((SX1278*)lora)->setDio0Action(setFlag); + state = ((SX1278 *)lora)->setCRC(crc); + ((SX1278 *)lora)->startReceive(); + ((SX1278 *)lora)->setDio0Action(setFlag); } else { - state = ((SX1268*)lora)->setCRC(crc); - ((SX1268*)lora)->startReceive(); - ((SX1268*)lora)->setDio1Action(setFlag); + state = ((SX1268 *)lora)->setCRC(crc); + ((SX1268 *)lora)->startReceive(); + ((SX1268 *)lora)->setDio1Action(setFlag); } readState(state); return state; } -int16_t Radio::remote_lsw(char* payload, size_t payload_len) +int16_t Radio::remote_lsw(char *payload, size_t payload_len) { uint8_t sw = _atoi(payload, payload_len); char strHex[2]; sprintf(strHex, "%1x", sw); Log::console(PSTR("Set lsw: %s"), strHex); - + int16_t state = 0; if (ConfigManager::getInstance().getBoardConfig().L_SX127X) - state = ((SX1278*)lora)->setSyncWord(sw); + state = ((SX1278 *)lora)->setSyncWord(sw); else - state = ((SX1268*)lora)->setSyncWord(sw, 0x44); + state = ((SX1268 *)lora)->setSyncWord(sw, 0x44); readState(state); return state; } -int16_t Radio::remote_fldro(char* payload, size_t payload_len) +int16_t Radio::remote_fldro(char *payload, size_t payload_len) { bool ldro = _atoi(payload, payload_len); Log::console(PSTR("Set ForceLDRO: %s"), ldro ? F("ON") : F("OFF")); @@ -656,52 +614,53 @@ int16_t Radio::remote_fldro(char* payload, size_t payload_len) int16_t state = 0; if (ConfigManager::getInstance().getBoardConfig().L_SX127X) { - state = ((SX1278*)lora)->forceLDRO(ldro); - ((SX1278*)lora)->startReceive(); - ((SX1278*)lora)->setDio0Action(setFlag); + state = ((SX1278 *)lora)->forceLDRO(ldro); + ((SX1278 *)lora)->startReceive(); + ((SX1278 *)lora)->setDio0Action(setFlag); } else { - state = ((SX1268*)lora)->forceLDRO(ldro); - ((SX1268*)lora)->startReceive(); - ((SX1268*)lora)->setDio1Action(setFlag); + state = ((SX1268 *)lora)->forceLDRO(ldro); + ((SX1268 *)lora)->startReceive(); + ((SX1268 *)lora)->setDio1Action(setFlag); } readState(state); - if (state == ERR_NONE) { + if (state == ERR_NONE) + { if (ldro) - status.modeminfo.fldro=true; - else - status.modeminfo.fldro=false; + status.modeminfo.fldro = true; + else + status.modeminfo.fldro = false; } return state; } -int16_t Radio::remote_aldro(char* payload, size_t payload_len) +int16_t Radio::remote_aldro(char *payload, size_t payload_len) { - Log::console(PSTR("Set AutoLDRO ")); + Log::console(PSTR("Set AutoLDRO ")); int16_t state = 0; if (ConfigManager::getInstance().getBoardConfig().L_SX127X) { - state = ((SX1278*)lora)->autoLDRO(); - ((SX1278*)lora)->startReceive(); - ((SX1278*)lora)->setDio0Action(setFlag); + state = ((SX1278 *)lora)->autoLDRO(); + ((SX1278 *)lora)->startReceive(); + ((SX1278 *)lora)->setDio0Action(setFlag); } else { - state = ((SX1268*)lora)->autoLDRO(); - ((SX1268*)lora)->startReceive(); - ((SX1268*)lora)->setDio1Action(setFlag); + state = ((SX1268 *)lora)->autoLDRO(); + ((SX1268 *)lora)->startReceive(); + ((SX1268 *)lora)->setDio1Action(setFlag); } readState(state); return state; } -int16_t Radio::remote_pl(char* payload, size_t payload_len) +int16_t Radio::remote_pl(char *payload, size_t payload_len) { uint16_t pl = _atoi(payload, payload_len); Log::console(PSTR("Set Preamble %u"), pl); @@ -709,15 +668,15 @@ int16_t Radio::remote_pl(char* payload, size_t payload_len) if (ConfigManager::getInstance().getBoardConfig().L_SX127X) { - state = ((SX1278*)lora)->setPreambleLength(pl); - ((SX1278*)lora)->startReceive(); - ((SX1278*)lora)->setDio0Action(setFlag); + state = ((SX1278 *)lora)->setPreambleLength(pl); + ((SX1278 *)lora)->startReceive(); + ((SX1278 *)lora)->setDio0Action(setFlag); } else { - state = ((SX1268*)lora)->setPreambleLength(pl); - ((SX1268*)lora)->startReceive(); - ((SX1268*)lora)->setDio1Action(setFlag); + state = ((SX1268 *)lora)->setPreambleLength(pl); + ((SX1268 *)lora)->startReceive(); + ((SX1268 *)lora)->setDio1Action(setFlag); } readState(state); @@ -727,20 +686,20 @@ int16_t Radio::remote_pl(char* payload, size_t payload_len) return state; } -int16_t Radio::remote_begin_lora(char* payload, size_t payload_len) +int16_t Radio::remote_begin_lora(char *payload, size_t payload_len) { DynamicJsonDocument doc(256); deserializeJson(doc, payload, payload_len); - float freq = doc[0]; - float bw = doc[1]; - uint8_t sf = doc[2]; - uint8_t cr = doc[3]; - uint8_t syncWord78 = doc[4]; - int8_t power = doc[5]; + float freq = doc[0]; + float bw = doc[1]; + uint8_t sf = doc[2]; + uint8_t cr = doc[3]; + uint8_t syncWord78 = doc[4]; + int8_t power = doc[5]; uint8_t current_limit = doc[6]; uint16_t preambleLength = doc[7]; uint8_t gain = doc[8]; - uint16_t syncWord68 = doc[4]; + uint16_t syncWord68 = doc[4]; char sw78StrHex[2]; char sw68StrHex[3]; @@ -752,134 +711,136 @@ int16_t Radio::remote_begin_lora(char* payload, size_t payload_len) int16_t state = 0; if (ConfigManager::getInstance().getBoardConfig().L_SX127X) { - ((SX1278*)lora)->sleep(); // sleep mandatory if FastHop isn't ON. - state = ((SX1278*)lora)->begin(freq+status.modeminfo.freqOffset, bw, sf, cr, syncWord78, power, preambleLength, gain); - ((SX1278*)lora)->startReceive(); - ((SX1278*)lora)->setDio0Action(setFlag); + ((SX1278 *)lora)->sleep(); // sleep mandatory if FastHop isn't ON. + state = ((SX1278 *)lora)->begin(freq + status.modeminfo.freqOffset, bw, sf, cr, syncWord78, power, preambleLength, gain); + ((SX1278 *)lora)->startReceive(); + ((SX1278 *)lora)->setDio0Action(setFlag); } else { - state = ((SX1268*)lora)->begin(freq+status.modeminfo.freqOffset, bw, sf, cr, syncWord68, power, preambleLength, ConfigManager::getInstance().getBoardConfig().L_TCXO_V); - ((SX1268*)lora)->startReceive(); - ((SX1268*)lora)->setDio1Action(setFlag); + state = ((SX1268 *)lora)->begin(freq + status.modeminfo.freqOffset, bw, sf, cr, syncWord68, power, preambleLength, ConfigManager::getInstance().getBoardConfig().L_TCXO_V); + ((SX1268 *)lora)->startReceive(); + ((SX1268 *)lora)->setDio1Action(setFlag); } - + readState(state); if (state == ERR_NONE) { status.modeminfo.modem_mode = "LoRa"; - status.modeminfo.frequency = freq; - status.modeminfo.bw = bw; - status.modeminfo.power = power ; + status.modeminfo.frequency = freq; + status.modeminfo.bw = bw; + status.modeminfo.power = power; status.modeminfo.preambleLength = preambleLength; - status.modeminfo.sf = sf; - status.modeminfo.cr = cr; + status.modeminfo.sf = sf; + status.modeminfo.cr = cr; } return state; } -int16_t Radio::remote_begin_fsk(char* payload, size_t payload_len) +int16_t Radio::remote_begin_fsk(char *payload, size_t payload_len) { DynamicJsonDocument doc(256); deserializeJson(doc, payload, payload_len); - float freq = doc[0]; - float br = doc[1]; - float freqDev = doc[2]; - float rxBw = doc[3]; - int8_t power = doc[4]; + float freq = doc[0]; + float br = doc[1]; + float freqDev = doc[2]; + float rxBw = doc[3]; + int8_t power = doc[4]; uint8_t currentlimit = doc[5]; uint16_t preambleLength = doc[6]; - uint8_t ook = doc[7]; // + uint8_t ook = doc[7]; // Log::console(PSTR("Set Frequency: %.3f MHz\nSet bit rate: %.3f\nSet Frequency deviation: %.3f kHz\nSet receiver bandwidth: %.3f kHz\nSet Power: %d"), freq, br, freqDev, rxBw, power); Log::console(PSTR("Set Current limit: %u\nSet Preamble Length: %u\nOOK Modulation %s\nSet datashaping %u"), currentlimit, preambleLength, (ook != 255) ? F("ON") : F("OFF"), ook); int16_t state = 0; - if (ConfigManager::getInstance().getBoardConfig().L_SX127X) { - state = ((SX1278*)lora)->beginFSK(freq+status.modeminfo.freqOffset, br, freqDev, rxBw, power, preambleLength, (ook != 255)); - ((SX1278*)lora)->setDataShaping(ook); - ((SX1278*)lora)->startReceive(); - ((SX1278*)lora)->setDio0Action(setFlag); - - } else { - state = ((SX1268*)lora)->beginFSK(freq+status.modeminfo.freqOffset, br, freqDev, rxBw, power, preambleLength, ConfigManager::getInstance().getBoardConfig().L_TCXO_V); - ((SX1268*)lora)->setDataShaping(ook); - ((SX1268*)lora)->startReceive(); - ((SX1268*)lora)->setDio1Action(setFlag); + if (ConfigManager::getInstance().getBoardConfig().L_SX127X) + { + state = ((SX1278 *)lora)->beginFSK(freq + status.modeminfo.freqOffset, br, freqDev, rxBw, power, preambleLength, (ook != 255)); + ((SX1278 *)lora)->setDataShaping(ook); + ((SX1278 *)lora)->startReceive(); + ((SX1278 *)lora)->setDio0Action(setFlag); + } + else + { + state = ((SX1268 *)lora)->beginFSK(freq + status.modeminfo.freqOffset, br, freqDev, rxBw, power, preambleLength, ConfigManager::getInstance().getBoardConfig().L_TCXO_V); + ((SX1268 *)lora)->setDataShaping(ook); + ((SX1268 *)lora)->startReceive(); + ((SX1268 *)lora)->setDio1Action(setFlag); } readState(state); - + if (state == ERR_NONE) { status.modeminfo.modem_mode = "FSK"; - status.modeminfo.frequency = freq; - status.modeminfo.bw = rxBw; - status.modeminfo.power = power; + status.modeminfo.frequency = freq; + status.modeminfo.bw = rxBw; + status.modeminfo.power = power; status.modeminfo.preambleLength = preambleLength; - status.modeminfo.bitrate = br; - status.modeminfo.freqDev = freqDev; - status.modeminfo.OOK = ook; + status.modeminfo.bitrate = br; + status.modeminfo.freqDev = freqDev; + status.modeminfo.OOK = ook; } return state; } -int16_t Radio::remote_br(char* payload, size_t payload_len) +int16_t Radio::remote_br(char *payload, size_t payload_len) { uint8_t br = _atoi(payload, payload_len); Log::console(PSTR("Set FSK Bit rate: %u"), br); int16_t state = 0; if (ConfigManager::getInstance().getBoardConfig().L_SX127X) - state = ((SX1278*)lora)->setBitRate(br); + state = ((SX1278 *)lora)->setBitRate(br); else - state = ((SX1268*)lora)->setBitRate(br); + state = ((SX1268 *)lora)->setBitRate(br); readState(state); if (state == ERR_NONE) status.modeminfo.bitrate = br; - + return state; } -int16_t Radio::remote_fd(char* payload, size_t payload_len) +int16_t Radio::remote_fd(char *payload, size_t payload_len) { uint8_t fd = _atoi(payload, payload_len); Log::console(PSTR("Set FSK Frequency Dev.: %u"), fd); int16_t state = 0; if (ConfigManager::getInstance().getBoardConfig().L_SX127X) - state = ((SX1278*)lora)->setFrequencyDeviation(fd); + state = ((SX1278 *)lora)->setFrequencyDeviation(fd); else - state = ((SX1268*)lora)->setFrequencyDeviation(fd); + state = ((SX1268 *)lora)->setFrequencyDeviation(fd); readState(state); - if (state == ERR_NONE) + if (state == ERR_NONE) status.modeminfo.freqDev = fd; return state; } -int16_t Radio::remote_fbw(char* payload, size_t payload_len) +int16_t Radio::remote_fbw(char *payload, size_t payload_len) { float frequency = _atof(payload, payload_len); Log::console(PSTR("Set FSK bandwidth: %.3f kHz"), frequency); int16_t state = 0; if (ConfigManager::getInstance().getBoardConfig().L_SX127X) - state = ((SX1278*)lora)->setRxBandwidth(frequency); + state = ((SX1278 *)lora)->setRxBandwidth(frequency); else - state = ((SX1268*)lora)->setRxBandwidth(frequency); + state = ((SX1268 *)lora)->setRxBandwidth(frequency); readState(state); if (state == ERR_NONE) - status.modeminfo.bw = frequency; - + status.modeminfo.bw = frequency; + return state; } -int16_t Radio::remote_fsw(char* payload, size_t payload_len) +int16_t Radio::remote_fsw(char *payload, size_t payload_len) { DynamicJsonDocument doc(256); deserializeJson(doc, payload, payload_len); @@ -887,29 +848,33 @@ int16_t Radio::remote_fsw(char* payload, size_t payload_len) uint8_t syncWord[synnwordsize]; Serial.println(""); - Serial.print(F("Set SyncWord Size ")); Serial.print(synnwordsize); Serial.print(F("-> ")); + Serial.print(F("Set SyncWord Size ")); + Serial.print(synnwordsize); + Serial.print(F("-> ")); - for (uint8_t words=0; wordssetSyncWord(syncWord, synnwordsize); + state = ((SX1278 *)lora)->setSyncWord(syncWord, synnwordsize); else - state = ((SX1268*)lora)->setSyncWord(syncWord, synnwordsize); + state = ((SX1268 *)lora)->setSyncWord(syncWord, synnwordsize); readState(state); return state; } -int16_t Radio::remote_fook(char* payload, size_t payload_len) +int16_t Radio::remote_fook(char *payload, size_t payload_len) { DynamicJsonDocument doc(60); deserializeJson(doc, payload, payload_len); - bool enableOOK = doc[0]; + bool enableOOK = doc[0]; uint8_t ook_shape = doc[1]; Log::console(PSTR("OOK Modulation: %s"), enableOOK ? F("ON") : F("OFF")); @@ -918,7 +883,7 @@ int16_t Radio::remote_fook(char* payload, size_t payload_len) int state = 0; if (ConfigManager::getInstance().getBoardConfig().L_SX127X) { - state = ((SX1278*)lora)->setOOK(enableOOK); + state = ((SX1278 *)lora)->setOOK(enableOOK); } else { @@ -929,13 +894,13 @@ int16_t Radio::remote_fook(char* payload, size_t payload_len) readState(state); if (ConfigManager::getInstance().getBoardConfig().L_SX127X) - state = ((SX1278*)lora)->setDataShapingOOK(ook_shape); + state = ((SX1278 *)lora)->setDataShapingOOK(ook_shape); readState(state); return state; } -void Radio::remote_SPIwriteRegister(char* payload, size_t payload_len) +void Radio::remote_SPIwriteRegister(char *payload, size_t payload_len) { DynamicJsonDocument doc(60); deserializeJson(doc, payload, payload_len); @@ -944,29 +909,29 @@ void Radio::remote_SPIwriteRegister(char* payload, size_t payload_len) Log::console(PSTR("REG ID: 0x%x to 0x%x"), reg, data); if (ConfigManager::getInstance().getBoardConfig().L_SX127X) - ((SX1278*)lora)->_mod->SPIwriteRegister(reg,data); -// else - // ((SX1268*)lora)->_mod->SPIwriteRegister(reg,data); + ((SX1278 *)lora)->_mod->SPIwriteRegister(reg, data); + // else + // ((SX1268*)lora)->_mod->SPIwriteRegister(reg,data); } -int16_t Radio::remote_SPIreadRegister(char* payload, size_t payload_len) +int16_t Radio::remote_SPIreadRegister(char *payload, size_t payload_len) { uint8_t reg = _atoi(payload, payload_len); uint8_t data = 0; int16_t state = 0; if (ConfigManager::getInstance().getBoardConfig().L_SX127X) - data = ((SX1278*)lora)->_mod->SPIreadRegister(reg); - // else - // data = ((SX1268*)lora)->_mod->SPIreadRegister(reg); + data = ((SX1278 *)lora)->_mod->SPIreadRegister(reg); + // else + // data = ((SX1268*)lora)->_mod->SPIreadRegister(reg); Log::console(PSTR("REG ID: 0x%x HEX : 0x%x BIN : %b"), reg, data, data); - + readState(state); return data; } -int16_t Radio::remote_SPIsetRegValue(char* payload, size_t payload_len) +int16_t Radio::remote_SPIsetRegValue(char *payload, size_t payload_len) { DynamicJsonDocument doc(120); deserializeJson(doc, payload, payload_len); @@ -975,7 +940,7 @@ int16_t Radio::remote_SPIsetRegValue(char* payload, size_t payload_len) uint8_t msb = doc[2]; uint8_t lsb = doc[3]; uint8_t checkinterval = doc[4]; - + Serial.println(""); Serial.print(F("REG ID: 0x")); Serial.println(reg, HEX); @@ -993,25 +958,25 @@ int16_t Radio::remote_SPIsetRegValue(char* payload, size_t payload_len) int16_t state = 0; if (ConfigManager::getInstance().getBoardConfig().L_SX127X) - state = ((SX1278*)lora)->_mod->SPIsetRegValue(reg, value, msb, lsb, checkinterval); + state = ((SX1278 *)lora)->_mod->SPIsetRegValue(reg, value, msb, lsb, checkinterval); else - // state = ((SX1268*)lora)->_mod->SPIsetRegValue(reg, value, msb, lsb, checkinterval); - - readState(state); + // state = ((SX1268*)lora)->_mod->SPIsetRegValue(reg, value, msb, lsb, checkinterval); + + readState(state); return state; } -double Radio::_atof(const char* buff, size_t length) +double Radio::_atof(const char *buff, size_t length) { - char str[length+1]; + char str[length + 1]; memcpy(str, buff, length); str[length] = '\0'; return atof(str); } -int Radio::_atoi(const char* buff, size_t length) +int Radio::_atoi(const char *buff, size_t length) { - char str[length+1]; + char str[length + 1]; memcpy(str, buff, length); str[length] = '\0'; return atoi(str); diff --git a/tinyGS/src/Status.h b/tinyGS/src/Status.h index 99604179..bdf38d19 100644 --- a/tinyGS/src/Status.h +++ b/tinyGS/src/Status.h @@ -51,6 +51,7 @@ struct ModemInfo { uint8_t gain = 0; uint32_t NORAD = 46494; // funny this remember me WARGames uint8_t fsw[8] = {0,0,0,0,0,0,0,0}; + uint8_t swSize = 0; uint8_t filter[8] = {0,0,0,0,0,0,0,0}; }; @@ -63,7 +64,7 @@ struct TextFrame { }; struct Status { - const uint32_t version = 2105253; // version year month day release + const uint32_t version = 2105260; // version year month day release const char* git_version = GIT_VERSION; bool mqtt_connected = false; bool radio_ready = false; diff --git a/tinyGS/tinyGS.ino b/tinyGS/tinyGS.ino index 1f55c91d..7c6d4213 100644 --- a/tinyGS/tinyGS.ino +++ b/tinyGS/tinyGS.ino @@ -170,9 +170,7 @@ void setup() { displayTurnOff(); } - - - + printControls(); }