committed by
JC
commit
44eb48749e
9 changed files with 204 additions and 0 deletions
Split View
Diff Options
-
1.gitignore
-
31README.md
-
10ha_configuration.yml
-
BINirl1.jpg
-
BINlogo.jpg
-
17mqttfastledplus.example.h
-
BINmqttfastledplus.fzz
-
145mqttfastledplus.ino
-
BINmqttfastledplus_bb.png
@ -0,0 +1 @@ |
|||
mqttfastledplus.h |
|||
@ -0,0 +1,31 @@ |
|||
Introduction |
|||
============ |
|||
|
|||
Effet cylon en rouge controlé par MQTT. |
|||
Pour lancer l'effet sur le LED Strip@D1 (GPIO5) : |
|||
```bash |
|||
mosquitto_pub -d -u "<USER>" -P "<PASS>" -t "<TOPIC>" -m "ON" |
|||
``` |
|||
Pour l'arreter : |
|||
```bash |
|||
mosquitto_pub -d -u "<USER>" -P "<PASS>" -t "<TOPIC>" -m "OFF" |
|||
``` |
|||
|
|||
Un exemple de configuration pour home-assistant se trouve dans [ha_configuration.yml](ha\_configuration.yml). |
|||
|
|||
Matériel |
|||
======== |
|||
* 1x ESP8266 Lolin (Nodemcu v3) |
|||
* 1x Breadboard |
|||
* 1x Resistance 220Ω (jusqu'à 1kΩ) |
|||
* 1x Condensateur 1000μF |
|||
* 1x LED Strip wb2812b |
|||
* 1x Logic Level Translator |
|||
* 1x Transformateur AC-DC 220v-5v |
|||
|
|||
Médias |
|||
====== |
|||
|
|||
 |
|||
 |
|||
|
|||
@ -0,0 +1,10 @@ |
|||
switch: |
|||
platform: "mqtt" |
|||
name: "Test LED" |
|||
command_topic: "homeassistant/switch1" #Topic sur lequel on publie l'état de l'interrupteur |
|||
payload_on: "ON" # A vous de choisir le message envoyé lorsque l'interrupteur est allumé |
|||
payload_off: "OFF" # et éteint |
|||
optimistic: true # Mettez à true pour maintenir l'état |
|||
qos: 0 |
|||
retain: true |
|||
value_template: '{{ value.x }}' |
|||
@ -0,0 +1,17 @@ |
|||
// LED |
|||
#define NUM_LEDS 300 |
|||
#define LED_PIN 5 // = D1 |
|||
#define CHIPSET WS2812B |
|||
#define COLOR_ORDER GRB |
|||
#define BRIGHTNESS 96 |
|||
#define FRAMES_PER_SECOND 120 |
|||
|
|||
// WIFI |
|||
#define wifi_ssid "XXX" |
|||
#define wifi_password "XXX" |
|||
|
|||
// MQTT |
|||
#define mqtt_server "XXX" |
|||
#define mqtt_port 1883 |
|||
#define mqtt_user "XXX" |
|||
#define mqtt_password "XXX" |
|||
@ -0,0 +1,145 @@ |
|||
#include "mqttfastledplus.h"
|
|||
|
|||
#define FASTLED_ESP8266_NODEMCU_PIN_ORDER
|
|||
#include <FastLED.h>
|
|||
#include <ESP8266WiFi.h>
|
|||
#include <PubSubClient.h>
|
|||
|
|||
// LED
|
|||
CRGB leds[NUM_LEDS]; |
|||
bool ledGo = false; |
|||
|
|||
// WIFI
|
|||
WiFiClient espClient; |
|||
|
|||
// MQTT
|
|||
char message_buff[100]; |
|||
PubSubClient client(espClient); |
|||
|
|||
void setup() { |
|||
Serial.begin(115200); |
|||
Serial.println("\nresetting"); |
|||
|
|||
// WIFI
|
|||
setupWifi(); |
|||
|
|||
// MQTT
|
|||
client.setServer(mqtt_server, mqtt_port); |
|||
client.setCallback(callbackMQTT); |
|||
testConnectMQTT(); |
|||
client.subscribe("homeassistant/switch1"); |
|||
|
|||
// LED
|
|||
LEDS.addLeds<CHIPSET,LED_PIN, COLOR_ORDER>(leds, NUM_LEDS); |
|||
FastLED.setBrightness(BRIGHTNESS); |
|||
ledBlackAll(); |
|||
} |
|||
|
|||
// WIFI
|
|||
void setupWifi() { |
|||
Serial.println(); |
|||
Serial.print("Connexion a "); |
|||
Serial.print(wifi_ssid); |
|||
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_password)) { |
|||
Serial.println("OK"); |
|||
} 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) { |
|||
unsigned int i = 0; |
|||
Serial.print("Received [" + String(topic) + "] : "); |
|||
|
|||
// create character buffer with ending null terminator (string)
|
|||
for(i = 0; i < length; i++) { |
|||
message_buff[i] = payload[i]; |
|||
} |
|||
message_buff[i] = '\0'; |
|||
|
|||
String msgString = String(message_buff); |
|||
Serial.println(msgString); |
|||
|
|||
if (msgString == "ON") { |
|||
ledGo = true; |
|||
} else { |
|||
ledGo = false; |
|||
} |
|||
} |
|||
|
|||
// LED
|
|||
void ledCylon() |
|||
{ |
|||
// Effet cylon : on allume une led, on attends, on eteinds, on passe à la suivante
|
|||
for(int i = 0; i < NUM_LEDS; i++) { |
|||
if ((i % 10) == 0) { |
|||
client.loop(); |
|||
} |
|||
|
|||
leds[i] = CRGB::Red; |
|||
FastLED.show(); |
|||
FastLED.delay(1000 / FRAMES_PER_SECOND); |
|||
leds[i] = CRGB::Black; |
|||
FastLED.show(); |
|||
FastLED.delay(1000 / FRAMES_PER_SECOND); |
|||
} |
|||
for(int i = NUM_LEDS - 1; i > 0; i--) { |
|||
if ((i % 10) == 0) { |
|||
client.loop(); |
|||
} |
|||
|
|||
leds[i] = CRGB::Red; |
|||
FastLED.show(); |
|||
FastLED.delay(1000 / FRAMES_PER_SECOND); |
|||
leds[i] = CRGB::Black; |
|||
FastLED.show(); |
|||
} |
|||
FastLED.delay(1000 / FRAMES_PER_SECOND); |
|||
} |
|||
|
|||
void ledBlackAll() |
|||
{ |
|||
fill_solid(leds, NUM_LEDS, CRGB::Black); |
|||
FastLED.show(); |
|||
FastLED.delay(1000 / FRAMES_PER_SECOND); |
|||
} |
|||
|
|||
void loop() { |
|||
// MQTT
|
|||
testConnectMQTT(); |
|||
client.loop(); |
|||
|
|||
EVERY_N_SECONDS(180) { |
|||
Serial.print("MQTT Subscribe refresh"); |
|||
client.subscribe("homeassistant/select1"); |
|||
Serial.println(" done"); |
|||
} |
|||
|
|||
// LED
|
|||
if (ledGo) { |
|||
ledCylon(); |
|||
} else { |
|||
ledBlackAll(); |
|||
} |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save