diff --git a/include/File.h b/include/File.h index d726c40..6f502ec 100644 --- a/include/File.h +++ b/include/File.h @@ -1,6 +1,16 @@ #include "FS.h" #include +// Initialize LittleFS +void initLittleFS() +{ + if (!LittleFS.begin(true)) + { + Serial.println("An error has occurred while mounting LittleFS"); + } + Serial.println("LittleFS mounted successfully"); +} + String readFile(fs::FS &fs, const char *path) { Serial.printf("Reading file: %s\r\n", path); @@ -24,6 +34,7 @@ String readFile(fs::FS &fs, const char *path) void writeFile(fs::FS &fs, const char *path, const char *message) { Serial.printf("Writing file: %s\r\n", path); + Serial.printf("Content: %s\r\n", message); File file = fs.open(path, FILE_WRITE); if (!file) @@ -34,6 +45,7 @@ void writeFile(fs::FS &fs, const char *path, const char *message) if (file.print(message)) { Serial.println("- file written"); + delay(500); } else { diff --git a/include/LiLyGo.h b/include/LiLyGo.h index c9c88ee..1d44603 100644 --- a/include/LiLyGo.h +++ b/include/LiLyGo.h @@ -35,6 +35,10 @@ SX1280 radio = new Module(RADIO_CS_PIN, RADIO_DIO1_PIN, RADIO_RST_PIN, RADIO_BUS // Default SPI on pins from pins_arduino.h SX1262 radio = new Module(RADIO_CS_PIN, RADIO_DIO1_PIN, RADIO_RST_PIN, RADIO_BUSY_PIN); #endif // end USING_SX1262 +#ifdef USING_LR1121 +// Default SPI on pins from pins_arduino.h +LR1121 radio = new Module(RADIO_CS_PIN, RADIO_DIO9_PIN, RADIO_RST_PIN, RADIO_BUSY_PIN); +#endif // end USING_LR1121 #ifdef USING_SX1276 // Default SPI on pins from pins_arduino.h SX1276 radio = new Module(RADIO_CS_PIN, RADIO_DIO1_PIN, RADIO_RST_PIN, RADIO_BUSY_PIN); diff --git a/include/WIFI_SERVER.h b/include/WIFI_SERVER.h index 0460306..89d95de 100644 --- a/include/WIFI_SERVER.h +++ b/include/WIFI_SERVER.h @@ -7,25 +7,23 @@ AsyncWebServer server(80); // Search for parameter in HTTP POST request -const char *PARAM_INPUT_1 = "ssid"; -const char *PARAM_INPUT_2 = "pass"; -const char *PARAM_INPUT_3 = "ip"; -const char *PARAM_INPUT_4 = "gateway"; -const char *PARAM_INPUT_5 = "fstart"; -const char *PARAM_INPUT_6 = "fend"; +const String SSID = "ssid"; +const String PASS = "pass"; +const String IP = "ip"; +const String GATEWAY = "gateway"; +const String FSTART = "fstart"; +const String FEND = "fend"; // File paths to save input values permanently -const char *ssidPath = "/ssid.txt"; -const char *passPath = "/pass.txt"; -const char *ipPath = "/ip.txt"; -const char *gatewayPath = "/gateway.txt"; +// const char *ssidPath = "/ssid.txt"; // Variables to save values from HTML form -String ssid = "LoraSA", pass = "1234567890", ip, gateway, fstart, fend; +String ssid = "LoraSA", pass = "1234567890", ip = "192.168.1.100", + gateway = "192.168.1.1", fstart = "", fend = ""; -IPAddress localIP(192, 168, 1, 200); +IPAddress localIP; // Set your Gateway IP address -IPAddress localGateway(192, 168, 1, 1); +IPAddress localGateway; IPAddress subnet(255, 255, 0, 0); // Timer variables @@ -35,6 +33,11 @@ const long interval = 10000; // interval to wait for Wi-Fi connection (milliseco // Initialize WiFi bool initWiFi() { + Serial.println("SSID:" + ssid); + Serial.println("PSWD:" + pass); + Serial.println("IP:" + ip); + Serial.println("SUB:" + subnet); + Serial.println("GATAWAY:" + gateway); if (ssid == "" || ip == "") { Serial.println("Undefined SSID or IP address."); @@ -42,8 +45,8 @@ bool initWiFi() } WiFi.mode(WIFI_STA); - // localIP.fromString(ip.c_str()); - // localGateway.fromString(gateway.c_str()); + localIP.fromString(ip.c_str()); + localGateway.fromString(gateway.c_str()); if (!WiFi.config(localIP, localGateway, subnet)) { @@ -70,37 +73,100 @@ bool initWiFi() return true; } +void writeParameterToFile(String value, String file) +{ + // Write file to save value + writeFile(LittleFS, file.c_str(), value.c_str()); +} + +void writeParameterToParameterFile(String param, String value) +{ + String file = String("/" + param + ".txt"); + // Write file to save value + writeParameterToFile(value, file.c_str()); +} + +String readParameterFromParameterFile(String param) +{ + String file = String("/" + param + ".txt"); + return readFile(LittleFS, file.c_str()); +} + +void serverServer() +{ + // Route for root / web page + server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) + { request->send(LittleFS, "/index.html", "text/html"); }); + + server.serveStatic("/", LittleFS, "/"); + + server.on("/", HTTP_POST, + [](AsyncWebServerRequest *request) + { + int params = request->params(); + for (int i = 0; i < params; i++) + { + Serial.println("Parameter " + String(i) + ": " + + request->getParam(i)->value()); + } + Serial.println(request->params()); + + String p = request->getParam(SSID, true)->value(); + writeParameterToParameterFile(SSID, p); + + p = request->getParam(PASS, true)->value(); + writeParameterToParameterFile(PASS, p); + + p = request->getParam(IP, true)->value(); + writeParameterToParameterFile(IP, p); + + p = request->getParam(GATEWAY, true)->value(); + writeParameterToParameterFile(GATEWAY, p); + + p = request->getParam(FSTART, true)->value(); + writeParameterToParameterFile(FSTART, p); + + p = request->getParam(FEND, true)->value(); + writeParameterToParameterFile(FEND, p); + + request->send(200, "text/plain", + "Done. ESP will restart, connect to your router and " + "go to IP address: " + + ip); + delay(3000); + ESP.restart(); + }); + + /* // Route to set GPIO state to HIGH + server.on("/on", HTTP_GET, + [](AsyncWebServerRequest *request) + { + digitalWrite(ledPin, HIGH); + request->send(LittleFS, "/index.html", "text/html", false, + processor); + }); + + // Route to set GPIO state to LOW + server.on("/off", HTTP_GET, + [](AsyncWebServerRequest *request) + { + digitalWrite(ledPin, LOW); + request->send(LittleFS, "/index.html", "text/html", false, + processor); + });*/ + server.begin(); +} + void serverStart() { if (initWiFi()) { - // Route for root / web page - server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) - { request->send(LittleFS, "/index.html", "text/html"); }); - server.serveStatic("/", LittleFS, "/"); - - /* // Route to set GPIO state to HIGH - server.on("/on", HTTP_GET, - [](AsyncWebServerRequest *request) - { - digitalWrite(ledPin, HIGH); - request->send(LittleFS, "/index.html", "text/html", false, - processor); - }); - - // Route to set GPIO state to LOW - server.on("/off", HTTP_GET, - [](AsyncWebServerRequest *request) - { - digitalWrite(ledPin, LOW); - request->send(LittleFS, "/index.html", "text/html", false, - processor); - });*/ - server.begin(); + Serial.println("Setting Secure WIFI (Access Point)"); + serverServer(); } else { - // Connect to Wi-Fi network with SSID and password + // Connect to Wi-Fi network with default SSID and password Serial.println("Setting AP (Access Point)"); // NULL sets an open Access Point WiFi.softAP("LoraSA", NULL); @@ -109,68 +175,6 @@ void serverStart() Serial.print("AP IP address: "); Serial.println(IP); - // Web Server Root URL - server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) - { request->send(LittleFS, "/index.html", "text/html"); }); - - server.serveStatic("/", LittleFS, "/"); - - server.on("/", HTTP_POST, - [](AsyncWebServerRequest *request) - { - int params = request->params(); - for (int i = 0; i < params; i++) - { - const AsyncWebParameter *p = request->getParam(i); - if (p->isPost()) - { - // HTTP POST ssid value - if (p->name() == PARAM_INPUT_1) - { - ssid = p->value().c_str(); - Serial.print("SSID set to: "); - Serial.println(ssid); - // Write file to save value - writeFile(LittleFS, ssidPath, ssid.c_str()); - } - // HTTP POST pass value - if (p->name() == PARAM_INPUT_2) - { - pass = p->value().c_str(); - Serial.print("Password set to: "); - Serial.println(pass); - // Write file to save value - writeFile(LittleFS, passPath, pass.c_str()); - } - // HTTP POST ip value - if (p->name() == PARAM_INPUT_3) - { - ip = p->value().c_str(); - Serial.print("IP Address set to: "); - Serial.println(ip); - // Write file to save value - writeFile(LittleFS, ipPath, ip.c_str()); - } - // HTTP POST gateway value - if (p->name() == PARAM_INPUT_4) - { - gateway = p->value().c_str(); - Serial.print("Gateway set to: "); - Serial.println(gateway); - // Write file to save value - writeFile(LittleFS, gatewayPath, gateway.c_str()); - } - // Serial.printf("POST[%s]: %s\n", p->name().c_str(), - // p->value().c_str()); - } - } - request->send(200, "text/plain", - "Done. ESP will restart, connect to your router and " - "go to IP address: " + - ip); - delay(3000); - ESP.restart(); - }); - server.begin(); + serverServer(); } } diff --git a/lib/scan/scan.h b/lib/scan/scan.h index c6b113c..38b610e 100644 --- a/lib/scan/scan.h +++ b/lib/scan/scan.h @@ -27,7 +27,9 @@ constexpr float HI_RSSI_THRESHOLD = -44.0; constexpr float LO_RSSI_THRESHOLD = HI_RSSI_THRESHOLD - 66; // number of samples for RSSI method -#define SAMPLES_RSSI 12 // 21 // +#ifndef SAMPLES_RSSI +#define SAMPLES_RSSI 13 // 21 // +#endif #ifdef USING_SX1280PA #define SAMPLES_RSSI 20 #endif diff --git a/platformio.ini b/platformio.ini index 2868df9..a1d92eb 100644 --- a/platformio.ini +++ b/platformio.ini @@ -76,6 +76,36 @@ build_flags = -DARDUINO_LILYGO_T3_S3_V1_X -DARDUINO_USB_MODE=1 +[env:lilygo-T3S3-v1-2-lr1121] +platform = espressif32 +board = t3_s3_v1_x +framework = arduino +upload_speed = 921600 +monitor_speed = 115200 +board_build.f_cpu = 240000000 +board_build.filesystem = littlefs +lib_deps = + ropg/Heltec_ESP32_LoRa_v3@^0.9.1 + RadioLib + U8g2 + XPowersLib + bblanchon/ArduinoJson@^7.2.0 + ESP Async WebServer +build_flags = + -DLILYGO + -DT3_S3_V1_2_LR1121 + -DT3_V1_3_SX1262 + -DARDUINO_LILYGO_T3S3_LR1121 + -DESP32 + -DSAMPLES_RSSI=5 + -DUSING_LR1121 + -DFREQ_BEGIN=2400 + -DFREQ_END=2500 + -DARDUINO_ARCH_ESP32 + -DARDUINO_USB_CDC_ON_BOOT=1 + -DARDUINO_LILYGO_T3_S3_V1_X + -DARDUINO_USB_MODE=1 + [env:lilygo-T3S3-v1-2-sx1280] platform = espressif32 diff --git a/src/main.cpp b/src/main.cpp index 20fa879..1d60514 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -54,6 +54,8 @@ #include #include +#include + #ifndef LILYGO #include // This file contains a binary patch for the SX1262 @@ -143,8 +145,9 @@ int SCAN_RANGES[] = {}; // MHZ per page // to put everything into one page set RANGE_PER_PAGE = FREQ_END - 800 -uint64_t RANGE_PER_PAGE = FREQ_END - FREQ_BEGIN; // FREQ_END - FREQ_BEGIN +uint64_t RANGE_PER_PAGE; // FREQ_END - FREQ_BEGIN +uint64_t CONF_FREQ_END, CONF_FREQ_BEGIN; // To Enable Multi Screen scan // uint64_t RANGE_PER_PAGE = 50; // Default Range on Menu Button Switch @@ -177,16 +180,8 @@ constexpr int WINDOW_SIZE = 15; // if more than 100 it can freeze #define SAMPLES 35 //(scan time = 1294) -#define RANGE (int)(FREQ_END - FREQ_BEGIN) - -#define SINGLE_STEP (float)(RANGE / (STEPS * SCAN_RBW_FACTOR)) - -uint64_t range = (int)(FREQ_END - FREQ_BEGIN); - -uint64_t iterations = RANGE / RANGE_PER_PAGE; - -// uint64_t range_frequency = FREQ_END - FREQ_BEGIN; -uint64_t median_frequency = (FREQ_BEGIN + FREQ_END) / 2; +uint64_t RANGE, range, iterations, median_frequency; +float SINGLE_STEP; // #define DISABLE_PLOT_CHART false // unused @@ -376,13 +371,20 @@ struct RadioScan : Scan float RadioScan::getRSSI() { -#ifdef USING_SX1280PA +#if defined(USING_SX1280PA) // radio.startReceive(); // get instantaneous RSSI value // When PR will be merged we can use radi.getRSSI(false); uint8_t data[3] = {0, 0, 0}; // RssiInst, Status, RFU radio.mod->SPIreadStream(RADIOLIB_SX128X_CMD_GET_RSSI_INST, data, 3); return ((float)data[0] / (-2.0)); + +#elif defined(USING_LR1121) + // Try getRssiInst + float rssi; + radio.getRssiInst(&rssi); + // pass the replies + return rssi; #else return radio.getRSSI(false); #endif @@ -401,7 +403,7 @@ void init_radio() { // initialize SX1262 FSK modem at the initial frequency both.println("Init radio"); -#ifdef USING_SX1280PA +#if defined(USING_SX1280PA) || defined(USING_LR1121) state = radio.beginGFSK(FREQ_BEGIN); #else state = radio.beginFSK(FREQ_BEGIN); @@ -590,7 +592,7 @@ void setup(void) pinMode(BUZZER_PIN, OUTPUT); pinMode(REB_PIN, OUTPUT); heltec_setup(); - serverStart(); + #ifdef JOYSTICK_ENABLED calibrate_joy(); pinMode(JOY_BTN_PIN, INPUT_PULLUP); @@ -612,16 +614,53 @@ void setup(void) init_radio(); - if (!LittleFS.begin(FORMAT_LITTLEFS_IF_FAILED)) - { - Serial.println("LittleFS Mount Failed"); - } + initLittleFS(); // writeFile(LittleFS, "/text.txt", "{WIFI:{name:\"sdfsdf\", Password:\"sdfsdf\"}"); - Serial.println(readFile(LittleFS, "/text.txt")); + ssid = readParameterFromParameterFile(SSID); + Serial.println("SSID: " + ssid); + + pass = readParameterFromParameterFile(PASS); + Serial.println("PASS: " + pass); + + ip = readParameterFromParameterFile(IP); + Serial.println("PASS: " + ip); + + gateway = readParameterFromParameterFile(GATEWAY); + Serial.println("GATEWAY: " + gateway); + + fstart = readParameterFromParameterFile(FSTART); + Serial.println("FSTART: " + fstart); + + fend = readParameterFromParameterFile(FEND); + Serial.println("FEND: " + fend); + + both.println("Starting WIFI-SERVER"); + serverStart(); + + CONF_FREQ_BEGIN = (fstart == "") ? FREQ_BEGIN : atoi(fstart.c_str()); + CONF_FREQ_END = (fend == "") ? FREQ_END : atoi(fend.c_str()); + + both.println("C FREQ BEGIN:" + String(CONF_FREQ_BEGIN)); + both.println("C FREQ END:" + String(CONF_FREQ_END)); + + RANGE_PER_PAGE = CONF_FREQ_END - CONF_FREQ_BEGIN; // FREQ_END - FREQ_BEGIN + + RANGE = (int)(CONF_FREQ_END - CONF_FREQ_BEGIN); + + SINGLE_STEP = (float)(RANGE / (STEPS * SCAN_RBW_FACTOR)); + + range = (int)(CONF_FREQ_END - CONF_FREQ_BEGIN); + + iterations = RANGE / RANGE_PER_PAGE; + + // uint64_t range_frequency = FREQ_END - FREQ_BEGIN; + median_frequency = (CONF_FREQ_BEGIN + CONF_FREQ_END) / 2; + #ifndef LILYGO vbat = heltec_vbat(); both.printf("V battery: %.2fV (%d%%)\n", vbat, heltec_battery_percent(vbat)); + delay(1000); #endif // end not LILYGO #ifdef WIFI_SCANNING_ENABLED WiFi.mode(WIFI_STA);