mirror of
https://github.com/anklimov/lighthub
synced 2025-12-06 11:49:51 +03:00
hard and soft reset, dht22 support, reset on lan init error, reset on many mqtt errors
This commit is contained in:
@@ -150,7 +150,7 @@ platformio device monitor -b 115200
|
|||||||
* LAN_INIT_DELAY=2000 // set lan init delay for Wiznet ethernet shield
|
* LAN_INIT_DELAY=2000 // set lan init delay for Wiznet ethernet shield
|
||||||
* ESP_WIFI_AP=MYAP // esp wifi access point name
|
* ESP_WIFI_AP=MYAP // esp wifi access point name
|
||||||
* ESP_WIFI_PWD=MYPWD // esp wifi access point password
|
* ESP_WIFI_PWD=MYPWD // esp wifi access point password
|
||||||
* WITHOUT_DHT //disable DHT Input support
|
* DHT_DISABLE //disable DHT Input support
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -23,6 +23,8 @@
|
|||||||
export FLAGS="$FLAGS -DCONTROLLINO"
|
export FLAGS="$FLAGS -DCONTROLLINO"
|
||||||
export FLAGS="$FLAGS -DESP_WIFI_AP=MYAP"
|
export FLAGS="$FLAGS -DESP_WIFI_AP=MYAP"
|
||||||
export FLAGS="$FLAGS -DESP_WIFI_PWD=MYPWD"
|
export FLAGS="$FLAGS -DESP_WIFI_PWD=MYPWD"
|
||||||
|
export FLAGS="$FLAGS -DDHT_DISABLE"
|
||||||
|
export FLAGS="$FLAGS -DRESET_PIN=5"
|
||||||
export PLATFORMIO_BUILD_FLAGS="$FLAGS"
|
export PLATFORMIO_BUILD_FLAGS="$FLAGS"
|
||||||
echo PLATFORMIO_BUILD_FLAGS=$PLATFORMIO_BUILD_FLAGS
|
echo PLATFORMIO_BUILD_FLAGS=$PLATFORMIO_BUILD_FLAGS
|
||||||
echo "==============================================Custom build flags END====================================================="
|
echo "==============================================Custom build flags END====================================================="
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"mqtt":["garden","192.168.10.115",1883,"test","test"],
|
"mqtt":["garden","192.168.10.114",1883,"test","test"],
|
||||||
"ow":{
|
"ow":{
|
||||||
"28FFADCE601705A3":{"emit":"t_soil1","item":"h_relay6"},
|
"28FFADCE601705A3":{"emit":"t_soil1","item":"h_relay6"},
|
||||||
"286164123FF96F55":{"emit":"t_soil2"},
|
"286164123FF96F55":{"emit":"t_soil2"},
|
||||||
@@ -17,6 +17,7 @@
|
|||||||
"h_auto":[6,29,1,1]
|
"h_auto":[6,29,1,1]
|
||||||
},
|
},
|
||||||
"in":{
|
"in":{
|
||||||
|
"5": {"T":4,"emit": "/myhome/s_out/t_dht1"},
|
||||||
"40":{"emit":"/myhome/s_out/g_in1","scmd":"CLOSED","rcmd":"OPEN"},
|
"40":{"emit":"/myhome/s_out/g_in1","scmd":"CLOSED","rcmd":"OPEN"},
|
||||||
"41":{"emit":"/myhome/s_out/g_in2","scmd":"CLOSED","rcmd":"OPEN"},
|
"41":{"emit":"/myhome/s_out/g_in2","scmd":"CLOSED","rcmd":"OPEN"},
|
||||||
"42":{"emit":"/myhome/s_out/g_in3","scmd":"CLOSED","rcmd":"OPEN"},
|
"42":{"emit":"/myhome/s_out/g_in3","scmd":"CLOSED","rcmd":"OPEN"},
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ e-mail anklimov@gmail.com
|
|||||||
#include "item.h"
|
#include "item.h"
|
||||||
#include <PubSubClient.h>
|
#include <PubSubClient.h>
|
||||||
|
|
||||||
#ifndef WITHOUT_DHT
|
#ifndef DHT_DISABLE
|
||||||
#include "DHT.h"
|
#include "DHT.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -96,7 +96,7 @@ int Input::poll() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Input::dht22Poll() {
|
void Input::dht22Poll() {
|
||||||
#ifndef WITHOUT_DHT
|
#ifndef DHT_DISABLE
|
||||||
if (store->nextPollMillis > millis())
|
if (store->nextPollMillis > millis())
|
||||||
return;
|
return;
|
||||||
DHT dht(pin, DHT22);
|
DHT dht(pin, DHT22);
|
||||||
@@ -110,10 +110,10 @@ void Input::dht22Poll() {
|
|||||||
char addrstr[100] = "";
|
char addrstr[100] = "";
|
||||||
strcat(addrstr, emit->valuestring);
|
strcat(addrstr, emit->valuestring);
|
||||||
strcat(addrstr, "T");
|
strcat(addrstr, "T");
|
||||||
sprintf(valstr, "%2.1f", temp);
|
printFloatValueToStr(temp, valstr);
|
||||||
mqttClient.publish(addrstr, valstr);
|
mqttClient.publish(addrstr, valstr);
|
||||||
addrstr[strlen(addrstr) - 1] = 'H';
|
addrstr[strlen(addrstr) - 1] = 'H';
|
||||||
sprintf(valstr, "%2.1f", humidity);
|
printFloatValueToStr(humidity, valstr);
|
||||||
mqttClient.publish(addrstr, valstr);
|
mqttClient.publish(addrstr, valstr);
|
||||||
store->nextPollMillis = millis() + DHT_POLL_DELAY_DEFAULT;
|
store->nextPollMillis = millis() + DHT_POLL_DELAY_DEFAULT;
|
||||||
Serial.print(" NextPollMillis=");Serial.println(store->nextPollMillis);
|
Serial.print(" NextPollMillis=");Serial.println(store->nextPollMillis);
|
||||||
@@ -123,6 +123,23 @@ void Input::dht22Poll() {
|
|||||||
#endif
|
#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() {
|
void Input::contactPoll() {
|
||||||
boolean currentInputState;
|
boolean currentInputState;
|
||||||
uint8_t inputPinMode, inputOnLevel;
|
uint8_t inputPinMode, inputOnLevel;
|
||||||
|
|||||||
@@ -98,4 +98,6 @@ class Input
|
|||||||
|
|
||||||
void dht22Poll();
|
void dht22Poll();
|
||||||
|
|
||||||
|
|
||||||
|
void printFloatValueToStr(float temp, char *valstr);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -120,10 +120,14 @@ PubSubClient mqttClient(ethClient);
|
|||||||
|
|
||||||
bool wifiInitialized;
|
bool wifiInitialized;
|
||||||
|
|
||||||
|
int mqtt_error_rate;
|
||||||
|
|
||||||
bool IsThermostat(const aJsonObject *item);
|
bool IsThermostat(const aJsonObject *item);
|
||||||
|
|
||||||
bool disabledDisconnected(const aJsonObject *thermoExtensionArray, int thermoLatestCommand);
|
bool disabledDisconnected(const aJsonObject *thermoExtensionArray, int thermoLatestCommand);
|
||||||
|
|
||||||
|
void resetFunc();
|
||||||
|
|
||||||
void watchdogSetup(void) {
|
void watchdogSetup(void) {
|
||||||
//Serial.begin(115200);
|
//Serial.begin(115200);
|
||||||
//Serial.println("Watchdog armed.");
|
//Serial.println("Watchdog armed.");
|
||||||
@@ -184,6 +188,7 @@ void mqttCallback(char *topic, byte *payload, unsigned int length) {
|
|||||||
} //valid item
|
} //valid item
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#ifndef __ESP__
|
#ifndef __ESP__
|
||||||
|
|
||||||
void printIPAddress() {
|
void printIPAddress() {
|
||||||
@@ -270,6 +275,9 @@ int lanLoop() {
|
|||||||
Serial.print(F("'ip [ip[,dns[,gw[,subnet]]]]' - set static IP\n"));
|
Serial.print(F("'ip [ip[,dns[,gw[,subnet]]]]' - set static IP\n"));
|
||||||
lanStatus = -10;
|
lanStatus = -10;
|
||||||
lanCheck = millis() + 60000;
|
lanCheck = millis() + 60000;
|
||||||
|
#ifdef RESET_PIN
|
||||||
|
resetFunc();
|
||||||
|
#endif
|
||||||
} else {
|
} else {
|
||||||
printIPAddress();
|
printIPAddress();
|
||||||
lanStatus = 1;
|
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
|
wdt_dis(); //potential unsafe for ethernetIdle(), but needed to avoid cyclic reboot if mosquitto out of order
|
||||||
if (mqttClient.connect(client_id, user, password)) {
|
if (mqttClient.connect(client_id, user, password)) {
|
||||||
|
mqtt_error_rate=0;
|
||||||
Serial.print(F("connected as "));
|
Serial.print(F("connected as "));
|
||||||
Serial.println(client_id);
|
Serial.println(client_id);
|
||||||
wdt_en();
|
wdt_en();
|
||||||
@@ -350,6 +359,13 @@ int lanLoop() {
|
|||||||
Serial.print(mqttClient.state());
|
Serial.print(mqttClient.state());
|
||||||
Serial.println(F(" try again in 5 seconds"));
|
Serial.println(F(" try again in 5 seconds"));
|
||||||
lanCheck = millis() + 5000;
|
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;
|
lanStatus = 12;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -448,6 +464,24 @@ int lanLoop() {
|
|||||||
return lanStatus;
|
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
|
#ifdef _owire
|
||||||
|
|
||||||
void Changed(int i, DeviceAddress addr, int val) {
|
void Changed(int i, DeviceAddress addr, int val) {
|
||||||
@@ -488,7 +522,6 @@ void Changed(int i, DeviceAddress addr, int val) {
|
|||||||
|
|
||||||
#endif //_owire
|
#endif //_owire
|
||||||
|
|
||||||
|
|
||||||
void cmdFunctionHelp(int arg_cnt, char **args)
|
void cmdFunctionHelp(int arg_cnt, char **args)
|
||||||
//(char* tokens)
|
//(char* tokens)
|
||||||
{
|
{
|
||||||
@@ -501,7 +534,8 @@ void cmdFunctionHelp(int arg_cnt, char **args)
|
|||||||
"'load' - load config from NVRAM\n"
|
"'load' - load config from NVRAM\n"
|
||||||
"'pwd' - define MQTT password\n"
|
"'pwd' - define MQTT password\n"
|
||||||
"'kill' - test watchdog\n"
|
"'kill' - test watchdog\n"
|
||||||
"'clear' - clear EEPROM"));
|
"'clear' - clear EEPROM\n"
|
||||||
|
"'reboot' - reboot controller"));
|
||||||
}
|
}
|
||||||
|
|
||||||
void cmdFunctionKill(int arg_cnt, char **args) {
|
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() {
|
void applyConfig() {
|
||||||
if (!root) return;
|
if (!root) return;
|
||||||
|
|
||||||
@@ -1041,7 +1080,7 @@ void printFirmwareVersionAndBuildOptions() {
|
|||||||
#else
|
#else
|
||||||
Serial.println(F("(+)OWIRE"));
|
Serial.println(F("(+)OWIRE"));
|
||||||
#endif
|
#endif
|
||||||
#ifndef WITHOUT_DHT
|
#ifndef DHT_DISABLE
|
||||||
Serial.println(F("(+)DHT"));
|
Serial.println(F("(+)DHT"));
|
||||||
#else
|
#else
|
||||||
Serial.println(F("(-)DHT"));
|
Serial.println(F("(-)DHT"));
|
||||||
@@ -1055,6 +1094,13 @@ void printFirmwareVersionAndBuildOptions() {
|
|||||||
Serial.println(F("(+)SDCARD"));
|
Serial.println(F("(+)SDCARD"));
|
||||||
#endif
|
#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("ip", cmdFunctionIp);
|
||||||
cmdAdd("pwd", cmdFunctionPwd);
|
cmdAdd("pwd", cmdFunctionPwd);
|
||||||
cmdAdd("clear",cmdFunctionClearEEPROM);
|
cmdAdd("clear",cmdFunctionClearEEPROM);
|
||||||
|
cmdAdd("reboot",cmdFunctionReboot);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop_main() {
|
void loop_main() {
|
||||||
|
|||||||
Reference in New Issue
Block a user