preparation pour platformio
This commit is contained in:
parent
bb62d0f75f
commit
452533ae25
4
arduino/.gitignore
vendored
Normal file
4
arduino/.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
.pioenvs
|
||||||
|
.piolibdeps
|
||||||
|
.clang_complete
|
||||||
|
.gcc-flags.json
|
||||||
210
arduino/mqttfastledmenu/mqttfastledmenu.cpp
Normal file
210
arduino/mqttfastledmenu/mqttfastledmenu.cpp
Normal file
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -38,3 +38,11 @@
|
|||||||
// TODO : essayer, devrait limiter le flikering
|
// TODO : essayer, devrait limiter le flikering
|
||||||
//#define FASTLED_ALLOW_INTERRUPTS 0
|
//#define FASTLED_ALLOW_INTERRUPTS 0
|
||||||
#define FASTLED_ESP8266_NODEMCU_PIN_ORDER
|
#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();
|
||||||
|
|||||||
@ -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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
18
arduino/platformio.ini
Normal file
18
arduino/platformio.ini
Normal file
@ -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
|
||||||
Loading…
x
Reference in New Issue
Block a user