diff --git a/.settings/org.eclipse.cdt.core.prefs b/.settings/org.eclipse.cdt.core.prefs index eac3c96..7383d27 100644 --- a/.settings/org.eclipse.cdt.core.prefs +++ b/.settings/org.eclipse.cdt.core.prefs @@ -184,10 +184,10 @@ environment/project/io.sloeber.core.toolChain.release.1856809793/A.EXTRA.TIME.DT environment/project/io.sloeber.core.toolChain.release.1856809793/A.EXTRA.TIME.DTS/value=3600 environment/project/io.sloeber.core.toolChain.release.1856809793/A.EXTRA.TIME.LOCAL/delimiter=\: environment/project/io.sloeber.core.toolChain.release.1856809793/A.EXTRA.TIME.LOCAL/operation=replace -environment/project/io.sloeber.core.toolChain.release.1856809793/A.EXTRA.TIME.LOCAL/value=1524433102 +environment/project/io.sloeber.core.toolChain.release.1856809793/A.EXTRA.TIME.LOCAL/value=1524511581 environment/project/io.sloeber.core.toolChain.release.1856809793/A.EXTRA.TIME.UTC/delimiter=\: environment/project/io.sloeber.core.toolChain.release.1856809793/A.EXTRA.TIME.UTC/operation=replace -environment/project/io.sloeber.core.toolChain.release.1856809793/A.EXTRA.TIME.UTC/value=1524425902 +environment/project/io.sloeber.core.toolChain.release.1856809793/A.EXTRA.TIME.UTC/value=1524504381 environment/project/io.sloeber.core.toolChain.release.1856809793/A.EXTRA.TIME.ZONE/delimiter=\: environment/project/io.sloeber.core.toolChain.release.1856809793/A.EXTRA.TIME.ZONE/operation=replace environment/project/io.sloeber.core.toolChain.release.1856809793/A.EXTRA.TIME.ZONE/value=3600 @@ -469,7 +469,7 @@ environment/project/io.sloeber.core.toolChain.release.1856809793/JANTJE.COM_PORT environment/project/io.sloeber.core.toolChain.release.1856809793/JANTJE.COM_PORT/value=/dev/ttyUSB5 environment/project/io.sloeber.core.toolChain.release.1856809793/JANTJE.ECLIPSE_LOCATION/delimiter=\: environment/project/io.sloeber.core.toolChain.release.1856809793/JANTJE.ECLIPSE_LOCATION/operation=replace -environment/project/io.sloeber.core.toolChain.release.1856809793/JANTJE.ECLIPSE_LOCATION/value=${eclipse_home}///////////////// +environment/project/io.sloeber.core.toolChain.release.1856809793/JANTJE.ECLIPSE_LOCATION/value=${eclipse_home}///////////////////////////////////// environment/project/io.sloeber.core.toolChain.release.1856809793/JANTJE.EXTRA.ALL/delimiter=\: environment/project/io.sloeber.core.toolChain.release.1856809793/JANTJE.EXTRA.ALL/operation=replace environment/project/io.sloeber.core.toolChain.release.1856809793/JANTJE.EXTRA.ALL/value= diff --git a/ConfigGenerator/configGen.py b/ConfigGenerator/configGen.py index 800b51d..9ef7431 100644 --- a/ConfigGenerator/configGen.py +++ b/ConfigGenerator/configGen.py @@ -19,11 +19,12 @@ configItems = [ {"label":"MQTT Topic", "key":"mqttTopic", "type":"C", "length":64, "default":"IoT/RainSensor1/Value"}, {"label":"MQTT DebugTopic", "key":"mqttDebugTopic", "type":"C", "length":64, "default":"IoT/RainSensor1/Debug"}, {"label":"DebugMode", "key":"debugMode", "type":"I", "default":0}, - {"label":"Period", "key":"period", "type":"I", "default":300} + {"label":"Period (s)", "key":"period", "type":"I", "default":5}, + {"label":"Debounce (us)", "key":"debounce", "type":"I", "default":300} ] -magic = 0xC0DE0003 +magic = 0xC0DE0004 appName = "ESP8266 based RainSensor" confWifiSsid = "espconfig" diff --git a/configuration.cpp b/configuration.cpp index bb60755..552c37c 100644 --- a/configuration.cpp +++ b/configuration.cpp @@ -9,7 +9,7 @@ tConfigBlock configBlock; -const uint32_t MAGIC = 3235774467; +const uint32_t MAGIC = 3235774468; const char* CONFIG_SSID = "espconfig"; extern ESP8266WebServer webServer; @@ -39,7 +39,8 @@ void configServeIndex() { strcpy(configBlock.mqttTopic, "IoT/RainSensor1/Value"); strcpy(configBlock.mqttDebugTopic, "IoT/RainSensor1/Debug"); configBlock.debugMode = 0; - configBlock.period = 500; + configBlock.period = 5; + configBlock.debounce = 300; } if (! checkAuthentication()) { @@ -244,7 +245,7 @@ void configServeIndex() { " " " " " " - " " + " " " " " " + " " + " " + " " + " " + " " + " " + " " @@ -307,6 +322,8 @@ void configServeGetConfiguration() { configBlock.debugMode = atoi(arg.c_str()); arg = webServer.arg("period"); configBlock.period = atoi(arg.c_str()); + arg = webServer.arg("debounce"); + configBlock.debounce = atoi(arg.c_str()); configBlock.magic = MAGIC; @@ -383,6 +400,10 @@ void showConfiguration() { Serial.print(configBlock.period); Serial.println(">"); + Serial.print("debounce = <"); + Serial.print(configBlock.debounce); + Serial.println(">"); + Serial.println("---"); } \ No newline at end of file diff --git a/configuration.h b/configuration.h index e1d2f65..6d97e03 100644 --- a/configuration.h +++ b/configuration.h @@ -13,6 +13,7 @@ typedef struct { char mqttDebugTopic[64]; uint32_t debugMode; uint32_t period; + uint32_t debounce; } tConfigBlock; extern const uint32_t MAGIC; diff --git a/defines.h b/defines.h index 01abc20..80fca96 100644 --- a/defines.h +++ b/defines.h @@ -13,6 +13,6 @@ #define EEPROM_ADDR 0 #define CONFIG_SWITCH 0 -#define HALL_PIN 2 +#define HALL_PIN 13 #endif /* DEFINES_H_ */ diff --git a/productionMode.cpp b/productionMode.cpp index 3280cf1..27ef324 100644 --- a/productionMode.cpp +++ b/productionMode.cpp @@ -27,7 +27,12 @@ PubSubClient client(espClient); void count() { - cnt++; + static uint32_t lastEvent = 0; + uint32_t currentEvent = micros(); + if (currentEvent > (lastEvent + configBlock.debounce)) { + lastEvent = currentEvent; + cnt++; + } } void setup_wifi() { @@ -124,7 +129,6 @@ void loopProduction() { static uint32_t lastMillis = 0; uint32_t currentMillis = millis(); - if (currentMillis > (lastMillis + (configBlock.period * 1000))) { lastMillis = currentMillis; @@ -134,10 +138,18 @@ void loopProduction() { cnt = 0; interrupts(); + uint16_t battery = analogRead(A0); + +#define BUF_SIZE 128 + char buf[BUF_SIZE]; + snprintf(buf, BUF_SIZE-1, "{\"value\":%ld, \"battery\":%ld}", myCnt, battery); + client.publish(configBlock.mqttTopic, buf); + + +#ifdef DEBUG Serial.println(myCnt); +#endif } - - }