Home › Forums › Mayfly Data Logger › Rain Gauge Tip Data Without Rain › Reply To: Rain Gauge Tip Data Without Rain
Below is the code I wrote several years ago that is still running on all our local tipping buckets. This was way before all the ModularSensors libraries and MMW portal, so things could be written differently now, but it can give you and idea on how the Mayfly handles the interrupt signal from the tipping bucket. There’s a 1-second delay after a tip is sensed to filter out any switch bouncing that sometimes occurs when a tip happens.
First you need to enable the pullup resistor on pin D10, which is already built in to the Mayfly board, you just have to close jumper SJ12 on the back of the Mayfly. Then this snippet of the code shows how to set up your sketch to look for a low trigger from the tipping bucket switch:
1 2 3 4 |
//enable the D10 pullup jumper on Mayfly solder jumper SJ12 in order for the next 2 lines to work pinMode(10, INPUT); //sets pin 10 as an input attachInterrupt(2, pin10interrupt, LOW); //sets up an interrupt looking for a LOW trigger //connect one terminal of tipping bucket to Mayfly pin D10, connect other side of bucket switch to Mayfly ground |
I’ve sent the full sketch code to various people in the past few years who have requested it, and I thought it was already posted somewhere here on the forum, but if not, here’s the entire sketch. The biggest weak point is probably the delays that surround the Xbee radio wakeup and sleep, totaling 5 seconds. If a bucket tip were to happen during these delays, they don’t always get counted. A better option would be to use a milli timer to let the Mayfly go back to looking for tips and then execute the radio on and off functions independently, without using delays. But missing 5 seconds out of a 5 minute period isn’t too bad. (We’ve got Hobo event loggers on these rain gauges to record the actual data, these Mayfly boards are just there to transmit the live tips to our in-house data viz page for realtime viewing.) If the Mayfly has to do even more time-consuming things like cellular transmissions, then having the secondary device like the Trinket would ensure that no tips are missed. There are certainly more efficient ways to build a simple counter, but cost-wise, it’s hard to beat a $7 Trinket. If someone simply wants to count tips of a rain gauge (or any switch close/open event), then this code minus the telemetry stuff works great as an event counter/logger.
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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 |
#include <Wire.h> #include <avr/sleep.h> #include <avr/wdt.h> #include <SPI.h> #include <SD.h> //SODAQ libraries #include <RTCTimer.h> #include <Sodaq_DS3231.h> #include <Sodaq_PcInt_Mod.h> String targetURL; #define READ_DELAY 1 //RTC Timer RTCTimer timer; String dataRec = ""; //a string to hold the data record int currentminute, currenthour; long currentepochtime = 0; float boardtemp = 0.0; int daily_tips = 0; //the number of tips volatile int TipFlag = 0; //flag indicating that a tip has occurred volatile int AlarmFlag = 0; //alarm flag int midnightflag = 0; //flag showing that it's midnight int batteryPin = A6; int batterysenseValue = 0; float batteryvoltage; #define XBEE_SleepPin 23 // sleep pin of the Xbee 900mhz radio module //RTC Interrupt pin #define RTC_PIN A7 #define RTC_INT_PERIOD EveryMinute #define SD_SS_PIN 12 //The data log file #define FILE_NAME "RainLog.txt" //Data header #define LOGGERNAME "Mayfly Rain Logger" #define DATA_HEADER "DateTime_UTC,TZ-Offset,Loggertime,BoardTemp,Battery_V,RainTips" void setup() { //Initialise the serial connection Serial.begin(57600); //computer connection Serial1.begin(9600); //xbee 900mhz radio module in bee header rtc.begin(); delay(200); pinMode(8, OUTPUT); //green LED pinMode(9, OUTPUT); //red LED greenred4flash(); //blink the LEDs to show the board is on setupLogFile(); //Setup timer events setupTimer(); //Setup sleep mode setupSleep(); //Make first call Serial.println("Power On, running: mayfly_rain_1.ino"); //enable the D10 pullup jumper on Mayfly solder jumper SJ12 in order for the next 2 lines to work pinMode(10, INPUT); //sets pin 10 as an input attachInterrupt(2, pin10interrupt, LOW); //sets up an interrupt looking for a LOW trigger //connect one terminal of tipping bucket to Mayfly pin D10, connect other side of bucket switch to Mayfly ground } void loop() { //Update the timer timer.update(); if (AlarmFlag == 1) { // Serial.println(" DS3231 Alarm "); AlarmFlag = 0; if (currenthour == 23 && currentminute == 59) { midnightflag = 1; digitalWrite(8, HIGH); dataRec = createDataRecord(); logData(dataRec); assembleURL(); delay(500); wakeXbee(); delay(3000); sendviaXbee(); delay(2500); sleepXbee(); delay(500); String dataRec = ""; digitalWrite(8, LOW); midnightflag = 0; daily_tips = 0; //start the daily total back to 0 delay(200); } if (currentminute % 5 == 0) { digitalWrite(8, HIGH); dataRec = createDataRecord(); logData(dataRec); assembleURL(); delay(500); wakeXbee(); delay(2000); sendviaXbee(); delay(2500); sleepXbee(); String dataRec = ""; digitalWrite(8, LOW); delay(100); } //end if minute % 5 = 0 rtc.clearINTStatus(); //This function call is a must to bring /INT pin HIGH after an interrupt. AlarmFlag = 0; } //end " if alarm_flag=1" if (TipFlag == 1) { // Serial.println(" TIP! "); daily_tips++; delay(1000); TipFlag = 0; } //Sleep //Serial.println("Going to sleep"); systemSleep(); // Serial.println("AWAKE!"); } void showTime(uint32_t ts) { //Retrieve and display the current date/time String dateTime = getDateTime(); //Serial.println(dateTime); } void setupTimer() { //Schedule the wakeup every minute timer.every(READ_DELAY, showTime); //Instruct the RTCTimer how to get the current time reading timer.setNowCallback(getNow); } void wakeISR() { AlarmFlag = 1; } void setupSleep() { pinMode(RTC_PIN, INPUT_PULLUP); PcInt::attachInterrupt(RTC_PIN, wakeISR); //Setup the RTC in interrupt mode rtc.enableInterrupts(RTC_INT_PERIOD); //Set the sleep mode set_sleep_mode(SLEEP_MODE_PWR_DOWN); } void systemSleep() { //Wait until the serial ports have finished transmitting Serial.flush(); Serial1.flush(); //The next timed interrupt will not be sent until this is cleared rtc.clearINTStatus(); //Disable ADC ADCSRA &= ~_BV(ADEN); //Sleep time noInterrupts(); sleep_enable(); interrupts(); sleep_cpu(); sleep_disable(); //Enbale ADC ADCSRA |= _BV(ADEN); } String getDateTime() { String dateTimeStr; //Create a DateTime object from the current time DateTime dt(rtc.makeDateTime(rtc.now().getEpoch())); currentepochtime = (dt.get()); //Unix time in seconds currentminute = (dt.minute()); currenthour = (dt.hour()); //Convert it to a String dt.addToString(dateTimeStr); return dateTimeStr; } uint32_t getNow() { currentepochtime = rtc.now().getEpoch(); return currentepochtime; } void greenred4flash() //fast blinks the LEDs 4 times { for (int i=1; i <= 4; i++){ digitalWrite(8, HIGH); digitalWrite(9, LOW); delay(50); digitalWrite(8, LOW); digitalWrite(9, HIGH); delay(50); } digitalWrite(9, LOW); } void setupLogFile() { //Initialise the SD card if (!SD.begin(SD_SS_PIN)) { Serial.println("Error: SD card failed to initialise or is missing."); //Hang // while (true); } //Check if the file already exists bool oldFile = SD.exists(FILE_NAME); //Open the file in write mode File logFile = SD.open(FILE_NAME, FILE_WRITE); //Add header information if the file did not already exist if (!oldFile) { logFile.println(LOGGERNAME); logFile.println(DATA_HEADER); } //Close the file to save it logFile.close(); } void logData(String rec) { //Re-open the file File logFile = SD.open(FILE_NAME, FILE_WRITE); //Write the CSV data logFile.println(rec); //Close the file to save it logFile.close(); } String createDataRecord() { //Create a String type data record in csv format String data = getDateTime(); data += ",0,"; //adds UTC-timezone offset if your RTC is set to something other than UTC rtc.convertTemperature(); //convert current temperature into registers boardtemp = rtc.getTemperature(); //Read temperature sensor value batterysenseValue = analogRead(batteryPin); batteryvoltage = (3.3/1023.) * 4.7 * batterysenseValue; data += currentepochtime; data += ","; addFloatToString(data, boardtemp, 3, 1); //float data += ","; addFloatToString(data, batteryvoltage, 4, 2); data += ","; data += daily_tips; //Serial.print("Data Record: "); //if you want to print to the serial port //Serial.println(data); return data; } static void addFloatToString(String & str, float val, char width, unsigned char precision) { char buffer[10]; dtostrf(val, width, precision, buffer); str += buffer; } void assembleURL() { targetURL = ""; targetURL = "http://somewebsite.com/capturescript.php?"; //put a php script on a server with mySQL database to capture the data targetURL += "LoggerID=SL053&Loggertime="; targetURL += currentepochtime; targetURL += "&BoardTemp="; addFloatToString(targetURL, boardtemp, 3, 1); //float targetURL += "&Battery="; addFloatToString(targetURL, batteryvoltage, 4, 2); //float targetURL += "&RainTips="; targetURL += daily_tips; targetURL += "&Summary="; targetURL += midnightflag; } void sendviaXbee() { Serial1.println(targetURL); } void sleepXbee() { delay (1000); pinMode (XBEE_SleepPin,OUTPUT); // put XBee to sleep digitalWrite(XBEE_SleepPin,HIGH); } void wakeXbee() { pinMode(XBEE_SleepPin,OUTPUT); // Set the "wake-up pin" to output digitalWrite(XBEE_SleepPin,LOW); // wake-up XBee delay(500); //make sure that XBee is ready } void pin10interrupt() { TipFlag = 1; //a tip was detected, set the tip flag } |