-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMySensorsUtilitySensor.ino
152 lines (126 loc) · 4.62 KB
/
MySensorsUtilitySensor.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Enable debug prints to serial monitor
//#define MY_DEBUG
// Enable and select radio type attached
#define MY_RADIO_NRF24
#include <SPI.h>
#include <MySensors.h>
#include <TimerOne.h>
#include "UtilitySensor.h"
#include "GasUtilitySensor.h"
#define SKETCH_NAME "Gasmeter"
#define SKETCH_MAJOR_VER "1"
#define SKETCH_MINOR_VER "1"
const int ChildID = 1;
const int GasSensorPin = 4;
const int GasLedPin = LED_BUILTIN; //5;
const int ElectricitySensorPin = 6;
const int ElectricityLedPin = 7;
const unsigned long SendFrequency = 30000; // Minimum time between send (in milliseconds). We don't want to spam the gateway.
const unsigned long DebounceDelay = 50;
const int TimerUs = 50000; // 50mS set timer duration in microseconds
MyMessage flowMsg(ChildID, V_FLOW);
MyMessage volumeMsg(ChildID, V_VOLUME);
MyMessage lastCounterMsg(ChildID, V_VAR1);
GasUtilitySensor gasSensor(GasSensorPin, GasLedPin);
UtilitySensor electricitySensor(ElectricitySensorPin, ElectricityLedPin);
boolean counterValueReceivedFromGateway = false;
unsigned long timeLastSend = 0;
void timerIsr()
{
unsigned long now = millis();
gasSensor.handleIrq(now);
electricitySensor.handleIrq(now);
}
void setup()
{
gasSensor.initialize();
electricitySensor.initialize();
Timer1.initialize(TimerUs);
Timer1.attachInterrupt(timerIsr);
}
void presentation()
{
// Send the sketch version information to the gateway and Controller
sendSketchInfo(SKETCH_NAME, SKETCH_MAJOR_VER "." SKETCH_MINOR_VER);
// Register this device as Gasflow sensor
present(ChildID, S_GAS);
}
void send_message(unsigned long counterValue, double liters_per_minute)
{
send(lastCounterMsg.set(counterValue)); // Send globalcounter value to gateway in VAR1
send(flowMsg.set(liters_per_minute, 2)); // Send flow value to gateway
send(volumeMsg.set(1.0 * counterValue / 1000, 3)); // Send volume value to gateway and convert from dm3 to m3
}
void receive(const MyMessage &message)
{
#ifdef MY_DEBUG
Serial.println("--------------- function receive ---------------");
#endif
unsigned long gatewayPulseCount = 0;
if (message.type == V_VAR1) {
gatewayPulseCount = message.getULong();
if (gasSensor.getCounter() != gatewayPulseCount)
{
//gasCount += gatewayPulseCount;
#ifdef MY_DEBUG
Serial.print("Received last pulse count from gateway: ");
Serial.println(gasCount);
#endif
counterValueReceivedFromGateway = true;
// During testing in can be handy to be able to set the values in domoticz; You can do this by using the REST request below:
// (replace x.x.x.x and deviceid by relevant numbers
// http://x.x.x.x:8080/json.htm?type=command¶m=udevice&idx=deviceid&nvalue=0&svalue=0
//use line below to reset your counter in domoticz; We needed it ;-)
unsigned long gasCount = 0;
gasSensor.setCounter(gasCount);
gasSensor.setOldCounter(gasCount);
//oldGasCount = gasCount;
}
}
#ifdef MY_DEBUG
Serial.println("---------------------------------------------------");
#endif
}
void loop()
{
Serial.println("in loop");
if (!counterValueReceivedFromGateway)
{
//Last Pulsecount not yet received from controller, request it again
#ifdef MY_DEBUG
Serial.println("Requesting as nothing was received from gateway yet!");
#endif
request(ChildID, V_VAR1);
wait(1000);
return;
}
else
{
unsigned long now = millis();
unsigned long timePastSinceLastSend = now - timeLastSend;
double liters_per_minute = gasSensor.litersPerMinute(); //litersPerMinute(timePastSinceLastSend, liters);
// If we have counted a pulse and the send frequency has passed, send message to gateway;
// Also send a message if last message has been sent more than x seconds ago and flow is still more than 0
if ((counterValueReceivedFromGateway) &&
(timePastSinceLastSend > SendFrequency) &&
(gasSensor.counterIncremented() || liters_per_minute > 0))
{
unsigned long gasCounter = gasSensor.getCounter(); // create copy, use it later on
send_message(gasCounter, liters_per_minute);
#ifdef MY_DEBUG
Serial.print("Liters per Minuut: ");
Serial.println(liters_per_minute);
Serial.print("Minutes Passed: ");
Serial.println(1.0 * timePastSinceLastSend / 60000.0);
Serial.print("Aantal Liters: ");
Serial.println(gasSensor.liters());
#endif
timeLastSend = now;
//oldGasCount = gasCount;
gasSensor.setOldCounter(gasCounter);
}
// save the gasSensorReading. Next time through the loop, it'll be the lastGasSensorState:
//lastGasSensorState = gasSensorReading;
//sleep(1000);
}
}