Refactor LED management (#745)
-replace low power led by led info -enable to change the led ON value -go upper case for the led macros Also transform BLE connect warning to notice in log Add Sonoff RF Bridge LED
This commit is contained in:
1 parent
5e2d86e963
commit
7b9944d1ca
6 files changed
+86
-65
No files matched your search
@@ -7,3 +7,18 @@
|
||||
## Erase the ESP settings
|
||||
|
||||
`mosquitto_pub -t "home/OpenMQTTGateway/commands/MQTTtoSYS/config" -m '{"cmd":"erase"}'`
|
||||
|
||||
# State LED usage
|
||||
|
||||
The gateway can support up to 3 LED to display its operating state:
|
||||
* LED_INFO
|
||||
switched ON when network and MQTT connection are OK
|
||||
5s ON, 5s OFF when WIFI is disconnected
|
||||
1s ON, 4s OFF when MQTT is disconnected
|
||||
|
||||
* LED_RECEIVE
|
||||
Blink for `TimeLedON` 0.5s when the gateway receive a signal from one of its module so as to send to MQTT
|
||||
|
||||
* LED_SEND
|
||||
Blink for `TimeLedON` 0.5s when the gateway send a signal with one of its module from an MQTT command
|
||||
|
||||
+27
-23
@@ -174,18 +174,12 @@ const char* certificate CERT_ATTRIBUTE = R"EOF("
|
||||
|
||||
/*------------------DEEP SLEEP parameters ------------------*/
|
||||
//DEFAULT_LOW_POWER_MODE 0 to normal mode (no power consumption optimisations)
|
||||
//DEFAULT_LOW_POWER_MODE 1 to activate deep sleep with LCD ON when a function is processing,
|
||||
//DEFAULT_LOW_POWER_MODE 2 to activate deep sleep with LED ON when a function is processing (LCD is turned OFF)
|
||||
//DEFAULT_LOW_POWER_MODE 1 to activate deep sleep
|
||||
//DEFAULT_LOW_POWER_MODE 2 to activate deep sleep (LCD is turned OFF)
|
||||
#ifdef ESP32
|
||||
# ifndef DEFAULT_LOW_POWER_MODE
|
||||
# define DEFAULT_LOW_POWER_MODE 0
|
||||
# endif
|
||||
# ifndef LOW_POWER_LED
|
||||
# define LOW_POWER_LED 2
|
||||
# endif
|
||||
# ifndef LOW_POWER_LED_OFF
|
||||
# define LOW_POWER_LED_OFF 1
|
||||
# endif
|
||||
int low_power_mode = DEFAULT_LOW_POWER_MODE;
|
||||
#endif
|
||||
|
||||
@@ -282,39 +276,48 @@ uint8_t wifiProtocol = 0; // default mode, automatic selection
|
||||
#endif
|
||||
|
||||
/*-------------DEFINE PINs FOR STATUS LEDs----------------*/
|
||||
#ifndef led_receive
|
||||
#ifndef LED_RECEIVE
|
||||
# ifdef ESP8266
|
||||
# define led_receive 40
|
||||
# define LED_RECEIVE 40
|
||||
# elif ESP32
|
||||
# define led_receive 40
|
||||
# define LED_RECEIVE 40
|
||||
# elif __AVR_ATmega2560__ //arduino mega
|
||||
# define led_receive 40
|
||||
# define LED_RECEIVE 40
|
||||
# else //arduino uno/nano
|
||||
# define led_receive 40
|
||||
# define LED_RECEIVE 40
|
||||
# endif
|
||||
#endif
|
||||
#ifndef led_send
|
||||
#ifndef LED_RECEIVE_ON
|
||||
# define LED_RECEIVE_ON HIGH
|
||||
#endif
|
||||
#ifndef LED_SEND
|
||||
# ifdef ESP8266
|
||||
# define led_send 42
|
||||
# define LED_SEND 42
|
||||
# elif ESP32
|
||||
# define led_send 42
|
||||
# define LED_SEND 42
|
||||
# elif __AVR_ATmega2560__ //arduino mega
|
||||
# define led_send 42
|
||||
# define LED_SEND 42
|
||||
# else //arduino uno/nano
|
||||
# define led_send 42
|
||||
# define LED_SEND 42
|
||||
# endif
|
||||
#endif
|
||||
#ifndef led_info
|
||||
#ifndef LED_SEND_ON
|
||||
# define LED_SEND_ON HIGH
|
||||
#endif
|
||||
#ifndef LED_INFO
|
||||
# ifdef ESP8266
|
||||
# define led_info 44
|
||||
# define LED_INFO 44
|
||||
# elif ESP32
|
||||
# define led_info 44
|
||||
# define LED_INFO 44
|
||||
# elif __AVR_ATmega2560__ //arduino mega
|
||||
# define led_info 44
|
||||
# define LED_INFO 44
|
||||
# else //arduino uno/nano
|
||||
# define led_info 44
|
||||
# define LED_INFO 44
|
||||
# endif
|
||||
#endif
|
||||
#ifndef LED_INFO_ON
|
||||
# define LED_INFO_ON HIGH
|
||||
#endif
|
||||
|
||||
#ifdef ESP8266
|
||||
//# define TRIGGER_GPIO 14 // pin D5 as full reset button (long press >10s)
|
||||
@@ -366,6 +369,7 @@ uint8_t wifiProtocol = 0; // default mode, automatic selection
|
||||
#endif
|
||||
|
||||
#define TimeBetweenReadingSYS 120 // time between (s) system readings (like memory)
|
||||
#define TimeLedON 0.5 // time LED are ON
|
||||
#define InitialMQTTConnectionTimeout 10 // time estimated (s) before the board is connected to MQTT
|
||||
#define subjectSYStoMQTT "/SYStoMQTT"
|
||||
#define subjectMQTTtoSYSset "/commands/MQTTtoSYS/config"
|
||||
|
||||
+2
-2
@@ -215,7 +215,7 @@ void drawLogo(int logoSize, int circle1X, int circle1Y, bool circle1, bool circl
|
||||
}
|
||||
|
||||
void M5Display(char* line1, char* line2, char* line3) {
|
||||
if (low_power_mode == 2) digitalWrite(LOW_POWER_LED, 1 - LOW_POWER_LED_OFF);
|
||||
if (low_power_mode == 2) digitalWrite(LED_INFO, LED_INFO_ON);
|
||||
wakeScreen(NORMAL_LCD_BRIGHTNESS);
|
||||
M5.Lcd.fillScreen(TFT_WHITE);
|
||||
drawLogo(M5.Lcd.width() * 0.1875, (M5.Lcd.width() / 2) - M5.Lcd.width() * 0.24, M5.Lcd.height() * 0.5, true, true, true, true, true, true);
|
||||
@@ -224,6 +224,6 @@ void M5Display(char* line1, char* line2, char* line3) {
|
||||
M5.Lcd.drawString(line2, 5, M5.Lcd.height() * 0.8, 1);
|
||||
M5.Lcd.drawString(line3, 5, M5.Lcd.height() * 0.9, 1);
|
||||
delay(2000);
|
||||
digitalWrite(LOW_POWER_LED, LOW_POWER_LED_OFF); // to switch off no need of condition
|
||||
digitalWrite(LED_INFO, !LED_INFO_ON); // to switch off no need of condition
|
||||
}
|
||||
#endif
|
||||
+5
-11
@@ -527,7 +527,7 @@ void notifyCB(
|
||||
String mactopic = subjectBTtoMQTT + String("/") + mac_adress;
|
||||
pub((char*)mactopic.c_str(), BLEdata);
|
||||
} else {
|
||||
Log.warning(F("Device not identified" CR));
|
||||
Log.notice(F("Device not identified" CR));
|
||||
}
|
||||
} else {
|
||||
Log.trace(F("Callback process canceled by processLock" CR));
|
||||
@@ -550,12 +550,12 @@ void BLEconnect() {
|
||||
BLEUUID charUUID("ebe0ccc1-7a0a-4b0c-8a1a-6ff2997da3a6");
|
||||
BLEAddress sensorAddress(p->macAdr);
|
||||
if (!pClient->connect(sensorAddress)) {
|
||||
Log.warning(F("Failed to find client: %s" CR), p->macAdr);
|
||||
Log.notice(F("Failed to find client: %s" CR), p->macAdr);
|
||||
NimBLEDevice::deleteClient(pClient);
|
||||
} else {
|
||||
BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
|
||||
if (!pRemoteService) {
|
||||
Log.warning(F("Failed to find service UUID: %s" CR), serviceUUID.toString().c_str());
|
||||
Log.notice(F("Failed to find service UUID: %s" CR), serviceUUID.toString().c_str());
|
||||
pClient->disconnect();
|
||||
} else {
|
||||
Log.trace(F("Found service: %s" CR), serviceUUID.toString().c_str());
|
||||
@@ -564,14 +564,14 @@ void BLEconnect() {
|
||||
Log.trace(F("Client isConnected, freeHeap: %d" CR), ESP.getFreeHeap());
|
||||
BLERemoteCharacteristic* pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
|
||||
if (!pRemoteCharacteristic) {
|
||||
Log.warning(F("Failed to find characteristic UUID: %s" CR), charUUID.toString().c_str());
|
||||
Log.notice(F("Failed to find characteristic UUID: %s" CR), charUUID.toString().c_str());
|
||||
pClient->disconnect();
|
||||
} else {
|
||||
if (pRemoteCharacteristic->canNotify()) {
|
||||
Log.trace(F("Registering notification" CR));
|
||||
pRemoteCharacteristic->subscribe(true, notifyCB);
|
||||
} else {
|
||||
Log.warning(F("Failed registering notification" CR));
|
||||
Log.notice(F("Failed registering notification" CR));
|
||||
pClient->disconnect();
|
||||
}
|
||||
}
|
||||
@@ -606,18 +606,12 @@ void coreTask(void* pvParameters) {
|
||||
if (client.state() != 0) {
|
||||
Log.warning(F("MQTT client disconnected no BLE scan" CR));
|
||||
} else {
|
||||
pinMode(LOW_POWER_LED, OUTPUT);
|
||||
if (low_power_mode == 2)
|
||||
digitalWrite(LOW_POWER_LED, 1 - LOW_POWER_LED_OFF);
|
||||
BLEscan();
|
||||
// Launching a connect every BLEscanBeforeConnect
|
||||
if (!(scanCount % BLEscanBeforeConnect) || scanCount == 1)
|
||||
BLEconnect();
|
||||
launchDiscovery();
|
||||
dumpDevices();
|
||||
// Only change LOW_POWER_LED if low power mode is enabled
|
||||
if (low_power_mode)
|
||||
digitalWrite(LOW_POWER_LED, LOW_POWER_LED_OFF);
|
||||
}
|
||||
if (low_power_mode) {
|
||||
lowPowerESP32();
|
||||
|
||||
+27
-21
@@ -144,6 +144,8 @@ bool connectedOnce = false; //indicate if we have been connected once to MQTT
|
||||
int failure_number_ntwk = 0; // number of failure connecting to network
|
||||
int failure_number_mqtt = 0; // number of failure connecting to MQTT
|
||||
|
||||
unsigned long timer_led_measures = 0;
|
||||
|
||||
#ifdef ESP32
|
||||
# include <ArduinoOTA.h>
|
||||
# include <FS.h>
|
||||
@@ -236,9 +238,9 @@ void pub(char* topicori, char* payload, bool retainFlag) {
|
||||
|
||||
void pub(char* topicori, JsonObject& data) {
|
||||
Log.notice(F("Subject: %s" CR), topicori);
|
||||
digitalWrite(LED_RECEIVE, LED_RECEIVE_ON);
|
||||
logJson(data);
|
||||
if (client.connected()) {
|
||||
digitalWrite(led_receive, HIGH);
|
||||
String topic = String(mqtt_topic) + String(topicori);
|
||||
#ifdef valueAsASubject
|
||||
SIGNAL_SIZE_UL_ULL value = data["value"];
|
||||
@@ -463,7 +465,10 @@ void connectMQTT() {
|
||||
#elif defined(SECURE_CONNECTION) && defined(ESP8266)
|
||||
Log.warning(F("failed, ssl error code=%d" CR), eClient.getLastSSLError());
|
||||
#endif
|
||||
delay(5000);
|
||||
digitalWrite(LED_INFO, LED_INFO_ON);
|
||||
delay(1000);
|
||||
digitalWrite(LED_INFO, !LED_INFO_ON);
|
||||
delay(4000);
|
||||
#if defined(ESP8266) || defined(ESP32)
|
||||
disconnection_handling(failure_number_mqtt);
|
||||
#endif
|
||||
@@ -538,12 +543,12 @@ void setup() {
|
||||
#endif
|
||||
|
||||
//setup LED status
|
||||
pinMode(led_receive, OUTPUT);
|
||||
pinMode(led_send, OUTPUT);
|
||||
pinMode(led_info, OUTPUT);
|
||||
digitalWrite(led_receive, LOW);
|
||||
digitalWrite(led_send, LOW);
|
||||
digitalWrite(led_info, LOW);
|
||||
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(MDNS_SD) && defined(ESP8266)
|
||||
Log.trace(F("Connecting to MQTT by mDNS without mqtt hostname" CR));
|
||||
@@ -1087,12 +1092,16 @@ void loop() {
|
||||
# endif
|
||||
#endif
|
||||
|
||||
digitalWrite(led_receive, LOW);
|
||||
digitalWrite(led_info, LOW);
|
||||
digitalWrite(led_send, LOW);
|
||||
|
||||
unsigned long now = millis();
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
#if defined(ESP8266) || defined(ESP32)
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
ArduinoOTA.handle();
|
||||
@@ -1104,7 +1113,7 @@ void loop() {
|
||||
#ifdef ZmqttDiscovery
|
||||
if (!connectedOnce) pubMqttDiscovery(); // at first connection we publish the discovery payloads
|
||||
#endif
|
||||
|
||||
digitalWrite(LED_INFO, LED_INFO_ON);
|
||||
connectedOnce = true;
|
||||
failure_number_ntwk = 0;
|
||||
|
||||
@@ -1199,7 +1208,10 @@ void loop() {
|
||||
} else { // disconnected from network
|
||||
#if defined(ESP8266) || defined(ESP32)
|
||||
Log.warning(F("wifi disconnected" CR));
|
||||
delay(10000); // add a delay to avoid ESP32 crash and reset
|
||||
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);
|
||||
failure_number_ntwk++;
|
||||
disconnection_handling(failure_number_ntwk);
|
||||
#else
|
||||
@@ -1413,7 +1425,6 @@ void receivingMQTT(char* topicOri, char* datacallback) {
|
||||
if (jsondata.success()) { // json object ok -> json decoding
|
||||
// log the received json
|
||||
logJson(jsondata);
|
||||
|
||||
#ifdef ZgatewayPilight // ZgatewayPilight is only defined with json publishing due to its numerous parameters
|
||||
MQTTtoPilight(topicOri, jsondata);
|
||||
#endif
|
||||
@@ -1452,8 +1463,7 @@ void receivingMQTT(char* topicOri, char* datacallback) {
|
||||
MQTTtoONOFF(topicOri, jsondata);
|
||||
# endif
|
||||
#endif
|
||||
|
||||
digitalWrite(led_send, HIGH);
|
||||
digitalWrite(LED_SEND, LED_SEND_ON);
|
||||
|
||||
MQTTtoSYS(topicOri, jsondata);
|
||||
} else { // not a json object --> simple decoding
|
||||
@@ -1486,11 +1496,7 @@ void receivingMQTT(char* topicOri, char* datacallback) {
|
||||
#ifdef ZactuatorONOFF
|
||||
MQTTtoONOFF(topicOri, datacallback);
|
||||
#endif
|
||||
|
||||
digitalWrite(led_send, HIGH);
|
||||
}
|
||||
//YELLOW OFF
|
||||
digitalWrite(led_send, HIGH);
|
||||
}
|
||||
|
||||
void MQTTtoSYS(char* topicOri, JsonObject& SYSdata) { // json object decoding
|
||||
|
||||
+10
-8
@@ -163,6 +163,8 @@ lib_deps =
|
||||
build_flags =
|
||||
${com-esp.build_flags}
|
||||
'-DZgatewaySRFB="SRFB"'
|
||||
'-DLED_INFO=13'
|
||||
'-DLED_INFO_ON=0'
|
||||
'-DGateway_Name="OpenMQTTGateway_SRFB"'
|
||||
board_build.flash_mode = dout
|
||||
|
||||
@@ -284,8 +286,8 @@ lib_deps =
|
||||
build_flags =
|
||||
${com-esp.build_flags}
|
||||
'-DZgatewayBT="BT"'
|
||||
'-DLOW_POWER_LED=2'
|
||||
'-DLOW_POWER_LED_OFF=1'
|
||||
'-DLED_RECEIVE=2'
|
||||
'-DLED_RECEIVE_ON=0'
|
||||
'-DGateway_Name="OpenMQTTGateway_ESP32_BLE"'
|
||||
|
||||
[env:esp32-m5stick-ble]
|
||||
@@ -300,8 +302,8 @@ build_flags =
|
||||
${com-esp.build_flags}
|
||||
'-DZgatewayBT="BT"'
|
||||
'-DZgatewayIR="IR"'
|
||||
'-DLOW_POWER_LED=19'
|
||||
'-DLOW_POWER_LED_OFF=0'
|
||||
'-DLED_RECEIVE=19'
|
||||
'-DLED_RECEIVE_ON=1'
|
||||
'-DTRIGGER_GPIO=35'
|
||||
'-DIR_EMITTER_GPIO=17'
|
||||
'-DGateway_Name="OpenMQTTGateway_ESP32_M5STICK_BLE_IR"'
|
||||
@@ -320,7 +322,7 @@ build_flags =
|
||||
'-DZgatewayBT="BT"'
|
||||
'-DZsensorGPIOInput="GPIOInput"'
|
||||
'-DZboardM5STACK="M5Stack"'
|
||||
'-DLOW_POWER_LED=15'
|
||||
'-DLED_RECEIVE=15'
|
||||
'-DTRIGGER_GPIO=37'
|
||||
'-DSLEEP_BUTTON=38'
|
||||
'-DINPUT_GPIO=39'
|
||||
@@ -344,8 +346,8 @@ build_flags =
|
||||
'-DZboardM5STICKC="M5StickC"'
|
||||
'-DACTUATOR_ONOFF_GPIO=10'
|
||||
'-DINPUT_GPIO=37'
|
||||
'-DLOW_POWER_LED=10'
|
||||
'-DLOW_POWER_LED_OFF=1'
|
||||
'-DLED_RECEIVE=10'
|
||||
'-DLED_RECEIVE_ON=0'
|
||||
'-DSLEEP_BUTTON=39'
|
||||
'-DTRIGGER_GPIO=39'
|
||||
'-DIR_EMITTER_INVERTED=true'
|
||||
@@ -485,7 +487,7 @@ build_flags =
|
||||
${com-esp.build_flags}
|
||||
'-DZgatewayIR="IR"'
|
||||
'-DTRIGGER_GPIO=13'
|
||||
'-Dled_send=4'
|
||||
'-DLED_SEND=4'
|
||||
'-DIR_EMITTER_GPIO=14'
|
||||
'-DIR_RECEIVER_GPIO=5'
|
||||
'-DGateway_Name="OpenMQTTGateway_AVATTO_IR"'
|
||||
|
||||
Reference in new issue
Block a user