main.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include <Arduino.h> // needed for PlatformIO
  2. #include <Mesh.h>
  3. #include "MyMesh.h"
  4. #ifdef DISPLAY_CLASS
  5. #include "UITask.h"
  6. static UITask ui_task(display);
  7. #endif
  8. StdRNG fast_rng;
  9. SimpleMeshTables tables;
  10. MyMesh the_mesh(board, radio_driver, *new ArduinoMillis(), fast_rng, rtc_clock, tables);
  11. void halt() {
  12. while (1) ;
  13. }
  14. static char command[160];
  15. // For power saving
  16. unsigned long lastActive = 0; // mark last active time
  17. unsigned long nextSleepinSecs = 120; // next sleep in seconds. The first sleep (if enabled) is after 2 minutes from boot
  18. #if defined(PIN_USER_BTN) && defined(_SEEED_SENSECAP_SOLAR_H_)
  19. static unsigned long userBtnDownAt = 0;
  20. #define USER_BTN_HOLD_OFF_MILLIS 1500
  21. #endif
  22. void setup() {
  23. Serial.begin(115200);
  24. delay(1000);
  25. board.begin();
  26. #if defined(MESH_DEBUG) && defined(NRF52_PLATFORM)
  27. // give some extra time for serial to settle so
  28. // boot debug messages can be seen on terminal
  29. delay(5000);
  30. #endif
  31. // For power saving
  32. lastActive = millis(); // mark last active time since boot
  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);
  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. int len = strlen(command);
  88. while (Serial.available() && len < sizeof(command)-1) {
  89. char c = Serial.read();
  90. if (c != '\n') {
  91. command[len++] = c;
  92. command[len] = 0;
  93. Serial.print(c);
  94. }
  95. if (c == '\r') break;
  96. }
  97. if (len == sizeof(command)-1) { // command buffer full
  98. command[sizeof(command)-1] = '\r';
  99. }
  100. if (len > 0 && command[len - 1] == '\r') { // received complete line
  101. Serial.print('\n');
  102. command[len - 1] = 0; // replace newline with C string null terminator
  103. char reply[160];
  104. the_mesh.handleCommand(0, command, reply); // NOTE: there is no sender_timestamp via serial!
  105. if (reply[0]) {
  106. Serial.print(" -> "); Serial.println(reply);
  107. }
  108. command[0] = 0; // reset command buffer
  109. }
  110. #if defined(PIN_USER_BTN) && defined(_SEEED_SENSECAP_SOLAR_H_)
  111. // Hold the user button to power off the SenseCAP Solar repeater.
  112. int btnState = digitalRead(PIN_USER_BTN);
  113. if (btnState == LOW) {
  114. if (userBtnDownAt == 0) {
  115. userBtnDownAt = millis();
  116. } else if ((unsigned long)(millis() - userBtnDownAt) >= USER_BTN_HOLD_OFF_MILLIS) {
  117. Serial.println("Powering off...");
  118. board.powerOff(); // does not return
  119. }
  120. } else {
  121. userBtnDownAt = 0;
  122. }
  123. #endif
  124. the_mesh.loop();
  125. sensors.loop();
  126. #ifdef DISPLAY_CLASS
  127. ui_task.loop();
  128. #endif
  129. rtc_clock.tick();
  130. if (the_mesh.getNodePrefs()->powersaving_enabled && !the_mesh.hasPendingWork()) {
  131. #if defined(NRF52_PLATFORM)
  132. board.sleep(1800); // nrf ignores seconds param, sleeps whenever possible
  133. #else
  134. if (the_mesh.millisHasNowPassed(lastActive + nextSleepinSecs * 1000)) { // To check if it is time to sleep
  135. board.sleep(1800); // To sleep. Wake up after 30 minutes or when receiving a LoRa packet
  136. lastActive = millis();
  137. nextSleepinSecs = 5; // Default: To work for 5s and sleep again
  138. } else {
  139. nextSleepinSecs += 5; // When there is pending work, to work another 5s
  140. }
  141. #endif
  142. }
  143. }