on remets ça comme j'ai l'habitude [WIP]
This commit is contained in:
parent
5e861b10ef
commit
0a4a92ac8b
204
arduino/sonoffliving/sonoffliving.cpp
Normal file
204
arduino/sonoffliving/sonoffliving.cpp
Normal file
@ -0,0 +1,204 @@
|
|||||||
|
#include <EEPROM.h>
|
||||||
|
#include <Ticker.h>
|
||||||
|
|
||||||
|
// Wifi
|
||||||
|
#include <ESP8266WiFi.h>
|
||||||
|
|
||||||
|
// MQTT
|
||||||
|
#include <PubSubClient.h>
|
||||||
|
|
||||||
|
// OTA
|
||||||
|
#include <ESP8266mDNS.h>
|
||||||
|
#include <WiFiUdp.h>
|
||||||
|
#include <ArduinoOTA.h>
|
||||||
|
|
||||||
|
#include "sonoffliving.h"
|
||||||
|
|
||||||
|
void callback(const MQTT::Publish& pub) {
|
||||||
|
if (pub.payload_string() == "stat") {
|
||||||
|
}
|
||||||
|
else if (pub.payload_string() == "on") {
|
||||||
|
digitalWrite(LED, LOW);
|
||||||
|
digitalWrite(RELAY, HIGH);
|
||||||
|
}
|
||||||
|
else if (pub.payload_string() == "off") {
|
||||||
|
digitalWrite(LED, HIGH);
|
||||||
|
digitalWrite(RELAY, LOW);
|
||||||
|
}
|
||||||
|
else if (pub.payload_string() == "reset") {
|
||||||
|
requestRestart = true;
|
||||||
|
}
|
||||||
|
sendStatus = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
pinMode(LED, OUTPUT);
|
||||||
|
pinMode(RELAY, OUTPUT);
|
||||||
|
pinMode(BUTTON, INPUT);
|
||||||
|
digitalWrite(LED, HIGH);
|
||||||
|
digitalWrite(RELAY, LOW);
|
||||||
|
Serial.begin(115200);
|
||||||
|
EEPROM.begin(8);
|
||||||
|
lastRelayState = EEPROM.read(0);
|
||||||
|
if (rememberRelayState && lastRelayState == 1) {
|
||||||
|
digitalWrite(LED, LOW);
|
||||||
|
digitalWrite(RELAY, HIGH);
|
||||||
|
}
|
||||||
|
btn_timer.attach(0.05, button);
|
||||||
|
mqttClient.set_callback(callback);
|
||||||
|
WiFi.mode(WIFI_STA);
|
||||||
|
WiFi.begin(WIFI_SSID, WIFI_PASS);
|
||||||
|
ArduinoOTA.onStart([]() {
|
||||||
|
OTAupdate = true;
|
||||||
|
blinkLED(LED, 400, 2);
|
||||||
|
digitalWrite(LED, HIGH);
|
||||||
|
Serial.println("OTA Update Initiated . . .");
|
||||||
|
});
|
||||||
|
ArduinoOTA.onEnd([]() {
|
||||||
|
Serial.println("\nOTA Update Ended . . .s");
|
||||||
|
ESP.restart();
|
||||||
|
});
|
||||||
|
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
|
||||||
|
digitalWrite(LED, LOW);
|
||||||
|
delay(5);
|
||||||
|
digitalWrite(LED, HIGH);
|
||||||
|
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
|
||||||
|
});
|
||||||
|
ArduinoOTA.onError([](ota_error_t error) {
|
||||||
|
blinkLED(LED, 40, 2);
|
||||||
|
OTAupdate = false;
|
||||||
|
Serial.printf("OTA Error [%u] ", error);
|
||||||
|
if (error == OTA_AUTH_ERROR) Serial.println(". . . . . . . . . . . . . . . Auth Failed");
|
||||||
|
else if (error == OTA_BEGIN_ERROR) Serial.println(". . . . . . . . . . . . . . . Begin Failed");
|
||||||
|
else if (error == OTA_CONNECT_ERROR) Serial.println(". . . . . . . . . . . . . . . Connect Failed");
|
||||||
|
else if (error == OTA_RECEIVE_ERROR) Serial.println(". . . . . . . . . . . . . . . Receive Failed");
|
||||||
|
else if (error == OTA_END_ERROR) Serial.println(". . . . . . . . . . . . . . . End Failed");
|
||||||
|
});
|
||||||
|
ArduinoOTA.begin();
|
||||||
|
Serial.println(VERSION);
|
||||||
|
Serial.print("\nUnit ID: ");
|
||||||
|
Serial.print("esp8266-");
|
||||||
|
Serial.print(ESP.getChipId(), HEX);
|
||||||
|
Serial.print("\nConnecting to "); Serial.print(WIFI_SSID); Serial.print(" Wifi");
|
||||||
|
while ((WiFi.status() != WL_CONNECTED) && kRetries --) {
|
||||||
|
delay(500);
|
||||||
|
Serial.print(" .");
|
||||||
|
}
|
||||||
|
if (WiFi.status() == WL_CONNECTED) {
|
||||||
|
Serial.println(" DONE");
|
||||||
|
Serial.print("IP Address is: "); Serial.println(WiFi.localIP());
|
||||||
|
Serial.print("Connecting to ");Serial.print(MQTT_SERVER);Serial.print(" Broker . .");
|
||||||
|
delay(500);
|
||||||
|
while (!mqttClient.connect(MQTT::Connect(MQTT_CLIENT).set_keepalive(90).set_auth(MQTT_USER, MQTT_PASS)) && kRetries --) {
|
||||||
|
Serial.print(" .");
|
||||||
|
delay(1000);
|
||||||
|
}
|
||||||
|
if(mqttClient.connected()) {
|
||||||
|
Serial.println(" DONE");
|
||||||
|
Serial.println("\n---------------------------- Logs ----------------------------");
|
||||||
|
Serial.println();
|
||||||
|
mqttClient.subscribe(MQTT_TOPIC);
|
||||||
|
blinkLED(LED, 40, 8);
|
||||||
|
if(digitalRead(RELAY) == HIGH) {
|
||||||
|
digitalWrite(LED, LOW);
|
||||||
|
} else {
|
||||||
|
digitalWrite(LED, HIGH);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Serial.println(" FAILED!");
|
||||||
|
Serial.println("\n----------------------------------------------------------------");
|
||||||
|
Serial.println();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Serial.println(" WiFi FAILED!");
|
||||||
|
Serial.println("\n----------------------------------------------------------------");
|
||||||
|
Serial.println();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
ArduinoOTA.handle();
|
||||||
|
if (OTAupdate == false) {
|
||||||
|
mqttClient.loop();
|
||||||
|
timedTasks();
|
||||||
|
checkStatus();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void blinkLED(int pin, int duration, int n) {
|
||||||
|
for(int i=0; i<n; i++) {
|
||||||
|
digitalWrite(pin, HIGH);
|
||||||
|
delay(duration);
|
||||||
|
digitalWrite(pin, LOW);
|
||||||
|
delay(duration);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void button() {
|
||||||
|
if (!digitalRead(BUTTON)) {
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (count > 1 && count <= 40) {
|
||||||
|
digitalWrite(LED, !digitalRead(LED));
|
||||||
|
digitalWrite(RELAY, !digitalRead(RELAY));
|
||||||
|
sendStatus = true;
|
||||||
|
}
|
||||||
|
else if (count >40){
|
||||||
|
Serial.println("\n\nSonoff Rebooting . . . . . . . . Please Wait");
|
||||||
|
requestRestart = true;
|
||||||
|
}
|
||||||
|
count=0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void checkConnection() {
|
||||||
|
if (WiFi.status() == WL_CONNECTED) {
|
||||||
|
if (mqttClient.connected()) {
|
||||||
|
Serial.println("mqtt broker connection . . . . . . . . . . OK");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Serial.println("mqtt broker connection . . . . . . . . . . LOST");
|
||||||
|
requestRestart = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Serial.println("WiFi connection . . . . . . . . . . LOST");
|
||||||
|
requestRestart = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void checkStatus() {
|
||||||
|
if (sendStatus) {
|
||||||
|
if(digitalRead(LED) == LOW) {
|
||||||
|
if (rememberRelayState) {
|
||||||
|
EEPROM.write(0, 1);
|
||||||
|
}
|
||||||
|
mqttClient.publish(MQTT::Publish(MQTT_TOPIC"/stat", "on").set_retain().set_qos(1));
|
||||||
|
Serial.println("Relay . . . . . . . . . . . . . . . . . . ON");
|
||||||
|
} else {
|
||||||
|
if (rememberRelayState) {
|
||||||
|
EEPROM.write(0, 0);
|
||||||
|
}
|
||||||
|
mqttClient.publish(MQTT::Publish(MQTT_TOPIC"/stat", "off").set_retain().set_qos(1));
|
||||||
|
Serial.println("Relay . . . . . . . . . . . . . . . . . . OFF");
|
||||||
|
}
|
||||||
|
if (rememberRelayState) {
|
||||||
|
EEPROM.commit();
|
||||||
|
}
|
||||||
|
sendStatus = false;
|
||||||
|
}
|
||||||
|
if (requestRestart) {
|
||||||
|
blinkLED(LED, 400, 4);
|
||||||
|
ESP.restart();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void timedTasks() {
|
||||||
|
if ((millis() > TTasks + (kUpdFreq*60000)) || (millis() < TTasks)) {
|
||||||
|
TTasks = millis();
|
||||||
|
checkConnection();
|
||||||
|
}
|
||||||
|
}
|
||||||
0
arduino/sonoffliving/sonoffliving.ino
Normal file
0
arduino/sonoffliving/sonoffliving.ino
Normal file
Loading…
x
Reference in New Issue
Block a user