|
|
|
@ -1,3 +1,5 @@ |
|
|
|
#include <Arduino.h>
|
|
|
|
|
|
|
|
#include "mqttfastledmenu.h"
|
|
|
|
|
|
|
|
#include <FastLED.h>
|
|
|
|
@ -20,27 +22,25 @@ char message_buff[100]; |
|
|
|
PubSubClient client(espClient); |
|
|
|
|
|
|
|
void setup() |
|
|
|
{ |
|
|
|
{ |
|
|
|
Serial.begin(SERIAL_SPEED); |
|
|
|
Serial.println("\nresetting"); |
|
|
|
|
|
|
|
|
|
|
|
// WIFI
|
|
|
|
setupWifi(); |
|
|
|
|
|
|
|
|
|
|
|
// MQTT
|
|
|
|
client.setServer(MQTT_SERVER, MQTT_PORT); |
|
|
|
client.setCallback(callbackMQTT); |
|
|
|
testConnectMQTT(); |
|
|
|
client.subscribe(MQTT_LED_COMMAND); |
|
|
|
client.subscribe(MQTT_LED_EFFECT_COMMAND); |
|
|
|
client.subscribe(MQTT_LED_BRIGHTNESS_COMMAND); |
|
|
|
client.subscribe(MQTT_LED_SPEED_COMMAND); |
|
|
|
client.subscribe(MQTT_LED_COLOR_COMMAND); |
|
|
|
|
|
|
|
// TODO : ne marche pas comme je le désire :
|
|
|
|
// au boot il prends les params par défaut, j'aimerais ceux de home assistant
|
|
|
|
|
|
|
|
// LED
|
|
|
|
LEDS.addLeds<LED_CHIPSET,LED_PIN, LED_COLOR_ORDER>(leds, LED_NUM).setCorrection(TypicalSMD5050); |
|
|
|
ledBlackAll(); |
|
|
|
FastLED.setBrightness(brightness); |
|
|
|
Serial.println("Ready"); |
|
|
|
} |
|
|
|
|
|
|
|
// WIFI
|
|
|
|
@ -50,13 +50,12 @@ void setupWifi() |
|
|
|
Serial.print(WIFI_SSID); |
|
|
|
WiFi.mode(WIFI_STA); |
|
|
|
WiFi.begin(WIFI_SSID, WIFI_PASSWORD); |
|
|
|
|
|
|
|
|
|
|
|
while (WiFi.status() != WL_CONNECTED) { |
|
|
|
delay(500); |
|
|
|
Serial.print("."); |
|
|
|
} |
|
|
|
|
|
|
|
Serial.println(); |
|
|
|
Serial.println(" OK"); |
|
|
|
Serial.print("IP : "); |
|
|
|
Serial.println(WiFi.localIP()); |
|
|
|
} |
|
|
|
@ -67,12 +66,13 @@ void testConnectMQTT() |
|
|
|
while (!client.connected()) { |
|
|
|
Serial.print("Connexion au serveur MQTT... "); |
|
|
|
if (client.connect("ESP8266Client", MQTT_USER, MQTT_PASS)) { |
|
|
|
Serial.println("OK"); |
|
|
|
Serial.print("OK\nSubscribe"); |
|
|
|
client.subscribe(MQTT_LED_COMMAND); |
|
|
|
client.subscribe(MQTT_LED_EFFECT_COMMAND); |
|
|
|
client.subscribe(MQTT_LED_BRIGHTNESS_COMMAND); |
|
|
|
client.subscribe(MQTT_LED_SPEED_COMMAND); |
|
|
|
client.subscribe(MQTT_LED_COLOR_COMMAND); |
|
|
|
Serial.println(" OK"); |
|
|
|
} else { |
|
|
|
Serial.print("KO, erreur : "); |
|
|
|
Serial.print(client.state()); |
|
|
|
@ -93,7 +93,7 @@ void callbackMQTT(char* topic, byte* payload, unsigned int length) |
|
|
|
} |
|
|
|
message_buff[i] = '\0'; |
|
|
|
String msgString = String(message_buff); |
|
|
|
|
|
|
|
|
|
|
|
Serial.print("Received [" + stopic + "] : "); |
|
|
|
Serial.println(msgString); |
|
|
|
|
|
|
|
@ -103,7 +103,8 @@ void callbackMQTT(char* topic, byte* payload, unsigned int length) |
|
|
|
client.publish(MQTT_LED_STATE, message_buff, true); |
|
|
|
} else { |
|
|
|
ledState = false; |
|
|
|
client.publish(MQTT_LED_STATE, message_buff, true); |
|
|
|
ledBlackAll(); |
|
|
|
client.publish(MQTT_LED_STATE, message_buff, false); |
|
|
|
} |
|
|
|
} else if (stopic == MQTT_LED_EFFECT_COMMAND) { |
|
|
|
// Si on ne repasse pas tout à noir, cela peut faire des effets surprenants
|
|
|
|
@ -116,7 +117,12 @@ void callbackMQTT(char* topic, byte* payload, unsigned int length) |
|
|
|
FastLED.setBrightness(brightness); |
|
|
|
client.publish(MQTT_LED_BRIGHTNESS_STATE, message_buff, true); |
|
|
|
} else if (stopic == MQTT_LED_COLOR_COMMAND) { |
|
|
|
color = msgString.toInt(); |
|
|
|
// Sample : 134,168,255
|
|
|
|
int red = msgString.substring(0, msgString.indexOf(',')).toInt(); |
|
|
|
int green = msgString.substring(msgString.indexOf(',') + 1, msgString.lastIndexOf(',')).toInt(); |
|
|
|
int blue = msgString.substring(msgString.lastIndexOf(',') + 1).toInt(); |
|
|
|
|
|
|
|
color=((red <<16)|(green <<8)|blue); |
|
|
|
client.publish(MQTT_LED_COLOR_STATE, message_buff, true); |
|
|
|
} else if (stopic == MQTT_LED_SPEED_COMMAND) { |
|
|
|
speed = msgString.toInt(); |
|
|
|
@ -129,39 +135,35 @@ void ledBlackAll() |
|
|
|
{ |
|
|
|
FastLED.clear(); |
|
|
|
FastLED.show(); |
|
|
|
FastLED.delay(1000 / speed); |
|
|
|
} |
|
|
|
|
|
|
|
void ledCylon() |
|
|
|
{ |
|
|
|
// Effet cylon : on allume une led, on attends, on eteinds, on passe à la suivante
|
|
|
|
// TODO : trop d'attente entre les clients.loop !!!!
|
|
|
|
for(int i = 0; i < LED_NUM; i++) { |
|
|
|
if ((i % 10) == 0) { |
|
|
|
client.loop(); |
|
|
|
if (ledEffect != LED_EFFECT_CYLON) { |
|
|
|
return; |
|
|
|
} |
|
|
|
EVERY_N_SECONDS(1) { |
|
|
|
client.loop(); |
|
|
|
if (ledEffect != LED_EFFECT_CYLON) { |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
leds[i] = color; |
|
|
|
FastLED.show(); |
|
|
|
FastLED.delay(1000 / speed); |
|
|
|
FastLED.delay(1000 / speed); |
|
|
|
leds[i] = CRGB::Black; |
|
|
|
FastLED.show(); |
|
|
|
FastLED.delay(1000 / speed); |
|
|
|
} |
|
|
|
|
|
|
|
for(int i = LED_NUM - 1; i > 0; i--) { |
|
|
|
if ((i % 10) == 0) { |
|
|
|
client.loop(); |
|
|
|
if (ledEffect != LED_EFFECT_CYLON) { |
|
|
|
return; |
|
|
|
} |
|
|
|
EVERY_N_SECONDS(1) { |
|
|
|
client.loop(); |
|
|
|
if (ledEffect != LED_EFFECT_CYLON) { |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
leds[i] = color; |
|
|
|
FastLED.show(); |
|
|
|
FastLED.delay(1000 / speed); |
|
|
|
FastLED.delay(1000 / speed); |
|
|
|
leds[i] = CRGB::Black; |
|
|
|
FastLED.show(); |
|
|
|
} |
|
|
|
@ -178,46 +180,30 @@ void ledError() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
FastLED.show(); |
|
|
|
FastLED.delay(1000 / speed); |
|
|
|
FastLED.delay(1000 / speed); |
|
|
|
} |
|
|
|
|
|
|
|
// TODO : ne doit pas rester à terme, ça ne sert à rien de garder une fonction comme ça
|
|
|
|
void ledFullRed() |
|
|
|
void ledFullColor() |
|
|
|
{ |
|
|
|
fill_solid(leds, LED_NUM, color); |
|
|
|
FastLED.show(); |
|
|
|
FastLED.delay(1000 / speed); |
|
|
|
FastLED.delay(1000 / speed); |
|
|
|
} |
|
|
|
|
|
|
|
void loop() { |
|
|
|
// MQTT
|
|
|
|
testConnectMQTT(); |
|
|
|
client.loop(); |
|
|
|
|
|
|
|
// TODO : à retirer je pense
|
|
|
|
//EVERY_N_SECONDS(180) {
|
|
|
|
// Serial.print("MQTT Subscribe refresh");
|
|
|
|
// client.subscribe(MQTT_LED_EFFECT_COMMAND);
|
|
|
|
// client.subscribe(MQTT_LED_BRIGHTNESS_COMMAND);
|
|
|
|
// client.subscribe(MQTT_LED_COLOR_COMMAND);
|
|
|
|
// client.subscribe(MQTT_LED_SPEED_COMMAND);
|
|
|
|
// Serial.println(" done");
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
// LED
|
|
|
|
if (!ledState) { |
|
|
|
ledBlackAll(); |
|
|
|
FastLED.delay(1000); |
|
|
|
FastLED.delay(1000); |
|
|
|
} else { |
|
|
|
if (ledEffect == LED_EFFECT_CYLON) { |
|
|
|
ledCylon(); |
|
|
|
} else if (ledEffect == LED_EFFECT_FULLRED) { |
|
|
|
ledFullRed(); |
|
|
|
ledFullColor(); |
|
|
|
} else { |
|
|
|
ledError(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// TODO : regrouper input et select en un seul group, l'input enverrait directement Off et les effets ne seraient que effets
|