on trie les fichiers
This commit is contained in:
40
arduino/mqttfastledmenu.example.h
Normal file
40
arduino/mqttfastledmenu.example.h
Normal file
@@ -0,0 +1,40 @@
|
||||
#define SERIAL_SPEED 115200
|
||||
|
||||
// LED
|
||||
#define LED_NUM 300
|
||||
#define LED_PIN 5 // = D1
|
||||
#define LED_CHIPSET WS2812B
|
||||
#define LED_COLOR_ORDER GRB
|
||||
#define LED_BRIGHTNESS_DEFAULT 96
|
||||
#define LED_SPEED_DEFAULT 120
|
||||
#define LED_COLOR_DEFAULT CRGB::Red
|
||||
|
||||
#define LED_EFFECT_CYLON "cylon"
|
||||
#define LED_EFFECT_FULLRED "full"
|
||||
#define LED_EFFECT_ERROR "error"
|
||||
|
||||
// WIFI
|
||||
#define WIFI_SSID "XXX"
|
||||
#define WIFI_PASSWORD "XXX"
|
||||
|
||||
// MQTT
|
||||
#define MQTT_SERVER "XXX"
|
||||
#define MQTT_PORT 1883
|
||||
#define MQTT_USER "XXX"
|
||||
#define MQTT_PASS "XXX"
|
||||
|
||||
#define MQTT_LED_STATE "strip1/switch"
|
||||
#define MQTT_LED_COMMAND "strip1/status"
|
||||
#define MQTT_LED_EFFECT_COMMAND "strip1/effect/switch"
|
||||
#define MQTT_LED_EFFECT_STATE "strip1/effect/status"
|
||||
#define MQTT_LED_BRIGHTNESS_COMMAND "strip1/brightness/switch"
|
||||
#define MQTT_LED_BRIGHTNESS_STATE "strip1/brightness/status"
|
||||
#define MQTT_LED_SPEED_COMMAND "strip1/speed/switch"
|
||||
#define MQTT_LED_SPEED_STATE "strip1/speed/status"
|
||||
#define MQTT_LED_COLOR_COMMAND "strip1/color/switch"
|
||||
#define MQTT_LED_COLOR_STATE "strip1/color/status"
|
||||
|
||||
// FastLED
|
||||
// TODO : essayer, devrait limiter le flikering
|
||||
//#define FASTLED_ALLOW_INTERRUPTS 0
|
||||
#define FASTLED_ESP8266_NODEMCU_PIN_ORDER
|
||||
208
arduino/mqttfastledmenu.ino
Normal file
208
arduino/mqttfastledmenu.ino
Normal file
@@ -0,0 +1,208 @@
|
||||
#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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user