Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
7 years, 8 months ago. This question has been closed. Reason: Off Topic
Time added to datalogger data
I'm learning about how to store data onto a SD card after learning about getting data from I2C sensors. I've read various Questions / answers / cookbook parts about setting up SD cards. I used part of the program answer to a previous question provided by Paul Staron about 1yr 4mths ago. As my program is pretty basic in what it's doing (I know there will be many more advanced ways of doing it) I'm struggling with getting the time implemented correctly. The temperature values are getting written to the SD card successfully and a 'time' also but the time is a) not changing b) not what I expected after I had changed to current time within the program. Any help or pointers to code probles greatly appreciated.....I'm stuck!!
/* */ #include "mbed.h" #include "TextLCD.h" #include "TMP102.h" #include "SDFileSystem.h" //Serial pc(USBTX, USBRX); I2C i2c(p28,p27);//p9-SDA,p10-SCL p28-SDA,p27-SCL I2C i2c_lcd(p28,p27); // sda, scl TMP102 TMP102(p28,p27,0x90); //Define SDA, SCL pin and i2c address SDFileSystem sd(p5,p6,p7,p8,"sd"); //MOSI, MISO, SCLK, CS TextLCD_I2C lcd(&i2c_lcd, 0x4E, TextLCD::LCD20x4); // I2C bus, 2004A Slaveaddress 0x4E, LCD Type // LED to know the status DigitalOut User_Led1(LED1); DigitalOut User_Led2(LED2); DigitalOut User_Led3(LED3); DigitalOut User_Led4(LED4); #define LED_ON 1 #define LED_OFF 0 char buffer[60]; //was[42] int err; int file_open = 0; struct tm t; // create an empty file pointer. FILE *TemperatureLogFile = NULL; float TempReading() { float value; value = TMP102.read(); return value; } void Datarecord(float data) { time_t seconds = time(NULL); strftime(buffer, 40, "%d/%m/%y-%T", localtime(&seconds)); FILE *fp = fopen("/sd/TemperatureDatalogger.txt", "a"); // the 'a' appends data to the existing file. fprintf (fp,"%2.1f,",data); fprintf (fp,"%s,\r",buffer); fclose(fp); } void SDreset() { FILE *fp = fopen("/sd/TemperatureDatalogger.txt", "w"); // creates an empty file Datalogger.txt on the SD card, old data will be lost. fclose(fp); } int main() { lcd.printf("I2CU! Searching for I2C devices...\n"); lcd.setBacklight(TextLCD::LightOn); int count = 0; for (int address=0; address<256; address+=2) { if (!i2c.write(address, NULL, 0)) { // 0 returned is ok lcd.printf(" - I2C device found at address 0x%02X\n", address); //pc.printf(" - I2C device found at address 0x%02X\n", address); count++; wait(5); lcd.cls(); } } lcd.printf("%d devices found\n", count); sd.disk_initialize(); FILE *TemperatureLogFile = fopen("/sd/TemperatureDatalogger.txt", "r"); // check, does Datalogger.txt exist, if not call SDreset to create a file. if(TemperatureLogFile == NULL) { err=1; User_Led1 = LED_ON; // file open fail file_open = 1; } else { User_Led2 = LED_ON; // file open file_open = 1; } if (err==1) {SDreset();} // ...... you will need to set time first! if (time(NULL) < 1489497558) {//set_time if not set, from unixtimestamp.com) t.tm_sec = 0; // 0-59 t.tm_min = 20; // 0-59 t.tm_hour = 13; // 0-23 t.tm_mday = 14; // 1-31 t.tm_mon = 3; // 0-11 t.tm_year = 117; // year since 1900 set_time(mktime(&t)); } while (1){ User_Led3 = LED_ON;//I'm in the loop for (int count=0; count<=59; count++) { float temperature = TempReading(); lcd.locate(0,2); lcd.printf("Temperature: %2.f degC\n", temperature); Datarecord(temperature); User_Led4 = LED_ON;//Data should have been recorded wait(60); //Wait a minute and get another reading User_Led3 = LED_OFF;//Something should have been recorded 1st pass only } SDreset(); } }