forked from sensebox/hydreon-rainsensor-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hydreon.cpp
97 lines (85 loc) · 2.13 KB
/
hydreon.cpp
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
/*
Hydreon Rainsensor Library for senseBox
(RG15Arduino Library builds unrobust connection)
*/
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "hydreon.h"
#define HYDREON_SERIAL_DELAY 1200 // large but necessary
HYDREON::HYDREON(Uart &serial) : sensor(serial) {}
void HYDREON::begin()
{
sensor.begin(9600); // opens serial port
sensor.println('M'); // force Metric
sensor.println('H'); // high Resulution
sensor.println('P'); // pooling mode (not continuous)
delay(3 * HYDREON_SERIAL_DELAY);
while (sensor.available())
sensor.read();
}
void HYDREON::readAllData()
{
sensor.println('R');
delay(HYDREON_SERIAL_DELAY);
if (sensor.available())
{
while (sensor.available())
data = sensor.readStringUntil('\n');
startIndex = data.indexOf("Acc") + 3;
endIndex = data.indexOf("mm,");
accumulation = data.substring(startIndex, endIndex).toFloat();
startIndex = data.indexOf("EventAcc") + 8;
endIndex = data.indexOf("mm,", startIndex);
eventAccumulation = data.substring(startIndex, endIndex).toFloat();
startIndex = data.indexOf("TotalAcc") + 8;
endIndex = data.indexOf("mm,", startIndex);
totalAccumulation = data.substring(startIndex, endIndex).toFloat();
startIndex = data.indexOf("RInt") + 4;
endIndex = data.indexOf("mmph", startIndex);
rainfallIntensity = data.substring(startIndex, endIndex).toFloat();
}
else
{
accumulation = -1;
eventAccumulation = -1;
totalAccumulation = -1;
rainfallIntensity = -1;
}
}
void HYDREON::setHighResolution(bool high)
{
sensor.println('H' ? high : 'L');
delay(HYDREON_SERIAL_DELAY);
while (sensor.available())
sensor.read();
}
void HYDREON::temporaryReset()
{
sensor.println('O');
delay(HYDREON_SERIAL_DELAY);
while (sensor.available())
sensor.read();
}
float HYDREON::getAccumulation()
{
return accumulation;
}
float HYDREON::getEventAccumulation()
{
return eventAccumulation;
}
float HYDREON::getTotalAccumulation()
{
return totalAccumulation;
}
float HYDREON::getRainfallIntensity()
{
return rainfallIntensity;
}
String HYDREON::getDataString()
{
return data;
}