Comme mqttfastled (Effet cylon sur un bandeau de LED contrôlé par HomeAssistant) mais en mieux (tout en 5v, condensateur)
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.

145 lines
3.0 KiB

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