#include // needed for PlatformIO #include #include "MyMesh.h" #if defined(ENABLE_WIFI_INTERFACE) && defined(ENABLE_ESP_MDNS) #define HAS_ESP_MDNS #include #endif // Believe it or not, this std C function is busted on some platforms! static uint32_t _atoi(const char* sp) { uint32_t n = 0; while (*sp && *sp >= '0' && *sp <= '9') { n *= 10; n += (*sp++ - '0'); } return n; } // interface manager #include MultiSerialInterface interface_manager; // include bluetooth interface #if defined(BLE_PIN_CODE) #ifdef ESP32 // include esp32 bluetooth interface #include SerialBLEInterface bluetooth_interface; #elif defined(NRF52_PLATFORM) // include nrf52 bluetooth interface #include SerialBLEInterface bluetooth_interface; #elif defined(RP2040_PLATFORM) // include rp2040 (Pico W / CYW43) bluetooth interface #include SerialBLEInterface bluetooth_interface; #else #error "SerialBLEInterface is not defined for this platform" #endif #endif // include wifi interface #ifdef ENABLE_WIFI_INTERFACE #ifndef WIFI_SSID #define WIFI_SSID "" #endif #ifndef WIFI_PWD #define WIFI_PWD "" #endif #ifndef TCP_PORT #define TCP_PORT 5000 #endif #ifndef WIFI_RETRY_INTERVAL #if defined(RP2040_PLATFORM) #define WIFI_RETRY_INTERVAL 30000 // each attempt blocks loop(), so retry less often #else #define WIFI_RETRY_INTERVAL 10000 // millis between reconnect attempts #endif #endif #ifndef WIFI_RETRY_TIMEOUT #define WIFI_RETRY_TIMEOUT 5000 // RP2040: cap on how long one join may block loop() #endif #if defined(ESP32) || defined(RP2040_PLATFORM) #include SerialWifiInterface wifi_interface; #else #error "SerialWifiInterface is not defined for this platform" #endif #endif // include usb interface #if defined(ENABLE_USB_INTERFACE) #include ArduinoSerialInterface usb_serial_interface; #endif // include ethernet interface #if defined(ETHERNET_ENABLED) #include ETHERNET_CLASS ethernet_interface; #endif // include hardware serial interface #if defined(SERIAL_RX) #include ArduinoSerialInterface hardware_serial_interface; HardwareSerial companion_serial(1); #endif // platform file system #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM) #include #if defined(QSPIFLASH) #include DataStore store(InternalFS, QSPIFlash, rtc_clock); #else #if defined(EXTRAFS) #include CustomLFS ExtraFS(0xD4000, 0x19000, 128); DataStore store(InternalFS, ExtraFS, rtc_clock); #else DataStore store(InternalFS, rtc_clock); #endif #endif #elif defined(RP2040_PLATFORM) #include DataStore store(LittleFS, rtc_clock); #elif defined(ESP32) #include DataStore store(SPIFFS, rtc_clock); #endif /* GLOBAL OBJECTS */ StdRNG fast_rng; SimpleMeshTables tables; MyMesh the_mesh(radio_driver, fast_rng, rtc_clock, tables, store); /* END GLOBAL OBJECTS */ void halt() { while (1) ; } /* WIFI RECONNECT TRACKERS */ #ifdef ENABLE_WIFI_INTERFACE bool wifi_needs_reconnect = false; unsigned long last_wifi_reconnect_attempt = 0; char wifi_ssid[33] = WIFI_SSID; // replaced by stored prefs at boot, if set char wifi_pwd[64] = WIFI_PWD; bool wifi_was_connected = false; bool wifi_enabled = false; // set at boot from prefs; false also when the effective SSID is blank #endif #if defined(DISPLAY_CLASS) && defined(EINK_REFRESH_LOOP) static void loopDuringDisplayRefresh(); #endif void setup() { Serial.begin(115200); board.begin(); #ifdef HAS_EXTERNAL_WATCHDOG external_watchdog.begin(); #endif #ifdef DISPLAY_CLASS DisplayDriver* disp = NULL; if (display.begin()) { disp = &display; disp->startFrame(); #ifdef ST7789 disp->setTextSize(2); #endif disp->drawTextCentered(disp->width() / 2, 28, "Loading..."); disp->endFrame(); } #endif if (!radio_init()) { halt(); } fast_rng.begin(radio_driver.getRngSeed()); #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM) InternalFS.begin(); #if defined(QSPIFLASH) if (!QSPIFlash.begin()) { // debug output might not be available at this point, might be too early. maybe should fall back to InternalFS here? MESH_DEBUG_PRINTLN("CustomLFS_QSPIFlash: failed to initialize"); } else { MESH_DEBUG_PRINTLN("CustomLFS_QSPIFlash: initialized successfully"); } #else #if defined(EXTRAFS) ExtraFS.begin(); #endif #endif store.begin(); the_mesh.begin(); #elif defined(RP2040_PLATFORM) LittleFS.begin(); store.begin(); the_mesh.begin(); #elif defined(ESP32) SPIFFS.begin(true); store.begin(); the_mesh.begin(); #else #error "need to define filesystem" #endif #ifdef BLE_PIN_CODE // 123456 by default if (the_mesh.getNodePrefs()->ble_pin == 0) { #ifdef DISPLAY_CLASS if (disp != NULL && BLE_PIN_CODE == 123456) { StdRNG rng; the_mesh.setBLEPin(rng.nextInt(100000, 999999)); // random pin each session } else { the_mesh.setBLEPin(BLE_PIN_CODE); // otherwise static pin } #else the_mesh.setBLEPin(BLE_PIN_CODE); // otherwise static pin #endif } else { the_mesh.setBLEPin(the_mesh.getNodePrefs()->ble_pin); } #else the_mesh.setBLEPin(0); #endif // add bluetooth interface #if defined(BLE_PIN_CODE) char *prefix = BLE_NAME_PREFIX; char *ble_name = the_mesh.getNodePrefs()->node_name; if (the_mesh.getNodePrefs()->ble_name[0]) { prefix = ""; ble_name = the_mesh.getNodePrefs()->ble_name; } bluetooth_interface.begin((const char *) prefix, ble_name, the_mesh.getBLEPin()); interface_manager.addInterface(InterfaceType::Bluetooth, &bluetooth_interface); #endif // add wifi interface #ifdef ENABLE_WIFI_INTERFACE // use wifi ssid and password from prefs if ssid is not empty, otherwise use the build flag defaults if (the_mesh.getNodePrefs()->wifi_ssid[0]) { strcpy(wifi_ssid, the_mesh.getNodePrefs()->wifi_ssid); strcpy(wifi_pwd, the_mesh.getNodePrefs()->wifi_pwd); } // only start wifi if enabled and ssid is not empty wifi_enabled = the_mesh.getNodePrefs()->wifi_enabled; if (wifi_enabled && wifi_ssid[0]) { #if defined(ESP32) board.setInhibitSleep(true); // prevent sleep when WiFi is active WiFi.setAutoReconnect(true); WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info){ if (event == ARDUINO_EVENT_WIFI_STA_DISCONNECTED) { WIFI_DEBUG_PRINTLN("WiFi disconnected (reason=%s). Flagging for reconnect...", WiFi.disconnectReasonName((wifi_err_reason_t)info.wifi_sta_disconnected.reason)); wifi_needs_reconnect = true; } else if (event == ARDUINO_EVENT_WIFI_STA_GOT_IP) { WIFI_DEBUG_PRINTLN("WiFi connected successfully!"); wifi_needs_reconnect = false; } }); #endif WIFI_DEBUG_PRINTLN("connecting to %s", wifi_ssid); #if defined(RP2040_PLATFORM) // the join itself blocks inside the core (CYW43::begin busy-waits for the // association), so every attempt stalls the mesh loop. beginNoBlock() only skips the // extra DHCP wait. Give the first connect a full window, then bound the retries below. WiFi.beginNoBlock(wifi_ssid, wifi_pwd); last_wifi_reconnect_attempt = millis(); // let DHCP finish before the poll can retry #else WiFi.begin(wifi_ssid, wifi_pwd); #endif wifi_interface.begin(TCP_PORT); interface_manager.addInterface(InterfaceType::WiFi, &wifi_interface); } else { WIFI_DEBUG_PRINTLN("wifi disabled"); } #endif // add usb interface #if defined(ENABLE_USB_INTERFACE) usb_serial_interface.begin(Serial); interface_manager.addInterface(InterfaceType::USB, &usb_serial_interface); #endif // add ethernet interface #if defined(ETHERNET_ENABLED) ethernet_interface.begin(); interface_manager.addInterface(InterfaceType::Ethernet, ðernet_interface); #endif #ifdef HAS_ESP_MDNS const char* nodeName = the_mesh.getNodeName(); // "meshcore_" prefix + sanitized node name, capped at 63 chars (max DNS label length) char mdnsName[64]; const size_t prefixLen = snprintf(mdnsName, sizeof(mdnsName), "meshcore_"); size_t pos = prefixLen; for (size_t i = 0; nodeName[i] != '\0' && pos < sizeof(mdnsName) - 1; ++i) { const char c = nodeName[i]; if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '-' || c == '_') { mdnsName[pos++] = c; } } mdnsName[pos] = '\0'; if (!MDNS.begin(mdnsName)) { WIFI_DEBUG_PRINTLN("mDNS failed to start!"); } else { MDNS.addService("meshcore", "tcp", TCP_PORT); MDNS.addServiceTxt("meshcore", "tcp", "node-name", nodeName); MDNS.addServiceTxt("meshcore", "tcp", "firmware-version", FIRMWARE_VERSION); char protocol[12]; snprintf(protocol, sizeof(protocol), "%d", (int)FIRMWARE_VER_CODE); MDNS.addServiceTxt("meshcore", "tcp", "protocol-version", (const char*)protocol); MDNS.addServiceTxt("meshcore", "tcp", "build-date", __DATE__); MDNS.addServiceTxt("meshcore", "tcp", "build-time", __TIME__); } #endif // add hardware serial interface #if defined(SERIAL_RX) companion_serial.setPins(SERIAL_RX, SERIAL_TX); companion_serial.begin(115200); hardware_serial_interface.begin(companion_serial); interface_manager.addInterface(InterfaceType::HardwareSerial, &hardware_serial_interface); #endif the_mesh.startInterface(interface_manager); sensors.begin(); #if ENV_INCLUDE_GPS == 1 the_mesh.applyGpsPrefs(); #endif #ifdef DISPLAY_CLASS ui_task.begin(disp, &sensors, the_mesh.getNodePrefs(), &interface_manager, &the_mesh, the_mesh.getBLEPin(), FIRMWARE_VERSION, FIRMWARE_BUILD_DATE); the_mesh.setListener(&ui_task); #ifdef EINK_REFRESH_LOOP if (disp != NULL) disp->setBusyHook(loopDuringDisplayRefresh); #endif #endif board.onBootComplete(); } // Work that must keep running even while the UI is blocked (e.g. by an e-ink refresh). // Called from loop() and from the display busy hook, so both always run the same set. static void loopCore() { the_mesh.loop(); interface_manager.loop(); sensors.loop(); } #if defined(DISPLAY_CLASS) && defined(EINK_REFRESH_LOOP) // Runs from inside the e-ink busy wait, so radio/BLE/sensors keep going during a panel refresh. // Must not touch the display: UI listener callbacks only update state and schedule the next frame. static void loopDuringDisplayRefresh() { static bool active = false; if (active) return; active = true; loopCore(); active = false; } #endif void loop() { loopCore(); #ifdef DISPLAY_CLASS ui_task.loop(); #endif rtc_clock.tick(); board.loop(); #ifdef HAS_EXTERNAL_WATCHDOG external_watchdog.loop(); #endif if (!the_mesh.hasPendingWork()) { #if defined(NRF52_PLATFORM) board.sleep(0); // nrf ignores seconds param, sleeps whenever possible #endif } #ifdef ENABLE_WIFI_INTERFACE if (wifi_enabled) { // RP2040 has no WiFi event callbacks, so poll the link state instead #if defined(RP2040_PLATFORM) wifi_needs_reconnect = (WiFi.status() != WL_CONNECTED); if (wifi_was_connected == wifi_needs_reconnect) { // link state changed wifi_was_connected = !wifi_needs_reconnect; if (wifi_was_connected) { WIFI_DEBUG_PRINTLN("connected, listening on %s:%d", WiFi.localIP().toString().c_str(), TCP_PORT); } else { WIFI_DEBUG_PRINTLN("link lost"); } } #endif // Safely attempt to reconnect if flagged. On RP2040 each attempt blocks the mesh loop // for up to WIFI_RETRY_TIMEOUT, so retry less often and cap how long a join may stall. if (wifi_needs_reconnect && (millis() - last_wifi_reconnect_attempt > WIFI_RETRY_INTERVAL)) { WIFI_DEBUG_PRINTLN("Attempting manual WiFi reconnect to %s (status %d)...", wifi_ssid, WiFi.status()); #if defined(RP2040_PLATFORM) WiFi.setTimeout(WIFI_RETRY_TIMEOUT); WiFi.beginNoBlock(wifi_ssid, wifi_pwd); // no reconnect() on this platform #else WiFi.disconnect(); WiFi.reconnect(); #endif last_wifi_reconnect_attempt = millis(); } } #endif }