Skip to content

Commit 529485c

Browse files
authored
Revert "Energy meter pulse sensor itslav (#1306)" (#1310)
This reverts commit 8257d0f.
1 parent 8257d0f commit 529485c

File tree

1 file changed

+53
-63
lines changed

1 file changed

+53
-63
lines changed
Lines changed: 53 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,38 @@
11
/*
2-
The MySensors Arduino library handles the wireless radio link and protocol
3-
between your home built sensors/actuators and HA controller of choice.
4-
The sensors forms a self healing radio network with optional repeaters. Each
5-
repeater and gateway builds a routing tables in EEPROM which keeps track of the
6-
network topology allowing messages to be routed to nodes.
7-
8-
Created by Henrik Ekblad <[email protected]>
9-
Copyright (C) 2013-2019 Sensnology AB
10-
Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors
11-
12-
Documentation: http://www.mysensors.org
13-
Support Forum: http://forum.mysensors.org
14-
15-
This program is free software; you can redistribute it and/or
16-
modify it under the terms of the GNU General Public License
17-
version 2 as published by the Free Software Foundation.
18-
2+
* The MySensors Arduino library handles the wireless radio link and protocol
3+
* between your home built sensors/actuators and HA controller of choice.
4+
* The sensors forms a self healing radio network with optional repeaters. Each
5+
* repeater and gateway builds a routing tables in EEPROM which keeps track of the
6+
* network topology allowing messages to be routed to nodes.
7+
*
8+
* Created by Henrik Ekblad <[email protected]>
9+
* Copyright (C) 2013-2019 Sensnology AB
10+
* Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors
11+
*
12+
* Documentation: http://www.mysensors.org
13+
* Support Forum: http://forum.mysensors.org
14+
*
15+
* This program is free software; you can redistribute it and/or
16+
* modify it under the terms of the GNU General Public License
17+
* version 2 as published by the Free Software Foundation.
18+
*
1919
*******************************
20-
21-
REVISION HISTORY
22-
Version 1.0 - Henrik Ekblad
23-
Version 1.1 - Peter Andersson added millis watt calculation if time between pulses > 1h
24-
25-
DESCRIPTION
26-
This sketch provides an example how to implement a LM393 PCB
27-
Use this sensor to measure kWh and Watt of your house meter
28-
You need to set the correct pulsefactor of your meter (blinks per kWh).
29-
The sensor starts by fetching current kWh value from gateway.
30-
Reports both kWh and Watt back to gateway.
31-
32-
Unfortunately millis() won't increment when the Arduino is in
33-
sleepmode. So we cannot make this sensor sleep if we also want
34-
to calculate/report watt value.
35-
http://www.mysensors.org/build/pulse_power
36-
*/
20+
*
21+
* REVISION HISTORY
22+
* Version 1.0 - Henrik Ekblad
23+
*
24+
* DESCRIPTION
25+
* This sketch provides an example how to implement a LM393 PCB
26+
* Use this sensor to measure kWh and Watt of your house meter
27+
* You need to set the correct pulsefactor of your meter (blinks per kWh).
28+
* The sensor starts by fetching current kWh value from gateway.
29+
* Reports both kWh and Watt back to gateway.
30+
*
31+
* Unfortunately millis() won't increment when the Arduino is in
32+
* sleepmode. So we cannot make this sensor sleep if we also want
33+
* to calculate/report watt value.
34+
* http://www.mysensors.org/build/pulse_power
35+
*/
3736

3837
// Enable debug prints
3938
#define MY_DEBUG
@@ -47,26 +46,25 @@
4746
#include <MySensors.h>
4847

4948
#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.
49+
#define PULSE_FACTOR 1000 // Number of blinks per of your meter
5150
#define SLEEP_MODE false // Watt value can only be reported when sleep mode is false.
5251
#define MAX_WATT 10000 // Max watt value to report. This filters outliers.
5352
#define CHILD_ID 1 // Id of the sensor child
5453

5554
uint32_t SEND_FREQUENCY =
5655
20000; // Minimum time between send (in milliseconds). We don't want to spam the gateway.
57-
double ppwh = ((double)PULSE_FACTOR) / 1000; // Pulses per watt hour
56+
double ppwh = ((double)PULSE_FACTOR)/1000; // Pulses per watt hour
5857
bool pcReceived = false;
5958
volatile uint32_t pulseCount = 0;
60-
volatile uint32_t lastBlinkmicros = 0;
61-
volatile uint32_t lastBlinkmillis = 0;
59+
volatile uint32_t lastBlink = 0;
6260
volatile uint32_t watt = 0;
6361
uint32_t oldPulseCount = 0;
6462
uint32_t oldWatt = 0;
6563
double oldkWh;
6664
uint32_t lastSend;
67-
MyMessage wattMsg(CHILD_ID, V_WATT);
68-
MyMessage kWhMsg(CHILD_ID, V_KWH);
69-
MyMessage pcMsg(CHILD_ID, V_VAR1);
65+
MyMessage wattMsg(CHILD_ID,V_WATT);
66+
MyMessage kWhMsg(CHILD_ID,V_KWH);
67+
MyMessage pcMsg(CHILD_ID,V_VAR1);
7068

7169

7270
void setup()
@@ -76,16 +74,16 @@ void setup()
7674

7775
// Use the internal pullup to be able to hook up this sketch directly to an energy meter with S0 output
7876
// If no pullup is used, the reported usage will be too high because of the floating pin
79-
pinMode(DIGITAL_INPUT_SENSOR, INPUT_PULLUP);
77+
pinMode(DIGITAL_INPUT_SENSOR,INPUT_PULLUP);
8078

8179
attachInterrupt(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR), onPulse, RISING);
82-
lastSend = millis();
80+
lastSend=millis();
8381
}
8482

8583
void presentation()
8684
{
8785
// Send the sketch version information to the gateway and Controller
88-
sendSketchInfo(F("Energy Meter"), F("1.1"));
86+
sendSketchInfo("Energy Meter", "1.0");
8987

9088
// Register this device as power sensor
9189
present(CHILD_ID, S_POWER);
@@ -99,9 +97,9 @@ void loop()
9997
if (pcReceived && (SLEEP_MODE || sendTime)) {
10098
// New watt value has been calculated
10199
if (!SLEEP_MODE && watt != oldWatt) {
102-
// Check that we don't get unreasonable large watt value, which
100+
// Check that we don't get unreasonable large watt value.
103101
// could happen when long wraps or false interrupt triggered
104-
if (watt < ((uint32_t)MAX_WATT)) {
102+
if (watt<((uint32_t)MAX_WATT)) {
105103
send(wattMsg.set(watt)); // Send watt value to gw
106104
}
107105
Serial.print("Watt:");
@@ -112,7 +110,7 @@ void loop()
112110
// Pulse count value has changed
113111
if (pulseCount != oldPulseCount) {
114112
send(pcMsg.set(pulseCount)); // Send pulse count value to gw
115-
double kWh = ((double)pulseCount / ((double)PULSE_FACTOR));
113+
double kWh = ((double)pulseCount/((double)PULSE_FACTOR));
116114
oldPulseCount = pulseCount;
117115
if (kWh != oldkWh) {
118116
send(kWhMsg.set(kWh, 4)); // Send kWh value to gw
@@ -121,9 +119,9 @@ void loop()
121119
}
122120
lastSend = now;
123121
} else if (sendTime && !pcReceived) {
124-
// No pulse count value received from controller. Try requesting it again.
122+
// No pulse count value received. Try requesting it again
125123
request(CHILD_ID, V_VAR1);
126-
lastSend = now;
124+
lastSend=now;
127125
}
128126

129127
if (SLEEP_MODE) {
@@ -133,7 +131,7 @@ void loop()
133131

134132
void receive(const MyMessage &message)
135133
{
136-
if (message.type == V_VAR1) {
134+
if (message.type==V_VAR1) {
137135
pulseCount = oldPulseCount = message.getLong();
138136
Serial.print("Received last pulse count value from gw:");
139137
Serial.println(pulseCount);
@@ -144,21 +142,13 @@ void receive(const MyMessage &message)
144142
void onPulse()
145143
{
146144
if (!SLEEP_MODE) {
147-
uint32_t newBlinkmicros = micros();
148-
uint32_t newBlinkmillis = millis();
149-
uint32_t intervalmicros = newBlinkmicros - lastBlinkmicros;
150-
uint32_t intervalmillis = newBlinkmillis - lastBlinkmillis;
151-
if (intervalmicros < 10000L && intervalmillis < 10L) { // Sometimes we get interrupt on RISING
145+
uint32_t newBlink = micros();
146+
uint32_t interval = newBlink-lastBlink;
147+
if (interval<10000L) { // Sometimes we get interrupt on RISING
152148
return;
153149
}
154-
if (intervalmillis < 360000) { // Less than an hour since last pulse, use microseconds
155-
watt = (3600000000.0 / intervalmicros) / ppwh;
156-
} else {
157-
watt = (3600000.0 / intervalmillis) /
158-
ppwh; // more thAn an hour since last pulse, use milliseconds as micros will overflow after 70min
159-
}
160-
lastBlinkmicros = newBlinkmicros;
161-
lastBlinkmillis = newBlinkmillis;
150+
watt = (3600000000.0 /interval) / ppwh;
151+
lastBlink = newBlink;
162152
}
163153
pulseCount++;
164154
}

0 commit comments

Comments
 (0)