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.

141 lines
3.0 KiB

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