Skip to content

Commit c54ff68

Browse files
authored
Merge pull request mysensors#8 from petewill/master
Update RainGauge example to be compatible with v2.0
2 parents 32c0587 + e8de4c0 commit c54ff68

File tree

1 file changed

+69
-65
lines changed

1 file changed

+69
-65
lines changed

examples/RainGauge/RainGauge.ino

Lines changed: 69 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
April 26, 2015
55
6-
Version 1.4.1 alpha
6+
Version 2.0
77
88
Arduino Tipping Bucket Rain Gauge
99
@@ -29,38 +29,36 @@
2929
* Optional Temp/Humidity (DHT-22 or DHT-11) and Light LUX (BH1750) sensors. To use, uncomment
3030
#define DHT_ON and/or #define LUX_ON
3131
* Optionally send total accumulation of each day's rainfall or send only individual days rainfall totals.
32-
Comment out #define USE_DAILY to display individual daily rainfall.
32+
Uncomment #define USE_DAILY to display individual daily rainfall. If it is commented out it will display
33+
a cumulative total rainfall (day4 = day1+day2+day3+day4 etc)
3334
3435
by @BulldogLowell and @PeteWill for free public use
3536
3637
*/
3738

38-
// Enable debug prints to serial monitor
39-
#define MY_DEBUG
39+
//#define MY_DEBUG // Enable MySensors debug prints to serial monitor
4040

4141
// Enable and select radio type attached
4242
#define MY_RADIO_NRF24
4343
//#define MY_RADIO_RFM69
4444

45-
//#define MY_NODE_ID 7
45+
//#define MY_NODE_ID 7 //uncomment this line to assign a static ID
4646

47-
#include <SPI.h>
48-
#include <MySensors.h>
4947
#include <math.h>
5048
#include <TimeLib.h>
49+
#include <MySensors.h>
5150

5251
#define SKETCH_NAME "Rain Gauge"
53-
#define SKETCH_VERSION "1.4.1a"
52+
#define SKETCH_VERSION "2.0"
5453

55-
#define DWELL_TIME 125 // this allows for radio to come back to power after a transmission, ideally 0
54+
#define DWELL_TIME 40 // this allows for radio to come back to power after a transmission, ideally 0
5655

57-
//#define DEBUG_ON // comment out this line to disable serial debug
56+
//#define DEBUG_ON // Rain gauge specific debug messages.
5857
#define DHT_ON // uncomment out this line to enable DHT sensor
5958
#define LUX_ON // uncomment out this line to enable BH1750 sensor
60-
//#define USE_DAILY // displays each time segment as an accumulation of prior periods inclusive. Comment out to display individual daily rainfall totals in the variables sent to your controller.
59+
//#define USE_DAILY // Uncomment to display individual daily rainfall totals in the variables sent to your controller. If it's commented it will add each day to the next for a cumulative total.
6160

6261
#define TIP_SENSOR_PIN 3
63-
6462
#define CALIBRATE_FACTOR 60 // amount of rain per rain bucket tip e.g. 5 is .05mm
6563
#define DHT_LUX_DELAY 300000 //Delay in milliseconds that the DHT and LUX sensors will wait before sending data
6664

@@ -114,40 +112,69 @@ MyMessage msgTrippedVar2(CHILD_ID_TRIPPED_INDICATOR, V_VAR2);
114112
BH1750 lightSensor;
115113
MyMessage msg(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
116114
unsigned int lastlux;
117-
byte heartbeat = 10; //Used to send the light lux to gateway as soon as the device is restarted and after the DHT_LUX_DELAY has happened 10 times
115+
uint8_t heartbeat = 10; //Used to send the light lux to gateway as soon as the device is restarted and after the DHT_LUX_DELAY has happened 10 times
118116
#endif
119117
unsigned long sensorPreviousMillis;
120118
int eepromIndex;
119+
int tipSensorPin = 3; // Pin the tipping bucket is connected to. Must be interrupt capable pin
121120
int ledPin = 5; // Pin the LED is connected to. PWM capable pin required
121+
#ifdef DEBUG_ON
122122
unsigned long dataMillis;
123123
unsigned long serialInterval = 600000UL;
124+
#endif
124125
const unsigned long oneHour = 3600000UL;
125126
unsigned long lastTipTime;
126127
unsigned long lastRainTime; //Used for rainRate calculation
127128
unsigned int rainBucket [RAIN_BUCKET_SIZE] ; /* 24 hours x 5 Days = 120 hours */
128129
unsigned int rainRate = 0;
129-
byte rainWindow = 72; //default rain window in hours. Will be overwritten with msgTrippedVar1.
130+
uint8_t rainWindow = 72; //default rain window in hours. Will be overwritten with msgTrippedVar1.
130131
volatile int wasTippedBuffer = 0;
131132
int rainSensorThreshold = 50; //default rain sensor sensitivity in hundredths. Will be overwritten with msgTrippedVar2.
132-
byte state = 0;
133-
byte oldState = -1;
133+
uint8_t state = 0;
134+
uint8_t oldState = 2; //Setting the default to something other than 1 or 0
134135
unsigned int lastRainRate = 0;
135136
int lastMeasure = 0;
136137
bool gotTime = false;
137-
byte lastHour;
138-
byte currentHour;
138+
uint8_t lastHour;
139+
uint8_t currentHour;
139140
//
141+
void presentation() {
142+
// Register all sensors to gw (they will be created as child devices)
143+
sendSketchInfo(SKETCH_NAME, SKETCH_VERSION);
144+
wait(DWELL_TIME);
145+
present(CHILD_ID_RAIN_LOG, S_RAIN);
146+
wait(DWELL_TIME);
147+
present(CHILD_ID_TRIPPED_INDICATOR, S_MOTION);
148+
wait(DWELL_TIME);
149+
150+
#ifdef DHT_ON
151+
present(CHILD_ID_HUM, S_HUM);
152+
wait(DWELL_TIME);
153+
present(CHILD_ID_TEMP, S_TEMP);
154+
wait(DWELL_TIME);
155+
#endif
156+
157+
158+
#ifdef LUX_ON
159+
present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
160+
#endif
161+
162+
DEBUG_PRINTLN(F("Sensor Presentation Complete"));
163+
}
164+
140165
void setup()
141166
{
142-
SERIAL_START(115200);
167+
#ifndef MY_DEBUG
168+
SERIAL_START(115200); //Start serial if MySensors debugging isn't enabled
169+
#endif
143170
//
144171
// Set up the IO
145172
pinMode(TIP_SENSOR_PIN, INPUT_PULLUP);
146173
attachInterrupt (digitalPinToInterrupt(TIP_SENSOR_PIN), sensorTipped, FALLING); // depending on location of the hall effect sensor may need CHANGE
147174
pinMode(ledPin, OUTPUT);
148175
digitalWrite(ledPin, HIGH);
149176
//
150-
//Sync time with the server, this will be called hourly in order to keep time from creeping with the crystal
177+
//Sync time with the server
151178
//
152179
unsigned long functionTimeout = millis();
153180
while (timeStatus() == timeNotSet && millis() - functionTimeout < 30000UL)
@@ -165,7 +192,7 @@ void setup()
165192
bool isDataOnEeprom = false;
166193
for (int i = 0; i < E_BUFFER_LENGTH; i++)
167194
{
168-
byte locator = loadState(EEPROM_BUFFER_LOCATION + i);
195+
uint8_t locator = loadState(EEPROM_BUFFER_LOCATION + i);
169196
if (locator == 0xFE) // found the EEPROM circular buffer index
170197
{
171198
eepromIndex = EEPROM_BUFFER_LOCATION + i;
@@ -190,8 +217,10 @@ void setup()
190217
saveState(i, 0x00);
191218
}
192219
}
220+
#ifdef DEBUG_ON
193221
dataMillis = millis();
194-
lastTipTime = millis() - oneHour; //why is this -oneHour?? Doesn't millis() start at 0 when first powered on?
222+
#endif
223+
lastTipTime = millis();
195224
//
196225
request(CHILD_ID_TRIPPED_INDICATOR, V_VAR1);
197226
wait(DWELL_TIME);
@@ -200,42 +229,15 @@ void setup()
200229
//
201230
#ifdef DHT_ON
202231
dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN);
203-
wait(DWELL_TIME);
204232
metric = getConfig().isMetric;
233+
wait(DWELL_TIME);
205234
#endif
206235
//
207236
#ifdef LUX_ON
208-
wait(DWELL_TIME);
209237
lightSensor.begin();
210238
#endif
211239
//
212-
DEBUG_PRINTLN(F("Radio Setup Complete!"));
213-
transmitRainData();
214-
}
215-
216-
217-
void presentation() {
218-
// Register all sensors to gw (they will be created as child devices)
219-
sendSketchInfo(SKETCH_NAME, SKETCH_VERSION);
220-
wait(DWELL_TIME);
221-
present(CHILD_ID_RAIN_LOG, S_RAIN);
222-
wait(DWELL_TIME);
223-
present(CHILD_ID_TRIPPED_INDICATOR, S_MOTION);
224-
wait(DWELL_TIME);
225-
226-
#ifdef DHT_ON
227-
present(CHILD_ID_HUM, S_HUM);
228-
wait(DWELL_TIME);
229-
present(CHILD_ID_TEMP, S_TEMP);
230-
wait(DWELL_TIME);
231-
#endif
232-
233-
234-
#ifdef LUX_ON
235-
present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
236-
#endif
237-
238-
DEBUG_PRINTLN(F("Sensor Presentation Complete"));
240+
transmitRainData(); //Setup complete send any data loaded from eeprom to gateway
239241
}
240242

241243
void loop()
@@ -339,7 +341,7 @@ void loop()
339341
send(msgRainRate.set(rainRate, 1));
340342
wait(DWELL_TIME);
341343
DEBUG_PRINTLN(F("Sending rainRate is 0 to controller"));
342-
lastHour = currentHour;
344+
lastHour = hour();
343345
}
344346
if (millis() - sensorPreviousMillis > DHT_LUX_DELAY)
345347
{
@@ -427,6 +429,7 @@ int rainTotal(int hours)
427429
return total;
428430
}
429431

432+
#ifdef DEBUG_ON
430433
void updateSerialData(int x)
431434
{
432435
DEBUG_PRINT(F("Rain last "));
@@ -440,28 +443,29 @@ void updateSerialData(int x)
440443
tipCount = tipCount / 100;
441444
DEBUG_PRINTLN(tipCount);
442445
}
446+
#endif
443447

444-
void loadRainArray(int value) // retrieve stored rain array from EEPROM on powerup
448+
void loadRainArray(int eValue) // retrieve stored rain array from EEPROM on powerup
445449
{
446-
for (int i = 0; i < RAIN_BUCKET_SIZE; i++)
450+
for (int i = 1; i < RAIN_BUCKET_SIZE; i++)
447451
{
448-
value = value - 2;
449-
if (value < EEPROM_BUFFER_LOCATION)
452+
eValue = eValue - 2;
453+
if (eValue < EEPROM_BUFFER_LOCATION)
450454
{
451-
value = EEPROM_BUFFER_LOCATION + E_BUFFER_LENGTH;
455+
eValue = EEPROM_BUFFER_LOCATION + E_BUFFER_LENGTH;
452456
}
453457
DEBUG_PRINT(F("EEPROM location: "));
454-
DEBUG_PRINTLN(value);
455-
byte rainValueHigh = loadState(value);
456-
byte rainValueLow = loadState(value + 1);
458+
DEBUG_PRINTLN(eValue);
459+
uint8_t rainValueHigh = loadState(eValue);
460+
uint8_t rainValueLow = loadState(eValue + 1);
457461
unsigned int rainValue = rainValueHigh << 8;
458462
rainValue |= rainValueLow;
459-
rainBucket[i + 1] = rainValue;
463+
rainBucket[i] = rainValue;
460464
//
461465
DEBUG_PRINT(F("rainBucket[ value: "));
462-
DEBUG_PRINT(i + 1);
466+
DEBUG_PRINT(i);
463467
DEBUG_PRINT(F("] value: "));
464-
DEBUG_PRINTLN(rainBucket[i + 1]);
468+
DEBUG_PRINTLN(rainBucket[i]);
465469
}
466470
}
467471

@@ -599,10 +603,10 @@ void slowFlash(void)
599603
}
600604
}
601605

602-
void receiveTime(unsigned long time)
606+
void receiveTime(unsigned long newTime)
603607
{
604608
DEBUG_PRINTLN(F("Time received..."));
605-
setTime(time);
609+
setTime(newTime);
606610
char theTime[6];
607611
sprintf(theTime, "%d:%2d", hour(), minute());
608612
DEBUG_PRINTLN(theTime);

0 commit comments

Comments
 (0)