Browse Source
Merge branch 'dev' of ssh://gitlab.cabillot.eu:2224/esp8266/mqttfastledmenu into dev
master
Merge branch 'dev' of ssh://gitlab.cabillot.eu:2224/esp8266/mqttfastledmenu into dev
master
committed by
Cabillot Julien
5 changed files with 240 additions and 208 deletions
Split View
Diff Options
-
4.gitignore
-
210arduino/mqttfastledmenu/mqttfastledmenu.cpp
-
8arduino/mqttfastledmenu/mqttfastledmenu.example.h
-
208arduino/mqttfastledmenu/mqttfastledmenu.ino
-
18arduino/platformio.ini
@ -1,2 +1,6 @@ |
|||
.vscode/* |
|||
arduino/mqttfastledmenu/mqttfastledmenu.h |
|||
arduino/.pioenvs |
|||
arduino/.piolibdeps |
|||
arduino/.clang_complete |
|||
arduino/.gcc-flags.json |
|||
@ -0,0 +1,210 @@ |
|||
#include <Arduino.h>
|
|||
|
|||
#include "mqttfastledmenu.h"
|
|||
|
|||
#include <FastLED.h>
|
|||
#include <ESP8266WiFi.h>
|
|||
#include <PubSubClient.h>
|
|||
|
|||
// 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<LED_CHIPSET,LED_PIN, LED_COLOR_ORDER>(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(); |
|||
} |
|||
} |
|||
} |
|||
@ -1,208 +0,0 @@ |
|||
#include "mqttfastledmenu.h"
|
|||
|
|||
#include <FastLED.h>
|
|||
#include <ESP8266WiFi.h>
|
|||
#include <PubSubClient.h>
|
|||
|
|||
// 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<LED_CHIPSET,LED_PIN, LED_COLOR_ORDER>(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(); |
|||
} |
|||
} |
|||
} |
|||
@ -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 |
|||
Write
Preview
Loading…
Cancel
Save