// Set this to the operator inbox for the firmware distribution. #ifndef LOGGER_PAIRING_REQUEST_EMAIL #define LOGGER_PAIRING_REQUEST_EMAIL "info@thesouthernbluegill.com" #endif // Southern Bluegill / XIAO ESP32S3 + B2B Wio-SX1262 kit (102010611). // Receiver revision 20: recover interrupted log writes and retry uploads. // OLED optional; SD disabled. Hold the Wio USER button (GPIO21) for 5 s for Wi-Fi setup. // New board starts a NEW filesystem; old receiver logs/settings do not migrate automatically. // Enabled reception at 907.8 MHz / 500 kHz / SF9 / CR4/7. No LoRa TX/ACK/relay. // See README.md for first-use storage initialization, upload options and verification limits. #include #include #include // Optional 1.3-inch SH1106 I2C: GND, 3V3, SCK=D5/GPIO6, SDA=D4/GPIO5. // Compile target: esp32 core 3.3.2, XIAO_ESP32S3, CDC enabled, default_8MB partition. U8G2_SH1106_128X64_NONAME_F_HW_I2C oled(U8G2_R0,U8X8_PIN_NONE,6,5); bool oledReady=false; #include #include #include #include #include // ArduinoJson 7.4.2 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include // Time zone for the clock (this one is US Central; change it for your zone) const char* TZ_INFO = "CST6CDT,M3.2.0,M11.1.0"; // SD access is deliberately disabled until a specific expansion/wiring is verified. // Definitions are dormant; do not copy the Heltec SD wiring onto this board. #define ENABLE_SD 0 #define SD_CS 3 #define SD_MOSI 9 #define SD_MISO 8 #define SD_SCK 7 constexpr float RADIO_MHZ=907.8f, RADIO_BW_KHZ=500.0f; constexpr uint8_t RADIO_SF=9, RADIO_CR=7; constexpr int8_t RADIO_DBM=10; SX1262 radio = new Module(41,39,42,40); // NSS, DIO1, reset, busy bool radioReady=false; unsigned long lastRadioRetry=0; WebServer server(80); SPIClass sdSPI(HSPI); bool sdOk = false, flashOk=false, storageError=false; uint32_t invalidPackets=0,savedReadings=0; String clearToken; constexpr int USER_BUTTON=21; // Wio expansion USER button; XIAO RESET is not USER uint8_t displayMode=0; // Normal, Dim, Off bool screenOn=true,buttonRaw=HIGH,buttonStable=HIGH; unsigned long buttonChanged=0,screenStarted=0,buttonPressedAt=0; bool buttonHoldHandled=false; struct Deployment; constexpr size_t REPORT_BODY_MAX=2048; struct ReportJob { char body[REPORT_BODY_MAX]; char key[45]; char credential[44]; char url[112]; bool pairing; uint8_t checkCount; }; bool savedReading(const String& line,char* key,String& stamp,ReportJob* job); String deploymentCSV(const String& node); bool commitDeployment(Deployment& d,String& error); void bindDeploymentToProbe(const String& node); void loadDeployment(); void noteProbe(const String& node); void startWifiSetup(); bool wifiSetupIsActive(); void enqueueReport(const String& node,const String& boot,const String& seq,const char* ts,float rawF,float offsetF,float tempF); void loadReporting(); void handlePairing(); void handleOwnerLink(); bool startReportingWorker(); bool pairingTokenOK(const String&); String pairingRandomKey(); String pairingHash(const String&); void serviceReporting(); void reportWorker(void*); bool reportDigits32(const String&,uint32_t&); bool reportNodeOK(const String&); bool reportTimestampOK(const char*); String reportScan(); bool startReportingWorker(); void handleReportingGet(); void handleReportingSave(); bool appendLine(const String& line); bool initLog(fs::FS& fs); bool markLogV5(fs::FS& fs); bool rotateLog(fs::FS& fs); bool writeLine(fs::FS& fs,const String& line,bool card); float probeOffset(const String& node); String oledTitle(); void updateDisplay(); void renderOLED(); bool savedUploadUsesLog(); String oledReportStatus(); void drawBrand(); void serviceOLED(); void serviceWeather(); void startWeatherWorker(); void screen(bool on) { screenOn=on; // Refresh OLED memory while it is still asleep, then reveal the current data. if(on) updateDisplay(); if(oledReady)oled.setPowerSave(on?0:1); // Short press toggles OLED and periodic Serial status. screenStarted=millis(); } void serviceButton() { unsigned long now=millis(); bool v=digitalRead(USER_BUTTON); if(v!=buttonRaw){buttonRaw=v;buttonChanged=now;} if(now-buttonChanged>=40 && v!=buttonStable){ buttonStable=v; if(v==LOW){buttonPressedAt=now;buttonHoldHandled=false;} else if(!buttonHoldHandled&&displayMode!=2){screen(!screenOn);} } if(buttonStable==LOW && !buttonHoldHandled && now-buttonPressedAt>=5000UL){ buttonHoldHandled=true;startWifiSetup(); } // Mains-powered receiver: display remains on until USER toggles it off. } volatile uint32_t radioIrqs=0; uint32_t handledRadioIrqs=0,radioReadErrors=0,radioCrcErrors=0,radioRearmErrors=0,radioDecoded=0; int16_t radioLastError=RADIOLIB_ERR_NONE; uint32_t logVersion=0; void IRAM_ATTR setFlag() { ++radioIrqs; } String lastTemp = "--"; String lastBatt = ""; String lastTime = "no data yet"; unsigned long lastRxMillis = 0; bool haveRx = false; float latestRSSI=NAN,latestSNR=NAN; String latestRadioNode=""; uint64_t lastRxSeconds=0; const char* receiverResetName() { switch(esp_reset_reason()) { case ESP_RST_POWERON: return "Power-on"; case ESP_RST_EXT: return "External reset"; case ESP_RST_SW: return "Software reset"; case ESP_RST_PANIC: return "Crash / panic"; case ESP_RST_INT_WDT: return "Interrupt watchdog"; case ESP_RST_TASK_WDT: return "Task watchdog"; case ESP_RST_WDT: return "Watchdog"; case ESP_RST_DEEPSLEEP: return "Deep-sleep wake"; case ESP_RST_BROWNOUT: return "Brownout / low supply"; case ESP_RST_SDIO: return "SDIO reset"; default: return "Other / unknown"; } } uint64_t receiverSeconds(){return (uint64_t)esp_timer_get_time()/1000000ULL;} String jsonFloat(float value){return isfinite(value)?String(value,2):String("null");} unsigned long lastDisplayMillis = 0; unsigned long lastWifiCheck = 0; // Wi-Fi onboarding: open only at first setup or after a physical 5-second hold. // Settings live in NVS; this code never formats or clears a log filesystem. struct WifiSettings { uint32_t magic; char ssid[33]; char password[65]; }; constexpr uint32_t WIFI_SETTINGS_MAGIC=0x42475731; constexpr unsigned long SETUP_WINDOW_MS=10UL*60UL*1000UL; constexpr unsigned long CONNECT_TIMEOUT_MS=30000UL; Preferences wifiPreferences; DNSServer setupDNS; WifiSettings savedWifi={},candidateWifi={}; bool preferencesOk=false,haveSavedWifi=false,setupActive=false; bool wifiTrying=false,wifiBeginPending=false,scanBusy=false,scanReady=false; bool setupCloseRequested=false,setupSucceeded=false; unsigned long setupStarted=0,wifiAttemptStarted=0,wifiBeginAt=0,setupCloseAt=0; String setupSSID,setupToken,wifiMessage="Not configured"; bool deadlineReached(unsigned long now,unsigned long deadline){return (long)(now-deadline)>=0;} bool onSetupNetwork(){return setupActive && server.client().localIP()==WiFi.softAPIP();} String jsonText(const String& s){ String out="\""; for(unsigned i=0;i