/* riffle_mcp9808_logging3c.ino is derived from riffle_basic_logging.ino This sketch logs temperature (°C) from three MCP9808 I2C sensor modules. The three modules have different I2C addresses determined by jumper wires. There is some extraneous code added to display results on the serial monitor for troubleshooting. I added a line to cut power to the sensor header after reading the data. */ #include "LowPower.h" //https://github.com/rocketscream/Low-Power #include #include #include //http://github.com/JChristensen/DS3232RTC #include #include #include #include "Adafruit_MCP9808.h" // for the Adafruit MCP9808 temperature sensor https://www.adafruit.com/products/1782?&main_page=product_info&products_id=1782 //sleeping stuff #define sleep_intervals 1 //RTC stuff //RTC_DS3231 RTC; //led #define led 9 //voltage stuff #define voltageAnalogMeasurePin A3 #define voltageReadCircuitSwitch 4 // debugging -- only do Serial output if debuggin #define debug 1 // 0: don't print anything out; 1: print out debugging statements #define sensorBoard 8 // the pin that powers the 555 subcircuit #define chipSelect 7 #define SDpower 6 #define analog_pin A0 // Create the MCP9808 temperature sensor objects. Each has a unique address Adafruit_MCP9808 tempsensor1 = Adafruit_MCP9808(); Adafruit_MCP9808 tempsensor2 = Adafruit_MCP9808(); Adafruit_MCP9808 tempsensor3 = Adafruit_MCP9808(); void setup() { // indicate successful startup for (int i=0;i<3;i++) { digitalWrite(led, HIGH); delay(1000); digitalWrite(led, LOW); delay(1000); } // RTC setup setSyncProvider(RTC.get); //Serial << F("RTC Sync"); //if (timeStatus() != timeSet) Serial << F(" FAIL!"); //Serial << endl; /// SD setup digitalWrite(SDpower,LOW); pinMode(SDpower,OUTPUT); digitalWrite(SDpower,LOW); if (!SD.begin(chipSelect)) { if (debug) Serial.println("Card failed, or not present"); // indicate SD problem with fast blink while(1) { digitalWrite(led,HIGH); delay(200); digitalWrite(led,LOW); delay(200); } } if (debug) Serial.begin(9600); // begin I2C protocol (necessary for RTC, and any other I2C on board Wire.begin(); // RTC ------------------------- //initialize_RTC(); // NOTE: need to initialize I2C first -- but also for any other I2C library // set mode for voltage circuit control pin, and turn the circuit off pinMode(voltageReadCircuitSwitch,OUTPUT); pinMode(sensorBoard,OUTPUT); //set the 555 board to 'output' mode digitalWrite(voltageReadCircuitSwitch, HIGH); pinMode(led, OUTPUT); //MCP9808 if (!tempsensor1.begin(0x19)) { Serial.println("Couldn't find MCP9808 A0!"); while (1); } if (!tempsensor2.begin(0x1A)) { Serial.println("Couldn't find MCP9808 A1!"); while (1); } if (!tempsensor3.begin()) { Serial.println("Couldn't find MCP9808 default!"); while (1); } } void loop () { static time_t tLast; time_t t; tmElements_t tm; t = now(); // RTC test digitalWrite(sensorBoard,LOW); //turns on the 555 timer and thermistor subcircuit uint8_t i; //measure the input voltage digitalWrite(voltageReadCircuitSwitch, LOW); //turn on voltage measurement circuit // wait a moment to let things settle delay(500); int voltageLevel = analogRead(voltageAnalogMeasurePin); // wait a moment delay(500); digitalWrite(voltageReadCircuitSwitch, HIGH); // turn it off // Onboard temp from the RTC float rtcTemp = RTC.temperature() / 4.; // measure an analog battery int analog_value = analogRead(analog_pin); //get the time //long unixNow = now.unixtime(); // Read sensor 1 Serial.println("wake up MCP9808 Sensor 1"); // wake up MSP9808 - power consumption ~200 mikro Ampere tempsensor1.shutdown_wake(0); // required before reading temp // Read and print out the temperature, then convert to *F float c1 = tempsensor1.readTempC(); float f = c1 * 9.0 / 5.0 + 32; Serial.print("Sensor 1:\t\t "); Serial.print(c1); Serial.print("*C\t"); Serial.print(f); Serial.println("*F"); delay(250); Serial.println("Shutdown MCP9808.... "); tempsensor1.shutdown_wake(1); // shutdown MSP9808 - power consumption ~0.1 mikro Ampere // Read sensor 2 Serial.println("wake up MCP9808 Sensor 2"); // wake up MSP9808 - power consumption ~200 mikro Ampere tempsensor2.shutdown_wake(0); // required before reading temp // Read and print out the temperature, then convert to *F float c2 = tempsensor2.readTempC(); float f2 = c2 * 9.0 / 5.0 + 32; Serial.print("Sensor 2:\t\t "); Serial.print(c2); Serial.print("*C\t"); Serial.print(f2); Serial.println("*F"); delay(250); Serial.println("Shutdown MCP9808.... "); tempsensor2.shutdown_wake(1); // shutdown MSP9808 - power consumption ~0.1 mikro Ampere // Read sensor 3 Serial.println("wake up MCP9808 Sensor 3"); // wake up MSP9808 - power consumption ~200 mikro Ampere tempsensor3.shutdown_wake(0); // required before reading temp // Read and print out the temperature, then convert to *F float c3 = tempsensor3.readTempC(); float f3 = c3 * 9.0 / 5.0 + 32; Serial.print("Sensor 3:\t\t "); Serial.print(c3); Serial.print("*C\t"); Serial.print(f3); Serial.println("*F"); delay(250); Serial.println("Shutdown MCP9808.... "); Serial.println(""); tempsensor3.shutdown_wake(1); // shutdown MSP9808 - power consumption ~0.1 mikro Ampere digitalWrite(sensorBoard,HIGH); // this cuts power to the 3.3V pinout // make a string for assembling the data to log: String dataString = ""; dataString += year(); dataString += "-"; dataString += padInt(month(), 2); dataString += "-"; dataString += padInt(day(), 2); dataString += " "; dataString += padInt(hour(), 2); dataString += ":"; dataString += padInt(minute(), 2); dataString += ":"; dataString += padInt(second(), 2); dataString += ","; char buffer[10]; dataString += dtostrf(rtcTemp, 5, 2, buffer); dataString += ","; dataString += String(analog_value); dataString += ","; dataString += String(voltageLevel); dataString += ","; dataString += dtostrf(c1, 5, 2, buffer); dataString += ","; dataString += dtostrf(c2, 5, 2, buffer); dataString += ","; dataString += dtostrf(c3, 5, 2, buffer); if(debug) Serial.println(dataString); // open the file. note that only one file can be open at a time, // so you have to close this one before opening another. File dataFile = SD.open("datalog1.csv", FILE_WRITE); // if the file is available, write to it: if (dataFile) { dataFile.println(dataString); dataFile.close(); // print to the serial port too: //indicate successful write with short blink digitalWrite(led, HIGH); delay(40); digitalWrite(led, LOW); } // if the file isn't open, pop up an error: else { if (debug) Serial.println("error opening datalog.txt"); } // sleep for a while if (debug==0) { sleep_for_8s_interval(sleep_intervals); } else { delay(sleep_intervals*8000); } } String padInt(int x, int pad) { String strInt = String(x); String str = ""; if (strInt.length() >= pad) { return strInt; } for (int i=0; i < (pad-strInt.length()); i++) { str += "0"; } str += strInt; return str; } String int2string(int x) { // formats an integer as a string assuming x is in 1/100ths String str = String(x); int strLen = str.length(); if (strLen <= 2) { str = "0." + str; } else if (strLen <= 3) { str = str.substring(0, 1) + "." + str.substring(1); } else if (strLen <= 4) { str = str.substring(0, 2) + "." + str.substring(2); } else { str = "-9999"; } return str; } void sleep_for_8s_interval(int numIntervals) { // will power down for numIntervals * 8 seconds for (int i=0;i