take over counter handling from old project

This commit is contained in:
2020-07-26 23:33:54 +02:00
parent 0c095c80ac
commit bd885752b4
4 changed files with 53 additions and 19 deletions

View File

@ -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_ */

View File

@ -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"
}

View File

@ -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}
]

View File

@ -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);
}
}