sonoffliving/arduino/sonoffliving/sonoffliving.cpp
2017-06-02 17:19:55 +02:00

214 lines
4.6 KiB
C++

// 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 "sonoffliving.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);
// On fait ça de suite pour gagner en réactivité
digitalWrite(LED_PIN, LOW);
digitalWrite(RELAY_PIN, HIGH);
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(MQTT_CLIENT, 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;
}
}