mirror of
https://github.com/anklimov/lighthub
synced 2025-12-07 20:29:50 +03:00
Post-merge fix: no local item control without emit tag, i2c scan fixed and removed
significant stability improvements: config re-load cleanup, MQTT submit only if connected (ESPx very sensetive on it) new: negative relay pin# - output invertion
This commit is contained in:
@@ -32,6 +32,7 @@ e-mail anklimov@gmail.com
|
||||
#endif
|
||||
|
||||
extern PubSubClient mqttClient;
|
||||
extern aJsonObject *root;
|
||||
|
||||
#if !defined(DHT_DISABLE) || !defined(COUNTER_DISABLE)
|
||||
static volatile unsigned long nextPollMillisValue[5];
|
||||
@@ -93,7 +94,7 @@ void Input::Parse()
|
||||
store = NULL;
|
||||
inType = 0;
|
||||
pin = 0;
|
||||
if (inputObj && (inputObj->type == aJson_Object)) {
|
||||
if (inputObj && (inputObj->type == aJson_Object) && root) {
|
||||
aJsonObject *itemBuffer;
|
||||
itemBuffer = aJson.getObjectItem(inputObj, "T");
|
||||
if (itemBuffer) inType = static_cast<uint8_t>(itemBuffer->valueint);
|
||||
@@ -110,7 +111,7 @@ void Input::Parse()
|
||||
|
||||
void Input::setup()
|
||||
{
|
||||
if (!isValid()) return;
|
||||
if (!isValid() || (!root)) return;
|
||||
|
||||
#ifndef CSSHDC_DISABLE
|
||||
in_ccs811 ccs811(this);
|
||||
@@ -222,7 +223,8 @@ void Input::counterPoll() {
|
||||
strncpy(addrstr,emit->valuestring,sizeof(addrstr));
|
||||
if (!strchr(addrstr,'/')) setTopic(addrstr,sizeof(addrstr),T_OUT,emit->valuestring);
|
||||
sprintf(valstr, "%d", counterValue);
|
||||
mqttClient.publish(addrstr, valstr);
|
||||
if (mqttClient.connected())
|
||||
mqttClient.publish(addrstr, valstr);
|
||||
setNextPollTime(millis() + DHT_POLL_DELAY_DEFAULT);
|
||||
debugSerial<<F(" NextPollMillis=")<<nextPollTime();
|
||||
}
|
||||
@@ -285,7 +287,8 @@ void Input::uptimePoll() {
|
||||
char valstr[11];
|
||||
// printUlongValueToStr(valstr,millis());
|
||||
printUlongValueToStr(valstr, millis());
|
||||
mqttClient.publish(emit->valuestring, valstr);
|
||||
if (mqttClient.connected())
|
||||
mqttClient.publish(emit->valuestring, valstr);
|
||||
}
|
||||
setNextPollTime(millis() + UPTIME_POLL_DELAY_DEFAULT);
|
||||
}
|
||||
@@ -384,10 +387,12 @@ void Input::dht22Poll() {
|
||||
if (!strchr(addrstr,'/')) setTopic(addrstr,sizeof(addrstr),T_OUT,emit->valuestring);
|
||||
strcat(addrstr, "T");
|
||||
printFloatValueToStr(temp, valstr);
|
||||
mqttClient.publish(addrstr, valstr);
|
||||
if (mqttClient.connected())
|
||||
mqttClient.publish(addrstr, valstr);
|
||||
addrstr[strlen(addrstr) - 1] = 'H';
|
||||
printFloatValueToStr(humidity, valstr);
|
||||
mqttClient.publish(addrstr, valstr);
|
||||
if (mqttClient.connected())
|
||||
mqttClient.publish(addrstr, valstr);
|
||||
|
||||
setNextPollTime(millis() + DHT_POLL_DELAY_DEFAULT);
|
||||
debugSerial << F(" NextPollMillis=") << nextPollTime() << endl;
|
||||
@@ -512,7 +517,8 @@ void Input::onContactChanged(int newValue) {
|
||||
aJsonObject *emit = aJson.getObjectItem(inputObj, "emit");
|
||||
if (emit) {
|
||||
#ifdef WITH_DOMOTICZ
|
||||
if (getIdxField()) { (newValue) ? publishDataToDomoticz(0, emit, "{\"command\":\"switchlight\",\"idx\":%s,\"switchcmd\":\"On\"}",
|
||||
if (getIdxField())
|
||||
{ (newValue) ? publishDataToDomoticz(0, emit, "{\"command\":\"switchlight\",\"idx\":%s,\"switchcmd\":\"On\"}",
|
||||
: publishDataToDomoticz(0,emit,"{\"command\":\"switchlight\",\"idx\":%s,\"switchcmd\":\"Off\"}",getIdxField()); getIdxField())
|
||||
: publishDataToDomoticz(0, emit,
|
||||
"{\"command\":\"switchlight\",\"idx\":%s,\"switchcmd\":\"Off\"}",
|
||||
@@ -522,6 +528,8 @@ void Input::onContactChanged(int newValue) {
|
||||
{
|
||||
char addrstr[MQTT_TOPIC_LENGTH];
|
||||
strncpy(addrstr,emit->valuestring,sizeof(addrstr));
|
||||
if (mqttClient.connected())
|
||||
{
|
||||
if (!strchr(addrstr,'/')) setTopic(addrstr,sizeof(addrstr),T_OUT,emit->valuestring);
|
||||
if (newValue) { //send set command
|
||||
if (!scmd) mqttClient.publish(addrstr, "ON", true);
|
||||
@@ -531,9 +539,11 @@ if (!strchr(addrstr,'/')) setTopic(addrstr,sizeof(addrstr),T_OUT,emit->valuestri
|
||||
if (!rcmd) mqttClient.publish(addrstr, "OFF", true);
|
||||
else if (strlen(rcmd->valuestring))mqttClient.publish(addrstr, rcmd->valuestring, true);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
} // emit
|
||||
if (item) {
|
||||
//debugSerial <<F("Controlled item:")<< item->valuestring <<endl;
|
||||
Item it(item->valuestring);
|
||||
if (it.isValid()) {
|
||||
if (newValue) { //send set command
|
||||
@@ -548,7 +558,6 @@ if (!strchr(addrstr,'/')) setTopic(addrstr,sizeof(addrstr),T_OUT,emit->valuestri
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Input::onAnalogChanged(int newValue) {
|
||||
debugSerial << F("IN:") << (pin) << F("=") << newValue << endl;
|
||||
@@ -569,7 +578,8 @@ void Input::onAnalogChanged(int newValue) {
|
||||
if (!strchr(addrstr,'/')) setTopic(addrstr,sizeof(addrstr),T_OUT,emit->valuestring);
|
||||
char strVal[16];
|
||||
itoa(newValue,strVal,10);
|
||||
mqttClient.publish(addrstr, strVal, true);
|
||||
if (mqttClient.connected())
|
||||
mqttClient.publish(addrstr, strVal, true);
|
||||
}
|
||||
|
||||
if (item) {
|
||||
@@ -591,7 +601,8 @@ bool Input::publishDataToDomoticz(int pollTimeIncrement, aJsonObject *emit, cons
|
||||
vsnprintf(valstr, sizeof(valstr) - 1, format, args);
|
||||
va_end(args);
|
||||
debugSerial << valstr;
|
||||
mqttClient.publish(emit->valuestring, valstr);
|
||||
if (mqttClient.connected())
|
||||
mqttClient.publish(emit->valuestring, valstr);
|
||||
if (pollTimeIncrement)
|
||||
setNextPollTime(millis() + pollTimeIncrement);
|
||||
debugSerial << F(" NextPollMillis=") << nextPollTime() << endl;
|
||||
|
||||
@@ -656,8 +656,18 @@ int Item::Ctrl(short cmd, short n, int *Parameters, boolean send, int subItemN)
|
||||
break;
|
||||
case CH_RELAY: {
|
||||
int k;
|
||||
short inverse = 0;
|
||||
|
||||
if (iaddr < 0) {
|
||||
iaddr = -iaddr;
|
||||
inverse = 1;
|
||||
}
|
||||
pinMode(iaddr, OUTPUT);
|
||||
digitalWrite(iaddr, k = ((cmd == CMD_ON || cmd == CMD_XON) ? HIGH : LOW));
|
||||
|
||||
if (inverse)
|
||||
digitalWrite(iaddr, k = ((cmd == CMD_ON || cmd == CMD_XON) ? LOW : HIGH));
|
||||
else
|
||||
digitalWrite(iaddr, k = ((cmd == CMD_ON || cmd == CMD_XON) ? HIGH : LOW));
|
||||
debugSerial<<F("Pin:")<<iaddr<<F("=")<<k<<endl;
|
||||
break;
|
||||
case CH_THERMO:
|
||||
@@ -667,6 +677,7 @@ int Item::Ctrl(short cmd, short n, int *Parameters, boolean send, int subItemN)
|
||||
case CH_PWM: {
|
||||
int k;
|
||||
short inverse = 0;
|
||||
|
||||
if (iaddr < 0) {
|
||||
iaddr = -iaddr;
|
||||
inverse = 1;
|
||||
@@ -1038,13 +1049,15 @@ int Item::checkFM() {
|
||||
else aJson.addNumberToObject(out, "pwr", 0);
|
||||
|
||||
if (ftemp > FM_OVERHEAT_CELSIUS && set) {
|
||||
mqttClient.publish("/alarm/ovrht", itemArr->name);
|
||||
if (mqttClient.connected())
|
||||
mqttClient.publish("/alarm/ovrht", itemArr->name);
|
||||
Ctrl(CMD_OFF); //Shut down
|
||||
}
|
||||
} else
|
||||
debugSerial << F("Modbus polling error=") << _HEX(result);
|
||||
outch = aJson.print(out);
|
||||
mqttClient.publish(addrstr, outch);
|
||||
if (mqttClient.connected())
|
||||
mqttClient.publish(addrstr, outch);
|
||||
free(outch);
|
||||
aJson.deleteItem(out);
|
||||
modbusBusy = 0;
|
||||
@@ -1283,7 +1296,8 @@ int Item::SendStatus(short cmd, short n, int *Par, boolean deffered) {
|
||||
return -1;
|
||||
}
|
||||
debugSerial<<F("Pub: ")<<addrstr<<F("->")<<valstr<<endl;
|
||||
mqttClient.publish(addrstr, valstr,true);
|
||||
if (mqttClient.connected())
|
||||
mqttClient.publish(addrstr, valstr,true);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -168,6 +168,24 @@ int mqttErrorRate;
|
||||
void watchdogSetup(void) {} //Do not remove - strong re-definition WDT Init for DUE
|
||||
#endif
|
||||
|
||||
void cleanConf()
|
||||
{
|
||||
root = NULL;
|
||||
inputs = NULL;
|
||||
items = NULL;
|
||||
topics = NULL;
|
||||
mqttArr = NULL;
|
||||
#ifndef DMX_DISABLE
|
||||
dmxArr = NULL;
|
||||
#endif
|
||||
#ifndef OWIRE_DISABLE
|
||||
owArr = NULL;
|
||||
#endif
|
||||
#ifndef MODBUS_DISABLE
|
||||
modbusArr = NULL;
|
||||
#endif
|
||||
}
|
||||
|
||||
void mqttCallback(char *topic, byte *payload, unsigned int length) {
|
||||
debugSerial<<F("\n[")<<topic<<F("] ");
|
||||
if (!payload) return;
|
||||
@@ -739,7 +757,7 @@ void Changed(int i, DeviceAddress addr, float currentTemp) {
|
||||
|
||||
SetBytes(addr, 8, addrstr);
|
||||
addrstr[17] = 0;
|
||||
|
||||
if (!root) return;
|
||||
printFloatValueToStr(currentTemp,valstr);
|
||||
debugSerial<<endl<<F("T:")<<valstr<<F("<");
|
||||
aJsonObject *owObj = aJson.getObjectItem(owArr, addrstr);
|
||||
@@ -758,7 +776,8 @@ void Changed(int i, DeviceAddress addr, float currentTemp) {
|
||||
char valstr[50];
|
||||
sprintf(valstr, "{\"idx\":%s,\"svalue\":\"%.1f\"}", idx->valuestring, currentTemp);
|
||||
debugSerial << valstr;
|
||||
mqttClient.publish(owEmitString, valstr);
|
||||
if (mqttClient.connected())
|
||||
mqttClient.publish(owEmitString, valstr);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
@@ -766,7 +785,8 @@ void Changed(int i, DeviceAddress addr, float currentTemp) {
|
||||
//strcpy_P(addrstr, outprefix);
|
||||
setTopic(addrstr,sizeof(addrstr),T_OUT);
|
||||
strncat(addrstr, owEmitString, sizeof(addrstr));
|
||||
mqttClient.publish(addrstr, valstr);
|
||||
if (mqttClient.connected())
|
||||
mqttClient.publish(addrstr, valstr);
|
||||
}
|
||||
// And translate temp to internal items
|
||||
owItem = aJson.getObjectItem(owObj, "item")->valuestring;
|
||||
@@ -870,8 +890,9 @@ void applyConfig() {
|
||||
if (item->type == aJson_Array && aJson.getArraySize(item)>1) {
|
||||
Item it(item);
|
||||
if (it.isValid()) {
|
||||
short inverse = 0;
|
||||
int pin=it.getArg();
|
||||
if (pin<0) pin=-pin;
|
||||
if (pin<0) {pin=-pin; inverse = 1;}
|
||||
int cmd = it.getCmd();
|
||||
switch (it.itemType) {
|
||||
case CH_THERMO:
|
||||
@@ -880,6 +901,9 @@ void applyConfig() {
|
||||
{
|
||||
int k;
|
||||
pinMode(pin, OUTPUT);
|
||||
if (inverse)
|
||||
digitalWrite(pin, k = ((cmd == CMD_ON) ? LOW : HIGH));
|
||||
else
|
||||
digitalWrite(pin, k = ((cmd == CMD_ON) ? HIGH : LOW));
|
||||
debugSerial<<F("Pin:")<<pin<<F("=")<<k<<F(",");
|
||||
}
|
||||
@@ -927,26 +951,28 @@ void cmdFunctionLoad(int arg_cnt, char **args) {
|
||||
// restoreState();
|
||||
}
|
||||
|
||||
|
||||
int loadConfigFromEEPROM()
|
||||
{
|
||||
char ch;
|
||||
debugSerial<<F("loading Config");
|
||||
debugSerial<<F("Loading Config from EEPROM")<<endl;
|
||||
|
||||
ch = EEPROM.read(EEPROM_offset);
|
||||
if (ch == '{') {
|
||||
aJsonEEPROMStream as = aJsonEEPROMStream(EEPROM_offset);
|
||||
aJson.deleteItem(root);
|
||||
cleanConf();
|
||||
root = aJson.parse(&as);
|
||||
if (!root) {
|
||||
debugSerial<<F("\nload failed\n");
|
||||
debugSerial<<F("load failed")<<endl;
|
||||
return 0;
|
||||
}
|
||||
debugSerial<<F("\nLoaded\n");
|
||||
debugSerial<<F("Loaded")<<endl;
|
||||
applyConfig();
|
||||
ethClient.stop(); //Refresh MQTT connect to get retained info
|
||||
return 1;
|
||||
} else {
|
||||
debugSerial<<F("\nNo stored config\n");
|
||||
debugSerial<<F("No stored config")<<endl;
|
||||
return 0;
|
||||
}
|
||||
return 0;
|
||||
@@ -1134,9 +1160,10 @@ lan_status loadConfigFromHttp(int arg_cnt, char **args)
|
||||
//byte hserver[] = { 192,168,88,2 };
|
||||
wdt_dis();
|
||||
HTTPClient hclient(configServer, 80);
|
||||
HTTPClient hclientPrint(configServer, 80);
|
||||
//HTTPClient hclientPrint(configServer, 80);
|
||||
// FILE is the return STREAM type of the HTTPClient
|
||||
configStream = hclient.getURI(URI);
|
||||
Serial.println("got--");delay(500);
|
||||
responseStatusCode = hclient.getLastReturnCode();
|
||||
wdt_en();
|
||||
|
||||
@@ -1148,6 +1175,7 @@ lan_status loadConfigFromHttp(int arg_cnt, char **args)
|
||||
aJsonFileStream as = aJsonFileStream(configStream);
|
||||
noInterrupts();
|
||||
aJson.deleteItem(root);
|
||||
cleanConf();
|
||||
root = aJson.parse(&as);
|
||||
interrupts();
|
||||
// debugSerial<<F("Parsed."));
|
||||
@@ -1210,6 +1238,7 @@ lan_status loadConfigFromHttp(int arg_cnt, char **args)
|
||||
|
||||
if (responseStatusCode == 200) {
|
||||
aJson.deleteItem(root);
|
||||
cleanConf()
|
||||
root = aJson.parse((char *) response.c_str());
|
||||
|
||||
if (!root) {
|
||||
@@ -1242,6 +1271,7 @@ lan_status loadConfigFromHttp(int arg_cnt, char **args)
|
||||
String response = httpClient.getString();
|
||||
debugSerial<<response;
|
||||
aJson.deleteItem(root);
|
||||
cleanConf()
|
||||
root = aJson.parse((char *) response.c_str());
|
||||
if (!root) {
|
||||
debugSerial<<F("Config parsing failed\n");
|
||||
@@ -1290,7 +1320,7 @@ void setup_main() {
|
||||
setupCmdArduino();
|
||||
printFirmwareVersionAndBuildOptions();
|
||||
|
||||
scan_i2c_bus();
|
||||
// scan_i2c_bus();
|
||||
|
||||
#ifdef SD_CARD_INSERTED
|
||||
sd_card_w5100_setup();
|
||||
|
||||
@@ -435,7 +435,7 @@ void scan_i2c_bus() {
|
||||
byte error, address;
|
||||
int nDevices;
|
||||
|
||||
debugSerial<<("Scanning...\n");
|
||||
debugSerial<<(F("Scanning...\n"));
|
||||
|
||||
nDevices = 0;
|
||||
for(address = 1; address < 127; address++ )
|
||||
@@ -448,26 +448,25 @@ void scan_i2c_bus() {
|
||||
|
||||
if (error == 0)
|
||||
{
|
||||
debugSerial<<("\nI2C device found at address 0x");
|
||||
if (address<16)
|
||||
debugSerial<<("0");
|
||||
debugSerial<<(address,HEX);
|
||||
debugSerial<<(" !");
|
||||
debugSerial<<(F("\nI2C device found at address "));
|
||||
// if (address<16)
|
||||
// debugSerial<<("0");
|
||||
debugSerial<<(address);
|
||||
|
||||
nDevices++;
|
||||
}
|
||||
else if (error==4)
|
||||
{
|
||||
debugSerial<<("\nUnknow error at address 0x");
|
||||
if (address<16)
|
||||
debugSerial<<("0");
|
||||
debugSerial<<(address,HEX);
|
||||
debugSerial<<(F("\nUnknow error at address "));
|
||||
// if (address<16)
|
||||
// debugSerial<<("0");
|
||||
debugSerial<<(address);
|
||||
}
|
||||
}
|
||||
if (nDevices == 0)
|
||||
debugSerial<<("No I2C devices found\n");
|
||||
debugSerial<<(F("No I2C devices found\n"));
|
||||
else
|
||||
debugSerial<<("done\n");
|
||||
debugSerial<<(F("done\n"));
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user