simplification, plus besoin de fastled
This commit is contained in:
parent
1cb4f8b6f5
commit
a77193497e
@ -2,8 +2,6 @@
|
|||||||
|
|
||||||
#include "mqttled.h"
|
#include "mqttled.h"
|
||||||
|
|
||||||
// C'est moche mais c'est pour récuperer EVERY_N_SECONDS :)
|
|
||||||
#include <FastLED.h>
|
|
||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
#include <PubSubClient.h>
|
#include <PubSubClient.h>
|
||||||
|
|
||||||
@ -26,24 +24,22 @@ void setup() {
|
|||||||
client.setServer(mqtt_server, mqtt_port);
|
client.setServer(mqtt_server, mqtt_port);
|
||||||
client.setCallback(callbackMQTT);
|
client.setCallback(callbackMQTT);
|
||||||
testConnectMQTT();
|
testConnectMQTT();
|
||||||
client.subscribe("homeassistant/switch1");
|
|
||||||
|
|
||||||
pinMode(LED_PIN, OUTPUT);
|
pinMode(LED_PIN, OUTPUT);
|
||||||
}
|
}
|
||||||
|
|
||||||
// WIFI
|
// WIFI
|
||||||
void setupWifi() {
|
void setupWifi() {
|
||||||
Serial.println();
|
|
||||||
Serial.print("Connexion a ");
|
Serial.print("Connexion a ");
|
||||||
Serial.print(wifi_ssid);
|
Serial.print(WIFI_SSID);
|
||||||
WiFi.begin(wifi_ssid, wifi_password);
|
WiFi.mode(WIFI_STA);
|
||||||
|
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
|
||||||
|
|
||||||
while (WiFi.status() != WL_CONNECTED) {
|
while (WiFi.status() != WL_CONNECTED) {
|
||||||
delay(500);
|
delay(500);
|
||||||
Serial.print(".");
|
Serial.print(".");
|
||||||
}
|
}
|
||||||
|
Serial.println(" OK");
|
||||||
Serial.println();
|
|
||||||
Serial.print("IP : ");
|
Serial.print("IP : ");
|
||||||
Serial.println(WiFi.localIP());
|
Serial.println(WiFi.localIP());
|
||||||
}
|
}
|
||||||
@ -53,6 +49,8 @@ void testConnectMQTT() {
|
|||||||
while (!client.connected()) {
|
while (!client.connected()) {
|
||||||
Serial.print("Connexion au serveur MQTT... ");
|
Serial.print("Connexion au serveur MQTT... ");
|
||||||
if (client.connect("ESP8266Client", mqtt_user, mqtt_password)) {
|
if (client.connect("ESP8266Client", mqtt_user, mqtt_password)) {
|
||||||
|
Serial.print("OK\nSubscribe");
|
||||||
|
client.subscribe("homeassistant/switch1");
|
||||||
Serial.println(" OK");
|
Serial.println(" OK");
|
||||||
} else {
|
} else {
|
||||||
Serial.print("KO, erreur : ");
|
Serial.print("KO, erreur : ");
|
||||||
@ -64,15 +62,16 @@ void testConnectMQTT() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void callbackMQTT(char* topic, byte* payload, unsigned int length) {
|
void callbackMQTT(char* topic, byte* payload, unsigned int length) {
|
||||||
unsigned int i = 0;
|
String stopic = String(topic);
|
||||||
Serial.print("Received [" + String(topic) + "] : ");
|
|
||||||
|
|
||||||
|
unsigned int i = 0;
|
||||||
for(i = 0; i < length; i++) {
|
for(i = 0; i < length; i++) {
|
||||||
message_buff[i] = payload[i];
|
message_buff[i] = payload[i];
|
||||||
}
|
}
|
||||||
message_buff[i] = '\0';
|
message_buff[i] = '\0';
|
||||||
String msgString = String(message_buff);
|
String msgString = String(message_buff);
|
||||||
Serial.print(msgString);
|
Serial.print("Received [" + stopic + "] : ");
|
||||||
|
Serial.println(msgString);
|
||||||
|
|
||||||
if (msgString == "ON") {
|
if (msgString == "ON") {
|
||||||
digitalWrite(LED_PIN, HIGH);
|
digitalWrite(LED_PIN, HIGH);
|
||||||
@ -83,11 +82,5 @@ void callbackMQTT(char* topic, byte* payload, unsigned int length) {
|
|||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
testConnectMQTT();
|
testConnectMQTT();
|
||||||
client.loop();
|
client.loop();=
|
||||||
|
|
||||||
EVERY_N_SECONDS(180) {
|
|
||||||
Serial.print("MQTT Subscribe refresh");
|
|
||||||
client.subscribe("homeassistant/select1");
|
|
||||||
Serial.println(" done");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user