From 18732895817a871f0f605cc277f66d8c36849018 Mon Sep 17 00:00:00 2001 From: Nagy Attila Gabor Date: Sat, 15 Aug 2020 22:45:18 +0200 Subject: [PATCH 1/3] Add support of #Axdiv10 to ADS1115 sensor module This way it works the same as the analog sensor module, and examples from the guide could be applied to this sensor as well. Signed-off-by: Nagy Attila Gabor --- tasmota/xsns_12_ads1115.ino | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tasmota/xsns_12_ads1115.ino b/tasmota/xsns_12_ads1115.ino index 0faa24dd0..5e4b05e6d 100644 --- a/tasmota/xsns_12_ads1115.ino +++ b/tasmota/xsns_12_ads1115.ino @@ -122,6 +122,7 @@ struct ADS1115 { uint8_t address; uint8_t addresses[4] = { ADS1115_ADDRESS_ADDR_GND, ADS1115_ADDRESS_ADDR_VDD, ADS1115_ADDRESS_ADDR_SDA, ADS1115_ADDRESS_ADDR_SCL }; uint8_t found[4] = {false,false,false,false}; + int16_t last_values[4][4] = {{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0}}; } Ads1115; //Ads1115StartComparator(channel, ADS1115_REG_CONFIG_MODE_SINGLE); @@ -191,7 +192,7 @@ void Ads1115Show(bool json) Ads1115.address = Ads1115.addresses[t]; for (uint32_t i = 0; i < 4; i++) { values[i] = Ads1115GetConversion(i); - //AddLog_P2(LOG_LEVEL_INFO, "Logging ADS1115 %02x (%i) = %i", address, i, values[i] ); + //AddLog_P2(LOG_LEVEL_INFO, "Logging ADS1115 %02x (%i) = %i", Ads1115.address, i, values[i] ); } Ads1115.address = old_address; @@ -208,6 +209,13 @@ void Ads1115Show(bool json) ResponseAppend_P(PSTR(",\"%s\":{"), label); for (uint32_t i = 0; i < 4; i++) { ResponseAppend_P(PSTR("%s\"A%d\":%d"), (0 == i) ? "" : ",", i, values[i]); + + // Check if value has changed more than 1 percent from last stored value + // we assume that gain is set up correctly, and we could use the whole 16bit result space + if (values[i] >= Ads1115.last_values[t][i] + 327 || values[i] <= Ads1115.last_values[t][i] - 327) { + ResponseAppend_P(PSTR(",\"A%ddiv10\":%d"), i, values[i]); + Ads1115.last_values[t][i] = values[i]; + } } ResponseJsonEnd(); } From c48e69c945995e53432c2b475f01897c7eb43e28 Mon Sep 17 00:00:00 2001 From: Nagy Attila Gabor Date: Fri, 21 Aug 2020 14:27:25 +0200 Subject: [PATCH 2/3] Ads1115 use the 250ms loop to check for 1% changes Signed-off-by: Nagy Attila Gabor --- tasmota/xsns_12_ads1115.ino | 67 +++++++++++++++++++++++++++++++++---- 1 file changed, 60 insertions(+), 7 deletions(-) diff --git a/tasmota/xsns_12_ads1115.ino b/tasmota/xsns_12_ads1115.ino index 5e4b05e6d..4de58ebc2 100644 --- a/tasmota/xsns_12_ads1115.ino +++ b/tasmota/xsns_12_ads1115.ino @@ -180,6 +180,61 @@ void Ads1115Detect(void) } } +#ifdef USE_RULES +// Check every 250ms if there are relevant changes in any of the analog inputs +// and if so then trigger a message +void AdsEvery250ms(void) +{ + int16_t value; + + for (uint32_t t = 0; t < sizeof(Ads1115.addresses); t++) { + if (Ads1115.found[t]) { + + uint8_t old_address = Ads1115.address; + Ads1115.address = Ads1115.addresses[t]; + + // collect first wich addresses have changed. We can save on rule processing this way + uint32_t changed = 0; + for (uint32_t i = 0; i < 4; i++) { + value = Ads1115GetConversion(i); + + // Check if value has changed more than 1 percent from last stored value + // we assume that gain is set up correctly, and we could use the whole 16bit result space + if (value >= Ads1115.last_values[t][i] + 327 || value <= Ads1115.last_values[t][i] - 327) { + Ads1115.last_values[t][i] = value; + bitSet(changed, i); + } + } + Ads1115.address = old_address; + if (changed) { + char label[15]; + if (1 == Ads1115.count) { + // "ADS1115":{"A0":3240,"A1":3235,"A2":3269,"A3":3269} + snprintf_P(label, sizeof(label), PSTR("ADS1115")); + } else { + // "ADS1115-48":{"A0":3240,"A1":3235,"A2":3269,"A3":3269},"ADS1115-49":{"A0":3240,"A1":3235,"A2":3269,"A3":3269} + snprintf_P(label, sizeof(label), PSTR("ADS1115%c%02x"), IndexSeparator(), Ads1115.addresses[t]); + } + + Response_P(PSTR("{\"%s\":{"), label); + + bool first = true; + for (uint32_t i = 0; i < 4; i++) { + if (bitRead(changed, i)) { + ResponseAppend_P(PSTR("%s\"A%ddiv10\":%d"), (first) ? "" : ",", i, Ads1115.last_values[t][i]); + first = false; + } + } + ResponseJsonEndEnd(); + + XdrvRulesProcess(); + } + + } + } +} +#endif // USE_RULES + void Ads1115Show(bool json) { int16_t values[4]; @@ -209,13 +264,6 @@ void Ads1115Show(bool json) ResponseAppend_P(PSTR(",\"%s\":{"), label); for (uint32_t i = 0; i < 4; i++) { ResponseAppend_P(PSTR("%s\"A%d\":%d"), (0 == i) ? "" : ",", i, values[i]); - - // Check if value has changed more than 1 percent from last stored value - // we assume that gain is set up correctly, and we could use the whole 16bit result space - if (values[i] >= Ads1115.last_values[t][i] + 327 || values[i] <= Ads1115.last_values[t][i] - 327) { - ResponseAppend_P(PSTR(",\"A%ddiv10\":%d"), i, values[i]); - Ads1115.last_values[t][i] = values[i]; - } } ResponseJsonEnd(); } @@ -245,6 +293,11 @@ bool Xsns12(uint8_t function) } else if (Ads1115.count) { switch (function) { +#ifdef USE_RULES + case FUNC_EVERY_250_MSECOND: + AdsEvery250ms(); + break; +#endif // USE_RULES case FUNC_JSON_APPEND: Ads1115Show(1); break; From 5f447e38a785cfc7103d5d044a891954eda97140 Mon Sep 17 00:00:00 2001 From: Nagy Attila Gabor Date: Fri, 21 Aug 2020 15:28:16 +0200 Subject: [PATCH 3/3] Ads1115 refactoring to avoid code duplication Signed-off-by: Nagy Attila Gabor --- tasmota/xsns_12_ads1115.ino | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/tasmota/xsns_12_ads1115.ino b/tasmota/xsns_12_ads1115.ino index 4de58ebc2..d6a251f79 100644 --- a/tasmota/xsns_12_ads1115.ino +++ b/tasmota/xsns_12_ads1115.ino @@ -180,6 +180,17 @@ void Ads1115Detect(void) } } +// Create the identifier of the the selected sensor +void Ads1115Label(char* label, uint32_t maxsize, uint8_t address) { + if (1 == Ads1115.count) { + // "ADS1115":{"A0":3240,"A1":3235,"A2":3269,"A3":3269} + snprintf_P(label, maxsize, PSTR("ADS1115")); + } else { + // "ADS1115-48":{"A0":3240,"A1":3235,"A2":3269,"A3":3269},"ADS1115-49":{"A0":3240,"A1":3235,"A2":3269,"A3":3269} + snprintf_P(label, maxsize, PSTR("ADS1115%c%02x"), IndexSeparator(), address); + } +} + #ifdef USE_RULES // Check every 250ms if there are relevant changes in any of the analog inputs // and if so then trigger a message @@ -208,13 +219,7 @@ void AdsEvery250ms(void) Ads1115.address = old_address; if (changed) { char label[15]; - if (1 == Ads1115.count) { - // "ADS1115":{"A0":3240,"A1":3235,"A2":3269,"A3":3269} - snprintf_P(label, sizeof(label), PSTR("ADS1115")); - } else { - // "ADS1115-48":{"A0":3240,"A1":3235,"A2":3269,"A3":3269},"ADS1115-49":{"A0":3240,"A1":3235,"A2":3269,"A3":3269} - snprintf_P(label, sizeof(label), PSTR("ADS1115%c%02x"), IndexSeparator(), Ads1115.addresses[t]); - } + Ads1115Label(label, sizeof(label), Ads1115.addresses[t]); Response_P(PSTR("{\"%s\":{"), label); @@ -252,13 +257,7 @@ void Ads1115Show(bool json) Ads1115.address = old_address; char label[15]; - if (1 == Ads1115.count) { - // "ADS1115":{"A0":3240,"A1":3235,"A2":3269,"A3":3269} - snprintf_P(label, sizeof(label), PSTR("ADS1115")); - } else { - // "ADS1115-48":{"A0":3240,"A1":3235,"A2":3269,"A3":3269},"ADS1115-49":{"A0":3240,"A1":3235,"A2":3269,"A3":3269} - snprintf_P(label, sizeof(label), PSTR("ADS1115%c%02x"), IndexSeparator(), Ads1115.addresses[t]); - } + Ads1115Label(label, sizeof(label), Ads1115.addresses[t]); if (json) { ResponseAppend_P(PSTR(",\"%s\":{"), label);