Skip to content

Commit 843f018

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents b66408e + 00bd605 commit 843f018

File tree

2 files changed

+109
-67
lines changed

2 files changed

+109
-67
lines changed

libraries/MySensors/examples/UVSensor/UVSensor.ino

+42-19
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,85 @@
1+
#include <SPI.h>
2+
#include <MySensor.h>
13
/*
2-
Vera Arduino UVM-30A
4+
Arduino UVM-30A
35
46
connect the sensor as follows :
57
68
+ >>> 5V
79
- >>> GND
810
out >>> A0
911
10-
Contribution: epierre, bulldoglowell
11-
Converted to 1.4 by Henrik Ekblad
12+
Contribution: epierre, bulldoglowell, gizmocuz
13+
14+
Index table taken from: http://www.elecrow.com/sensors-c-111/environment-c-111_112/uv-sensor-moduleuvm30a-p-716.html
15+
Because this table is pretty lineair, we can calculate a UVI with one decimal
1216
1317
License: Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)
14-
1518
*/
1619

1720
#include <MySensor.h>
1821
#include <SPI.h>
1922

20-
#define CHILD_ID_UV 0
2123
#define UV_SENSOR_ANALOG_PIN 0
24+
25+
#define CHILD_ID_UV 0
26+
2227
unsigned long SLEEP_TIME = 30*1000; // Sleep time between reads (in milliseconds)
2328

2429
MySensor gw;
2530
MyMessage uvMsg(CHILD_ID_UV, V_UV);
26-
int lastUV = -1;
27-
int uvIndexValue [13] = { 50, 227, 318, 408, 503, 606, 696, 795, 881, 976, 1079, 1170, 3000};
28-
int uvIndex;
31+
32+
unsigned long lastSend =0;
33+
float uvIndex;
34+
float lastUV = -1;
35+
int uvIndexValue [12] = { 50, 227, 318, 408, 503, 606, 696, 795, 881, 976, 1079, 1170};
2936

3037
void setup()
3138
{
3239
gw.begin();
3340

3441
// Send the sketch version information to the gateway and Controller
35-
gw.sendSketchInfo("UV Sensor", "1.1");
42+
gw.sendSketchInfo("UV Sensor", "1.2");
3643

3744
// Register all sensors to gateway (they will be created as child devices)
3845
gw.present(CHILD_ID_UV, S_UV);
39-
4046
}
4147

4248
void loop()
43-
{
44-
uint16_t uv = analogRead(0);// Get UV value
45-
Serial.print("Uv reading: ");
46-
Serial.println(uv);
47-
for (int i = 0; i < 13; i++)
49+
{
50+
unsigned long currentTime = millis();
51+
52+
uint16_t uv = analogRead(UV_SENSOR_ANALOG_PIN);// Get UV value
53+
if (uv>1170)
54+
uv=1170;
55+
56+
//Serial.print("UV Analog reading: ");
57+
//Serial.println(uv);
58+
59+
int i;
60+
for (i = 0; i < 12; i++)
4861
{
4962
if (uv <= uvIndexValue[i])
5063
{
5164
uvIndex = i;
5265
break;
5366
}
5467
}
55-
Serial.print("Uv index: ");
56-
Serial.println(uvIndex);
68+
69+
//calculate 1 decimal if possible
70+
if (i>0) {
71+
float vRange=uvIndexValue[i]-uvIndexValue[i-1];
72+
float vCalc=uv-uvIndexValue[i-1];
73+
uvIndex+=(1.0/vRange)*vCalc-1.0;
74+
}
75+
76+
//Serial.print("UVI: ");
77+
//Serial.println(uvIndex,2);
5778

58-
if (uvIndex != lastUV) {
59-
gw.send(uvMsg.set(uvIndex));
79+
//Send value to gateway if changed, or at least every 5 minutes
80+
if ((uvIndex != lastUV)||(currentTime-lastSend >= 5*60*1000)) {
81+
lastSend=currentTime;
82+
gw.send(uvMsg.set(uvIndex,2));
6083
lastUV = uvIndex;
6184
}
6285

libraries/MySensors/examples/WaterMeterPulseSensor/WaterMeterPulseSensor.ino

+67-48
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,23 @@
1313
#include <MySensor.h>
1414

1515
#define DIGITAL_INPUT_SENSOR 3 // The digital input you attached your sensor. (Only 2 and 3 generates interrupt!)
16+
#define SENSOR_INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
17+
1618
#define PULSE_FACTOR 1000 // Nummber of blinks per m3 of your meter (One rotation/liter)
19+
1720
#define SLEEP_MODE false // flowvalue can only be reported when sleep mode is false.
18-
#define MAX_FLOW 40 // Max flow (l/min) value to report. This filetrs outliers.
19-
#define INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
20-
#define CHILD_ID 5 // Id of the sensor child
21+
22+
#define MAX_FLOW 40 // Max flow (l/min) value to report. This filters outliers.
23+
24+
#define CHILD_ID 1 // Id of the sensor child
25+
2126
unsigned long SEND_FREQUENCY = 20000; // Minimum time between send (in milliseconds). We don't want to spam the gateway.
2227

2328
MySensor gw;
24-
29+
MyMessage flowMsg(CHILD_ID,V_FLOW);
30+
MyMessage volumeMsg(CHILD_ID,V_VOLUME);
31+
MyMessage lastCounterMsg(CHILD_ID,V_VAR1);
32+
2533
double ppl = ((double)PULSE_FACTOR)/1000; // Pulses per liter
2634

2735
volatile unsigned long pulseCount = 0;
@@ -31,108 +39,119 @@ boolean pcReceived = false;
3139
unsigned long oldPulseCount = 0;
3240
unsigned long newBlink = 0;
3341
double oldflow = 0;
34-
double volume;
35-
double oldvolume;
36-
unsigned long lastSend;
37-
unsigned long lastPulse;
38-
unsigned long currentTime;
39-
boolean metric;
40-
MyMessage flowMsg(CHILD_ID,V_FLOW);
41-
MyMessage volumeMsg(CHILD_ID,V_VOLUME);
42-
MyMessage pcMsg(CHILD_ID,V_VAR1);
43-
42+
double volume =0;
43+
double oldvolume =0;
44+
unsigned long lastSend =0;
45+
unsigned long lastPulse =0;
4446

4547
void setup()
4648
{
4749
gw.begin(incomingMessage);
4850

4951
// Send the sketch version information to the gateway and Controller
50-
gw.sendSketchInfo("Water Meter", "1.0");
52+
gw.sendSketchInfo("Water Meter", "1.2");
5153

5254
// Register this device as Waterflow sensor
5355
gw.present(CHILD_ID, S_WATER);
5456

57+
pulseCount = oldPulseCount = 0;
58+
5559
// Fetch last known pulse count value from gw
5660
gw.request(CHILD_ID, V_VAR1);
5761

58-
attachInterrupt(INTERRUPT, onPulse, RISING);
59-
lastSend = millis();
62+
lastSend = lastPulse = millis();
63+
64+
attachInterrupt(SENSOR_INTERRUPT, onPulse, RISING);
6065
}
6166

6267

6368
void loop()
6469
{
6570
gw.process();
66-
currentTime = millis();
71+
unsigned long currentTime = millis();
6772

6873
// Only send values at a maximum frequency or woken up from sleep
69-
bool sendTime = currentTime - lastSend > SEND_FREQUENCY;
70-
if (pcReceived && (SLEEP_MODE || sendTime)) {
71-
// New flow value has been calculated
74+
if (SLEEP_MODE || (currentTime - lastSend > SEND_FREQUENCY))
75+
{
76+
lastSend=currentTime;
77+
78+
if (!pcReceived) {
79+
//Last Pulsecount not yet received from controller, request it again
80+
gw.request(CHILD_ID, V_VAR1);
81+
return;
82+
}
83+
7284
if (!SLEEP_MODE && flow != oldflow) {
85+
oldflow = flow;
86+
87+
Serial.print("l/min:");
88+
Serial.println(flow);
89+
7390
// Check that we dont get unresonable large flow value.
7491
// could hapen when long wraps or false interrupt triggered
7592
if (flow<((unsigned long)MAX_FLOW)) {
7693
gw.send(flowMsg.set(flow, 2)); // Send flow value to gw
7794
}
78-
//Serial.print("l/min:");
79-
//Serial.println(flow);
80-
oldflow = flow;
8195
}
8296

83-
// No Pulse count in 2min
97+
// No Pulse count received in 2min
8498
if(currentTime - lastPulse > 120000){
8599
flow = 0;
86100
}
87-
88-
101+
89102
// Pulse count has changed
90103
if (pulseCount != oldPulseCount) {
91-
gw.send(pcMsg.set(pulseCount)); // Send volumevalue to gw VAR1
92-
double volume = ((double)pulseCount/((double)PULSE_FACTOR));
93104
oldPulseCount = pulseCount;
105+
106+
Serial.print("pulsecount:");
107+
Serial.println(pulseCount);
108+
109+
gw.send(lastCounterMsg.set(pulseCount)); // Send pulsecount value to gw in VAR1
110+
111+
double volume = ((double)pulseCount/((double)PULSE_FACTOR));
94112
if (volume != oldvolume) {
95-
gw.send(volumeMsg.set(volume, 3)); // Send volume value to gw
96113
oldvolume = volume;
114+
115+
Serial.print("volume:");
116+
Serial.println(volume, 3);
117+
118+
gw.send(volumeMsg.set(volume, 3)); // Send volume value to gw
97119
}
98120
}
99-
lastSend = currentTime;
100-
} else if (sendTime && !pcReceived) {
101-
// No count received. Try requesting it again
102-
gw.request(CHILD_ID, V_VAR1);
103-
lastSend=currentTime;
104121
}
105-
106122
if (SLEEP_MODE) {
107123
gw.sleep(SEND_FREQUENCY);
108124
}
109125
}
110126

111127
void incomingMessage(const MyMessage &message) {
112-
if (message.type==V_VAR1) {
113-
pulseCount = oldPulseCount = message.getLong();
128+
if (message.type==V_VAR1) {
129+
unsigned long gwPulseCount=message.getULong();
130+
pulseCount += gwPulseCount;
114131
Serial.print("Received last pulse count from gw:");
115132
Serial.println(pulseCount);
116133
pcReceived = true;
117134
}
118135
}
119136

120-
121137
void onPulse()
122-
{
123-
if (!SLEEP_MODE) {
138+
{
139+
if (!SLEEP_MODE)
140+
{
124141
unsigned long newBlink = micros();
125142
unsigned long interval = newBlink-lastBlink;
126-
lastPulse = millis();
127-
if (interval<500000L) {
128-
// Sometimes we get interrupt on RISING, 500000 = 0.5sek debounce ( max 120 l/min)
129-
return;
143+
144+
if (interval!=0)
145+
{
146+
lastPulse = millis();
147+
if (interval<500000L) {
148+
// Sometimes we get interrupt on RISING, 500000 = 0.5sek debounce ( max 120 l/min)
149+
return;
150+
}
151+
flow = (60000000.0 /interval) / ppl;
130152
}
131-
flow = (60000000.0 /interval) / ppl;
132153
lastBlink = newBlink;
133154
}
134155
pulseCount++;
135156
}
136157

137-
138-

0 commit comments

Comments
 (0)