main.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. void setup() {
  16. Serial.begin(115200);
  17. delay(1000);
  18. board.begin();
  19. #ifdef DISPLAY_CLASS
  20. if (display.begin()) {
  21. display.startFrame();
  22. display.setCursor(0, 0);
  23. display.print("Please wait...");
  24. display.endFrame();
  25. }
  26. #endif
  27. if (!radio_init()) { halt(); }
  28. fast_rng.begin(radio_get_rng_seed());
  29. FILESYSTEM* fs;
  30. #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
  31. InternalFS.begin();
  32. fs = &InternalFS;
  33. IdentityStore store(InternalFS, "");
  34. #elif defined(ESP32)
  35. SPIFFS.begin(true);
  36. fs = &SPIFFS;
  37. IdentityStore store(SPIFFS, "/identity");
  38. #elif defined(RP2040_PLATFORM)
  39. LittleFS.begin();
  40. fs = &LittleFS;
  41. IdentityStore store(LittleFS, "/identity");
  42. store.begin();
  43. #else
  44. #error "need to define filesystem"
  45. #endif
  46. if (!store.load("_main", the_mesh.self_id)) {
  47. MESH_DEBUG_PRINTLN("Generating new keypair");
  48. the_mesh.self_id = radio_new_identity(); // create new random identity
  49. int count = 0;
  50. while (count < 10 && (the_mesh.self_id.pub_key[0] == 0x00 || the_mesh.self_id.pub_key[0] == 0xFF)) { // reserved id hashes
  51. the_mesh.self_id = radio_new_identity(); count++;
  52. }
  53. store.save("_main", the_mesh.self_id);
  54. }
  55. Serial.print("Repeater ID: ");
  56. mesh::Utils::printHex(Serial, the_mesh.self_id.pub_key, PUB_KEY_SIZE); Serial.println();
  57. command[0] = 0;
  58. sensors.begin();
  59. the_mesh.begin(fs);
  60. #ifdef DISPLAY_CLASS
  61. ui_task.begin(the_mesh.getNodePrefs(), FIRMWARE_BUILD_DATE, FIRMWARE_VERSION);
  62. #endif
  63. // send out initial Advertisement to the mesh
  64. the_mesh.sendSelfAdvertisement(16000);
  65. }
  66. void loop() {
  67. int len = strlen(command);
  68. while (Serial.available() && len < sizeof(command)-1) {
  69. char c = Serial.read();
  70. if (c != '\n') {
  71. command[len++] = c;
  72. command[len] = 0;
  73. }
  74. Serial.print(c);
  75. }
  76. if (len == sizeof(command)-1) { // command buffer full
  77. command[sizeof(command)-1] = '\r';
  78. }
  79. if (len > 0 && command[len - 1] == '\r') { // received complete line
  80. command[len - 1] = 0; // replace newline with C string null terminator
  81. char reply[160];
  82. the_mesh.handleCommand(0, command, reply); // NOTE: there is no sender_timestamp via serial!
  83. if (reply[0]) {
  84. Serial.print(" -> "); Serial.println(reply);
  85. }
  86. command[0] = 0; // reset command buffer
  87. }
  88. the_mesh.loop();
  89. sensors.loop();
  90. #ifdef DISPLAY_CLASS
  91. ui_task.loop();
  92. #endif
  93. }