Effet cylon sur un bandeau de LED contrôlé par HomeAssistant
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
3.0 KiB

3 years ago
  1. #include "mqttfastled.local.h"
  2. #include "mqttfastled.h"
  3. #define FASTLED_ESP8266_NODEMCU_PIN_ORDER
  4. #include <FastLED.h>
  5. #include <ESP8266WiFi.h>
  6. #include <PubSubClient.h>
  7. // LED
  8. CRGB leds[NUM_LEDS];
  9. bool ledGo = false;
  10. // WIFI
  11. WiFiClient espClient;
  12. // MQTT
  13. char message_buff[100];
  14. PubSubClient client(espClient);
  15. void setup() {
  16. Serial.begin(115200);
  17. Serial.println("\nresetting");
  18. // WIFI
  19. setupWifi();
  20. // MQTT
  21. client.setServer(mqtt_server, mqtt_port);
  22. client.setCallback(callbackMQTT);
  23. testConnectMQTT();
  24. client.subscribe("homeassistant/switch1");
  25. // LED
  26. LEDS.addLeds<CHIPSET,LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
  27. FastLED.setBrightness(BRIGHTNESS);
  28. fill_solid(leds, NUM_LEDS, CRGB::Black);
  29. FastLED.show();
  30. }
  31. // WIFI
  32. void setupWifi() {
  33. Serial.println();
  34. Serial.print("Connexion a ");
  35. Serial.print(wifi_ssid);
  36. WiFi.begin(wifi_ssid, wifi_password);
  37. while (WiFi.status() != WL_CONNECTED) {
  38. delay(500);
  39. Serial.print(".");
  40. }
  41. Serial.println();
  42. Serial.print("IP : ");
  43. Serial.println(WiFi.localIP());
  44. }
  45. // MQTT
  46. void testConnectMQTT() {
  47. while (!client.connected()) {
  48. Serial.print("Connexion au serveur MQTT... ");
  49. if (client.connect("ESP8266Client", mqtt_user, mqtt_password)) {
  50. Serial.println("OK");
  51. } else {
  52. Serial.print("KO, erreur : ");
  53. Serial.print(client.state());
  54. Serial.println(", on attend 5 secondes avant de recommencer");
  55. delay(5000);
  56. }
  57. }
  58. }
  59. // Déclenche les actions à la réception d'un message
  60. void callbackMQTT(char* topic, byte* payload, unsigned int length) {
  61. unsigned int i = 0;
  62. Serial.print("Received [" + String(topic) + "] : ");
  63. // create character buffer with ending null terminator (string)
  64. for(i = 0; i < length; i++) {
  65. message_buff[i] = payload[i];
  66. }
  67. message_buff[i] = '\0';
  68. String msgString = String(message_buff);
  69. Serial.println(msgString);
  70. if (msgString == "ON") {
  71. ledGo = true;
  72. } else {
  73. ledGo = false;
  74. }
  75. }
  76. // LED
  77. void ledCylon()
  78. {
  79. // Effet cylon : on allume une led, on attends, on eteinds, on passe à la suivante
  80. for(int i = 0; i < NUM_LEDS; i++) {
  81. if ((i % 10) == 0) {
  82. client.loop();
  83. }
  84. leds[i] = CRGB::Red;
  85. FastLED.show();
  86. FastLED.delay(1000 / FRAMES_PER_SECOND);
  87. leds[i] = CRGB::Black;
  88. FastLED.show();
  89. FastLED.delay(1000 / FRAMES_PER_SECOND);
  90. }
  91. for(int i = NUM_LEDS - 1; i > 0; i--) {
  92. if ((i % 10) == 0) {
  93. client.loop();
  94. }
  95. leds[i] = CRGB::Red;
  96. FastLED.show();
  97. FastLED.delay(1000 / FRAMES_PER_SECOND);
  98. leds[i] = CRGB::Black;
  99. FastLED.show();
  100. }
  101. FastLED.delay(1000 / FRAMES_PER_SECOND);
  102. }
  103. void loop() {
  104. // MQTT
  105. testConnectMQTT();
  106. client.loop();
  107. EVERY_N_SECONDS(180) {
  108. Serial.print("MQTT Subscribe refresh");
  109. client.subscribe("homeassistant/select1");
  110. Serial.println(" done");
  111. }
  112. // LED
  113. if (ledGo) {
  114. ledCylon();
  115. } else {
  116. fill_solid(leds, NUM_LEDS, CRGB::Black);
  117. FastLED.show();
  118. FastLED.delay(1000 / FRAMES_PER_SECOND);
  119. }
  120. }