From 452533ae251d290ad38348819af4dcaceb589f38 Mon Sep 17 00:00:00 2001 From: Julien Cabillot Date: Tue, 7 Mar 2017 09:17:50 +0100 Subject: [PATCH] preparation pour platformio --- arduino/.gitignore | 4 + arduino/mqttfastledmenu/mqttfastledmenu.cpp | 210 ++++++++++++++++++ .../mqttfastledmenu/mqttfastledmenu.example.h | 8 + arduino/mqttfastledmenu/mqttfastledmenu.ino | 208 ----------------- arduino/platformio.ini | 18 ++ 5 files changed, 240 insertions(+), 208 deletions(-) create mode 100644 arduino/.gitignore create mode 100644 arduino/mqttfastledmenu/mqttfastledmenu.cpp create mode 100644 arduino/platformio.ini diff --git a/arduino/.gitignore b/arduino/.gitignore new file mode 100644 index 0000000..5dac9f5 --- /dev/null +++ b/arduino/.gitignore @@ -0,0 +1,4 @@ +.pioenvs +.piolibdeps +.clang_complete +.gcc-flags.json diff --git a/arduino/mqttfastledmenu/mqttfastledmenu.cpp b/arduino/mqttfastledmenu/mqttfastledmenu.cpp new file mode 100644 index 0000000..770d184 --- /dev/null +++ b/arduino/mqttfastledmenu/mqttfastledmenu.cpp @@ -0,0 +1,210 @@ +#include + +#include "mqttfastledmenu.h" + +#include +#include +#include + +// LED +int brightness = LED_BRIGHTNESS_DEFAULT; +int color = LED_COLOR_DEFAULT; +int speed = LED_SPEED_DEFAULT; +CRGB leds[LED_NUM]; +String ledEffect = LED_EFFECT_CYLON; +boolean ledState = false; + +// WIFI +WiFiClient espClient; + +// MQTT +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); + + // LED + LEDS.addLeds(leds, LED_NUM).setCorrection(TypicalSMD5050); + ledBlackAll(); + FastLED.setBrightness(brightness); +} + +// WIFI +void setupWifi() +{ + Serial.print("Connexion a "); + 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.print("IP : "); + Serial.println(WiFi.localIP()); +} + +// MQTT +void testConnectMQTT() +{ + while (!client.connected()) { + Serial.print("Connexion au serveur MQTT... "); + if (client.connect("ESP8266Client", MQTT_USER, MQTT_PASS)) { + Serial.println("OK"); + 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); + } else { + Serial.print("KO, erreur : "); + Serial.print(client.state()); + Serial.println(", on attend 5 secondes avant de recommencer"); + delay(5000); + } + } +} + +// Déclenche les actions à la réception d'un message +void callbackMQTT(char* topic, byte* payload, unsigned int length) +{ + String stopic = String(topic); + + unsigned int i = 0; + for(i = 0; i < length; i++) { + message_buff[i] = payload[i]; + } + message_buff[i] = '\0'; + String msgString = String(message_buff); + + Serial.print("Received [" + stopic + "] : "); + Serial.println(msgString); + + if (stopic == MQTT_LED_COMMAND) { + if (msgString == "ON") { + ledState = true; + client.publish(MQTT_LED_STATE, message_buff, true); + } else { + ledState = false; + client.publish(MQTT_LED_STATE, message_buff, true); + } + } else if (stopic == MQTT_LED_EFFECT_COMMAND) { + // Si on ne repasse pas tout à noir, cela peut faire des effets surprenants + ledBlackAll(); + ledEffect = msgString; + // TODO : a vraiment tester + client.publish(MQTT_LED_EFFECT_STATE, message_buff, true); + } else if (stopic == MQTT_LED_BRIGHTNESS_COMMAND) { + brightness = msgString.toInt(); + FastLED.setBrightness(brightness); + client.publish(MQTT_LED_BRIGHTNESS_STATE, message_buff, true); + } else if (stopic == MQTT_LED_COLOR_COMMAND) { + // Sample : 134,168,255 + color = CRGB( + msgString.substring(0,3).toInt(), + msgString.substring(4,7).toInt(), + msgString.substring(8,11).toInt() + ); + client.publish(MQTT_LED_COLOR_STATE, message_buff, true); + } else if (stopic == MQTT_LED_SPEED_COMMAND) { + speed = msgString.toInt(); + client.publish(MQTT_LED_SPEED_STATE, message_buff, true); + } +} + +// LED +void ledBlackAll() +{ + FastLED.clear(); + FastLED.show(); +} + +void ledCylon() +{ + // Effet cylon : on allume une led, on attends, on eteinds, on passe à la suivante + for(int i = 0; i < LED_NUM; i++) { + EVERY_N_SECONDS(1) { + client.loop(); + if (ledEffect != LED_EFFECT_CYLON) { + return; + } + } + + leds[i] = color; + FastLED.delay(1000 / speed); + leds[i] = CRGB::Black; + FastLED.delay(1000 / speed); + } + + for(int i = LED_NUM - 1; i > 0; i--) { + EVERY_N_SECONDS(1) { + client.loop(); + if (ledEffect != LED_EFFECT_CYLON) { + return; + } + } + + leds[i] = color; + FastLED.delay(1000 / speed); + leds[i] = CRGB::Black; + FastLED.show(); + } + FastLED.delay(1000 / speed); +} + +void ledError() +{ + for(int i = 0; i < LED_NUM; i++) { + if ((i % 2) == 0) { + leds[i] = CRGB::Black; + } else { + leds[i] = color; + } + } + + FastLED.delay(1000 / speed); +} + +void ledFullColor() +{ + fill_solid(leds, LED_NUM, color); + FastLED.delay(1000 / speed); +} + +void loop() { + // MQTT + testConnectMQTT(); + client.loop(); + + // LED + if (!ledState) { + FastLED.delay(1000); + } else { + if (ledEffect == LED_EFFECT_CYLON) { + ledCylon(); + } else if (ledEffect == LED_EFFECT_FULLRED) { + ledFullColor(); + } else { + ledError(); + } + } +} diff --git a/arduino/mqttfastledmenu/mqttfastledmenu.example.h b/arduino/mqttfastledmenu/mqttfastledmenu.example.h index c11aca0..553b5cd 100644 --- a/arduino/mqttfastledmenu/mqttfastledmenu.example.h +++ b/arduino/mqttfastledmenu/mqttfastledmenu.example.h @@ -38,3 +38,11 @@ // TODO : essayer, devrait limiter le flikering //#define FASTLED_ALLOW_INTERRUPTS 0 #define FASTLED_ESP8266_NODEMCU_PIN_ORDER + +void setupWifi(); +void testConnectMQTT(); +void callbackMQTT(char* topic, byte* payload, unsigned int length); +void ledBlackAll(); +void ledCylon(); +void ledError(); +void ledFullColor(); diff --git a/arduino/mqttfastledmenu/mqttfastledmenu.ino b/arduino/mqttfastledmenu/mqttfastledmenu.ino index b113763..e69de29 100644 --- a/arduino/mqttfastledmenu/mqttfastledmenu.ino +++ b/arduino/mqttfastledmenu/mqttfastledmenu.ino @@ -1,208 +0,0 @@ -#include "mqttfastledmenu.h" - -#include -#include -#include - -// LED -int brightness = LED_BRIGHTNESS_DEFAULT; -int color = LED_COLOR_DEFAULT; -int speed = LED_SPEED_DEFAULT; -CRGB leds[LED_NUM]; -String ledEffect = LED_EFFECT_CYLON; -boolean ledState = false; - -// WIFI -WiFiClient espClient; - -// MQTT -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); - - // LED - LEDS.addLeds(leds, LED_NUM).setCorrection(TypicalSMD5050); - ledBlackAll(); - FastLED.setBrightness(brightness); -} - -// WIFI -void setupWifi() -{ - Serial.print("Connexion a "); - 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.print("IP : "); - Serial.println(WiFi.localIP()); -} - -// MQTT -void testConnectMQTT() -{ - while (!client.connected()) { - Serial.print("Connexion au serveur MQTT... "); - if (client.connect("ESP8266Client", MQTT_USER, MQTT_PASS)) { - Serial.println("OK"); - 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); - } else { - Serial.print("KO, erreur : "); - Serial.print(client.state()); - Serial.println(", on attend 5 secondes avant de recommencer"); - delay(5000); - } - } -} - -// Déclenche les actions à la réception d'un message -void callbackMQTT(char* topic, byte* payload, unsigned int length) -{ - String stopic = String(topic); - - unsigned int i = 0; - for(i = 0; i < length; i++) { - message_buff[i] = payload[i]; - } - message_buff[i] = '\0'; - String msgString = String(message_buff); - - Serial.print("Received [" + stopic + "] : "); - Serial.println(msgString); - - if (stopic == MQTT_LED_COMMAND) { - if (msgString == "ON") { - ledState = true; - client.publish(MQTT_LED_STATE, message_buff, true); - } else { - ledState = false; - client.publish(MQTT_LED_STATE, message_buff, true); - } - } else if (stopic == MQTT_LED_EFFECT_COMMAND) { - // Si on ne repasse pas tout à noir, cela peut faire des effets surprenants - ledBlackAll(); - ledEffect = msgString; - // TODO : a vraiment tester - client.publish(MQTT_LED_EFFECT_STATE, message_buff, true); - } else if (stopic == MQTT_LED_BRIGHTNESS_COMMAND) { - brightness = msgString.toInt(); - FastLED.setBrightness(brightness); - client.publish(MQTT_LED_BRIGHTNESS_STATE, message_buff, true); - } else if (stopic == MQTT_LED_COLOR_COMMAND) { - // Sample : 134,168,255 - color = CRGB( - msgString.substring(0,3).toInt(), - msgString.substring(4,7).toInt(), - msgString.substring(8,11).toInt() - ); - client.publish(MQTT_LED_COLOR_STATE, message_buff, true); - } else if (stopic == MQTT_LED_SPEED_COMMAND) { - speed = msgString.toInt(); - client.publish(MQTT_LED_SPEED_STATE, message_buff, true); - } -} - -// LED -void ledBlackAll() -{ - FastLED.clear(); - FastLED.show(); -} - -void ledCylon() -{ - // Effet cylon : on allume une led, on attends, on eteinds, on passe à la suivante - for(int i = 0; i < LED_NUM; i++) { - EVERY_N_SECONDS(1) { - client.loop(); - if (ledEffect != LED_EFFECT_CYLON) { - return; - } - } - - leds[i] = color; - FastLED.delay(1000 / speed); - leds[i] = CRGB::Black; - FastLED.delay(1000 / speed); - } - - for(int i = LED_NUM - 1; i > 0; i--) { - EVERY_N_SECONDS(1) { - client.loop(); - if (ledEffect != LED_EFFECT_CYLON) { - return; - } - } - - leds[i] = color; - FastLED.delay(1000 / speed); - leds[i] = CRGB::Black; - FastLED.show(); - } - FastLED.delay(1000 / speed); -} - -void ledError() -{ - for(int i = 0; i < LED_NUM; i++) { - if ((i % 2) == 0) { - leds[i] = CRGB::Black; - } else { - leds[i] = color; - } - } - - FastLED.delay(1000 / speed); -} - -void ledFullColor() -{ - fill_solid(leds, LED_NUM, color); - FastLED.delay(1000 / speed); -} - -void loop() { - // MQTT - testConnectMQTT(); - client.loop(); - - // LED - if (!ledState) { - FastLED.delay(1000); - } else { - if (ledEffect == LED_EFFECT_CYLON) { - ledCylon(); - } else if (ledEffect == LED_EFFECT_FULLRED) { - ledFullColor(); - } else { - ledError(); - } - } -} \ No newline at end of file diff --git a/arduino/platformio.ini b/arduino/platformio.ini new file mode 100644 index 0000000..deddd6f --- /dev/null +++ b/arduino/platformio.ini @@ -0,0 +1,18 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; http://docs.platformio.org/page/projectconf.html + +[env:nodemcuv2] +platform=espressif8266 +board=nodemcuv2 +framework=arduino + +[platformio] +src_dir=mqttfastledmenu +lib_dir=/home/jcabillot/Arduino/libraries