Home › Forums › Mayfly Data Logger › Atlas Scientific EZO logging program › Reply To: Atlas Scientific EZO logging program
2017-03-14 at 8:45 PM
#2082
I would appreciate help on a problem with my code derived from the Mayfly-sleep.ino example. I am using the SoftwareSerialMod.h library as recommended to resolve a pin conflict. However, when I activate myserial.begin(9600) the program does
-
not
go into sleep mode.
attached is my code:
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 |
/* This example sketch puts the Mayfly board into sleep mode. It wakes up at specific times, records the temperature and battery voltage onto the microSD card, prints the data string to the serial port, and goes back to sleep. 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. may need to serial.flush JOM 3/12/17 problem was not plugged into grove 10/11 socket!! JOM 3/13/17 does not go to sleep even with minimal EZO code JOM 3/14/17 activating myserial.begin(9600) causes sleep mode not to work. */ #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; long currentepochtime = 0; float boardtemp; int batteryPin = A6; // select the input pin for the potentiometer 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(); delay(100); 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 Serial.println("Power On, running: mayfly_sleep_sample1.ino"); showTime(getNow()); } void loop() { //Update the timer timer.update(); if (currentminute % 1 == 0) // change "2" to "5" to wake up logger every 5 minutes instead { //Serial.println(" Initiating sensor reading and logging data to SDcard...."); dataRec = createDataRecord(); //Save the data record to the log file logData(dataRec); //Echo the data to the serial connection //Serial.println(); Serial.print("Data Record: "); Serial.println(dataRec); String dataRec = ""; while (!sensor_stringcomplete && (n <= 10) ) { recieveSensorData(); Serial.print("n= "); Serial.println(n); delay(1000); n++; } n = 0; } delay(1000); //Sleep systemSleep(); } //end loop void sensorsSleep() { 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 } void sensorsWake() { digitalWrite(22, true); //Turn on power to EZO ckt Serial.println("..I'm awake!"); delay(5000); } 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 } } if (sensor_stringcomplete) { //if a string from the EZO product has been received //in its entirety dataRec = createDataRecord(); logData(dataRec); //Save the data record to the log file Serial.print(" "); Serial.println(dataRec); 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; } |