main.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. void setup() {
  19. Serial.begin(115200);
  20. delay(1000);
  21. board.begin();
  22. #if defined(MESH_DEBUG) && defined(NRF52_PLATFORM)
  23. // give some extra time for serial to settle so
  24. // boot debug messages can be seen on terminal
  25. delay(5000);
  26. #endif
  27. // For power saving
  28. lastActive = millis(); // mark last active time since boot
  29. #ifdef DISPLAY_CLASS
  30. if (display.begin()) {
  31. display.startFrame();
  32. display.setCursor(0, 0);
  33. display.print("Please wait...");
  34. display.endFrame();
  35. }
  36. #endif
  37. if (!radio_init()) {
  38. MESH_DEBUG_PRINTLN("Radio init failed!");
  39. halt();
  40. }
  41. fast_rng.begin(radio_get_rng_seed());
  42. FILESYSTEM* fs;
  43. #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
  44. InternalFS.begin();
  45. fs = &InternalFS;
  46. IdentityStore store(InternalFS, "");
  47. #elif defined(ESP32)
  48. SPIFFS.begin(true);
  49. fs = &SPIFFS;
  50. IdentityStore store(SPIFFS, "/identity");
  51. #elif defined(RP2040_PLATFORM)
  52. LittleFS.begin();
  53. fs = &LittleFS;
  54. IdentityStore store(LittleFS, "/identity");
  55. store.begin();
  56. #else
  57. #error "need to define filesystem"
  58. #endif
  59. if (!store.load("_main", the_mesh.self_id)) {
  60. MESH_DEBUG_PRINTLN("Generating new keypair");
  61. the_mesh.self_id = radio_new_identity(); // create new random identity
  62. int count = 0;
  63. while (count < 10 && (the_mesh.self_id.pub_key[0] == 0x00 || the_mesh.self_id.pub_key[0] == 0xFF)) { // reserved id hashes
  64. the_mesh.self_id = radio_new_identity(); count++;
  65. }
  66. store.save("_main", the_mesh.self_id);
  67. }
  68. Serial.print("Repeater ID: ");
  69. mesh::Utils::printHex(Serial, the_mesh.self_id.pub_key, PUB_KEY_SIZE); Serial.println();
  70. command[0] = 0;
  71. sensors.begin();
  72. the_mesh.begin(fs);
  73. #ifdef DISPLAY_CLASS
  74. ui_task.begin(the_mesh.getNodePrefs(), FIRMWARE_BUILD_DATE, FIRMWARE_VERSION);
  75. #endif
  76. // send out initial zero hop Advertisement to the mesh
  77. #if ENABLE_ADVERT_ON_BOOT == 1
  78. the_mesh.sendSelfAdvertisement(16000, false);
  79. #endif
  80. }
  81. void loop() {
  82. int len = strlen(command);
  83. while (Serial.available() && len < sizeof(command)-1) {
  84. char c = Serial.read();
  85. if (c != '\n') {
  86. command[len++] = c;
  87. command[len] = 0;
  88. Serial.print(c);
  89. }
  90. if (c == '\r') break;
  91. }
  92. if (len == sizeof(command)-1) { // command buffer full
  93. command[sizeof(command)-1] = '\r';
  94. }
  95. if (len > 0 && command[len - 1] == '\r') { // received complete line
  96. Serial.print('\n');
  97. command[len - 1] = 0; // replace newline with C string null terminator
  98. char reply[160];
  99. the_mesh.handleCommand(0, command, reply); // NOTE: there is no sender_timestamp via serial!
  100. if (reply[0]) {
  101. Serial.print(" -> "); Serial.println(reply);
  102. }
  103. command[0] = 0; // reset command buffer
  104. }
  105. the_mesh.loop();
  106. sensors.loop();
  107. #ifdef DISPLAY_CLASS
  108. ui_task.loop();
  109. #endif
  110. rtc_clock.tick();
  111. if (the_mesh.getNodePrefs()->powersaving_enabled && // To check if power saving is enabled
  112. the_mesh.millisHasNowPassed(lastActive + nextSleepinSecs * 1000)) { // To check if it is time to sleep
  113. if (!the_mesh.hasPendingWork()) { // No pending work. Safe to sleep
  114. board.sleep(1800); // To sleep. Wake up after 30 minutes or when receiving a LoRa packet
  115. lastActive = millis();
  116. nextSleepinSecs = 5; // Default: To work for 5s and sleep again
  117. } else {
  118. nextSleepinSecs += 5; // When there is pending work, to work another 5s
  119. }
  120. }
  121. }