diff --git a/README.md b/README.md index d8a43ae..8221ca5 100644 --- a/README.md +++ b/README.md @@ -150,6 +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 @@ -170,5 +171,6 @@ platformio device monitor -b 115200 * Defailt MQTT input topic: /myhome/in * Default MQTT topic to publish device status: /myhome/s_out * Default Alarm output topic /alarm +* DHT support enabled If you've using Arduino IDE to compile & flash firmware, it will use Default options above and you will not able to configure additional compilers options except edit "options.h" file diff --git a/config-examples/c4-3e-11-03-1b-1e.config.json b/config-examples/c4-3e-11-03-1b-1e.config.json new file mode 100644 index 0000000..67d98a8 --- /dev/null +++ b/config-examples/c4-3e-11-03-1b-1e.config.json @@ -0,0 +1,29 @@ +{ + "mqtt":["garden","192.168.10.115",1883,"test","test"], + "ow":{ + "28FFADCE601705A3":{"emit":"t_soil1","item":"h_relay6"}, + "286164123FF96F55":{"emit":"t_soil2"}, + "28FFEF6D60170335":{"emit":"t_soil3"} + }, + "items":{ + "h_relay1":[6,39,1,1], + "h_relay2":[6,38,1,1], + "h_relay3":[6,37,1,1], + "h_relay4":[6,36,1,1], + "h_relay5":[6,35,1,1], + "h_relay6":[5,34,27,1], + "h_relay7":[6,33,1,1], + "h_relay8":[6,32,1,1], + "h_auto":[6,29,1,1] + }, + "in":{ + "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"}, + "43":{"emit":"/myhome/s_out/g_in4","scmd":"CLOSED","rcmd":"OPEN"}, + "44":{"emit":"/myhome/s_out/g_in5","scmd":"CLOSED","rcmd":"OPEN"}, + "45":{"emit":"/myhome/s_out/g_in6","scmd":"CLOSED","rcmd":"OPEN"}, + "46":{"emit":"/myhome/s_out/g_in7","scmd":"CLOSED","rcmd":"OPEN"}, + "47":{"emit":"/myhome/s_out/g_in8","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 new file mode 100755 index 0000000..7a9adbd --- /dev/null +++ b/config-examples/c4-3e-1f-03-1b-1b.config.json @@ -0,0 +1,9 @@ +{ + "mqtt": ["test_esp","192.168.10.1",1883,"test","test"], + "items":{ + "h_speed0": [6,9], + "h_speed1": [6,10]}, + "in": { + "5": {"T":4,"emit": "/myhome/s_out/t_vent_street"} + } +} diff --git a/config/de-ad-be-ef-fe-ed.config.json b/config-examples/de-ad-be-ef-fe-ed.config.json similarity index 100% rename from config/de-ad-be-ef-fe-ed.config.json rename to config-examples/de-ad-be-ef-fe-ed.config.json diff --git a/config/de-ad-be-ef-fe-ef.config.json b/config-examples/de-ad-be-ef-fe-ef.config.json similarity index 100% rename from config/de-ad-be-ef-fe-ef.config.json rename to config-examples/de-ad-be-ef-fe-ef.config.json diff --git a/config/c4-3e-11-03-1b-1e.config.json b/config/c4-3e-11-03-1b-1e.config.json deleted file mode 100644 index 037f27d..0000000 --- a/config/c4-3e-11-03-1b-1e.config.json +++ /dev/null @@ -1,29 +0,0 @@ -{ - "mqtt":["garden","192.168.10.115",1883,"test","test"], - "ow":{ - "28FFADCE601705A3":{"emit":"t_soil1","item":"h_relay6"}, - "286164123FF96F55":{"emit":"t_soil2"}, - "28FFEF6D60170335":{"emit":"t_soil3"} - }, - "items":{ - "h_relay1":[6,39,1,1], - "h_relay2":[6,38,1,1], - "h_relay3":[6,37,1,1], - "h_relay4":[6,36,1,1], - "h_relay5":[6,35,1,1], - "h_relay6":[5,34,23], - "h_relay7":[6,33,1,1], - "h_relay8":[6,32,1,1], - "h_auto":[6,29,1,1] - }, - "in":{ - "40":{"emit":"/garden/s_in/1"}, - "41":{"emit":"/garden/s_in/2"}, - "42":{"emit":"/garden/s_in/3"}, - "43":{"emit":"/garden/s_in/4"}, - "44":{"emit":"/garden/s_in/5"}, - "45":{"emit":"/garden/s_in/6"}, - "46":{"emit":"/garden/s_in/7"}, - "47":{"emit":"/garden/s_in/8"} - } -} diff --git a/lighthub/dmx.cpp b/lighthub/dmx.cpp index e9c8280..e46d283 100644 --- a/lighthub/dmx.cpp +++ b/lighthub/dmx.cpp @@ -139,7 +139,7 @@ for (short tch=0; tch<=3 ; tch++) { D_State |= (1< "); Serial.print(t);Serial.println(); + //Serial.print("onContactChanged :"); Serial.print(DMXin[tch*4+trh]); Serial.print(" => "); Serial.print(t);Serial.println(); DMXin[base+trh]=t; //DMXImmediateUpdate(tch,trh,t); //break; diff --git a/lighthub/esp.cpp b/lighthub/esp.cpp deleted file mode 100644 index 5348513..0000000 --- a/lighthub/esp.cpp +++ /dev/null @@ -1,54 +0,0 @@ -#include "options.h" -#ifdef __ESP__ -#include "esp.h" - -ESP8266WiFiMulti wifiMulti; -WiFiClient ethClient; - -char mqtt_password[16]; - -//default custom static IP -//char static_ip[16] = "10.0.1.56"; -//char static_gw[16] = "10.0.1.1"; -//char static_sn[16] = "255.255.255.0"; - -//flag for saving data -bool shouldSaveConfig = false; - -//callback notifying us of the need to save config -void saveConfigCallback () { - Serial.println(F("Should save config")); - shouldSaveConfig = true; -} - - -void espSetup () { - Serial.println(F("Setting up Wifi")); - shouldSaveConfig = true; - //WiFiManager - - WiFiManagerParameter custom_mqtt_password("", "mqtt password", mqtt_password, 16); - //Local intialization. Once its business is done, there is no need to keep it around - WiFiManager wifiManager; - - wifiManager.setSaveConfigCallback(saveConfigCallback); - - wifiManager.addParameter(&custom_mqtt_password); - wifiManager.setMinimumSignalQuality(); - -if (!wifiManager.autoConnect()) { - Serial.println(F("failed to connect and hit timeout")); - delay(3000); - //reset and try again, or maybe put it to deep sleep - ESP.reset(); - delay(5000); - } - - //if you get here you have connected to the WiFi - Serial.println(F("connected...yeey :)")); - - //read updated parameters - strcpy(mqtt_password, custom_mqtt_password.getValue()); - -} -#endif diff --git a/lighthub/esp.h b/lighthub/esp.h deleted file mode 100644 index 2238e09..0000000 --- a/lighthub/esp.h +++ /dev/null @@ -1,24 +0,0 @@ -#include -//needed for library -#include -#include -#include //https://github.com/tzapu/WiFiManager -#include - -extern ESP8266WiFiMulti wifiMulti; -extern WiFiClient ethClient; -//WiFiManager wifiManager; - -//define your default values here, if there are different values in config.json, they are overwritten. -//length should be max size + 1 -extern char mqtt_password[16]; - -//default custom static IP -//char static_ip[16] = "10.0.1.56"; -//char static_gw[16] = "10.0.1.1"; -//char static_sn[16] = "255.255.255.0"; - -//flag for saving data -extern bool shouldSaveConfig; - -void espSetup (); diff --git a/lighthub/inputs.cpp b/lighthub/inputs.cpp index c5011dd..76e5fec 100644 --- a/lighthub/inputs.cpp +++ b/lighthub/inputs.cpp @@ -19,11 +19,15 @@ e-mail anklimov@gmail.com */ #include "inputs.h" -#include "aJSON.h" #include "item.h" #include +#ifndef WITHOUT_DHT +#include "DHT.h" +#endif + extern PubSubClient mqttClient; +//DHT dht(); Input::Input(char * name) //Constructor { @@ -56,59 +60,94 @@ boolean Input::isValid () void Input::Parse() { - store = NULL; - inType = 0; - pin = 0; + store = NULL; + inType = 0; + pin = 0; - if (inputObj && (inputObj->type==aJson_Object)) - { - aJsonObject * s; + if (inputObj && (inputObj->type == aJson_Object)) { + aJsonObject *s; - s = aJson.getObjectItem(inputObj,"T"); - if (s) inType = s->valueint; + s = aJson.getObjectItem(inputObj, "T"); + if (s) inType = s->valueint; - pin = atoi(inputObj->name); + pin = atoi(inputObj->name); + s = aJson.getObjectItem(inputObj, "S"); + if (!s) { + Serial.print(F("In: ")); + Serial.print(pin); + Serial.print(F("/")); + Serial.println(inType); + aJson.addNumberToObject(inputObj, "S", 0); + s = aJson.getObjectItem(inputObj, "S"); + } - s = aJson.getObjectItem(inputObj,"S"); - if (!s) { Serial.print(F("In: "));Serial.print(pin);Serial.print(F("/"));Serial.println(inType); - aJson.addNumberToObject(inputObj,"S", 0); - s = aJson.getObjectItem(inputObj,"S"); - } - - if (s) store= (inStore *) &s->valueint; - } + if (s) store = (inStore *) &s->valueint; + } } -int Input::Poll() -{ - boolean v; - if (!isValid()) return -1; - - - if (inType & IN_ACTIVE_HIGH) - { pinMode(pin, INPUT); - v = (digitalRead(pin)==HIGH); - } - else - { pinMode(pin, INPUT_PULLUP); - v = (digitalRead(pin)==LOW); - } - if (v!=store->cur) // value changed - { - if (store->bounce) store->bounce--; - else //confirmed change - { - Changed(v); - store->cur=v; - } - } - else // no change - store->bounce=3; - return 0; +int Input::poll() { + if (!isValid()) return -1; + if (inType & IN_PUSH_ON) + contactPoll(); + else if (inType & IN_DHT22) + dht22Poll(); + return 0; } -void Input::Changed (int val) +void Input::dht22Poll() { +#ifndef WITHOUT_DHT + if (store->nextPollMillis > millis()) + return; + DHT dht(pin, DHT22); + float temp = dht.readTemperature(); + float humidity = dht.readHumidity(); + aJsonObject *emit = aJson.getObjectItem(inputObj, "emit"); + Serial.print(F("IN:"));Serial.print(pin);Serial.print(F(" DHT22 type. T="));Serial.print(temp); + Serial.print(F("°C H="));Serial.print(humidity);Serial.print(F("%")); + if (emit && temp && humidity && temp == temp && humidity == humidity) { + char valstr[10]; + char addrstr[100] = ""; + strcat(addrstr, emit->valuestring); + strcat(addrstr, "T"); + sprintf(valstr, "%2.1f", temp); + mqttClient.publish(addrstr, valstr); + addrstr[strlen(addrstr) - 1] = 'H'; + sprintf(valstr, "%2.1f", humidity); + mqttClient.publish(addrstr, valstr); + store->nextPollMillis = millis() + DHT_POLL_DELAY_DEFAULT; + Serial.print(" NextPollMillis=");Serial.println(store->nextPollMillis); + } + else + store->nextPollMillis = millis() + DHT_POLL_DELAY_DEFAULT/3; +#endif +} + +void Input::contactPoll() { + boolean currentInputState; + uint8_t inputPinMode, inputOnLevel; + if (inType & IN_ACTIVE_HIGH) { + inputOnLevel = HIGH; + inputPinMode = INPUT; + } else { + inputOnLevel = LOW; + inputPinMode = INPUT_PULLUP; + } + pinMode(pin, inputPinMode); + currentInputState = (digitalRead(pin) == inputOnLevel); + if (currentInputState != store->currentValue) // value changed + { + if (store->bounce) store->bounce = store->bounce - 1; + else //confirmed change + { + onContactChanged(currentInputState); + store->currentValue = currentInputState; + } + } else // no change + store->bounce = SAME_STATE_ATTEMPTS; +} + +void Input::onContactChanged(int val) { Serial.print(F("IN:")); Serial.print(pin);Serial.print(F("="));Serial.println(val); aJsonObject * item = aJson.getObjectItem(inputObj,"item"); diff --git a/lighthub/inputs.h b/lighthub/inputs.h index b93c02b..41e0e74 100644 --- a/lighthub/inputs.h +++ b/lighthub/inputs.h @@ -18,7 +18,7 @@ e-mail anklimov@gmail.com */ -#include "aJSON.h" +#include #define IN_ACTIVE_HIGH 2 // High level = PUSHED/ CLOSED/ ON othervise :Low Level #define IN_ANALOG 64 // Analog input @@ -26,7 +26,9 @@ e-mail anklimov@gmail.com #define IN_PUSH_ON 0 // PUSH - ON, Release - OFF (ovverrided by pcmd/rcmd) - DEFAULT #define IN_PUSH_TOGGLE 1 // Every physicall push toggle logical switch on/off +#define IN_DHT22 4 +#define SAME_STATE_ATTEMPTS 3 // in syntaxis // "pin": { "T":"N", "emit":"out_emit", item:"out_item", "scmd": "ON,OFF,TOGGLE,INCREASE,DECREASE", "rcmd": "ON,OFF,TOGGLE,INCREASE,DECREASE", "rcmd":"repeat_command" } @@ -57,19 +59,20 @@ e-mail anklimov@gmail.com //"pin1": { "T":"0", "emit":"/light1", item:"light1", "scmd": "ON", repcmd:"INCREASE"} //"pin2": { "T":"0", "emit":"/light1", item:"light1", "scmd": "OFF", repcmd:"INCREASE"} + extern aJsonObject *inputs; typedef union { - long int aslong; - struct - { - int8_t reserve; - int8_t logicState; - int8_t bounce; - int8_t cur; - }; + long int aslong; + struct { + int8_t reserve; + int8_t logicState; + int8_t bounce; + int8_t currentValue; + }; + unsigned long nextPollMillis; } inStore; class Input @@ -85,9 +88,14 @@ class Input Input(char * name); boolean isValid (); - void Changed (int val); + void onContactChanged(int val); - int Poll(); + int poll(); protected: void Parse(); + + void contactPoll(); + + void dht22Poll(); + }; diff --git a/lighthub/item.cpp b/lighthub/item.cpp index e8020cf..1e6b135 100644 --- a/lighthub/item.cpp +++ b/lighthub/item.cpp @@ -741,7 +741,7 @@ OFF POLL 2101x10 [22:27:29] <= Response: 0A 03 14 00 23 00 00 27 10 13 88 0B 9C 00 32 00 F8 00 F2 06 FA 01 3F AD D0 -[22:27:29] => Poll: 0A 03 08 34 00 0A 87 18 +[22:27:29] => poll: 0A 03 08 34 00 0A 87 18 */ diff --git a/lighthub/item.h b/lighthub/item.h index ab7c6d4..bb777dc 100644 --- a/lighthub/item.h +++ b/lighthub/item.h @@ -127,7 +127,7 @@ class Item class PooledItem : public Item { public: - virtual int Changed() = 0; + virtual int onContactChanged() = 0; virtual void Idle (); protected: int PoolingInterval; diff --git a/lighthub/main.cpp b/lighthub/main.cpp index e1c2a8a..22cfa8a 100644 --- a/lighthub/main.cpp +++ b/lighthub/main.cpp @@ -64,7 +64,6 @@ ESP32 PWM Out */ - #include "Arduino.h" #include "main.h" #include "options.h" @@ -78,6 +77,12 @@ EthernetClient ethClient; EthernetClient ethClient; #endif +#ifdef __ESP__ +#include +#include +WiFiClient ethClient; +#endif + const char outprefix[] PROGMEM = OUTTOPIC; const char inprefix[] PROGMEM = INTOPIC; const char configserver[] PROGMEM = CONFIG_SERVER; @@ -91,7 +96,6 @@ aJsonObject *mqttArr = NULL; aJsonObject *modbusArr = NULL; aJsonObject *owArr = NULL; aJsonObject *dmxArr = NULL; -aJsonObject *dhtArr = NULL; unsigned long nextPollingCheck = 0; unsigned long nextInputCheck = 0; @@ -114,6 +118,12 @@ byte mac[6]; PubSubClient mqttClient(ethClient); +bool wifiInitialized; + +bool IsThermostat(const aJsonObject *item); + +bool disabledDisconnected(const aJsonObject *thermoExtensionArray, int thermoLatestCommand); + void watchdogSetup(void) { //Serial.begin(115200); //Serial.println("Watchdog armed."); @@ -213,13 +223,25 @@ int lanLoop() { case 0: //Ethernet.begin(mac,ip); { #ifdef __ESP__ - WiFi.mode(WIFI_STA); - Serial.print(F("WIFI AP/Password:")); - Serial.print(QUOTE(ESP_WIFI_AP)); - Serial.print(F("/")); - Serial.println(QUOTE(ESP_WIFI_PWD)); - wifiMulti.addAP(QUOTE(ESP_WIFI_AP), QUOTE(ESP_WIFI_PWD)); - if((wifiMulti.run() == WL_CONNECTED)) lanStatus=1; + if(!wifiInitialized) { + WiFi.mode(WIFI_STA); + Serial.print(F("WIFI AP/Password:")); + Serial.print(QUOTE(ESP_WIFI_AP)); + Serial.print(F("/")); + Serial.println(QUOTE(ESP_WIFI_PWD)); + wifi_set_macaddr(STATION_IF,mac); + WiFi.begin(QUOTE(ESP_WIFI_AP), QUOTE(ESP_WIFI_PWD)); + wifiInitialized = true; + } + + if (WiFi.status() == WL_CONNECTED) { + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); + lanStatus=1; + } + + #else IPAddress ip; IPAddress dns; @@ -426,7 +448,6 @@ int lanLoop() { return lanStatus; } - #ifdef _owire void Changed(int i, DeviceAddress addr, int val) { @@ -436,13 +457,9 @@ void Changed(int i, DeviceAddress addr, int val) { char *owEmit = NULL; char *owItem = NULL; - //PrintBytes(addr,8); - // Serial.print("Emit: "); SetBytes(addr, 8, addrbuf); addrbuf[17] = 0; - //Serial.println(addrbuf); - aJsonObject *owObj = aJson.getObjectItem(owArr, addrbuf); if (owObj) { owEmit = aJson.getObjectItem(owObj, "emit")->valuestring; @@ -453,56 +470,14 @@ void Changed(int i, DeviceAddress addr, int val) { Serial.println(val); } owItem = aJson.getObjectItem(owObj, "item")->valuestring; - } else Serial.println(F("Not find")); - - - /* No sw support anymore - switch (addr[0]){ - case 0x29: // DS2408 - snprintf(addrstr,sizeof(addrstr),"%sS0%s",outprefix,addrbuf); - // Serial.println(addrstr); - client.publish(addrstr, (val & SW_STAT0)?"ON":"OFF"); - snprintf(addrstr,sizeof(addrstr),"%sS1%s",outprefix,addrbuf); - // Serial.println(addrstr); - client.publish(addrstr, (val & SW_STAT1)?"ON":"OFF"); - snprintf(addrstr,sizeof(addrstr),"%sS2%s",outprefix,addrbuf); - // Serial.println(addrstr); - client.publish(addrstr, (val & SW_AUX0)?"OFF":"ON"); - snprintf(addrstr,sizeof(addrstr),"%sS3%s",outprefix,addrbuf); - // Serial.println(addrstr); - client.publish(addrstr, (val & SW_AUX1)?"OFF":"ON"); - break; - - case 0x28: // Thermomerer - - snprintf(addrstr,sizeof(addrstr),"%s%s",outprefix,addrbuf); - sprintf(valstr,"%d",val); - //Serial.println(val); - //Serial.println(valstr); - client.publish(addrstr, valstr); - - if (owItem) - { - thermoSetCurTemp(owItem,val); - } - break; - - case 0x01: - case 0x81: - snprintf(addrstr,sizeof(addrstr),"%sDS%s",outprefix,addrbuf); - if (val) sprintf(valstr,"%s","ON"); else sprintf(valstr,"%s","OFF"); - client.publish(addrstr, valstr); - } - */ + } else Serial.println(F("1w-item not found in config")); if ((val == -127) || (val == 85) || (val == 0)) { //ToDo: 1-w short circuit mapped to "0" celsium -// Serial.print("Temp err ");Serial.println(t); return; } strcpy_P(addrstr, outprefix); strncat(addrstr, addrbuf, sizeof(addrstr)); - //snprintf(addrstr,sizeof(addrstr),"%s%s",F(outprefix),addrbuf); sprintf(valstr, "%d", val); mqttClient.publish(addrstr, valstr); @@ -538,9 +513,7 @@ void cmdFunctionKill(int arg_cnt, char **args) { void applyConfig() { if (!root) return; -#ifdef DHT_ENABLE - dhtArr = aJson.getObjectItem(root, "dht"); -#endif + #ifdef _dmxin int itemsCount; dmxArr = aJson.getObjectItem(root, "dmxin"); @@ -630,8 +603,6 @@ void printConfigSummary() { printBool(mqttArr); Serial.print(F("1-wire ")); printBool(owArr); - Serial.print(F("dht ")); - printBool(dhtArr); } void cmdFunctionLoad(int arg_cnt, char **args) { @@ -825,27 +796,28 @@ int getConfig(int arg_cnt, char **args) Serial.println(URI); #if defined(__AVR__) - FILE *result; + FILE *configStream; //byte hserver[] = { 192,168,88,2 }; wdt_dis(); - HTTPClient hclient(configServer, 80); + HTTPClient hclientPrint(configServer, 80); // FILE is the return STREAM type of the HTTPClient - result = hclient.getURI(URI); + configStream = hclient.getURI(URI); responseStatusCode = hclient.getLastReturnCode(); wdt_en(); - if (result != NULL) { + if (configStream != NULL) { if (responseStatusCode == 200) { Serial.println(F("got Config")); - aJsonFileStream as = aJsonFileStream(result); + char c; + aJsonFileStream as = aJsonFileStream(configStream); noInterrupts(); aJson.deleteItem(root); root = aJson.parse(&as); interrupts(); // Serial.println(F("Parsed.")); - hclient.closeStream(result); // this is very important -- be sure to close the STREAM + hclient.closeStream(configStream); // this is very important -- be sure to close the STREAM if (!root) { Serial.println(F("Config parsing failed")); @@ -982,11 +954,6 @@ void setup_main() { #ifdef SD_CARD_INSERTED sd_card_w5100_setup(); #endif - -#ifdef __ESP__ - espSetup(); -#endif - setupMacAddress(); loadConfigFromEEPROM(0, NULL); @@ -1074,6 +1041,15 @@ void printFirmwareVersionAndBuildOptions() { #else Serial.println(F("(+)OWIRE")); #endif +#ifndef WITHOUT_DHT + Serial.println(F("(+)DHT")); +#else + Serial.println(F("(-)DHT")); +#endif + +#ifdef Wiz5500 + Serial.println(F("(+)Wiz5500")); +#endif #ifdef SD_CARD_INSERTED Serial.println(F("(+)SDCARD")); @@ -1210,7 +1186,7 @@ void inputLoop(void) { while (input) { if ((input->type == aJson_Object)) { Input in(input); - in.Poll(); + in.poll(); } input = input->next; } @@ -1238,63 +1214,69 @@ void pollingLoop(void) { } #endif +bool isThermostatWithMinArraySize(aJsonObject *item, int minimalArraySize) { + return (item->type == aJson_Array) && (aJson.getArrayItem(item, I_TYPE)->valueint == CH_THERMO) && + (aJson.getArraySize(item) >= minimalArraySize); +} + +bool thermoDisabledOrDisconnected(aJsonObject *thermoExtensionArray, int thermoStateCommand) { + return thermoStateCommand == CMD_OFF || thermoStateCommand == CMD_HALT || + aJson.getArrayItem(thermoExtensionArray, IET_ATTEMPTS)->valueint == 0; +} + //TODO: refactoring - void thermoLoop(void) { if (millis() < nextThermostatCheck) return; - bool thermostatCheckPrinted = false; - aJsonObject *item = items->child; - while (item) { - if ((item->type == aJson_Array) && (aJson.getArrayItem(item, 0)->valueint == CH_THERMO) && - (aJson.getArraySize(item) > 4)) { - int itemPin = aJson.getArrayItem(item, I_ARG)->valueint; - int itemTempSetting = aJson.getArrayItem(item, I_VAL)->valueint; - int itemCommand = aJson.getArrayItem(item, I_CMD)->valueint; - aJsonObject *itemExtensionArray = aJson.getArrayItem(item, I_EXT); + for (aJsonObject *thermoItem = items->child; thermoItem; thermoItem = thermoItem->next) { + if (isThermostatWithMinArraySize(thermoItem, 5)) { + aJsonObject *thermoExtensionArray = aJson.getArrayItem(thermoItem, I_EXT); + if (thermoExtensionArray && (aJson.getArraySize(thermoExtensionArray) > 1)) { + int thermoPin = aJson.getArrayItem(thermoItem, I_ARG)->valueint; + int thermoSetting = aJson.getArrayItem(thermoItem, I_VAL)->valueint; + int thermoStateCommand = aJson.getArrayItem(thermoItem, I_CMD)->valueint; + int curTemp = aJson.getArrayItem(thermoExtensionArray, IET_TEMP)->valueint; - if (itemExtensionArray && (aJson.getArraySize(itemExtensionArray) > 1)) { - int curtemp = aJson.getArrayItem(itemExtensionArray, IET_TEMP)->valueint; - if (!aJson.getArrayItem(itemExtensionArray, IET_ATTEMPTS)->valueint) { - Serial.print(item->name); + if (!aJson.getArrayItem(thermoExtensionArray, IET_ATTEMPTS)->valueint) { + Serial.print(thermoItem->name); Serial.println(F(" Expired")); } else { - if (!(--aJson.getArrayItem(itemExtensionArray, IET_ATTEMPTS)->valueint)) - mqttClient.publish("/alarm/snsr", item->name); + if (!(--aJson.getArrayItem(thermoExtensionArray, IET_ATTEMPTS)->valueint)) + mqttClient.publish("/alarm/snsr", thermoItem->name); } - if (curtemp > THERMO_OVERHEAT_CELSIUS) mqttClient.publish("/alarm/ovrht", item->name); + if (curTemp > THERMO_OVERHEAT_CELSIUS) mqttClient.publish("/alarm/ovrht", thermoItem->name); - thermostatCheckPrinted = true; - Serial.print(item->name); + + Serial.print(thermoItem->name); Serial.print(F(" Set:")); - Serial.print(itemTempSetting); - Serial.print(F(" Curtemp:")); - Serial.print(curtemp); + Serial.print(thermoSetting); + Serial.print(F(" Cur:")); + Serial.print(curTemp); Serial.print(F(" cmd:")); - Serial.print(itemCommand), pinMode(itemPin, OUTPUT); - if (itemCommand == CMD_OFF || itemCommand == CMD_HALT || - aJson.getArrayItem(itemExtensionArray, IET_ATTEMPTS)->valueint == 0) { - digitalWrite(itemPin, LOW); + Serial.print(thermoStateCommand); + pinMode(thermoPin, OUTPUT); + if (thermoDisabledOrDisconnected(thermoExtensionArray, thermoStateCommand)) { + digitalWrite(thermoPin, LOW); Serial.println(F(" OFF")); } else { - if (curtemp + THERMO_GIST_CELSIUS < itemTempSetting) { - digitalWrite(itemPin, HIGH); + if (curTemp < thermoSetting - THERMO_GIST_CELSIUS) { + digitalWrite(thermoPin, HIGH); Serial.println(F(" ON")); } //too cold - else if (itemTempSetting <= curtemp) { - digitalWrite(itemPin, LOW); + else if (curTemp >= thermoSetting) { + digitalWrite(thermoPin, LOW); Serial.println(F(" OFF")); } //Reached settings - else Serial.println(F(" --")); // Nothing to do + else Serial.println(F(" -target zone-")); // Nothing to do } + thermostatCheckPrinted = true; } } - item = item->next; } nextThermostatCheck = millis() + THERMOSTAT_CHECK_PERIOD; @@ -1306,15 +1288,13 @@ void thermoLoop(void) { #endif } - short thermoSetCurTemp(char *name, short t) { if (items) { - aJsonObject *item = aJson.getObjectItem(items, name); - if (item && (item->type == aJson_Array) && (aJson.getArrayItem(item, I_TYPE)->valueint == CH_THERMO) && - (aJson.getArraySize(item) >= 4)) { + aJsonObject *thermoItem = aJson.getObjectItem(items, name); + if (isThermostatWithMinArraySize(thermoItem, 4)) { aJsonObject *extArray = NULL; - if (aJson.getArraySize(item) == 4) //No thermo extension yet + if (aJson.getArraySize(thermoItem) == 4) //No thermo extension yet { extArray = aJson.createArray(); //Create Ext Array @@ -1322,17 +1302,17 @@ short thermoSetCurTemp(char *name, short t) { aJsonObject *oattempts = aJson.createItem(T_ATTEMPTS); //Create int aJson.addItemToArray(extArray, ocurt); aJson.addItemToArray(extArray, oattempts); - aJson.addItemToArray(item, extArray); //Adding to item - } //if - else if (extArray = aJson.getArrayItem(item, I_EXT)) { + aJson.addItemToArray(thermoItem, extArray); //Adding to thermoItem + } + else if (extArray = aJson.getArrayItem(thermoItem, I_EXT)) { aJsonObject *att = aJson.getArrayItem(extArray, IET_ATTEMPTS); aJson.getArrayItem(extArray, IET_TEMP)->valueint = t; - if (att->valueint == 0) mqttClient.publish("/alarmoff/snsr", item->name); + if (att->valueint == 0) mqttClient.publish("/alarmoff/snsr", thermoItem->name); att->valueint = (int) T_ATTEMPTS; - } //if + } + } + } - } //if - } // if items +} -} //proc diff --git a/lighthub/main.h b/lighthub/main.h index 92f154c..52fd68b 100644 --- a/lighthub/main.h +++ b/lighthub/main.h @@ -61,7 +61,6 @@ #if defined(__ESP__) #include //this needs to be first, or it all crashes and burns... -#include "esp.h" #include #include #endif diff --git a/lighthub/options.h b/lighthub/options.h index 9267257..7b8a2b8 100644 --- a/lighthub/options.h +++ b/lighthub/options.h @@ -34,6 +34,10 @@ #define INTERVAL_POLLING 100 #define THERMOSTAT_CHECK_PERIOD 5000 +#ifndef OW_UPDATE_INTERVAL +#define OW_UPDATE_INTERVAL 5000 +#endif + #ifndef MODBUS_SERIAL_BAUD #define MODBUS_SERIAL_BAUD 9600 #endif @@ -131,4 +135,6 @@ #ifndef ESP_WIFI_PWD #define ESP_WIFI_PWD mywifipass -#endif \ No newline at end of file +#endif + +#define DHT_POLL_DELAY_DEFAULT 15000 \ No newline at end of file diff --git a/lighthub/owTerm.cpp b/lighthub/owTerm.cpp index 4e13727..f965f31 100644 --- a/lighthub/owTerm.cpp +++ b/lighthub/owTerm.cpp @@ -41,7 +41,7 @@ unsigned long owTimer = 0; owChangedType owChanged; int owUpdate() { - unsigned long finish = millis() + 5000; + unsigned long finish = millis() + OW_UPDATE_INTERVAL; short sr; //net.setStrongPullup(); diff --git a/platformio.ini b/platformio.ini index 7b87a41..7ebec1e 100644 --- a/platformio.ini +++ b/platformio.ini @@ -10,10 +10,10 @@ [platformio] src_dir = lighthub env_default = -; megaatmega2560 + megaatmega2560 ; due ; esp8266 - megaatmega2560-5500 +; megaatmega2560-5500 ; due-5500 ; controllino @@ -40,6 +40,8 @@ lib_deps = FastLED SD SdFat + Adafruit Unified Sensor + DHT sensor library [env:megaatmega2560] @@ -64,6 +66,8 @@ lib_deps = https://github.com/anklimov/Artnet.git FastLED EEPROM + Adafruit Unified Sensor + DHT sensor library [env:esp8266] @@ -71,7 +75,7 @@ platform = espressif8266 framework = arduino board = nodemcuv2 lib_ldf_mode = chain+ -build_flags = !echo -n "-DPIO_SRC_REV="$(git log --pretty=format:%h_%ad -1 --date=short) +build_flags = !echo -n "-DMODBUS_DISABLE -DPIO_SRC_REV="$(git log --pretty=format:%h_%ad -1 --date=short) ;extra_scripts = pre:my_build_flags.py lib_deps = https://github.com/anklimov/Arduino-Temperature-Control-Library.git @@ -84,7 +88,10 @@ lib_deps = https://github.com/knolleary/pubsubclient.git https://github.com/anklimov/Artnet.git FastLED - WifiManager + Adafruit Unified Sensor + DHT sensor library for ESPx + DHT sensor library + [env:megaatmega2560-5500] platform = atmelavr @@ -109,6 +116,8 @@ lib_deps = https://github.com/anklimov/Artnet.git FastLED EEPROM + Adafruit Unified Sensor + DHT sensor library [env:due-5500] platform = atmelsam @@ -134,7 +143,8 @@ lib_deps = FastLED SD SdFat - + Adafruit Unified Sensor + DHT sensor library [env:controllino] platform = atmelavr @@ -159,4 +169,5 @@ lib_deps = https://github.com/anklimov/Artnet.git FastLED EEPROM - + Adafruit Unified Sensor + DHT sensor library