Upload test

Dependencies:   mbed SDFileSystem RTCtester

Committer:
mc2075
Date:
Tue Jan 08 12:29:18 2019 +0000
Revision:
7:0806122373bf
Parent:
6:2641d53a460a
Ad

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mc2075 7:0806122373bf 1 /********************************************************
mc2075 7:0806122373bf 2 * Real Time Clock
mc2075 7:0806122373bf 3 * By Mollie Coleman - University of Bath, 2018
mc2075 7:0806122373bf 4 *
mc2075 7:0806122373bf 5 * This code uses a pre-defined library created
mc2075 7:0806122373bf 6 * by Justin Jordan which has specified functions
mc2075 7:0806122373bf 7 * that can be used to set the time and access it
mc2075 7:0806122373bf 8 *
mc2075 7:0806122373bf 9 * Hardware:
mc2075 7:0806122373bf 10 * 1. mbed LPC1768
mc2075 7:0806122373bf 11 * 2. DS3231MPMB1 Peripheral Module
mc2075 7:0806122373bf 12 * 3. CR1025 Battery
mc2075 7:0806122373bf 13 * 4. 4x Jumper Cables
mc2075 7:0806122373bf 14 *
mc2075 7:0806122373bf 15 * Connectivity: (RTC --> mbed)
mc2075 7:0806122373bf 16 * J1-3 SCL --> P27
mc2075 7:0806122373bf 17 * J1-4 SDA --> P28
mc2075 7:0806122373bf 18 * J1-5 GND --> GND
mc2075 7:0806122373bf 19 * J1-6 VCC --> VOUT
mc2075 7:0806122373bf 20 *
mc2075 7:0806122373bf 21 * References:
mc2075 7:0806122373bf 22 * 1. https://os.mbed.com/components/DS3231/
mc2075 7:0806122373bf 23 * 2. https://os.mbed.com/users/techstep/code
mc2075 7:0806122373bf 24 * /DS3231/file/1607610a4ee9/DS3231.cpp/
mc2075 7:0806122373bf 25 * 3. https://os.mbed.com/handbook/Timer
mc2075 7:0806122373bf 26 ********************************************************/
mc2075 7:0806122373bf 27
mc2075 7:0806122373bf 28 /***********************************
mc2075 7:0806122373bf 29 * Importing of the required libs *
mc2075 7:0806122373bf 30 ***********************************/
mc2075 7:0806122373bf 31 #include "ds3231.h"
mc2075 7:0806122373bf 32 #include "iostream"
mc2075 7:0806122373bf 33 #include "SDFileSystem.h"
mc2075 7:0806122373bf 34 #include "string"
mc2075 7:0806122373bf 35 #include "sstream"
mc2075 7:0806122373bf 36 #include "inttypes.h"
mc2075 7:0806122373bf 37 #include "stdio.h"
mc2075 7:0806122373bf 38 #define ESC 0x1B
mc2075 7:0806122373bf 39 #define PRIx16 "hx"
mc2075 7:0806122373bf 40
mc2075 7:0806122373bf 41 /************************************
mc2075 7:0806122373bf 42 * Defining the ports that the RTC *
mc2075 7:0806122373bf 43 * and SD reader is attatched to. *
mc2075 7:0806122373bf 44 ************************************/
mc2075 7:0806122373bf 45 Ds3231 rtc(p28, p27);
mc2075 7:0806122373bf 46 SDFileSystem sd (p5, p6, p7, p8, "sd");
mc2075 7:0806122373bf 47
mc2075 7:0806122373bf 48 /************************************
mc2075 7:0806122373bf 49 * Creating a timer variable to be *
mc2075 7:0806122373bf 50 * used to deal with the time taken *
mc2075 7:0806122373bf 51 * to save the data *
mc2075 7:0806122373bf 52 ************************************/
mc2075 7:0806122373bf 53 Timer t;
j3 2:76cd47a603b5 54
mc2075 7:0806122373bf 55 /************************************
mc2075 7:0806122373bf 56 * This can be updated for more *
mc2075 7:0806122373bf 57 * variables, or change the type to *
mc2075 7:0806122373bf 58 * suit the needs. These are the *
mc2075 7:0806122373bf 59 * variables to be stored in on the *
mc2075 7:0806122373bf 60 * SD card in a .txt file *
mc2075 7:0806122373bf 61 ************************************/
mc2075 7:0806122373bf 62 float temperature = 0.0;
mc2075 7:0806122373bf 63 float uvData = 0.0;
mc2075 7:0806122373bf 64 float LUXData = 0.0;
mc2075 7:0806122373bf 65 char buffer[12];
mc2075 7:0806122373bf 66
mc2075 7:0806122373bf 67 /********************************************************
mc2075 7:0806122373bf 68 * settingCalTimeVal Funtion
mc2075 7:0806122373bf 69 *
mc2075 7:0806122373bf 70 * Parameters:
mc2075 7:0806122373bf 71 * 1. ds3231_time_t rtc_time
mc2075 7:0806122373bf 72 * 2. ds3231_calendar_t rtc_calendar
mc2075 7:0806122373bf 73 *
mc2075 7:0806122373bf 74 * This funtion is only used when the time
mc2075 7:0806122373bf 75 * needs to be set for the first time. Once
mc2075 7:0806122373bf 76 * done, this is no longer required as the
mc2075 7:0806122373bf 77 * time and date is stored and the RTC keeps
mc2075 7:0806122373bf 78 * it up to date.
mc2075 7:0806122373bf 79 *
mc2075 7:0806122373bf 80 * The data is stored in a tempoary variable,
mc2075 7:0806122373bf 81 * temp. A pointer is used and assigned the
mc2075 7:0806122373bf 82 * reference point for that specific piece
mc2075 7:0806122373bf 83 * of data.The vale of temp is then stored
mc2075 7:0806122373bf 84 * at that location in memory.
mc2075 7:0806122373bf 85 *
mc2075 7:0806122373bf 86 * Once all the assigning and storing of data
mc2075 7:0806122373bf 87 * is complete, the time and data are set and
mc2075 7:0806122373bf 88 * stored indefinitely.
mc2075 7:0806122373bf 89 *
mc2075 7:0806122373bf 90 * rtc.set_time() <- Function defined in ds3231.cpp
mc2075 7:0806122373bf 91 * rtc.set_calendar() <- Function defined in ds3231.cpp
mc2075 7:0806122373bf 92 * If the time/date cannot be set, the function will
mc2075 7:0806122373bf 93 * return 1, where the code is then terminated.
mc2075 7:0806122373bf 94 *
mc2075 7:0806122373bf 95 * (If the time needs to be updated, the numbers in the
mc2075 7:0806122373bf 96 * below can be altered accordingly)
mc2075 7:0806122373bf 97 ********************************************************/
mc2075 7:0806122373bf 98 void settingCalTimeVal(ds3231_time_t rtc_time, ds3231_calendar_t rtc_calendar)
mc2075 7:0806122373bf 99 {
mc2075 7:0806122373bf 100 uint32_t temp;
mc2075 7:0806122373bf 101 uint32_t* mem;
mc2075 7:0806122373bf 102 bool* mem2;
mc2075 7:0806122373bf 103
mc2075 7:0806122373bf 104 temp = 17;
mc2075 7:0806122373bf 105 mem = &rtc_time.hours;
mc2075 7:0806122373bf 106 *mem = temp;
mc2075 7:0806122373bf 107
mc2075 7:0806122373bf 108 temp = 30;
mc2075 7:0806122373bf 109 mem = &rtc_time.minutes;
mc2075 7:0806122373bf 110 *mem = temp;
mc2075 7:0806122373bf 111
mc2075 7:0806122373bf 112 temp = 30;
mc2075 7:0806122373bf 113 mem = &rtc_time.seconds;
mc2075 7:0806122373bf 114 *mem = temp;
mc2075 7:0806122373bf 115
mc2075 7:0806122373bf 116 temp = 0;
mc2075 7:0806122373bf 117 mem2 = &rtc_time.mode;
mc2075 7:0806122373bf 118 *mem2 = temp;
mc2075 7:0806122373bf 119
mc2075 7:0806122373bf 120 temp = 2;
mc2075 7:0806122373bf 121 mem = &rtc_calendar.day;
mc2075 7:0806122373bf 122 *mem = temp;
mc2075 7:0806122373bf 123
mc2075 7:0806122373bf 124 temp = 06;
mc2075 7:0806122373bf 125 mem = &rtc_calendar.date;
mc2075 7:0806122373bf 126 *mem = temp;
mc2075 7:0806122373bf 127
mc2075 7:0806122373bf 128 temp = 11;
mc2075 7:0806122373bf 129 mem = &rtc_calendar.month;
mc2075 7:0806122373bf 130 *mem = temp;
mc2075 7:0806122373bf 131
mc2075 7:0806122373bf 132 temp = 18;
mc2075 7:0806122373bf 133 mem = &rtc_calendar.year;
mc2075 7:0806122373bf 134 *mem = temp;
mc2075 7:0806122373bf 135
mc2075 7:0806122373bf 136 //Setting the time/date and storing it
mc2075 7:0806122373bf 137 if(rtc.set_time(rtc_time) || rtc.set_calendar(rtc_calendar))
mc2075 7:0806122373bf 138 {
mc2075 7:0806122373bf 139 printf("uh oh");
mc2075 7:0806122373bf 140 exit(0); //terminates the program
mc2075 7:0806122373bf 141 }
mc2075 7:0806122373bf 142 }
j3 1:7db4a1cc7abb 143
mc2075 7:0806122373bf 144 /*
mc2075 7:0806122373bf 145 * Converts the input into a type String
mc2075 7:0806122373bf 146 */
mc2075 7:0806122373bf 147 template <typename T>
mc2075 7:0806122373bf 148 std::string to_string(T value)
mc2075 7:0806122373bf 149 {
mc2075 7:0806122373bf 150 std::ostringstream os;
mc2075 7:0806122373bf 151 os << value ;
mc2075 7:0806122373bf 152 return os.str() ;
mc2075 7:0806122373bf 153 }
j3 0:6be499dd402a 154
mc2075 7:0806122373bf 155 /***********************************************************
mc2075 7:0806122373bf 156 * Main Function
mc2075 7:0806122373bf 157 *
mc2075 7:0806122373bf 158 * Calls the function that sets the time it it is needed,
mc2075 7:0806122373bf 159 * else it is commented out and the time is retrieved.
mc2075 7:0806122373bf 160 *
mc2075 7:0806122373bf 161 * rtc.get_time() <- Function defined in ds3231.cpp
mc2075 7:0806122373bf 162 * rtc.get_calendar() <- Function defined in ds3231.cpp
mc2075 7:0806122373bf 163 * These are called if the time has already been
mc2075 7:0806122373bf 164 * set and we do not want to over-write what has
mc2075 7:0806122373bf 165 * been stored in the memory.
mc2075 7:0806122373bf 166 *
mc2075 7:0806122373bf 167 * When the mbed is disconencted, it will continue to
mc2075 7:0806122373bf 168 * run and update the time. This means, when re-attatched
mc2075 7:0806122373bf 169 * the time is what it should be, not the last time it was
mc2075 7:0806122373bf 170 * when it was switched off.
mc2075 7:0806122373bf 171 ************************************************************/
j3 0:6be499dd402a 172 int main(void)
j3 0:6be499dd402a 173 {
j3 2:76cd47a603b5 174 time_t epoch_time;
mc2075 7:0806122373bf 175
mc2075 7:0806122373bf 176 /* default, use bit masks in ds3231.h for desired operation */
j3 6:2641d53a460a 177 ds3231_cntl_stat_t rtc_control_status = {0,0};
j3 6:2641d53a460a 178 ds3231_time_t rtc_time;
j3 6:2641d53a460a 179 ds3231_calendar_t rtc_calendar;
j3 1:7db4a1cc7abb 180 rtc.set_cntl_stat_reg(rtc_control_status);
j3 1:7db4a1cc7abb 181
mc2075 7:0806122373bf 182 /* Uncomment if you want to set the time */
mc2075 7:0806122373bf 183 //settingCalTimeVal(rtc_time, rtc_calendar);
mc2075 7:0806122373bf 184
mc2075 7:0806122373bf 185 rtc.get_time(&rtc_time);
mc2075 7:0806122373bf 186 rtc.get_calendar(&rtc_calendar);
mc2075 7:0806122373bf 187 //std::string CalendarResult = to_string((uint16_t)dates);
mc2075 7:0806122373bf 188 //printf("Cal: %lu", (unsigned long)dates);
j3 1:7db4a1cc7abb 189
mc2075 7:0806122373bf 190 /*
mc2075 7:0806122373bf 191 * This if statement checks to see if there is a directory already made on the
mc2075 7:0806122373bf 192 * sd card and if there is not, then it will create the wanted directory. This
mc2075 7:0806122373bf 193 * means that any SD card that is plugged in will have this directory created
mc2075 7:0806122373bf 194 * automatically.
mc2075 7:0806122373bf 195 */
mc2075 7:0806122373bf 196 DIR* dir = opendir("/sd/data");
mc2075 7:0806122373bf 197 if(dir)
j3 2:76cd47a603b5 198 {
mc2075 7:0806122373bf 199 printf("The directory already exits");
j3 2:76cd47a603b5 200 }
j3 2:76cd47a603b5 201 else
j3 2:76cd47a603b5 202 {
mc2075 7:0806122373bf 203 mkdir("/sd/data", 0777);
mc2075 7:0806122373bf 204 printf("The directory has been created on the SD card");
j3 2:76cd47a603b5 205 }
j3 2:76cd47a603b5 206
mc2075 7:0806122373bf 207 /*
mc2075 7:0806122373bf 208 * This section deals with the setting of the file name to be the date. This
mc2075 7:0806122373bf 209 * is also where the data is stored into the file, in a specific format.
mc2075 7:0806122373bf 210 */
mc2075 7:0806122373bf 211 epoch_time = rtc.get_epoch();
mc2075 7:0806122373bf 212 strftime(buffer, 32, "%I:%M:%S", localtime(&epoch_time));
j3 1:7db4a1cc7abb 213
mc2075 7:0806122373bf 214 struct tm tm = *localtime(&epoch_time);
mc2075 7:0806122373bf 215 double date = tm.tm_mday;
mc2075 7:0806122373bf 216 double month = tm.tm_mon + 1;
mc2075 7:0806122373bf 217 double year = tm.tm_year + 1900;
mc2075 7:0806122373bf 218 std::string dateS = to_string(date);
mc2075 7:0806122373bf 219 std::string monthS = to_string(month);
mc2075 7:0806122373bf 220 std::string yearS = to_string(year);
mc2075 7:0806122373bf 221
mc2075 7:0806122373bf 222 //Setting the filename to be the date in the format date-month-year
mc2075 7:0806122373bf 223 std::string fileNameS = "/sd/data/" + dateS + "-" + monthS + "-" + yearS + ".txt";
mc2075 7:0806122373bf 224 const char *fileNameC = fileNameS.c_str();
mc2075 7:0806122373bf 225 FILE *out_file = fopen(fileNameC, "w");
mc2075 7:0806122373bf 226
mc2075 7:0806122373bf 227 if(out_file == NULL)
j3 0:6be499dd402a 228 {
mc2075 7:0806122373bf 229 error("Could not open file for write\n");
mc2075 7:0806122373bf 230 }
mc2075 7:0806122373bf 231
mc2075 7:0806122373bf 232 fseek(out_file, 0, SEEK_END); // goto end of file
mc2075 7:0806122373bf 233 if(ftell(out_file) == 0)
mc2075 7:0806122373bf 234 {
mc2075 7:0806122373bf 235 fprintf(out_file, "-----------------------------------------------\r\n| Time | Temperature | UV Data | LUX Data |\r\n-----------------------------------------------\r\n");
j3 0:6be499dd402a 236 }
mc2075 7:0806122373bf 237
mc2075 7:0806122373bf 238 if(temperature != 0 && uvData != 0 && LUXData != 0)
j3 2:76cd47a603b5 239 {
mc2075 7:0806122373bf 240 fseek(out_file, 0, SEEK_END);
mc2075 7:0806122373bf 241 epoch_time = rtc.get_epoch();
mc2075 7:0806122373bf 242 strftime(buffer, 32, "%I:%M:%S %p\n", localtime(&epoch_time));
j3 2:76cd47a603b5 243
mc2075 7:0806122373bf 244 fprintf(out_file, "| %s | %f | UV Data | LUX Data |\r\n-----------------------------------------------\r\n", buffer, temperature);
j3 2:76cd47a603b5 245 }
mc2075 7:0806122373bf 246 printf("Finished writing to the file");
mc2075 7:0806122373bf 247 fclose(out_file);
mc2075 7:0806122373bf 248 }