49 #define DIGITAL_INPUT_SENSOR 3 // The digital input you attached your light sensor. (Only 2 and 3 generates interrupt!)
50 #define PULSE_FACTOR 1000 // Number of blinks per kWh of your meter. Normally 1000.
51 #define SLEEP_MODE false // Watt value can only be reported when sleep mode is false.
52 #define MAX_WATT 10000 // Max watt value to report. This filters outliers.
53 #define CHILD_ID 1 // Id of the sensor child
55 uint32_t SEND_FREQUENCY =
57 double ppwh = ((double)PULSE_FACTOR) / 1000;
58 bool pcReceived =
false;
59 volatile uint32_t pulseCount = 0;
60 volatile uint32_t lastBlinkmicros = 0;
61 volatile uint32_t lastBlinkmillis = 0;
62 volatile uint32_t watt = 0;
63 uint32_t oldPulseCount = 0;
71 #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
72 #define IRQ_HANDLER_ATTR ICACHE_RAM_ATTR
74 #define IRQ_HANDLER_ATTR
80 uint32_t newBlinkmicros = micros();
81 uint32_t newBlinkmillis = millis();
82 uint32_t intervalmicros = newBlinkmicros - lastBlinkmicros;
83 uint32_t intervalmillis = newBlinkmillis - lastBlinkmillis;
84 if (intervalmicros < 10000L && intervalmillis < 10L) {
87 if (intervalmillis < 360000) {
88 watt = (3600000000.0 / intervalmicros) / ppwh;
90 watt = (3600000.0 / intervalmillis) /
93 lastBlinkmicros = newBlinkmicros;
94 lastBlinkmillis = newBlinkmillis;
106 pinMode(DIGITAL_INPUT_SENSOR, INPUT_PULLUP);
108 attachInterrupt(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR), onPulse, RISING);
123 uint32_t now = millis();
125 bool sendTime = now - lastSend > SEND_FREQUENCY;
126 if (pcReceived && (SLEEP_MODE || sendTime)) {
128 if (!SLEEP_MODE && watt != oldWatt) {
131 if (watt < ((uint32_t)MAX_WATT)) {
134 Serial.print(
"Watt:");
135 Serial.println(watt);
140 if (pulseCount != oldPulseCount) {
142 double kWh = ((double)pulseCount / ((
double)PULSE_FACTOR));
143 oldPulseCount = pulseCount;
150 }
else if (sendTime && !pcReceived) {
157 sleep(SEND_FREQUENCY,
false);
163 if (message.
getType()==V_VAR1) {
164 pulseCount = oldPulseCount = message.
getLong();
165 Serial.print(
"Received last pulse count value from gw:");
166 Serial.println(pulseCount);