Skip to content

Commit

Permalink
Implement error LED and status codes. (#1006)
Browse files Browse the repository at this point in the history
Merges LED_RECEIVE/LED_SEND into LED_SEND_RECEIVE pin definition.

Adds LED_ERROR to display error status based on blink times as follows:

Disconnected from Wifi: 2 seconds on 2 seconds off.
Disconnected from broker: 2 seconds on 5 seconds off.

Other status LED indications added:
WifiManager configuration web portal active: LED_INFO and LED_SEND_RECEIVE = ON.
OTA Update in progress: LED_SEND_RECIEVE and LED_ERROR = ON.
  • Loading branch information
h2zero authored Jul 1, 2021
1 parent 76f880d commit 05dddf4
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 34 deletions.
28 changes: 14 additions & 14 deletions main/User_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -334,33 +334,33 @@ int lowpowermode = DEFAULT_LOW_POWER_MODE;
#endif

/*-------------DEFINE PINs FOR STATUS LEDs----------------*/
#ifndef LED_RECEIVE
#ifndef LED_SEND_RECEIVE
# ifdef ESP8266
# define LED_RECEIVE 40
# define LED_SEND_RECEIVE 40
# elif ESP32
# define LED_RECEIVE 40
# define LED_SEND_RECEIVE 40
# elif __AVR_ATmega2560__ //arduino mega
# define LED_RECEIVE 40
# define LED_SEND_RECEIVE 40
# else //arduino uno/nano
# define LED_RECEIVE 40
# define LED_SEND_RECEIVE 40
# endif
#endif
#ifndef LED_RECEIVE_ON
# define LED_RECEIVE_ON HIGH
#ifndef LED_SEND_RECEIVE_ON
# define LED_SEND_RECEIVE_ON HIGH
#endif
#ifndef LED_SEND
#ifndef LED_ERROR
# ifdef ESP8266
# define LED_SEND 42
# define LED_ERROR 42
# elif ESP32
# define LED_SEND 42
# define LED_ERROR 42
# elif __AVR_ATmega2560__ //arduino mega
# define LED_SEND 42
# define LED_ERROR 42
# else //arduino uno/nano
# define LED_SEND 42
# define LED_ERROR 42
# endif
#endif
#ifndef LED_SEND_ON
# define LED_SEND_ON HIGH
#ifndef LED_ERROR_ON
# define LED_ERROR_ON HIGH
#endif
#ifndef LED_INFO
# ifdef ESP8266
Expand Down
54 changes: 34 additions & 20 deletions main/main.ino
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ void pub(const char* topicori, const char* payload, bool retainFlag) {

void pub(const char* topicori, JsonObject& data) {
Log.notice(F("Subject: %s" CR), topicori);
digitalWrite(LED_RECEIVE, LED_RECEIVE_ON);
digitalWrite(LED_SEND_RECEIVE, LED_SEND_RECEIVE_ON);
logJson(data);
if (client.connected()) {
String topic = String(mqtt_topic) + String(gateway_name) + String(topicori);
Expand Down Expand Up @@ -528,10 +528,10 @@ void connectMQTT() {
#elif defined(ESP8266)
Log.warning(F("failed, ssl error code=%d" CR), ((WiFiClientSecure*)eClient)->getLastSSLError());
#endif
digitalWrite(LED_INFO, LED_INFO_ON);
delay(1000);
digitalWrite(LED_INFO, !LED_INFO_ON);
delay(4000);
digitalWrite(LED_ERROR, LED_ERROR_ON);
delay(2000);
digitalWrite(LED_ERROR, !LED_ERROR_ON);
delay(5000);
}
}

Expand Down Expand Up @@ -563,6 +563,14 @@ void setup() {
Log.begin(LOG_LEVEL, &Serial);
Log.notice(F(CR "************* WELCOME TO OpenMQTTGateway **************" CR));

//setup LED status
pinMode(LED_SEND_RECEIVE, OUTPUT);
pinMode(LED_INFO, OUTPUT);
pinMode(LED_ERROR, OUTPUT);
digitalWrite(LED_SEND_RECEIVE, !LED_SEND_RECEIVE_ON);
digitalWrite(LED_INFO, !LED_INFO_ON);
digitalWrite(LED_ERROR, !LED_ERROR_ON);

#if defined(ESP8266) || defined(ESP32)
# ifdef ESP8266
# ifndef ZgatewaySRFB // if we are not in sonoff rf bridge case we apply the ESP8266 GPIO optimization
Expand Down Expand Up @@ -600,14 +608,6 @@ void setup() {
setup_ethernet();
#endif

//setup LED status
pinMode(LED_RECEIVE, OUTPUT);
pinMode(LED_SEND, OUTPUT);
pinMode(LED_INFO, OUTPUT);
digitalWrite(LED_RECEIVE, !LED_RECEIVE_ON);
digitalWrite(LED_SEND, !LED_SEND_ON);
digitalWrite(LED_INFO, !LED_INFO_ON);

#if defined(ESP8266) || defined(ESP32)
if (mqtt_secure) {
eClient = new WiFiClientSecure;
Expand Down Expand Up @@ -819,6 +819,8 @@ void setOTA() {

ArduinoOTA.onStart([]() {
Log.trace(F("Start OTA, lock other functions" CR));
digitalWrite(LED_SEND_RECEIVE, LED_SEND_RECEIVE_ON);
digitalWrite(LED_ERROR, LED_ERROR_ON);
# if defined(ZgatewayBT) && defined(ESP32)
stopProcessing();
# endif
Expand All @@ -828,6 +830,8 @@ void setOTA() {
});
ArduinoOTA.onEnd([]() {
Log.trace(F("\nOTA done" CR));
digitalWrite(LED_SEND_RECEIVE, !LED_SEND_RECEIVE_ON);
digitalWrite(LED_ERROR, !LED_ERROR_ON);
# if defined(ZgatewayBT) && defined(ESP32)
startProcessing();
# endif
Expand All @@ -839,6 +843,8 @@ void setOTA() {
Log.trace(F("Progress: %u%%\r" CR), (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
digitalWrite(LED_SEND_RECEIVE, !LED_SEND_RECEIVE_ON);
digitalWrite(LED_ERROR, !LED_ERROR_ON);
# if defined(ZgatewayBT) && defined(ESP32)
startProcessing();
# endif
Expand Down Expand Up @@ -1137,6 +1143,8 @@ void setup_wifimanager(bool reset_settings) {
}
# endif

digitalWrite(LED_ERROR, LED_ERROR_ON);
digitalWrite(LED_INFO, LED_INFO_ON);
Log.notice(F("Connect your phone to WIFI AP: %s with PWD: %s" CR), WifiManager_ssid, WifiManager_password);
//fetches ssid and pass and tries to connect
//if it does not connect it starts an access point with the specified name
Expand All @@ -1152,6 +1160,8 @@ void setup_wifimanager(bool reset_settings) {
# endif
delay(5000);
}
digitalWrite(LED_ERROR, !LED_ERROR_ON);
digitalWrite(LED_INFO, !LED_INFO_ON);
}

# if defined(ZboardM5STICKC) || defined(ZboardM5STICKCP) || defined(ZboardM5STACK)
Expand Down Expand Up @@ -1289,9 +1299,8 @@ void loop() {
// Switch off of the LED after TimeLedON
if (now > (timer_led_measures + (TimeLedON * 1000))) {
timer_led_measures = millis();
digitalWrite(LED_RECEIVE, !LED_RECEIVE_ON);
digitalWrite(LED_INFO, !LED_INFO_ON);
digitalWrite(LED_SEND, !LED_SEND_ON);
digitalWrite(LED_SEND_RECEIVE, !LED_SEND_RECEIVE_ON);
}

#if defined(ESP8266) || defined(ESP32)
Expand Down Expand Up @@ -1428,10 +1437,10 @@ void loop() {
}
} else { // disconnected from network
Log.warning(F("Network disconnected:" CR));
digitalWrite(LED_INFO, LED_INFO_ON);
delay(5000); // add a delay to avoid ESP32 crash and reset
digitalWrite(LED_INFO, !LED_INFO_ON);
delay(5000);
digitalWrite(LED_ERROR, LED_ERROR_ON);
delay(2000); // add a delay to avoid ESP32 crash and reset
digitalWrite(LED_ERROR, !LED_ERROR_ON);
delay(2000);
#if defined(ESP8266) || defined(ESP32) && !defined(ESP32_ETHERNET)
# ifdef ESP32 // If used with ESP8266 this method prevent the reconnection
WiFi.reconnect();
Expand Down Expand Up @@ -1663,7 +1672,7 @@ void receivingMQTT(char* topicOri, char* datacallback) {
MQTTHttpsFWUpdate(topicOri, jsondata);
# endif
#endif
digitalWrite(LED_SEND, LED_SEND_ON);
digitalWrite(LED_SEND_RECEIVE, LED_SEND_RECEIVE_ON);

MQTTtoSYS(topicOri, jsondata);
} else { // not a json object --> simple decoding
Expand Down Expand Up @@ -1734,6 +1743,8 @@ void MQTTHttpsFWUpdate(char* topicOri, JsonObject& HttpsFwUpdateData) {
# endif

Log.warning(F("Starting firmware update" CR));
digitalWrite(LED_SEND_RECEIVE, LED_SEND_RECEIVE_ON);
digitalWrite(LED_ERROR, LED_ERROR_ON);

# if defined(ZgatewayBT) && defined(ESP32)
stopProcessing();
Expand Down Expand Up @@ -1806,6 +1817,9 @@ void MQTTHttpsFWUpdate(char* topicOri, JsonObject& HttpsFwUpdateData) {
break;
}

digitalWrite(LED_SEND_RECEIVE, !LED_SEND_RECEIVE_ON);
digitalWrite(LED_ERROR, !LED_ERROR_ON);

# if defined(ZgatewayBT) && defined(ESP32)
startProcessing();
# endif
Expand Down

0 comments on commit 05dddf4

Please sign in to comment.