|
16 | 16 | You should have received a copy of the GNU General Public License
|
17 | 17 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
18 | 18 | */
|
19 |
| - |
20 |
| -/* |
21 |
| - ==================================================== |
| 19 | +/*==================================================== |
22 | 20 | Prerequisites:
|
23 | 21 | - Change libraries/PubSubClient/src/PubSubClient.h
|
24 | 22 | #define MQTT_MAX_PACKET_SIZE 512
|
25 | 23 |
|
26 | 24 | - Select IDE Tools - Flash size: "1M (no SPIFFS)"
|
27 |
| - ==================================================== |
28 |
| -*/ |
| 25 | + ====================================================*/ |
29 | 26 |
|
30 |
| -#define VERSION 0x05010000 // 5.1.0 |
| 27 | +#define VERSION 0x05010100 // 5.1.1 |
31 | 28 |
|
32 | 29 | enum log_t {LOG_LEVEL_NONE, LOG_LEVEL_ERROR, LOG_LEVEL_INFO, LOG_LEVEL_DEBUG, LOG_LEVEL_DEBUG_MORE, LOG_LEVEL_ALL};
|
33 | 30 | enum week_t {Last, First, Second, Third, Fourth};
|
@@ -254,6 +251,7 @@ uint8_t blink_powersave; // Blink start power save state
|
254 | 251 | uint16_t mqtt_cmnd_publish = 0; // ignore flag for publish command
|
255 | 252 | uint8_t latching_power = 0; // Power state at latching start
|
256 | 253 | uint8_t latching_relay_pulse = 0; // Latching relay pulse timer
|
| 254 | +unsigned long pTimeLast[4]; // Last counter time in milli seconds |
257 | 255 |
|
258 | 256 | #ifdef USE_MQTT_TLS
|
259 | 257 | WiFiClientSecure espClient; // Wifi Secure Client
|
@@ -706,7 +704,7 @@ boolean mqtt_command(boolean grpflg, char *type, uint16_t index, char *dataBuf,
|
706 | 704 | }
|
707 | 705 | snprintf_P(svalue, ssvalue, PSTR("{\"MqttPassword\":\"%s\"}"), sysCfg.mqtt_pwd);
|
708 | 706 | }
|
709 |
| - else if (!grpflg && !strcmp_P(type,PSTR("FULLTOPIC"))) { |
| 707 | + else if (!strcmp_P(type,PSTR("FULLTOPIC"))) { |
710 | 708 | if ((data_len > 0) && (data_len < sizeof(sysCfg.mqtt_fulltopic))) {
|
711 | 709 | for (i = 0; i <= data_len; i++) {
|
712 | 710 | if ((dataBuf[i] == '+') || (dataBuf[i] == '#') || (dataBuf[i] == ' ')) {
|
@@ -1213,6 +1211,27 @@ void mqttDataCb(char* topic, byte* data, unsigned int data_len)
|
1213 | 1211 | }
|
1214 | 1212 | snprintf_P(svalue, sizeof(svalue), PSTR("%s}}"),svalue);
|
1215 | 1213 | }
|
| 1214 | + else if (!strcmp_P(type,PSTR("COUNTER")) && (index > 0) && (index <= 4)) { |
| 1215 | + if ((data_len > 0) && (pin[GPIO_CNTR1 + index -1] < 99)) { |
| 1216 | + rtcMem.pCounter[index -1] = payload16; |
| 1217 | + sysCfg.pCounter[index -1] = payload16; |
| 1218 | + } |
| 1219 | + snprintf_P(svalue, sizeof(svalue), PSTR("{\"Counter%d\":%d}"), index, rtcMem.pCounter[index -1]); |
| 1220 | + } |
| 1221 | + else if (!strcmp_P(type,PSTR("COUNTERTYPE")) && (index > 0) && (index <= 4)) { |
| 1222 | + if ((data_len > 0) && (payload >= 0) && (payload <= 1) && (pin[GPIO_CNTR1 + index -1] < 99)) { |
| 1223 | + bitWrite(sysCfg.pCounterType, index -1, payload &1); |
| 1224 | + rtcMem.pCounter[index -1] = 0; |
| 1225 | + sysCfg.pCounter[index -1] = 0; |
| 1226 | + } |
| 1227 | + snprintf_P(svalue, sizeof(svalue), PSTR("{\"CounterType%d\":%d}"), index, bitRead(sysCfg.pCounterType, index -1)); |
| 1228 | + } |
| 1229 | + else if (!strcmp_P(type,PSTR("COUNTERDEBOUNCE"))) { |
| 1230 | + if ((data_len > 0) && (payload16 < 32001) && (pin[GPIO_CNTR1 + index -1] < 99)) { |
| 1231 | + sysCfg.pCounterDebounce = payload16; |
| 1232 | + } |
| 1233 | + snprintf_P(svalue, sizeof(svalue), PSTR("{\"CounterDebounce\":%d}"), sysCfg.pCounterDebounce); |
| 1234 | + } |
1216 | 1235 | else if (!strcmp_P(type,PSTR("SLEEP"))) {
|
1217 | 1236 | if ((data_len > 0) && (payload >= 0) && (payload < 251)) {
|
1218 | 1237 | if ((!sysCfg.sleep && payload) || (sysCfg.sleep && !payload)) {
|
@@ -1527,7 +1546,9 @@ void mqttDataCb(char* topic, byte* data, unsigned int data_len)
|
1527 | 1546 | snprintf_P(svalue, sizeof(svalue), PSTR("{\"Command\":\"Unknown\"}"));
|
1528 | 1547 | type = (char*)topicBuf;
|
1529 | 1548 | }
|
1530 |
| - mqtt_publish_topic_P(5, type, svalue); |
| 1549 | + if (svalue[0] != '\0') { |
| 1550 | + mqtt_publish_topic_P(5, type, svalue); |
| 1551 | + } |
1531 | 1552 | }
|
1532 | 1553 |
|
1533 | 1554 | /********************************************************************************************/
|
@@ -1786,6 +1807,12 @@ void sensors_mqttPresent(char* svalue, uint16_t ssvalue, uint8_t* djson)
|
1786 | 1807 | *djson = 1;
|
1787 | 1808 | }
|
1788 | 1809 | }
|
| 1810 | + for (byte i = 0; i < 4; i++) { |
| 1811 | + if (pin[GPIO_CNTR1 +i] < 99) { |
| 1812 | + snprintf_P(svalue, ssvalue, PSTR("%s, {\"Counter%d\":%d}"), svalue, i +1, rtcMem.pCounter[i]); |
| 1813 | + *djson = 1; |
| 1814 | + } |
| 1815 | + } |
1789 | 1816 | #ifndef USE_ADC_VCC
|
1790 | 1817 | if (pin[GPIO_ADC0] < 99) {
|
1791 | 1818 | snprintf_P(svalue, ssvalue, PSTR("%s, \"AnalogInput0\":%d"), svalue, analogRead(A0));
|
@@ -2187,6 +2214,9 @@ void stateloop()
|
2187 | 2214 | }
|
2188 | 2215 | break;
|
2189 | 2216 | case (STATES/10)*4:
|
| 2217 | + if (rtc_midnight_now()) { |
| 2218 | + counter_savestate(); |
| 2219 | + } |
2190 | 2220 | if (savedatacounter) {
|
2191 | 2221 | savedatacounter--;
|
2192 | 2222 | if (savedatacounter <= 0) {
|
@@ -2221,6 +2251,7 @@ void stateloop()
|
2221 | 2251 | if (hlw_flg) {
|
2222 | 2252 | hlw_savestate();
|
2223 | 2253 | }
|
| 2254 | + counter_savestate(); |
2224 | 2255 | CFG_Save();
|
2225 | 2256 | restartflag--;
|
2226 | 2257 | if (restartflag <= 0) {
|
@@ -2436,7 +2467,8 @@ void GPIO_init()
|
2436 | 2467 | analogWrite(pin[GPIO_PWM1 +i], sysCfg.pwmvalue[i]);
|
2437 | 2468 | }
|
2438 | 2469 | }
|
2439 |
| - |
| 2470 | + counter_init(); |
| 2471 | + |
2440 | 2472 | if (EXS_RELAY == sysCfg.module) {
|
2441 | 2473 | setLatchingRelay(0,2);
|
2442 | 2474 | setLatchingRelay(1,2);
|
@@ -2469,7 +2501,8 @@ void GPIO_init()
|
2469 | 2501 |
|
2470 | 2502 | #ifdef USE_WS2812
|
2471 | 2503 | if (pin[GPIO_WS2812] < 99) {
|
2472 |
| - ws2812_init(); |
| 2504 | + Maxdevice++; |
| 2505 | + ws2812_init(Maxdevice); |
2473 | 2506 | }
|
2474 | 2507 | #endif // USE_WS2812
|
2475 | 2508 |
|
|
0 commit comments