main.cpp 4.2 KB

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