mirror of
https://github.com/anklimov/lighthub
synced 2025-12-09 21:29:49 +03:00
hard and soft reset, dht22 support, reset on lan init error, reset on many mqtt errors
This commit is contained in:
@@ -120,10 +120,14 @@ PubSubClient mqttClient(ethClient);
|
||||
|
||||
bool wifiInitialized;
|
||||
|
||||
int mqtt_error_rate;
|
||||
|
||||
bool IsThermostat(const aJsonObject *item);
|
||||
|
||||
bool disabledDisconnected(const aJsonObject *thermoExtensionArray, int thermoLatestCommand);
|
||||
|
||||
void resetFunc();
|
||||
|
||||
void watchdogSetup(void) {
|
||||
//Serial.begin(115200);
|
||||
//Serial.println("Watchdog armed.");
|
||||
@@ -184,6 +188,7 @@ void mqttCallback(char *topic, byte *payload, unsigned int length) {
|
||||
} //valid item
|
||||
}
|
||||
|
||||
|
||||
#ifndef __ESP__
|
||||
|
||||
void printIPAddress() {
|
||||
@@ -270,6 +275,9 @@ int lanLoop() {
|
||||
Serial.print(F("'ip [ip[,dns[,gw[,subnet]]]]' - set static IP\n"));
|
||||
lanStatus = -10;
|
||||
lanCheck = millis() + 60000;
|
||||
#ifdef RESET_PIN
|
||||
resetFunc();
|
||||
#endif
|
||||
} else {
|
||||
printIPAddress();
|
||||
lanStatus = 1;
|
||||
@@ -324,6 +332,7 @@ int lanLoop() {
|
||||
|
||||
wdt_dis(); //potential unsafe for ethernetIdle(), but needed to avoid cyclic reboot if mosquitto out of order
|
||||
if (mqttClient.connect(client_id, user, password)) {
|
||||
mqtt_error_rate=0;
|
||||
Serial.print(F("connected as "));
|
||||
Serial.println(client_id);
|
||||
wdt_en();
|
||||
@@ -350,6 +359,13 @@ int lanLoop() {
|
||||
Serial.print(mqttClient.state());
|
||||
Serial.println(F(" try again in 5 seconds"));
|
||||
lanCheck = millis() + 5000;
|
||||
#ifdef RESET_PIN
|
||||
mqtt_error_rate++;
|
||||
if(mqtt_error_rate>50){
|
||||
Serial.print(F("Too many MQTT connection errors. Resetting."));
|
||||
resetFunc();
|
||||
}
|
||||
#endif
|
||||
lanStatus = 12;
|
||||
}
|
||||
}
|
||||
@@ -448,6 +464,24 @@ int lanLoop() {
|
||||
return lanStatus;
|
||||
|
||||
}
|
||||
|
||||
void (*softResetFunc)(void) = 0;
|
||||
|
||||
void resetFunc() {
|
||||
#ifdef RESET_PIN
|
||||
Serial.println(F("Reset arduino with digital pin "));
|
||||
Serial.print(QUOTE(RESET_PIN));
|
||||
delay(1000);
|
||||
pinMode(RESET_PIN, OUTPUT);
|
||||
digitalWrite(RESET_PIN,LOW);
|
||||
delay(1000);
|
||||
digitalWrite(RESET_PIN,HIGH);
|
||||
#endif
|
||||
Serial.println(F("Hardware reset not working! Use soft reset... "));
|
||||
delay(1000);
|
||||
softResetFunc();
|
||||
}
|
||||
|
||||
#ifdef _owire
|
||||
|
||||
void Changed(int i, DeviceAddress addr, int val) {
|
||||
@@ -488,7 +522,6 @@ void Changed(int i, DeviceAddress addr, int val) {
|
||||
|
||||
#endif //_owire
|
||||
|
||||
|
||||
void cmdFunctionHelp(int arg_cnt, char **args)
|
||||
//(char* tokens)
|
||||
{
|
||||
@@ -501,7 +534,8 @@ void cmdFunctionHelp(int arg_cnt, char **args)
|
||||
"'load' - load config from NVRAM\n"
|
||||
"'pwd' - define MQTT password\n"
|
||||
"'kill' - test watchdog\n"
|
||||
"'clear' - clear EEPROM"));
|
||||
"'clear' - clear EEPROM\n"
|
||||
"'reboot' - reboot controller"));
|
||||
}
|
||||
|
||||
void cmdFunctionKill(int arg_cnt, char **args) {
|
||||
@@ -511,6 +545,11 @@ void cmdFunctionKill(int arg_cnt, char **args) {
|
||||
};
|
||||
}
|
||||
|
||||
void cmdFunctionReboot(int arg_cnt, char **args) {
|
||||
Serial.println(F("Rebooting..."));
|
||||
resetFunc();
|
||||
}
|
||||
|
||||
void applyConfig() {
|
||||
if (!root) return;
|
||||
|
||||
@@ -1041,7 +1080,7 @@ void printFirmwareVersionAndBuildOptions() {
|
||||
#else
|
||||
Serial.println(F("(+)OWIRE"));
|
||||
#endif
|
||||
#ifndef WITHOUT_DHT
|
||||
#ifndef DHT_DISABLE
|
||||
Serial.println(F("(+)DHT"));
|
||||
#else
|
||||
Serial.println(F("(-)DHT"));
|
||||
@@ -1055,6 +1094,13 @@ void printFirmwareVersionAndBuildOptions() {
|
||||
Serial.println(F("(+)SDCARD"));
|
||||
#endif
|
||||
|
||||
#ifdef RESET_PIN
|
||||
Serial.println(F("(+)HARDRESET on pin="));
|
||||
Serial.print(F(QUOTE(RESET_PIN)));
|
||||
#else
|
||||
Serial.println("(-)HARDRESET, using soft");
|
||||
#endif
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1095,6 +1141,7 @@ void setupCmdArduino() {
|
||||
cmdAdd("ip", cmdFunctionIp);
|
||||
cmdAdd("pwd", cmdFunctionPwd);
|
||||
cmdAdd("clear",cmdFunctionClearEEPROM);
|
||||
cmdAdd("reboot",cmdFunctionReboot);
|
||||
}
|
||||
|
||||
void loop_main() {
|
||||
|
||||
Reference in New Issue
Block a user