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;
74 uint32_t newBlinkmicros = micros();
75 uint32_t newBlinkmillis = millis();
76 uint32_t intervalmicros = newBlinkmicros - lastBlinkmicros;
77 uint32_t intervalmillis = newBlinkmillis - lastBlinkmillis;
78 if (intervalmicros < 10000L && intervalmillis < 10L) {
81 if (intervalmillis < 360000) {
82 watt = (3600000000.0 / intervalmicros) / ppwh;
84 watt = (3600000.0 / intervalmillis) /
87 lastBlinkmicros = newBlinkmicros;
88 lastBlinkmillis = newBlinkmillis;
100 pinMode(DIGITAL_INPUT_SENSOR, INPUT_PULLUP);
102 attachInterrupt(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR), onPulse, RISING);
117 uint32_t now = millis();
119 bool sendTime = now - lastSend > SEND_FREQUENCY;
120 if (pcReceived && (SLEEP_MODE || sendTime)) {
122 if (!SLEEP_MODE && watt != oldWatt) {
125 if (watt < ((uint32_t)MAX_WATT)) {
128 Serial.print(
"Watt:");
129 Serial.println(watt);
134 if (pulseCount != oldPulseCount) {
136 double kWh = ((double)pulseCount / ((
double)PULSE_FACTOR));
137 oldPulseCount = pulseCount;
144 }
else if (sendTime && !pcReceived) {
151 sleep(SEND_FREQUENCY,
false);
157 if (message.
getType()==V_VAR1) {
158 pulseCount = oldPulseCount = message.
getLong();
159 Serial.print(
"Received last pulse count value from gw:");
160 Serial.println(pulseCount);