From bd885752b4af71486a0717a661bdd86b12bf3c96 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Sun, 26 Jul 2020 23:33:54 +0200 Subject: [PATCH] take over counter handling from old project --- libraries/includes/defines.h | 18 ++------------ releaseInfo.json | 6 ++--- sketch/ConfigDataStructure.py | 2 ++ sketch/application.cpp | 46 +++++++++++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 19 deletions(-) diff --git a/libraries/includes/defines.h b/libraries/includes/defines.h index 3f7cce0..9771957 100644 --- a/libraries/includes/defines.h +++ b/libraries/includes/defines.h @@ -1,7 +1,7 @@ /* * defines.h * - * Created on: Aug 20, 2017 + * Created on: Jul 26, 2020 * Author: wn */ @@ -12,8 +12,6 @@ -// #define WS2811 -#define PL9823 #define NODEMCU // #define ESP01 @@ -28,20 +26,8 @@ #define CONFIG_SWITCH D2 #endif -#ifdef WS2811 -#define PIXEL_PIN 1 // NODEMCU numbering -#endif -#ifdef PL9823 -#ifdef ESP01 -#define PIXEL_PIN 2 -#endif -#ifdef NODEMCU -#define PIXEL_PIN D1 -#endif -#endif - -#define NUM_OF_LEDs 300 +#define REED_PIN 13 #endif /* DEFINES_H_ */ diff --git a/releaseInfo.json b/releaseInfo.json index d512fcb..1f68e65 100644 --- a/releaseInfo.json +++ b/releaseInfo.json @@ -1,8 +1,8 @@ { - "releaseTag": "v1.0.6", + "releaseTag": "v1.0.0", "createReleaseTag": "true", - "releaseName": "colorPattern and watchdog", - "description": "introduce the colorPattern config option (swap R and G) and use the mqtt boilerplate code with watchdog support" + "releaseName": "initial", + "description": "initial" } diff --git a/sketch/ConfigDataStructure.py b/sketch/ConfigDataStructure.py index 48473a7..66463b0 100644 --- a/sketch/ConfigDataStructure.py +++ b/sketch/ConfigDataStructure.py @@ -11,6 +11,8 @@ configItems = [ {"label":"MQTT ValuesTopic", "key":"mqttValuesTopic", "type":"C", "length":64, "default":"IoT/RainSensor2/Values"}, {"label":"MQTT DebugTopic", "key":"mqttDebugTopic", "type":"C", "length":64, "default":"IoT/RainSensor2/Debug"}, {"label":"MQTT WatchdogTopic", "key":"mqttWatchdogTopic", "type":"C", "length":64, "default":"IoT/Watchdog"}, + {"label":"Values period (s)", "key":"valuesPeriod", "type":"I", "default":3600}, + {"label":"Debounce time (us)", "key":"debounce", "type":"I", "default":300}, {"label":"DebugMode", "key":"debugMode", "type":"I", "default":0} ] diff --git a/sketch/application.cpp b/sketch/application.cpp index 758e606..f16d839 100644 --- a/sketch/application.cpp +++ b/sketch/application.cpp @@ -24,6 +24,20 @@ +volatile uint32_t cnt = 0; +uint32_t uptime = 0; + +void count() { + static uint32_t lastEvent = 0; + uint32_t currentEvent = micros(); + if (currentEvent > (lastEvent + configBlock.debounce)) { + lastEvent = currentEvent; + cnt++; + } +} + + + // void subscribeApplication() { // } @@ -36,12 +50,44 @@ void setupApplication() { mqttSetup(); + pinMode(REED_PIN, INPUT_PULLUP); + attachInterrupt(REED_PIN, count, FALLING); } void loopApplication() { mqttLoop(); + uint32_t currentMillis = millis(); + + static uint32_t lastUptimeMillis = 0; + if (currentMillis > (lastUptimeMillis + 1000)) { + lastUptimeMillis = currentMillis; + uptime++; + } + + static uint32_t lastMillis = 0; + if (currentMillis > (lastMillis + (configBlock.period * 1000))) { + lastMillis = currentMillis; + + // rain sensor + uint32_t rainCnt = 0; + noInterrupts(); + rainCnt = cnt; + cnt = 0; + interrupts(); +#ifdef DEBUG + Serial.print(rainCnt); + Serial.println(); +#endif + + // MQTT publishing +#define BUF_SIZE 256 + char buf[BUF_SIZE]; + snprintf(buf, BUF_SIZE-1, "{\"raincnt\":%ld, \"uptime\":%ld}", rainCnt, uptime); + mqttClient.publish(configBlock.mqttValuesTopic, buf); + } + }