|
|
@ -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); |
|
|
|
|
|
WiFi.begin(wifi_ssid, wifi_password); |
|
|
|
|
|
|
|
|
Serial.print(WIFI_SSID); |
|
|
|
|
|
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(); |
|
|
|
|
|
|
|
|
Serial.println(" OK"); |
|
|
Serial.print("IP : "); |
|
|
Serial.print("IP : "); |
|
|
Serial.println(WiFi.localIP()); |
|
|
Serial.println(WiFi.localIP()); |
|
|
} |
|
|
} |
|
|
@ -53,7 +49,9 @@ 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.println("OK"); |
|
|
|
|
|
|
|
|
Serial.print("OK\nSubscribe"); |
|
|
|
|
|
client.subscribe("homeassistant/switch1"); |
|
|
|
|
|
Serial.println(" OK"); |
|
|
} else { |
|
|
} else { |
|
|
Serial.print("KO, erreur : "); |
|
|
Serial.print("KO, erreur : "); |
|
|
Serial.print(client.state()); |
|
|
Serial.print(client.state()); |
|
|
@ -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; |
|
|
|
|
|
Serial.print("Received [" + String(topic) + "] : "); |
|
|
|
|
|
|
|
|
String stopic = 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(); |
|
|
|
|
|
|
|
|
|
|
|
EVERY_N_SECONDS(180) { |
|
|
|
|
|
Serial.print("MQTT Subscribe refresh"); |
|
|
|
|
|
client.subscribe("homeassistant/select1"); |
|
|
|
|
|
Serial.println(" done"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
client.loop();= |
|
|
} |
|
|
} |