From 15cea6b222880cc681bf6d89ac1d8f94232e5bfd Mon Sep 17 00:00:00 2001 From: 8666 Date: Sun, 31 Dec 2017 14:31:13 +0100 Subject: [PATCH] Add support for presenting status with 3 LEDs (#147) * Add suport for presenting status with 3 LEDs * Add support for presenting status with 3 LEDs * Update Test_config.h * Update User_config.h Better names for LED variables * Change names for LED variables --- OpenMQTTGateway.ino | 48 ++++++++++++++++++++++++++++++++++++++++----- User_config.h | 8 ++++++++ tests/Test_config.h | 7 +++++++ 3 files changed, 58 insertions(+), 5 deletions(-) diff --git a/OpenMQTTGateway.ino b/OpenMQTTGateway.ino index bfa7e68..2f59fe9 100644 --- a/OpenMQTTGateway.ino +++ b/OpenMQTTGateway.ino @@ -82,6 +82,11 @@ PubSubClient client(mqtt_server, mqtt_port, callback, eClient); //MQTT last attemps reconnection date unsigned long lastReconnectAttempt = 0; +//timers for LED indicators +unsigned long timer_led_receive = 0; +unsigned long timer_led_send = 0; +unsigned long timer_led_error = 0; + boolean reconnect() { // Loop until we're reconnected while (!client.connected()) { @@ -179,6 +184,16 @@ void setup() Serial.begin(SERIAL_BAUD); //Begining ethernet connection in case of Arduino + W5100 setup_ethernet(); + //setup LED status, turn all ON for short amount then leave only the RED LED ON + pinMode(led_receive, OUTPUT); + pinMode(led_send, OUTPUT); + pinMode(led_error, OUTPUT); + digitalWrite(led_receive, LOW); + digitalWrite(led_send, LOW); + digitalWrite(led_error, LOW); + delay(500); + digitalWrite(led_receive, HIGH); + digitalWrite(led_send, HIGH); #endif delay(1500); @@ -253,9 +268,12 @@ void setup_ethernet() { void loop() { + unsigned long now = millis(); //MQTT client connexion management if (!client.connected()) { // not connected - unsigned long now = millis(); + //RED ON + digitalWrite(led_error, LOW); + if (now - lastReconnectAttempt > 5000) { lastReconnectAttempt = now; // Attempt to reconnect @@ -265,6 +283,13 @@ void loop() } } else { //connected // MQTT loop + //RED OFF + if (now - timer_led_receive > 300) { + timer_led_receive = now; + digitalWrite(led_receive, HIGH); + } + digitalWrite(led_error, HIGH); + client.loop(); #if defined(ESP8266) || defined(ESP32) @@ -291,21 +316,31 @@ void loop() #endif // Receive loop, if data received by RF433 or IR send it by MQTT #ifdef ZgatewayRF - if(RFtoMQTT()) + if(RFtoMQTT()){ trc(F("RFtoMQTT OK")); + //GREEN ON + digitalWrite(led_receive, LOW); + timer_led_receive = millis(); + } #endif #ifdef ZgatewayRF2 - if(RF2toMQTT()) + if(RF2toMQTT()){ trc(F("RF2toMQTT OK")); + digitalWrite(led_receive, LOW); + timer_led_receive = millis(); + } #endif #ifdef ZgatewaySRFB if(SRFBtoMQTT()) trc(F("SRFBtoMQTT OK")); #endif #ifdef ZgatewayIR - if(IRtoMQTT()) + if(IRtoMQTT()) { trc(F("IRtoMQTT OK")); + digitalWrite(led_receive, LOW); + timer_led_receive = millis(); delay(100); + } #endif #ifdef ZgatewayBT #ifndef ESP32 @@ -376,7 +411,8 @@ void receivingMQTT(char * topicOri, char * datacallback) { storeValue(data); trc(F("Data stored")); } - +//YELLOW ON +digitalWrite(led_send, LOW); #ifdef ZgatewayRF MQTTtoRF(topicOri, datacallback); #endif @@ -392,6 +428,8 @@ void receivingMQTT(char * topicOri, char * datacallback) { #ifdef ZgatewayRFM69 MQTTtoRFM69(topicOri, datacallback); #endif +//YELLOW OFF +digitalWrite(led_send, HIGH); } void extract_char(char * token_char, char * subset, int start ,int l, boolean reverse, boolean isNumber){ diff --git a/User_config.h b/User_config.h index 7f39ee9..3e70bef 100644 --- a/User_config.h +++ b/User_config.h @@ -64,6 +64,14 @@ const byte subnet[] = { 255, 255, 255, 0 }; //ip adress #define ota_password "OTAPASSWORD" #define ota_port 8266 +/*-------------DEFINE PINs FOR STATUS LEDs----------------*/ +#define led_receive 40 +#define led_send 42 +#define led_error 44 + +// VCC ------------D|-----------/\/\/\/\ ----------------- Arduino PIN +// LED Resistor 270-510R + /*-------------DEFINE THE MODULES YOU WANT BELOW----------------*/ //Addons and module management, comment the Z line and the config file if you don't use #ifdef ESP8266 // for nodemcu, weemos and esp8266 diff --git a/tests/Test_config.h b/tests/Test_config.h index e6508c2..d942f14 100644 --- a/tests/Test_config.h +++ b/tests/Test_config.h @@ -63,6 +63,13 @@ const byte subnet[] = { 255, 255, 255, 0 }; //ip adress #define ota_hostname "OTAHOSTNAME" #define ota_password "OTAPASSWORD" #define ota_port 8266 +/*-------------DEFINE PINs FOR STATUS LEDs----------------*/ +#define led_receive 40 +#define led_send 42 +#define led_error 44 + +// VCC ------------D|-----------/\/\/\/\ ----------------- Arduino PIN +// LED Resistor 270-510R /*-------------DEFINE THE MODULES YOU WANT BELOW----------------*/ //Addons and module management, comment the Z line and the config file if you don't use