@ -1,12 +1,25 @@
# include <Arduino.h>
// LED
// TODO : essayer, devrait limiter le flikering
//#define FASTLED_ALLOW_INTERRUPTS 0
# define FASTLED_ESP8266_NODEMCU_PIN_ORDER
# include <FastLED.h>
// WIFI
# include <ESP8266WiFi.h>
// MQTT
# include <PubSubClient.h>
// OTA
# include <ESP8266mDNS.h>
# include <WiFiUdp.h>
# include <ArduinoOTA.h>
// RemoteDebug
# include <RemoteDebug.h>
# include "mqttfastledmenu.h"
// LED
@ -33,6 +46,12 @@ void setup()
// WIFI
setupWifi ( ) ;
// OTA
setupOTA ( ) ;
// RemoteDebug
Debug . begin ( " chamber " ) ;
// LED
/*
brightness = LED_BRIGHTNESS_DEFAULT ;
@ -56,7 +75,7 @@ void setup()
client . setCallback ( callbackMQTT ) ;
testConnectMQTT ( ) ;
Serial . println ( " Ready " ) ;
Debug . println ( " Ready " ) ;
/* MQTT
* Il est important de faire un loop avant toute chose ,
@ -69,7 +88,47 @@ void setup()
client . loop ( ) ;
}
Serial . println ( " End of setup " ) ;
Debug . println ( " End of setup " ) ;
}
// OTA
void setupOTA ( )
{
ArduinoOTA . setHostname ( " mqttfastledmenu " ) ; // on donne une petit nom a notre module
ArduinoOTA . setPassword ( OTA_PASSWORD ) ;
ArduinoOTA . onStart ( [ ] ( ) {
Debug . println ( " OTA Starting " ) ;
Serial . println ( " OTA Starting " ) ;
} ) ;
ArduinoOTA . onEnd ( [ ] ( ) {
Debug . println ( " \n OTA End " ) ;
Serial . println ( " \n OTA End " ) ;
} ) ;
ArduinoOTA . onProgress ( [ ] ( unsigned int progress , unsigned int total ) {
Debug . printf ( " OTA Progress: %u%% \r " , ( progress / ( total / 100 ) ) ) ;
Serial . printf ( " OTA Progress: %u%% \r " , ( progress / ( total / 100 ) ) ) ;
} ) ;
ArduinoOTA . onError ( [ ] ( ota_error_t error ) {
Serial . printf ( " OTA Error[%u]: " , error ) ;
Debug . printf ( " OTA Error[%u]: " , error ) ;
if ( error = = OTA_AUTH_ERROR ) {
Serial . println ( " Auth Failed " ) ;
Debug . println ( " Auth Failed " ) ;
} else if ( error = = OTA_BEGIN_ERROR ) {
Serial . println ( " Begin Failed " ) ;
Debug . println ( " Begin Failed " ) ;
} else if ( error = = OTA_CONNECT_ERROR ) {
Serial . println ( " Connect Failed " ) ;
Debug . println ( " Connect Failed " ) ;
} else if ( error = = OTA_RECEIVE_ERROR ) {
Serial . println ( " Receive Failed " ) ;
Debug . println ( " Receive Failed " ) ;
} else if ( error = = OTA_END_ERROR ) {
Serial . println ( " End Failed " ) ;
Debug . println ( " End Failed " ) ;
}
} ) ;
ArduinoOTA . begin ( ) ;
}
// WIFI
@ -93,27 +152,27 @@ void setupWifi()
void testConnectMQTT ( )
{
while ( ! client . connected ( ) ) {
Serial . print ( " Connexion au serveur MQTT... " ) ;
Debug . print ( " Connexion au serveur MQTT... " ) ;
if ( client . connect ( " ESP8266Client " , MQTT_USER , MQTT_PASS ) ) {
Serial . print ( " OK \n Send Current State " ) ;
Debug . print ( " OK \n Send Current State " ) ;
mqttSendState ( ) ;
mqttSendSpeedState ( ) ;
mqttSendBrightnessState ( ) ;
mqttSendEffectState ( ) ;
mqttSendColorState ( ) ;
Serial . print ( " OK \n Subscribe " ) ;
Debug . print ( " OK \n Subscribe " ) ;
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 " ) ;
Debug . println ( " OK " ) ;
} else {
Serial . print ( " KO, erreur : " ) ;
Serial . print ( client . state ( ) ) ;
Serial . println ( " , on attend 5 secondes avant de recommencer " ) ;
Debug . print ( " KO, erreur : " ) ;
Debug . print ( client . state ( ) ) ;
Debug . println ( " , on attend 5 secondes avant de recommencer " ) ;
delay ( 5000 ) ;
}
}
@ -131,8 +190,8 @@ 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 ) ;
Debug . print ( " Received [ " + stopic + " ] : " ) ;
Debug . println ( msgString ) ;
if ( stopic = = MQTT_LED_COMMAND ) {
if ( msgString = = " ON " ) {
@ -322,7 +381,7 @@ void ledError()
* Pour é viter un é clairage basique , on applique un breath qui permet
* de faire respirer la couleur ( brightness ) .
*/
void ledFullColor ( )
void ledBreathing ( )
{
// Source : http://sean.voisen.org/blog/2011/10/breathing-led-with-arduino/
@ -332,7 +391,7 @@ void ledFullColor()
// J'ai essayé de mapper breath sur 3;brightness pour ne pas eteindre les leds,
// mais l'effet est plus saccadé
fill_solid ( leds , LED_NUM , color ) ;
FastLED . setBrightness ( breath ) ;
FastLED . show ( ) ;
@ -508,6 +567,12 @@ void fire()
//////////////////////// FastLED-3.1.5/examples/Fire202 ///////////////////////
void loop ( ) {
// OTA
ArduinoOTA . handle ( ) ;
// RemoteDebug
Debug . handle ( ) ;
// MQTT
testConnectMQTT ( ) ;
client . loop ( ) ;
@ -518,9 +583,8 @@ void loop() {
} else {
if ( ledEffect = = LED_EFFECT_CYLON ) {
ledCylon ( ) ;
} else if ( ledEffect = = LED_EFFECT_FULLRED ) {
//TODO : degager ce nom : LED_EFFECT_FULLRED
ledFullColor ( ) ;
} else if ( ledEffect = = LED_EFFECT_BREATHING ) {
ledBreathing ( ) ;
} else if ( ledEffect = = LED_EFFECT_COLORPATTERN ) {
ledColorPattern ( ) ;
} else if ( ledEffect = = LED_EFFECT_COLORTEMP ) {