diff --git a/README.md b/README.md index 8221ca5..6d46dca 100644 --- a/README.md +++ b/README.md @@ -150,7 +150,7 @@ platformio device monitor -b 115200 * LAN_INIT_DELAY=2000 // set lan init delay for Wiznet ethernet shield * ESP_WIFI_AP=MYAP // esp wifi access point name * ESP_WIFI_PWD=MYPWD // esp wifi access point password -* WITHOUT_DHT //disable DHT Input support +* DHT_DISABLE //disable DHT Input support diff --git a/build_flags_template.sh b/build_flags_template.sh index 0ec4b85..5c8c933 100644 --- a/build_flags_template.sh +++ b/build_flags_template.sh @@ -23,6 +23,8 @@ export FLAGS="$FLAGS -DCONTROLLINO" export FLAGS="$FLAGS -DESP_WIFI_AP=MYAP" export FLAGS="$FLAGS -DESP_WIFI_PWD=MYPWD" + export FLAGS="$FLAGS -DDHT_DISABLE" + export FLAGS="$FLAGS -DRESET_PIN=5" export PLATFORMIO_BUILD_FLAGS="$FLAGS" echo PLATFORMIO_BUILD_FLAGS=$PLATFORMIO_BUILD_FLAGS echo "==============================================Custom build flags END=====================================================" diff --git a/config-examples/c4-3e-11-03-1b-1e.config.json b/config-examples/c4-3e-11-03-1b-1e.config.json index 67d98a8..fe0266b 100644 --- a/config-examples/c4-3e-11-03-1b-1e.config.json +++ b/config-examples/c4-3e-11-03-1b-1e.config.json @@ -1,5 +1,5 @@ { - "mqtt":["garden","192.168.10.115",1883,"test","test"], + "mqtt":["garden","192.168.10.114",1883,"test","test"], "ow":{ "28FFADCE601705A3":{"emit":"t_soil1","item":"h_relay6"}, "286164123FF96F55":{"emit":"t_soil2"}, @@ -17,6 +17,7 @@ "h_auto":[6,29,1,1] }, "in":{ + "5": {"T":4,"emit": "/myhome/s_out/t_dht1"}, "40":{"emit":"/myhome/s_out/g_in1","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"}, diff --git a/config-examples/c4-3e-1f-03-1b-1b.config.json b/config-examples/c4-3e-1f-03-1b-1b.config.json index 7a9adbd..501a30d 100755 --- a/config-examples/c4-3e-1f-03-1b-1b.config.json +++ b/config-examples/c4-3e-1f-03-1b-1b.config.json @@ -6,4 +6,4 @@ "in": { "5": {"T":4,"emit": "/myhome/s_out/t_vent_street"} } -} +} \ No newline at end of file diff --git a/lighthub/inputs.cpp b/lighthub/inputs.cpp index 76e5fec..b1a5dd8 100644 --- a/lighthub/inputs.cpp +++ b/lighthub/inputs.cpp @@ -22,7 +22,7 @@ e-mail anklimov@gmail.com #include "item.h" #include -#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; diff --git a/lighthub/inputs.h b/lighthub/inputs.h index 41e0e74..6055503 100644 --- a/lighthub/inputs.h +++ b/lighthub/inputs.h @@ -98,4 +98,6 @@ class Input void dht22Poll(); + + void printFloatValueToStr(float temp, char *valstr); }; diff --git a/lighthub/main.cpp b/lighthub/main.cpp index 22cfa8a..fe5147c 100644 --- a/lighthub/main.cpp +++ b/lighthub/main.cpp @@ -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() {