1
1
/*
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
+ *
19
19
*******************************
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
+ */
37
36
38
37
// Enable debug prints
39
38
#define MY_DEBUG
47
46
#include < MySensors.h>
48
47
49
48
#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
51
50
#define SLEEP_MODE false // Watt value can only be reported when sleep mode is false.
52
51
#define MAX_WATT 10000 // Max watt value to report. This filters outliers.
53
52
#define CHILD_ID 1 // Id of the sensor child
54
53
55
54
uint32_t SEND_FREQUENCY =
56
55
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
58
57
bool pcReceived = false ;
59
58
volatile uint32_t pulseCount = 0 ;
60
- volatile uint32_t lastBlinkmicros = 0 ;
61
- volatile uint32_t lastBlinkmillis = 0 ;
59
+ volatile uint32_t lastBlink = 0 ;
62
60
volatile uint32_t watt = 0 ;
63
61
uint32_t oldPulseCount = 0 ;
64
62
uint32_t oldWatt = 0 ;
65
63
double oldkWh;
66
64
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);
70
68
71
69
72
70
void setup ()
@@ -76,16 +74,16 @@ void setup()
76
74
77
75
// Use the internal pullup to be able to hook up this sketch directly to an energy meter with S0 output
78
76
// 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);
80
78
81
79
attachInterrupt (digitalPinToInterrupt (DIGITAL_INPUT_SENSOR), onPulse, RISING);
82
- lastSend = millis ();
80
+ lastSend= millis ();
83
81
}
84
82
85
83
void presentation ()
86
84
{
87
85
// Send the sketch version information to the gateway and Controller
88
- sendSketchInfo (F ( " Energy Meter" ), F ( " 1.1 " ) );
86
+ sendSketchInfo (" Energy Meter" , " 1.0 " );
89
87
90
88
// Register this device as power sensor
91
89
present (CHILD_ID, S_POWER);
@@ -99,9 +97,9 @@ void loop()
99
97
if (pcReceived && (SLEEP_MODE || sendTime)) {
100
98
// New watt value has been calculated
101
99
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.
103
101
// could happen when long wraps or false interrupt triggered
104
- if (watt < ((uint32_t )MAX_WATT)) {
102
+ if (watt< ((uint32_t )MAX_WATT)) {
105
103
send (wattMsg.set (watt)); // Send watt value to gw
106
104
}
107
105
Serial.print (" Watt:" );
@@ -112,7 +110,7 @@ void loop()
112
110
// Pulse count value has changed
113
111
if (pulseCount != oldPulseCount) {
114
112
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));
116
114
oldPulseCount = pulseCount;
117
115
if (kWh != oldkWh) {
118
116
send (kWhMsg .set (kWh , 4 )); // Send kWh value to gw
@@ -121,9 +119,9 @@ void loop()
121
119
}
122
120
lastSend = now;
123
121
} 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
125
123
request (CHILD_ID, V_VAR1);
126
- lastSend = now;
124
+ lastSend= now;
127
125
}
128
126
129
127
if (SLEEP_MODE) {
@@ -133,7 +131,7 @@ void loop()
133
131
134
132
void receive (const MyMessage &message)
135
133
{
136
- if (message.type == V_VAR1) {
134
+ if (message.type == V_VAR1) {
137
135
pulseCount = oldPulseCount = message.getLong ();
138
136
Serial.print (" Received last pulse count value from gw:" );
139
137
Serial.println (pulseCount);
@@ -144,21 +142,13 @@ void receive(const MyMessage &message)
144
142
void onPulse ()
145
143
{
146
144
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
152
148
return ;
153
149
}
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;
162
152
}
163
153
pulseCount++;
164
154
}
0 commit comments