Browse Source

Counter module

fastled
Xose Pérez 7 years ago
parent
commit
7beceb19af
9 changed files with 3660 additions and 3532 deletions
  1. +2
    -0
      code/espurna/analog.ino
  2. +1
    -0
      code/espurna/config/arduino.h
  3. +17
    -0
      code/espurna/config/sensors.h
  4. +90
    -0
      code/espurna/counter.ino
  5. BIN
      code/espurna/data/index.html.gz
  6. +6
    -0
      code/espurna/espurna.ino
  7. +3534
    -3532
      code/espurna/static/index.html.gz.h
  8. +5
    -0
      code/espurna/web.ino
  9. +5
    -0
      code/html/index.html

+ 2
- 0
code/espurna/analog.ino View File

@ -26,6 +26,8 @@ void analogSetup() {
}); });
#endif #endif
DEBUG_MSG_P(PSTR("[ANALOG] Monitoring analog values\n"));
} }
void analogLoop() { void analogLoop() {


+ 1
- 0
code/espurna/config/arduino.h View File

@ -46,6 +46,7 @@
//#define ALEXA_SUPPORT 0 //#define ALEXA_SUPPORT 0
//#define ANALOG_SUPPORT 1 //#define ANALOG_SUPPORT 1
//#define COUNTER_SUPPORT 1
//#define DEBUG_SERIAL_SUPPORT 0 //#define DEBUG_SERIAL_SUPPORT 0
//#define DEBUG_UDP_SUPPORT 1 //#define DEBUG_UDP_SUPPORT 1
//#define DHT_SUPPORT 1 //#define DHT_SUPPORT 1


+ 17
- 0
code/espurna/config/sensors.h View File

@ -51,6 +51,23 @@
#define ADC_VCC_ENABLED 0 #define ADC_VCC_ENABLED 0
#endif #endif
//--------------------------------------------------------------------------------
// Counter sensor
// Enable support by passing COUNTER_SUPPORT=1 build flag
//--------------------------------------------------------------------------------
#ifndef COUNTER_SUPPORT
#define COUNTER_SUPPORT 0 // Do not build with counter support by default
#endif
#define COUNTER_PIN 2 // GPIO to monitor
#define COUNTER_PIN_MODE INPUT // INPUT, INPUT_PULLUP
#define COUNTER_INTERRUPT_MODE RISING // RISING, FALLING, BOTH
#define COUNTER_UPDATE_INTERVAL 5000 // Update counter every this millis
#define COUNTER_REPORT_EVERY 12 // Report counter every this updates (1 minute)
#define COUNTER_DEBOUNCE 10 // Do not register events within less than 10 millis
#define COUNTER_TOPIC "counter" // Default topic for MQTT, API and InfluxDB
//-------------------------------------------------------------------------------- //--------------------------------------------------------------------------------
// DS18B20 temperature sensor // DS18B20 temperature sensor
// Enable support by passing DS18B20_SUPPORT=1 build flag // Enable support by passing DS18B20_SUPPORT=1 build flag


+ 90
- 0
code/espurna/counter.ino View File

@ -0,0 +1,90 @@
/*
COUNTER MODULE
Copyright (C) 2017 by Xose Pérez <xose dot perez at gmail dot com>
*/
#if COUNTER_SUPPORT
volatile unsigned long _counterCurrent = 0;
volatile unsigned long _counterLast = 0;
unsigned long _counterBuffer[COUNTER_REPORT_EVERY] = {0};
unsigned char _counterBufferPointer = 0;
unsigned long _counterValue = 0;
// -----------------------------------------------------------------------------
// COUNTER
// -----------------------------------------------------------------------------
void ICACHE_RAM_ATTR _counterISR() {
if (millis() - _counterLast > COUNTER_DEBOUNCE) {
++_counterCurrent;
_counterLast = millis();
}
}
unsigned long getCounter() {
return _counterValue;
}
void counterSetup() {
pinMode(COUNTER_PIN, COUNTER_PIN_MODE);
attachInterrupt(COUNTER_PIN, _counterISR, COUNTER_INTERRUPT_MODE);
#if WEB_SUPPORT
apiRegister(COUNTER_TOPIC, COUNTER_TOPIC, [](char * buffer, size_t len) {
snprintf_P(buffer, len, PSTR("%d"), getCounter());
});
#endif
DEBUG_MSG_P(PSTR("[COUNTER] Counter on GPIO %d\n"), COUNTER_PIN);
}
void counterLoop() {
// Check if we should read new data
static unsigned long last_update = 0;
if ((millis() - last_update) < COUNTER_UPDATE_INTERVAL) return;
last_update = millis();
// Update buffer counts
_counterValue = _counterValue - _counterBuffer[_counterBufferPointer] + _counterCurrent;
_counterBuffer[_counterBufferPointer] = _counterCurrent;
_counterCurrent = 0;
_counterBufferPointer = (_counterBufferPointer + 1) % COUNTER_REPORT_EVERY;
DEBUG_MSG_P(PSTR("[COUNTER] Value: %d\n"), _counterValue);
// Update websocket clients
#if WEB_SUPPORT
char buffer[100];
snprintf_P(buffer, sizeof(buffer), PSTR("{\"counterVisible\": 1, \"counterValue\": %d}"), _counterValue);
wsSend(buffer);
#endif
// Do we have to report?
if (_counterBufferPointer == 0) {
// Send MQTT messages
mqttSend(getSetting("counterTopic", COUNTER_TOPIC).c_str(), String(_counterValue).c_str());
// Send to Domoticz
#if DOMOTICZ_SUPPORT
domoticzSend("dczCountIdx", 0, String(_counterValue).c_str());
#endif
// Send to InfluxDB
#if INFLUXDB_SUPPORT
influxDBSend(COUNTER_TOPIC, _counterValue);
#endif
}
}
#endif // COUNTER_SUPPORT

BIN
code/espurna/data/index.html.gz View File


+ 6
- 0
code/espurna/espurna.ino View File

@ -249,6 +249,9 @@ void setup() {
#if ANALOG_SUPPORT #if ANALOG_SUPPORT
analogSetup(); analogSetup();
#endif #endif
#if COUNTER_SUPPORT
counterSetup();
#endif
#if DHT_SUPPORT #if DHT_SUPPORT
dhtSetup(); dhtSetup();
#endif #endif
@ -300,6 +303,9 @@ void loop() {
#if ANALOG_SUPPORT #if ANALOG_SUPPORT
analogLoop(); analogLoop();
#endif #endif
#if COUNTER_SUPPORT
counterLoop();
#endif
#if DHT_SUPPORT #if DHT_SUPPORT
dhtLoop(); dhtLoop();
#endif #endif


+ 3534
- 3532
code/espurna/static/index.html.gz.h
File diff suppressed because it is too large
View File


+ 5
- 0
code/espurna/web.ino View File

@ -583,6 +583,11 @@ void _wsStart(uint32_t client_id) {
root["analogValue"] = getAnalog(); root["analogValue"] = getAnalog();
#endif #endif
#if COUNTER_SUPPORT
root["counterVisible"] = 1;
root["counterValue"] = getCounter();
#endif
#if HLW8012_SUPPORT #if HLW8012_SUPPORT
root["powVisible"] = 1; root["powVisible"] = 1;
root["powActivePower"] = getActivePower(); root["powActivePower"] = getActivePower();


+ 5
- 0
code/html/index.html View File

@ -170,6 +170,11 @@
<input class="pure-u-1 pure-u-sm-3-4" type="text" name="analogValue" readonly /> <input class="pure-u-1 pure-u-sm-3-4" type="text" name="analogValue" readonly />
</div> </div>
<div class="pure-g module module-counter">
<label class="pure-u-1 pure-u-sm-1-4" for="counterValue">Counts / last minute</label>
<input class="pure-u-1 pure-u-sm-3-4" type="text" name="counterValue" readonly />
</div>
<div class="pure-g module module-ds"> <div class="pure-g module module-ds">
<label class="pure-u-1 pure-u-sm-1-4" for="dsTmp">Temperature (<span id="tmpUnit"></span>)</label> <label class="pure-u-1 pure-u-sm-1-4" for="dsTmp">Temperature (<span id="tmpUnit"></span>)</label>
<input class="pure-u-1 pure-u-sm-3-4" type="text" name="dsTmp" readonly /> <input class="pure-u-1 pure-u-sm-3-4" type="text" name="dsTmp" readonly />


Loading…
Cancel
Save