import
This commit is contained in:
@@ -0,0 +1,209 @@
|
||||
// Bouton
|
||||
#include <Ticker.h>
|
||||
|
||||
// Wifi
|
||||
#include <ESP8266WiFi.h>
|
||||
|
||||
// MQTT
|
||||
#include <PubSubClient.h>
|
||||
|
||||
// OTA
|
||||
#include <ESP8266mDNS.h>
|
||||
#include <WiFiUdp.h>
|
||||
#include <ArduinoOTA.h>
|
||||
|
||||
// RemoteDebug
|
||||
#include <RemoteDebug.h>
|
||||
|
||||
#include "sonoffentrance.h"
|
||||
|
||||
void setup() {
|
||||
Serial.begin(SERIAL_SPEED);
|
||||
Serial.println("\nresetting");
|
||||
|
||||
setupSonOff();
|
||||
|
||||
// WIFI
|
||||
setupWifi();
|
||||
|
||||
// OTA
|
||||
setupOTA();
|
||||
|
||||
// RemoteDebug
|
||||
Debug.begin(REMDEB_CLIENT);
|
||||
|
||||
// MQTT
|
||||
client.setServer(MQTT_SERVER, MQTT_PORT);
|
||||
client.setCallback(callbackMQTT);
|
||||
testConnectMQTT();
|
||||
}
|
||||
|
||||
void setupSonOff() {
|
||||
pinMode(LED_PIN, OUTPUT);
|
||||
pinMode(RELAY_PIN, OUTPUT);
|
||||
pinMode(BUTTON_PIN, INPUT);
|
||||
relayState = true;
|
||||
btn_timer.attach(0.05, button);
|
||||
}
|
||||
|
||||
// OTA
|
||||
void setupOTA()
|
||||
{
|
||||
ArduinoOTA.setHostname(OTA_CLIENT); // on donne une petit nom a notre module
|
||||
ArduinoOTA.setPassword(OTA_PASSWORD);
|
||||
ArduinoOTA.onStart([]() {
|
||||
Debug.println("OTA Starting");
|
||||
Serial.println("OTA Starting");
|
||||
blinkLED(LED_PIN, 400, 2);
|
||||
digitalWrite(LED_PIN, HIGH);
|
||||
});
|
||||
ArduinoOTA.onEnd([]() {
|
||||
Debug.println("\nOTA End");
|
||||
Serial.println("\nOTA End");
|
||||
});
|
||||
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
|
||||
Debug.printf("OTA Progress: %u%%\r", (progress / (total / 100)));
|
||||
Serial.printf("OTA Progress: %u%%\r", (progress / (total / 100)));
|
||||
digitalWrite(LED_PIN, LOW);
|
||||
delay(5);
|
||||
digitalWrite(LED_PIN, HIGH);
|
||||
});
|
||||
ArduinoOTA.onError([](ota_error_t error) {
|
||||
Serial.printf("OTA Error[%u]: ", error);
|
||||
Debug.printf("OTA Error[%u]: ", error);
|
||||
blinkLED(LED_PIN, 40, 2);
|
||||
if (error == OTA_AUTH_ERROR) {
|
||||
Serial.println("Auth Failed");
|
||||
Debug.println("Auth Failed");
|
||||
} else if (error == OTA_BEGIN_ERROR) {
|
||||
Serial.println("Begin Failed");
|
||||
Debug.println("Begin Failed");
|
||||
} else if (error == OTA_CONNECT_ERROR) {
|
||||
Serial.println("Connect Failed");
|
||||
Debug.println("Connect Failed");
|
||||
} else if (error == OTA_RECEIVE_ERROR) {
|
||||
Serial.println("Receive Failed");
|
||||
Debug.println("Receive Failed");
|
||||
} else if (error == OTA_END_ERROR) {
|
||||
Serial.println("End Failed");
|
||||
Debug.println("End Failed");
|
||||
}
|
||||
});
|
||||
ArduinoOTA.begin();
|
||||
}
|
||||
|
||||
// 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(" OK");
|
||||
Serial.print("IP : ");
|
||||
Serial.println(WiFi.localIP());
|
||||
}
|
||||
|
||||
// MQTT
|
||||
void testConnectMQTT()
|
||||
{
|
||||
while (!client.connected()) {
|
||||
Debug.print("Connexion au serveur MQTT... ");
|
||||
if (client.connect("ESP8266Client", MQTT_USER, MQTT_PASS)) {
|
||||
Debug.print("OK\nSend Current State");
|
||||
mqttSendState();
|
||||
|
||||
Debug.print("OK\nSubscribe");
|
||||
client.subscribe(MQTT_COMMAND);
|
||||
|
||||
Debug.println(" OK");
|
||||
} else {
|
||||
Debug.print("KO, erreur : ");
|
||||
Debug.print(client.state());
|
||||
Debug.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);
|
||||
|
||||
Debug.print("Received [" + stopic + "] : ");
|
||||
Debug.println(msgString);
|
||||
|
||||
if (stopic == MQTT_COMMAND) {
|
||||
if (msgString == "ON") {
|
||||
relayState = true;
|
||||
} else if (msgString == "OFF") {
|
||||
relayState = false;
|
||||
} else if (msgString == "reset"){
|
||||
ESP.restart();
|
||||
}
|
||||
mqttSendState();
|
||||
}
|
||||
}
|
||||
|
||||
void mqttSendState()
|
||||
{
|
||||
client.publish(MQTT_STATE, (relayState) ? "ON": "OFF", true);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// OTA
|
||||
ArduinoOTA.handle();
|
||||
|
||||
// RemoteDebug
|
||||
Debug.handle();
|
||||
|
||||
// MQTT
|
||||
testConnectMQTT();
|
||||
client.loop();
|
||||
|
||||
if (relayState) {
|
||||
digitalWrite(LED_PIN, LOW);
|
||||
digitalWrite(RELAY_PIN, HIGH);
|
||||
} else {
|
||||
digitalWrite(LED_PIN, HIGH);
|
||||
digitalWrite(RELAY_PIN, LOW);
|
||||
}
|
||||
}
|
||||
|
||||
void blinkLED(int pin, int duration, int n) {
|
||||
for(int i = 0; i < n; i++) {
|
||||
digitalWrite(LED_PIN, HIGH);
|
||||
delay(duration);
|
||||
digitalWrite(LED_PIN, LOW);
|
||||
delay(duration);
|
||||
}
|
||||
}
|
||||
|
||||
void button() {
|
||||
if (!digitalRead(BUTTON_PIN)) {
|
||||
count++;
|
||||
}
|
||||
else {
|
||||
if (count > 1 && count <= 40) {
|
||||
digitalWrite(LED_PIN, !digitalRead(LED_PIN));
|
||||
digitalWrite(RELAY_PIN, !digitalRead(RELAY_PIN));
|
||||
}
|
||||
else if (count > 40) {
|
||||
ESP.restart();
|
||||
}
|
||||
count = 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
#define SERIAL_SPEED 115200
|
||||
|
||||
// OTA
|
||||
#define OTA_PASSWORD "XXX"
|
||||
#define OTA_CLIENT "sonoff_living"
|
||||
|
||||
// DebugRemote
|
||||
#define REMDEB_CLIENT "sonoff_living"
|
||||
RemoteDebug Debug;
|
||||
|
||||
// WIFI
|
||||
#define WIFI_SSID "XXX"
|
||||
#define WIFI_PASSWORD "XXX"
|
||||
|
||||
WiFiClient espClient;
|
||||
|
||||
#define BUTTON 0
|
||||
#define RELAY 12
|
||||
#define LED 13
|
||||
|
||||
// MQTT
|
||||
#define MQTT_SERVER "XXX"
|
||||
#define MQTT_PORT 1883
|
||||
#define MQTT_USER "XXX"
|
||||
#define MQTT_PASS "XXX"
|
||||
|
||||
#define MQTT_CLIENT "sonoff_living"
|
||||
#define MQTT_COMMAND "sonoff_living/switch"
|
||||
#define MQTT_STATE "sonoff_living/status"
|
||||
|
||||
char message_buff[100];
|
||||
PubSubClient client(espClient);
|
||||
|
||||
// Bouton
|
||||
Ticker btn_timer;
|
||||
unsigned long count = 0;
|
||||
|
||||
bool relayState;
|
||||
|
||||
void setup();
|
||||
void loop();
|
||||
void blinkLED(int pin, int duration, int n);
|
||||
void button();
|
||||
void setupOTA();
|
||||
void setupWifi();
|
||||
void testConnectMQTT();
|
||||
void callbackMQTT(char* topic, byte* payload, unsigned int length);
|
||||
void setupSonOff();
|
||||
void mqttSendState();
|
||||
Reference in New Issue
Block a user