mirror of
https://github.com/anklimov/lighthub
synced 2025-12-12 14:49:50 +03:00
hard and soft reset, dht22 support, reset on lan init error, reset on many mqtt errors
This commit is contained in:
@@ -22,7 +22,7 @@ e-mail anklimov@gmail.com
|
||||
#include "item.h"
|
||||
#include <PubSubClient.h>
|
||||
|
||||
#ifndef WITHOUT_DHT
|
||||
#ifndef DHT_DISABLE
|
||||
#include "DHT.h"
|
||||
#endif
|
||||
|
||||
@@ -96,7 +96,7 @@ int Input::poll() {
|
||||
}
|
||||
|
||||
void Input::dht22Poll() {
|
||||
#ifndef WITHOUT_DHT
|
||||
#ifndef DHT_DISABLE
|
||||
if (store->nextPollMillis > millis())
|
||||
return;
|
||||
DHT dht(pin, DHT22);
|
||||
@@ -110,10 +110,10 @@ void Input::dht22Poll() {
|
||||
char addrstr[100] = "";
|
||||
strcat(addrstr, emit->valuestring);
|
||||
strcat(addrstr, "T");
|
||||
sprintf(valstr, "%2.1f", temp);
|
||||
printFloatValueToStr(temp, valstr);
|
||||
mqttClient.publish(addrstr, valstr);
|
||||
addrstr[strlen(addrstr) - 1] = 'H';
|
||||
sprintf(valstr, "%2.1f", humidity);
|
||||
printFloatValueToStr(humidity, valstr);
|
||||
mqttClient.publish(addrstr, valstr);
|
||||
store->nextPollMillis = millis() + DHT_POLL_DELAY_DEFAULT;
|
||||
Serial.print(" NextPollMillis=");Serial.println(store->nextPollMillis);
|
||||
@@ -123,6 +123,23 @@ void Input::dht22Poll() {
|
||||
#endif
|
||||
}
|
||||
|
||||
void Input::printFloatValueToStr(float temp, char *valstr) {
|
||||
#if defined(__ESP__)
|
||||
sprintf(valstr, "%2.1f", temp);
|
||||
#endif
|
||||
#if defined(__AVR__)
|
||||
sprintf(valstr, "%d", (int)temp);
|
||||
int fractional = 10.0*((float)abs(temp)-(float)abs((int)temp));
|
||||
int val_len =strlen(valstr);
|
||||
valstr[val_len]='.';
|
||||
valstr[val_len+1]='0'+fractional;
|
||||
valstr[val_len+2]='\0';
|
||||
#endif
|
||||
#if defined(__SAM3X8E__)
|
||||
sprintf(valstr, "%2.1f", temp);
|
||||
#endif
|
||||
}
|
||||
|
||||
void Input::contactPoll() {
|
||||
boolean currentInputState;
|
||||
uint8_t inputPinMode, inputOnLevel;
|
||||
|
||||
@@ -98,4 +98,6 @@ class Input
|
||||
|
||||
void dht22Poll();
|
||||
|
||||
|
||||
void printFloatValueToStr(float temp, char *valstr);
|
||||
};
|
||||
|
||||
@@ -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