beautification
This commit is contained in:
parent
9ca0251153
commit
7b29c1dab6
@ -39,11 +39,10 @@ void setup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void setupSonOff() {
|
void setupSonOff() {
|
||||||
pinMode(LED, OUTPUT);
|
pinMode(LED_PIN, OUTPUT);
|
||||||
pinMode(RELAY, OUTPUT);
|
pinMode(RELAY_PIN, OUTPUT);
|
||||||
pinMode(BUTTON, INPUT);
|
pinMode(BUTTON_PIN, INPUT);
|
||||||
digitalWrite(LED, LOW);
|
relayState = false;
|
||||||
digitalWrite(RELAY, HIGH);
|
|
||||||
btn_timer.attach(0.05, button);
|
btn_timer.attach(0.05, button);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -55,8 +54,8 @@ void setupOTA()
|
|||||||
ArduinoOTA.onStart([]() {
|
ArduinoOTA.onStart([]() {
|
||||||
Debug.println("OTA Starting");
|
Debug.println("OTA Starting");
|
||||||
Serial.println("OTA Starting");
|
Serial.println("OTA Starting");
|
||||||
blinkLED(LED, 400, 2);
|
blinkLED(LED_PIN, 400, 2);
|
||||||
digitalWrite(LED, HIGH);
|
digitalWrite(LED_PIN, HIGH);
|
||||||
});
|
});
|
||||||
ArduinoOTA.onEnd([]() {
|
ArduinoOTA.onEnd([]() {
|
||||||
Debug.println("\nOTA End");
|
Debug.println("\nOTA End");
|
||||||
@ -65,14 +64,14 @@ void setupOTA()
|
|||||||
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
|
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
|
||||||
Debug.printf("OTA Progress: %u%%\r", (progress / (total / 100)));
|
Debug.printf("OTA Progress: %u%%\r", (progress / (total / 100)));
|
||||||
Serial.printf("OTA Progress: %u%%\r", (progress / (total / 100)));
|
Serial.printf("OTA Progress: %u%%\r", (progress / (total / 100)));
|
||||||
digitalWrite(LED, LOW);
|
digitalWrite(LED_PIN, LOW);
|
||||||
delay(5);
|
delay(5);
|
||||||
digitalWrite(LED, HIGH);
|
digitalWrite(LED_PIN, HIGH);
|
||||||
});
|
});
|
||||||
ArduinoOTA.onError([](ota_error_t error) {
|
ArduinoOTA.onError([](ota_error_t error) {
|
||||||
Serial.printf("OTA Error[%u]: ", error);
|
Serial.printf("OTA Error[%u]: ", error);
|
||||||
Debug.printf("OTA Error[%u]: ", error);
|
Debug.printf("OTA Error[%u]: ", error);
|
||||||
blinkLED(LED, 40, 2);
|
blinkLED(LED_PIN, 40, 2);
|
||||||
if (error == OTA_AUTH_ERROR) {
|
if (error == OTA_AUTH_ERROR) {
|
||||||
Serial.println("Auth Failed");
|
Serial.println("Auth Failed");
|
||||||
Debug.println("Auth Failed");
|
Debug.println("Auth Failed");
|
||||||
@ -117,20 +116,16 @@ void testConnectMQTT()
|
|||||||
Debug.print("Connexion au serveur MQTT... ");
|
Debug.print("Connexion au serveur MQTT... ");
|
||||||
if (client.connect("ESP8266Client", MQTT_USER, MQTT_PASS)) {
|
if (client.connect("ESP8266Client", MQTT_USER, MQTT_PASS)) {
|
||||||
Debug.print("OK\nSend Current State");
|
Debug.print("OK\nSend Current State");
|
||||||
/*
|
|
||||||
mqttSendState();
|
mqttSendState();
|
||||||
mqttSendBrightnessState();
|
|
||||||
mqttSendColorState();
|
|
||||||
*/
|
|
||||||
|
|
||||||
Debug.print("OK\nSubscribe");
|
Debug.print("OK\nSubscribe");
|
||||||
client.subscribe(MQTT_COMMAND);
|
client.subscribe(MQTT_COMMAND);
|
||||||
|
|
||||||
blinkLED(LED, 40, 8);
|
blinkLED(LED_PIN, 40, 8);
|
||||||
if(digitalRead(RELAY) == HIGH) {
|
if(digitalRead(RELAY_PIN) == HIGH) {
|
||||||
digitalWrite(LED, LOW);
|
digitalWrite(LED_PIN, LOW);
|
||||||
} else {
|
} else {
|
||||||
digitalWrite(LED, HIGH);
|
digitalWrite(LED_PIN, HIGH);
|
||||||
}
|
}
|
||||||
|
|
||||||
Debug.println(" OK");
|
Debug.println(" OK");
|
||||||
@ -158,17 +153,23 @@ void callbackMQTT(char* topic, byte* payload, unsigned int length)
|
|||||||
Debug.print("Received [" + stopic + "] : ");
|
Debug.print("Received [" + stopic + "] : ");
|
||||||
Debug.println(msgString);
|
Debug.println(msgString);
|
||||||
|
|
||||||
if (msgString == "on") {
|
if (stopic == MQTT_COMMAND) {
|
||||||
digitalWrite(LED, LOW);
|
if (msgString == "on") {
|
||||||
digitalWrite(RELAY, HIGH);
|
relayState = true;
|
||||||
} else if (msgString == "off") {
|
} else if (msgString == "off") {
|
||||||
digitalWrite(LED, HIGH);
|
relayState = false;
|
||||||
digitalWrite(RELAY, LOW);
|
} else if (msgString == "reset"){
|
||||||
} else if (msgString == "reset"){
|
ESP.restart();
|
||||||
// TODO : esp restart
|
}
|
||||||
|
mqttSendState();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void mqttSendState()
|
||||||
|
{
|
||||||
|
client.publish(MQTT_STATE, (relayState) ? "ON": "OFF", true);
|
||||||
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
// OTA
|
// OTA
|
||||||
ArduinoOTA.handle();
|
ArduinoOTA.handle();
|
||||||
@ -179,30 +180,37 @@ void loop() {
|
|||||||
// MQTT
|
// MQTT
|
||||||
testConnectMQTT();
|
testConnectMQTT();
|
||||||
client.loop();
|
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) {
|
void blinkLED(int pin, int duration, int n) {
|
||||||
for(int i=0; i<n; i++) {
|
for(int i = 0; i < n; i++) {
|
||||||
digitalWrite(pin, HIGH);
|
digitalWrite(LED_PIN, HIGH);
|
||||||
delay(duration);
|
delay(duration);
|
||||||
digitalWrite(pin, LOW);
|
digitalWrite(LED_PIN, LOW);
|
||||||
delay(duration);
|
delay(duration);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void button() {
|
void button() {
|
||||||
if (!digitalRead(BUTTON)) {
|
if (!digitalRead(BUTTON_PIN)) {
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (count > 1 && count <= 40) {
|
if (count > 1 && count <= 40) {
|
||||||
digitalWrite(LED, !digitalRead(LED));
|
digitalWrite(LED_PIN, !digitalRead(LED_PIN));
|
||||||
digitalWrite(RELAY, !digitalRead(RELAY));
|
digitalWrite(RELAY_PIN, !digitalRead(RELAY_PIN));
|
||||||
}
|
}
|
||||||
else if (count >40){
|
else if (count > 40) {
|
||||||
Serial.println("\n\nSonoff Rebooting . . . . . . . . Please Wait");
|
ESP.restart();
|
||||||
// TODO : esp restart
|
|
||||||
}
|
}
|
||||||
count=0;
|
count = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -35,6 +35,8 @@ PubSubClient client(espClient);
|
|||||||
Ticker btn_timer;
|
Ticker btn_timer;
|
||||||
unsigned long count = 0;
|
unsigned long count = 0;
|
||||||
|
|
||||||
|
bool relayState;
|
||||||
|
|
||||||
void setup();
|
void setup();
|
||||||
void loop();
|
void loop();
|
||||||
void blinkLED(int pin, int duration, int n);
|
void blinkLED(int pin, int duration, int n);
|
||||||
@ -44,3 +46,4 @@ void setupWifi();
|
|||||||
void testConnectMQTT();
|
void testConnectMQTT();
|
||||||
void callbackMQTT(char* topic, byte* payload, unsigned int length);
|
void callbackMQTT(char* topic, byte* payload, unsigned int length);
|
||||||
void setupSonOff();
|
void setupSonOff();
|
||||||
|
void mqttSendState();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user