main.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #include <Arduino.h> // needed for PlatformIO
  2. #include <Mesh.h>
  3. #include "MyMesh.h"
  4. #include <helpers/ArchiveStorage.h>
  5. #ifdef DISPLAY_CLASS
  6. #include "UITask.h"
  7. static UITask ui_task(display);
  8. #endif
  9. StdRNG fast_rng;
  10. SimpleMeshTables tables;
  11. ArchiveStorage archive;
  12. MyMesh the_mesh(board, radio_driver, *new ArduinoMillis(), fast_rng, rtc_clock, tables);
  13. void halt() {
  14. while (1) ;
  15. }
  16. static char command[160];
  17. // For power saving
  18. unsigned long POWERSAVING_FIRSTSLEEP_SECS = 120; // The first sleep (if enabled) from boot
  19. #if defined(PIN_USER_BTN) && defined(_SEEED_SENSECAP_SOLAR_H_)
  20. static unsigned long userBtnDownAt = 0;
  21. #define USER_BTN_HOLD_OFF_MILLIS 1500
  22. #endif
  23. void setup() {
  24. Serial.begin(115200);
  25. delay(1000);
  26. board.begin();
  27. archive.begin();
  28. #if defined(MESH_DEBUG) && defined(NRF52_PLATFORM)
  29. // give some extra time for serial to settle so
  30. // boot debug messages can be seen on terminal
  31. delay(5000);
  32. #endif
  33. #ifdef DISPLAY_CLASS
  34. if (display.begin()) {
  35. display.startFrame();
  36. display.setCursor(0, 0);
  37. display.print("Please wait...");
  38. display.endFrame();
  39. }
  40. #endif
  41. if (!radio_init()) {
  42. MESH_DEBUG_PRINTLN("Radio init failed!");
  43. halt();
  44. }
  45. fast_rng.begin(radio_driver.getRngSeed());
  46. FILESYSTEM* fs;
  47. #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
  48. InternalFS.begin();
  49. fs = &InternalFS;
  50. IdentityStore store(InternalFS, "");
  51. #elif defined(ESP32)
  52. SPIFFS.begin(true);
  53. fs = &SPIFFS;
  54. IdentityStore store(SPIFFS, "/identity");
  55. #elif defined(RP2040_PLATFORM)
  56. LittleFS.begin();
  57. fs = &LittleFS;
  58. IdentityStore store(LittleFS, "/identity");
  59. store.begin();
  60. #else
  61. #error "need to define filesystem"
  62. #endif
  63. if (!store.load("_main", the_mesh.self_id)) {
  64. MESH_DEBUG_PRINTLN("Generating new keypair");
  65. the_mesh.self_id = radio_new_identity(); // create new random identity
  66. int count = 0;
  67. while (count < 10 && (the_mesh.self_id.pub_key[0] == 0x00 || the_mesh.self_id.pub_key[0] == 0xFF)) { // reserved id hashes
  68. the_mesh.self_id = radio_new_identity(); count++;
  69. }
  70. store.save("_main", the_mesh.self_id);
  71. }
  72. Serial.print("Repeater ID: ");
  73. mesh::Utils::printHex(Serial, the_mesh.self_id.pub_key, PUB_KEY_SIZE); Serial.println();
  74. command[0] = 0;
  75. sensors.begin();
  76. the_mesh.begin(fs, &archive);
  77. #ifdef DISPLAY_CLASS
  78. ui_task.begin(the_mesh.getNodePrefs(), FIRMWARE_BUILD_DATE, FIRMWARE_VERSION);
  79. #endif
  80. // send out initial zero hop Advertisement to the mesh
  81. #if ENABLE_ADVERT_ON_BOOT == 1
  82. the_mesh.sendSelfAdvertisement(16000, false);
  83. #endif
  84. board.onBootComplete();
  85. }
  86. void loop() {
  87. #if defined(TBEAM_1W)
  88. board.updateFanControl();
  89. #endif
  90. int len = strlen(command);
  91. while (Serial.available() && len < sizeof(command)-1) {
  92. char c = Serial.read();
  93. if (c != '\n') {
  94. command[len++] = c;
  95. command[len] = 0;
  96. Serial.print(c);
  97. }
  98. if (c == '\r') break;
  99. }
  100. if (len == sizeof(command)-1) { // command buffer full
  101. command[sizeof(command)-1] = '\r';
  102. }
  103. if (len > 0 && command[len - 1] == '\r') { // received complete line
  104. Serial.print('\n');
  105. command[len - 1] = 0; // replace newline with C string null terminator
  106. char reply[160];
  107. the_mesh.handleCommand(0, command, reply); // NOTE: there is no sender_timestamp via serial!
  108. if (reply[0]) {
  109. Serial.print(" -> "); Serial.println(reply);
  110. }
  111. command[0] = 0; // reset command buffer
  112. }
  113. #if defined(PIN_USER_BTN) && defined(_SEEED_SENSECAP_SOLAR_H_)
  114. // Hold the user button to power off the SenseCAP Solar repeater.
  115. int btnState = digitalRead(PIN_USER_BTN);
  116. if (btnState == LOW) {
  117. if (userBtnDownAt == 0) {
  118. userBtnDownAt = millis();
  119. } else if ((unsigned long)(millis() - userBtnDownAt) >= USER_BTN_HOLD_OFF_MILLIS) {
  120. Serial.println("Powering off...");
  121. board.powerOff(); // does not return
  122. }
  123. } else {
  124. userBtnDownAt = 0;
  125. }
  126. #endif
  127. the_mesh.loop();
  128. sensors.loop();
  129. #ifdef DISPLAY_CLASS
  130. ui_task.loop();
  131. #endif
  132. rtc_clock.tick();
  133. if (the_mesh.getNodePrefs()->powersaving_enabled && !the_mesh.hasPendingWork()) {
  134. #if defined(NRF52_PLATFORM)
  135. board.sleep(0); // nrf ignores seconds param, sleeps whenever possible
  136. #else
  137. if (the_mesh.millisHasNowPassed(POWERSAVING_FIRSTSLEEP_SECS * 1000)) { // To check if it is time to sleep
  138. board.sleep(30); // Sleep. Wake up after a while or when receiving a LoRa packet
  139. }
  140. #endif
  141. }
  142. }