Home › Forums › Mayfly Data Logger › Atlas Scientific EZO logging program › Reply To: Atlas Scientific EZO logging program
I finally solved the never-sleep problem. The problem seems to be related to the slow decay of the power to the Grove connector. With the sensor connected the decay is approximately 50ms. In the senosrSleep() function the power and rx/tx are turned off but the Vcc voltage doesn’t get a chance to decay before this function is exited which seems to cause an immediate wakeup. I added a 100ms delay to that function and now seems to work as planned.
I am not sure what causes the immediate wake up but must be related to the residual voltage on the pwr pin. With the sensor disconnected the code works with out the delay probably because there is no capacitance to hold up the voltage.
I cleaned up the code a bit and here is rev 1.0
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 |
/* This example sketch puts the Mayfly board into sleep mode. It wakes up at specific times to turn on the Atlas Scientific EZO sensors. The EZO sensor is configured to output sensor data once per second (c,1 command)which is logged to the SD card along with time stamp, board temp, and battery voltage. Note: if sleep time is greater than 1 minute the board will still wake for 100ms which is less than the turn on time of the sensor. JOM 2/9/17 added power on/off to EZO board JOM 2/12/17 downloaded ...Mod.h libraries to fix RTC interupt conflicts. Need to turn off rx and tx when putting to sleep otherwise rx/tx powers the sensor after Vcc is turned off JOM 2/13/17 Problem sensor on most of time. JOM 3/18/17 The never-sleep problem was fixed by adding a 100ms delay to the sensorsSleep() function. This allows Vcc to the sensor to decay to zero which is ~50 ms. The pwr on takes only ~5us. */ #include <Wire.h> #include <avr/sleep.h> #include <avr/wdt.h> #include <SPI.h> #include <SD.h> #include <SoftwareSerialMod.h> #include <RTCTimer.h> #include <Sodaq_DS3231.h> #include <Sodaq_PcInt_Mod.h> RTCTimer timer; #define rx 11 //D11 to Tx on EZO (white on Grove conn) #define tx 10 //D10 to Rx on EZO (yellow on Grove conn) SoftwareSerialMod myserial(rx, tx); //define how the soft serial port is going to work. String dataRec = ""; String sensorstring = "EZO data"; //a string to hold the data from the EZO product int currentminute; int n = 0; int nMax = 20; int sleepMinutes = 5; //total minutes the Mayfly is in sleep mode long currentepochtime = 0; float boardtemp; int batteryPin = A6; // to read battery voltage int batterysenseValue = 0; // variable to store the value coming from the sensor float batteryvoltage; boolean sensor_stringcomplete = false; //have we received all the data from the EZO product //RTC Interrupt pin #define RTC_PIN A7 #define RTC_INT_PERIOD EveryMinute #define SD_SS_PIN 12 //orig had pin 11 //The data log file #define FILE_NAME "datafile.txt" //Data header #define LOGGERNAME "SampleLogger" #define DATA_HEADER "DateTime_EST,Loggertime,BoardTemp_C,Battery_V" void setup() { //Initialise the serial connection Serial.begin(9600); myserial.begin(9600); //set baud rate for software serial port_3 to 9600 //if myserial active sleep doesn't work rtc.begin(); pinMode(8, OUTPUT); pinMode(9, OUTPUT); pinMode(22, OUTPUT); //Power to Grove connectors sensorstring.reserve(30); //set aside some bytes for receiving data from EZO product greenred4flash(); //blink the LEDs to show the board is on setupLogFile(); setupTimer(); //Setup timer events setupSleep(); //Setup sleep mode digitalWrite(10, false); //Turn off tx to EZO ckt digitalWrite(11, false); //Turn off rx to EZO ckt digitalWrite(22, true); //Turn on power to EZO ckt Serial.println("Power On, running: mayfly_sleep_sample1.ino"); showTime(getNow()); } void loop() { //Update the timer timer.update(); if (currentminute % sleepMinutes == 0) //will wake every minute for set delay untill condition //is satisfied { while (!sensor_stringcomplete && (n < nMax)) { recieveSensorData(); } n = 0; } delay(100); //should be less than sensor turnon time which is ~1sec //Sleep systemSleep(); } //end loop void sensorsSleep() { Serial.println("..going to sleep!"); digitalWrite(22, false); //Turn off power to EZO ckt digitalWrite(10, false); //Turn off tx to EZO ckt digitalWrite(11, false); //Turn off rx to EZO ckt delay(100); //adding delay fixed the never-sleep problem } void sensorsWake() { digitalWrite(22, true); //Turn on power to EZO ckt Serial.println("..I'm awake!"); } void recieveSensorData() { if (myserial.available() > 0) { //if we see that the EZO product has sent a character. char inchar = (char)myserial.read(); //get the char we just received sensorstring += inchar; if (inchar == '\r') { sensor_stringcomplete = true; //if the incoming character is a <CR>, set the flag n++; } } if (sensor_stringcomplete) { //if a string from the EZO product has been received //in its entirety dataRec = createDataRecord(); if (n >= 3) //don't log sensor startup response { logData(dataRec); //Save the data record to the log file } Serial.print(" "); Serial.println(dataRec); Serial.print("n= "); Serial.println(n); sensorstring = ""; //clear the string: sensor_stringcomplete = false; //reset the flag used to tell if we have received a } } String createDataRecord() { //Create a String type data record in csv format //TimeDate, Loggertime,Temp_DS, Diff1, Diff2, boardtemp String data = getDateTime(); data += ","; rtc.convertTemperature(); //convert current temperature into registers boardtemp = rtc.getTemperature(); //Read temperature sensor value batterysenseValue = analogRead(batteryPin); batteryvoltage = (3.3 / 1023.) * 1.47 * batterysenseValue; data += currentepochtime; data += ","; addFloatToString(data, boardtemp, 3, 1); //float data += ","; addFloatToString(data, batteryvoltage, 4, 2); data += " , "; //adds a comma between values data += sensorstring; //adds pH to the data string return data; } 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(1, showTime); //Instruct the RTCTimer how to get the current time reading timer.setNowCallback(getNow); } void wakeISR() { //Leave this blank } 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() { //This method handles any sensor specific sleep setup sensorsSleep(); //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); //This method handles any sensor specific wake setup sensorsWake(); } 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()); //Convert it to a String dt.addToString(dateTimeStr); return dateTimeStr; } uint32_t getNow() { currentepochtime = rtc.now().getEpoch(); return currentepochtime; } void greenred4flash() { 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(); } static void addFloatToString(String & str, float val, char width, unsigned char precision) { char buffer[10]; dtostrf(val, width, precision, buffer); str += buffer; } |