CRV2.ino 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include <Arduino.h>
  2. #include <stdio.h>
  3. #include <esp_now.h>
  4. #include <WiFi.h>
  5. #include "buzzer.h"
  6. #include "coaster_switch.h"
  7. #include "crv2_esp_now.h"
  8. #include "configuration.h"
  9. #include "credentials.h"
  10. #include "display.h"
  11. #include "neighbors.h"
  12. #include "ota.h"
  13. #include "led_ring.h"
  14. Neighbor_List NList;
  15. void setup() {
  16. Serial.begin(115200);
  17. Serial.println("Welcome to the CRV2!");
  18. +
  19. Serial.println("Here's what I know about myself:");
  20. Serial.print(" ESP_NOW_MAX_DATA_LEN = "); Serial.println(ESP_NOW_MAX_DATA_LEN);
  21. // Initialize Display
  22. Serial.println("Initializing display...");
  23. display_setup();
  24. // Initialize leds first to turn of pwm
  25. Serial.println("Initializing LEDs...");
  26. led_setup();
  27. led_setall(255, 0, 0); // Start at red
  28. Serial.println("Initializing Buzzer...");
  29. buzzer_setup();
  30. beep(); delay(200); beep(); delay(200); beep(); delay(200);
  31. //Set device in STA mode to begin with
  32. Serial.println("Initializing WiFi...");
  33. #ifdef WIFI_CONNECT
  34. ota_setup();
  35. #else
  36. WiFi.mode(WIFI_STA);
  37. #endif
  38. // This is the mac address of the Master in Station Mode
  39. Serial.print("WiFi Channel: "); Serial.println(WiFi.channel());
  40. Serial.print("STA MAC: "); Serial.println(WiFi.macAddress());
  41. Serial.println("Initializing ESP Now");
  42. if (esp_now_init() == ESP_OK) {
  43. Serial.println("ESPNow Init Success");
  44. }
  45. else {
  46. Serial.println("ESPNow Init Failed");
  47. ESP.restart();
  48. }
  49. esp_now_register_send_cb(OnDataSent);
  50. esp_now_register_recv_cb(OnDataRecv);
  51. Serial.println("Initializing coaster...");
  52. coaster_setup();
  53. // Initializing broadcast
  54. initBroadcast();
  55. led_setall(0, 0, 255); // Blue after initialization
  56. }
  57. void loop() {
  58. receive_buffer_t *incoming;
  59. if(WIFI_Enabled) ota_handle(); // handle OTA updates on if enabled and connected successfully
  60. while( (incoming = get_message()) != NULL ) {
  61. process_message(*incoming);
  62. }
  63. NList.beacon();
  64. if(millis() % 1000 == 0 && DEBUG) {
  65. Serial.print("Debug: Coaster Value: "); Serial.println(coaster_current_value());
  66. }
  67. if(millis() % 5000 == 0 && DEBUG) {
  68. Serial.print("Debug: Neighbor List: "); NList.print_list();
  69. }
  70. //if(NList.ready()) {
  71. // led_setall(0, 255, 0); // Green if we're good
  72. //} else {
  73. // led_setall(0, 0, 255); // Blue for determining
  74. //}
  75. //buzz(); delay(200);
  76. //buzz(); delay(200);
  77. //buzz(); delay(200);
  78. //led_examples(); // testing
  79. }