main.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #include "SensorMesh.h"
  2. #ifdef DISPLAY_CLASS
  3. #include "UITask.h"
  4. static UITask ui_task(display);
  5. #endif
  6. class MyMesh : public SensorMesh {
  7. public:
  8. MyMesh(mesh::MainBoard& board, mesh::Radio& radio, mesh::MillisecondClock& ms, mesh::RNG& rng, mesh::RTCClock& rtc, mesh::MeshTables& tables)
  9. : SensorMesh(board, radio, ms, rng, rtc, tables) { }
  10. protected:
  11. /* ========================== custom alert logic here ========================== */
  12. Trigger low_batt;
  13. void checkForAlerts() override {
  14. alertIfLow(low_batt, getVoltage(TELEM_CHANNEL_SELF), 3.4f, "Battery low!");
  15. // alertIf ...
  16. // alertIf ...
  17. }
  18. /* ============================================================================= */
  19. };
  20. StdRNG fast_rng;
  21. SimpleMeshTables tables;
  22. MyMesh the_mesh(board, radio_driver, *new ArduinoMillis(), fast_rng, rtc_clock, tables);
  23. void halt() {
  24. while (1) ;
  25. }
  26. static char command[80];
  27. void setup() {
  28. Serial.begin(115200);
  29. delay(1000);
  30. board.begin();
  31. #ifdef DISPLAY_CLASS
  32. if (display.begin()) {
  33. display.startFrame();
  34. display.print("Please wait...");
  35. display.endFrame();
  36. }
  37. #endif
  38. if (!radio_init()) { halt(); }
  39. fast_rng.begin(radio_get_rng_seed());
  40. FILESYSTEM* fs;
  41. #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
  42. InternalFS.begin();
  43. fs = &InternalFS;
  44. IdentityStore store(InternalFS, "");
  45. #elif defined(ESP32)
  46. SPIFFS.begin(true);
  47. fs = &SPIFFS;
  48. IdentityStore store(SPIFFS, "/identity");
  49. #elif defined(RP2040_PLATFORM)
  50. LittleFS.begin();
  51. fs = &LittleFS;
  52. IdentityStore store(LittleFS, "/identity");
  53. store.begin();
  54. #else
  55. #error "need to define filesystem"
  56. #endif
  57. if (!store.load("_main", the_mesh.self_id)) {
  58. MESH_DEBUG_PRINTLN("Generating new keypair");
  59. the_mesh.self_id = radio_new_identity(); // create new random identity
  60. int count = 0;
  61. while (count < 10 && (the_mesh.self_id.pub_key[0] == 0x00 || the_mesh.self_id.pub_key[0] == 0xFF)) { // reserved id hashes
  62. the_mesh.self_id = radio_new_identity(); count++;
  63. }
  64. store.save("_main", the_mesh.self_id);
  65. }
  66. Serial.print("Sensor ID: ");
  67. mesh::Utils::printHex(Serial, the_mesh.self_id.pub_key, PUB_KEY_SIZE); Serial.println();
  68. command[0] = 0;
  69. sensors.begin();
  70. the_mesh.begin(fs);
  71. #ifdef DISPLAY_CLASS
  72. ui_task.begin(the_mesh.getNodePrefs(), FIRMWARE_BUILD_DATE, FIRMWARE_VERSION);
  73. #endif
  74. // send out initial Advertisement to the mesh
  75. the_mesh.sendSelfAdvertisement(16000);
  76. }
  77. void loop() {
  78. int len = strlen(command);
  79. while (Serial.available() && len < sizeof(command)-1) {
  80. char c = Serial.read();
  81. if (c != '\n') {
  82. command[len++] = c;
  83. command[len] = 0;
  84. }
  85. Serial.print(c);
  86. }
  87. if (len == sizeof(command)-1) { // command buffer full
  88. command[sizeof(command)-1] = '\r';
  89. }
  90. if (len > 0 && command[len - 1] == '\r') { // received complete line
  91. command[len - 1] = 0; // replace newline with C string null terminator
  92. char reply[160];
  93. the_mesh.getCLI()->handleCommand(0, command, reply); // NOTE: there is no sender_timestamp via serial!
  94. if (reply[0]) {
  95. Serial.print(" -> "); Serial.println(reply);
  96. }
  97. command[0] = 0; // reset command buffer
  98. }
  99. the_mesh.loop();
  100. sensors.loop();
  101. #ifdef DISPLAY_CLASS
  102. ui_task.loop();
  103. #endif
  104. }