Network, SD, Readall. comments added

Dependencies:   Peripherals Buffer_lib_v2 SD_Lib_EffedUP_ERP Time_Lib_v2 Year3_Version5 BMP280 Network_Lib TextLCD BME280

Committer:
emilytrembeth
Date:
Mon Nov 19 20:54:41 2018 +0000
Revision:
5:60e116a1e913
Parent:
4:da63962bc0f1
Child:
6:2ef9c06ce506
x

Who changed what in which revision?

UserRevisionLine numberNew contents of line
noutram 0:90e393878517 1 #include "mbed.h"
emilytrembeth 4:da63962bc0f1 2 #include "TextLCD.h"
emilytrembeth 4:da63962bc0f1 3 #include "SDBlockDevice.h"
emilytrembeth 4:da63962bc0f1 4 #include "FATFileSystem.h"
emilytrembeth 4:da63962bc0f1 5 #include "sample_hardware.hpp"
noutram 0:90e393878517 6
emilytrembeth 5:60e116a1e913 7 #define Day 1
emilytrembeth 5:60e116a1e913 8 #define Month 2
emilytrembeth 5:60e116a1e913 9 #define Year 3
emilytrembeth 5:60e116a1e913 10 #define Hour 4
emilytrembeth 5:60e116a1e913 11 #define Minute 5
emilytrembeth 5:60e116a1e913 12 #define Second 6
emilytrembeth 5:60e116a1e913 13
noutram 0:90e393878517 14 //Function declarations
emilytrembeth 4:da63962bc0f1 15 void displayOnLcd();
emilytrembeth 4:da63962bc0f1 16 void updateRealTimeClock(char *buffer);
emilytrembeth 4:da63962bc0f1 17 void getLineFromSerial(char *keyBuffer, int bufferLength);
emilytrembeth 4:da63962bc0f1 18 void displayMessageOnConsole();
emilytrembeth 5:60e116a1e913 19 void SetingTimeWithButtons ();
emilytrembeth 5:60e116a1e913 20 void FunctionSensor();
emilytrembeth 5:60e116a1e913 21 void FunctionTime();
noutram 0:90e393878517 22
emilytrembeth 4:da63962bc0f1 23 TextLCD lcd(D9, D8, D7, D6, D4, D2); // rs, e, d4-d7
emilytrembeth 4:da63962bc0f1 24 SDBlockDevice sd(PB_5, D12, D13, D10); // mosi, miso, sclk, cs
emilytrembeth 4:da63962bc0f1 25 Serial pc(SERIAL_TX, SERIAL_RX);
emilytrembeth 4:da63962bc0f1 26 time_t currentTime;
emilytrembeth 4:da63962bc0f1 27 char lcdBuffer[32];
emilytrembeth 4:da63962bc0f1 28 DigitalOut myled(LED1);
emilytrembeth 4:da63962bc0f1 29 Ticker ticker;
emilytrembeth 4:da63962bc0f1 30 DigitalIn SWUser(USER_BUTTON);
emilytrembeth 4:da63962bc0f1 31 Mutex date_mutex;
emilytrembeth 5:60e116a1e913 32 int setting;
emilytrembeth 5:60e116a1e913 33 int t;
emilytrembeth 5:60e116a1e913 34 //InterruptIn SW1;
noutram 0:90e393878517 35
noutram 1:948bd552a2a2 36 Thread t1;
noutram 1:948bd552a2a2 37 Thread t2;
emilytrembeth 4:da63962bc0f1 38
emilytrembeth 4:da63962bc0f1 39
emilytrembeth 4:da63962bc0f1 40 void displayOnLcd() {
emilytrembeth 4:da63962bc0f1 41 date_mutex.lock();
emilytrembeth 4:da63962bc0f1 42
emilytrembeth 4:da63962bc0f1 43 myled = !myled;
emilytrembeth 4:da63962bc0f1 44
emilytrembeth 4:da63962bc0f1 45 currentTime = time(NULL);
emilytrembeth 5:60e116a1e913 46 strftime(lcdBuffer, 32, "%d/%m/%Y %H:%M:%S", localtime(&currentTime));
emilytrembeth 4:da63962bc0f1 47
emilytrembeth 4:da63962bc0f1 48 //lcd.cls();
emilytrembeth 5:60e116a1e913 49 // lcd.locate(0,0);
emilytrembeth 4:da63962bc0f1 50 pc.printf("%s\n\r", lcdBuffer);
emilytrembeth 4:da63962bc0f1 51
emilytrembeth 4:da63962bc0f1 52 myled = !myled;
emilytrembeth 4:da63962bc0f1 53 date_mutex.unlock();
emilytrembeth 4:da63962bc0f1 54 };
noutram 0:90e393878517 55
emilytrembeth 4:da63962bc0f1 56 void updateRealTimeClock(char *buffer) {
emilytrembeth 4:da63962bc0f1 57 date_mutex.lock();
emilytrembeth 4:da63962bc0f1 58
emilytrembeth 4:da63962bc0f1 59 char *tp;
emilytrembeth 4:da63962bc0f1 60 char *timeArray[6];
emilytrembeth 4:da63962bc0f1 61 int arrayIndex;
emilytrembeth 4:da63962bc0f1 62 struct tm struct_time;
emilytrembeth 4:da63962bc0f1 63
emilytrembeth 4:da63962bc0f1 64 // extract number from string
emilytrembeth 4:da63962bc0f1 65 arrayIndex = 0;
emilytrembeth 4:da63962bc0f1 66 tp = strtok( buffer, " /:-" );
emilytrembeth 4:da63962bc0f1 67 timeArray[arrayIndex++] = tp;
emilytrembeth 4:da63962bc0f1 68 printf("%d ", atoi(tp));
emilytrembeth 4:da63962bc0f1 69 while ( tp != NULL && arrayIndex < 6 ) {
emilytrembeth 4:da63962bc0f1 70 tp = strtok( NULL," /:-" );
emilytrembeth 4:da63962bc0f1 71 timeArray[arrayIndex++] = tp;
emilytrembeth 4:da63962bc0f1 72 if ( tp != NULL ) {
emilytrembeth 4:da63962bc0f1 73 printf("%d ", atoi(tp));
emilytrembeth 4:da63962bc0f1 74 }
emilytrembeth 4:da63962bc0f1 75 }
emilytrembeth 4:da63962bc0f1 76 printf("\r\n");
emilytrembeth 4:da63962bc0f1 77
emilytrembeth 4:da63962bc0f1 78 // store number into time struct
emilytrembeth 5:60e116a1e913 79 struct_time.tm_mday = atoi(timeArray[0]);
emilytrembeth 4:da63962bc0f1 80 struct_time.tm_mon = atoi(timeArray[1]) - 1;
emilytrembeth 5:60e116a1e913 81 struct_time.tm_year = atoi(timeArray[2]) - 1900;
emilytrembeth 4:da63962bc0f1 82 struct_time.tm_hour = atoi(timeArray[3]);
emilytrembeth 4:da63962bc0f1 83 struct_time.tm_min = atoi(timeArray[4]);
emilytrembeth 4:da63962bc0f1 84 struct_time.tm_sec = atoi(timeArray[5]);
emilytrembeth 4:da63962bc0f1 85
emilytrembeth 4:da63962bc0f1 86 currentTime = mktime(&struct_time);
emilytrembeth 4:da63962bc0f1 87 set_time(currentTime);
emilytrembeth 4:da63962bc0f1 88
emilytrembeth 4:da63962bc0f1 89 date_mutex.unlock();
emilytrembeth 4:da63962bc0f1 90 }
emilytrembeth 4:da63962bc0f1 91
emilytrembeth 4:da63962bc0f1 92 void getLineFromSerial(char *keyBuffer, int bufferLength)
emilytrembeth 4:da63962bc0f1 93 {
emilytrembeth 4:da63962bc0f1 94 date_mutex.lock();
emilytrembeth 4:da63962bc0f1 95
emilytrembeth 4:da63962bc0f1 96 char c;
emilytrembeth 4:da63962bc0f1 97 int index = 0;
emilytrembeth 4:da63962bc0f1 98
emilytrembeth 4:da63962bc0f1 99 for (;;) {
emilytrembeth 4:da63962bc0f1 100 // break if keyBuffer is full
emilytrembeth 4:da63962bc0f1 101 if (index >= bufferLength) {
emilytrembeth 4:da63962bc0f1 102 break;
emilytrembeth 4:da63962bc0f1 103 }
emilytrembeth 4:da63962bc0f1 104
emilytrembeth 4:da63962bc0f1 105 // read input
emilytrembeth 4:da63962bc0f1 106 c = pc.getc();
emilytrembeth 4:da63962bc0f1 107 //printf(" %d ", c);
emilytrembeth 4:da63962bc0f1 108 pc.putc(c);
emilytrembeth 4:da63962bc0f1 109
emilytrembeth 4:da63962bc0f1 110 // break if end
emilytrembeth 4:da63962bc0f1 111 if (c == '\r') {
emilytrembeth 4:da63962bc0f1 112 keyBuffer[index++] = c;
emilytrembeth 4:da63962bc0f1 113 printf("\n");
emilytrembeth 4:da63962bc0f1 114 break;
emilytrembeth 4:da63962bc0f1 115 }
emilytrembeth 4:da63962bc0f1 116
emilytrembeth 4:da63962bc0f1 117 // store in keyBuffer
emilytrembeth 4:da63962bc0f1 118 keyBuffer[index++] = c;
emilytrembeth 4:da63962bc0f1 119
emilytrembeth 4:da63962bc0f1 120 date_mutex.unlock();
emilytrembeth 4:da63962bc0f1 121 }
emilytrembeth 4:da63962bc0f1 122 }
emilytrembeth 4:da63962bc0f1 123
emilytrembeth 4:da63962bc0f1 124 void displayMessageOnConsole() {
emilytrembeth 4:da63962bc0f1 125 date_mutex.lock();
emilytrembeth 4:da63962bc0f1 126
emilytrembeth 4:da63962bc0f1 127 currentTime = time(NULL);
emilytrembeth 5:60e116a1e913 128 //strftime(lcdBuffer, 32, "%Y/%m/%d %H:%M:%S", localtime(&currentTime));
emilytrembeth 5:60e116a1e913 129 strftime(lcdBuffer, 32, "%d/%m/%Y %H:%M:%S", localtime(&currentTime));
emilytrembeth 4:da63962bc0f1 130
emilytrembeth 4:da63962bc0f1 131 printf("Current Time:%s\r\n", lcdBuffer);
emilytrembeth 5:60e116a1e913 132 printf("Enter new Time (dd/mm/YYYY HH:MM:SS)\r\n");
emilytrembeth 4:da63962bc0f1 133
emilytrembeth 4:da63962bc0f1 134 date_mutex.unlock();
emilytrembeth 4:da63962bc0f1 135 }
emilytrembeth 4:da63962bc0f1 136
emilytrembeth 5:60e116a1e913 137 void SetingTimeWithButtons () {
emilytrembeth 5:60e116a1e913 138
emilytrembeth 5:60e116a1e913 139 int day = 0;
emilytrembeth 5:60e116a1e913 140 int month = 0;
emilytrembeth 5:60e116a1e913 141 int year = 2017;
emilytrembeth 5:60e116a1e913 142 int hour = 0;
emilytrembeth 5:60e116a1e913 143 int min = 0;
emilytrembeth 5:60e116a1e913 144 int sec = 0;
emilytrembeth 5:60e116a1e913 145
emilytrembeth 5:60e116a1e913 146 setting = Day;
emilytrembeth 5:60e116a1e913 147 t = 1;
emilytrembeth 5:60e116a1e913 148
emilytrembeth 5:60e116a1e913 149 while(t == 1) {
emilytrembeth 5:60e116a1e913 150 switch (setting) {
emilytrembeth 5:60e116a1e913 151 case Day:
emilytrembeth 5:60e116a1e913 152 if (SW1 == 1) {
emilytrembeth 5:60e116a1e913 153 wait(0.2);
emilytrembeth 5:60e116a1e913 154 day = day + 1;
emilytrembeth 5:60e116a1e913 155 lcd.cls();
emilytrembeth 5:60e116a1e913 156 lcd.locate(0,0);
emilytrembeth 5:60e116a1e913 157 lcd.printf("Day = %d\n\r", day);
emilytrembeth 5:60e116a1e913 158 }
emilytrembeth 5:60e116a1e913 159 if (SW2 == 1) {
emilytrembeth 5:60e116a1e913 160 setting = Month;
emilytrembeth 5:60e116a1e913 161 wait(0.5);
emilytrembeth 5:60e116a1e913 162 }
emilytrembeth 5:60e116a1e913 163 break;
emilytrembeth 5:60e116a1e913 164
emilytrembeth 5:60e116a1e913 165 case Month:
emilytrembeth 5:60e116a1e913 166 if (SW1 == 1) {
emilytrembeth 5:60e116a1e913 167 wait(0.2);
emilytrembeth 5:60e116a1e913 168 month = month + 1;
emilytrembeth 5:60e116a1e913 169 lcd.cls();
emilytrembeth 5:60e116a1e913 170 lcd.locate(0,0);
emilytrembeth 5:60e116a1e913 171 lcd.printf("Month = %d\n\r", month);
emilytrembeth 5:60e116a1e913 172 }
emilytrembeth 5:60e116a1e913 173 if (SW2 == 1) {
emilytrembeth 5:60e116a1e913 174 setting = Year;
emilytrembeth 5:60e116a1e913 175 wait(0.5);
emilytrembeth 5:60e116a1e913 176 }
emilytrembeth 5:60e116a1e913 177 break;
emilytrembeth 5:60e116a1e913 178
emilytrembeth 5:60e116a1e913 179 case Year:
emilytrembeth 5:60e116a1e913 180 if (SW1 == 1) {
emilytrembeth 5:60e116a1e913 181 wait(0.2);
emilytrembeth 5:60e116a1e913 182 year = year + 1;
emilytrembeth 5:60e116a1e913 183 lcd.cls();
emilytrembeth 5:60e116a1e913 184 lcd.locate(0,0);
emilytrembeth 5:60e116a1e913 185 lcd.printf("Year = %d\n\r", year);
emilytrembeth 5:60e116a1e913 186 }
emilytrembeth 5:60e116a1e913 187 if (SW2 == 1) {
emilytrembeth 5:60e116a1e913 188 setting = Hour;
emilytrembeth 5:60e116a1e913 189 wait(0.5);
emilytrembeth 5:60e116a1e913 190 }
emilytrembeth 5:60e116a1e913 191 break;
emilytrembeth 5:60e116a1e913 192
emilytrembeth 5:60e116a1e913 193 case Hour:
emilytrembeth 5:60e116a1e913 194 if (SW1 == 1) {
emilytrembeth 5:60e116a1e913 195 wait(0.2);
emilytrembeth 5:60e116a1e913 196 hour = hour + 1;
emilytrembeth 5:60e116a1e913 197 lcd.cls();
emilytrembeth 5:60e116a1e913 198 lcd.locate(0,0);
emilytrembeth 5:60e116a1e913 199 lcd.printf("Hour = %d\n\r", hour);
emilytrembeth 5:60e116a1e913 200 }
emilytrembeth 5:60e116a1e913 201 if (SW2 == 1) {
emilytrembeth 5:60e116a1e913 202 setting = Minute;
emilytrembeth 5:60e116a1e913 203 wait(0.5);
emilytrembeth 5:60e116a1e913 204 }
emilytrembeth 5:60e116a1e913 205 break;
emilytrembeth 5:60e116a1e913 206
emilytrembeth 5:60e116a1e913 207 case Minute:
emilytrembeth 5:60e116a1e913 208 if (SW1 == 1) {
emilytrembeth 5:60e116a1e913 209 wait(0.2);
emilytrembeth 5:60e116a1e913 210 min = min + 1;
emilytrembeth 5:60e116a1e913 211 lcd.cls();
emilytrembeth 5:60e116a1e913 212 lcd.locate(0,0);
emilytrembeth 5:60e116a1e913 213 lcd.printf("Minute = %d\n\r", min);
emilytrembeth 5:60e116a1e913 214 }
emilytrembeth 5:60e116a1e913 215 if (SW2 == 1) {
emilytrembeth 5:60e116a1e913 216 setting = Second;
emilytrembeth 5:60e116a1e913 217 wait(0.5);
emilytrembeth 5:60e116a1e913 218 }
emilytrembeth 5:60e116a1e913 219 break;
emilytrembeth 5:60e116a1e913 220
emilytrembeth 5:60e116a1e913 221 case Second:
emilytrembeth 5:60e116a1e913 222 if (SW1 == 1) {
emilytrembeth 5:60e116a1e913 223 wait(0.2);
emilytrembeth 5:60e116a1e913 224 sec = sec + 1;
emilytrembeth 5:60e116a1e913 225 lcd.cls();
emilytrembeth 5:60e116a1e913 226 lcd.locate(0,0);
emilytrembeth 5:60e116a1e913 227 lcd.printf("Second = %d\n\r", sec);
emilytrembeth 5:60e116a1e913 228 }
emilytrembeth 5:60e116a1e913 229 if (SW2 == 1) {
emilytrembeth 5:60e116a1e913 230 wait(0.5);
emilytrembeth 5:60e116a1e913 231 t = 0;
emilytrembeth 5:60e116a1e913 232 }
emilytrembeth 5:60e116a1e913 233 break;
emilytrembeth 5:60e116a1e913 234 }
emilytrembeth 5:60e116a1e913 235 }
emilytrembeth 5:60e116a1e913 236
emilytrembeth 5:60e116a1e913 237 struct tm struct_time;
emilytrembeth 5:60e116a1e913 238
emilytrembeth 5:60e116a1e913 239 //store number into time struct
emilytrembeth 5:60e116a1e913 240 struct_time.tm_year = year - 1900;
emilytrembeth 5:60e116a1e913 241 struct_time.tm_mon = month;
emilytrembeth 5:60e116a1e913 242 struct_time.tm_mday = day;
emilytrembeth 5:60e116a1e913 243 struct_time.tm_hour = hour;
emilytrembeth 5:60e116a1e913 244 struct_time.tm_min = min;
emilytrembeth 5:60e116a1e913 245 struct_time.tm_sec = sec;
emilytrembeth 5:60e116a1e913 246
emilytrembeth 5:60e116a1e913 247 // Coverting to time
emilytrembeth 5:60e116a1e913 248 time_t currentTime = mktime(&struct_time);
emilytrembeth 5:60e116a1e913 249 set_time(currentTime);
emilytrembeth 5:60e116a1e913 250
emilytrembeth 5:60e116a1e913 251 }
noutram 0:90e393878517 252
emilytrembeth 5:60e116a1e913 253
emilytrembeth 5:60e116a1e913 254 void FunctionSensor()
noutram 0:90e393878517 255 {
emilytrembeth 4:da63962bc0f1 256 while (true) {
emilytrembeth 4:da63962bc0f1 257 double temp = sensor.getTemperature();
emilytrembeth 4:da63962bc0f1 258 double pressure = sensor.getPressure();
emilytrembeth 4:da63962bc0f1 259 lcd.cls();
emilytrembeth 4:da63962bc0f1 260 lcd.printf("Temp Pressure\n");
emilytrembeth 4:da63962bc0f1 261 lcd.printf("%6.1f ",temp);
emilytrembeth 4:da63962bc0f1 262 lcd.printf("%.2f\n",pressure);
emilytrembeth 4:da63962bc0f1 263
emilytrembeth 4:da63962bc0f1 264 //Write to SD
emilytrembeth 4:da63962bc0f1 265 //fprintf(fp, "%6.1f,%.2f\n\r", temp, pressure);
emilytrembeth 4:da63962bc0f1 266
emilytrembeth 4:da63962bc0f1 267 Thread::wait(15000); //Updates the state every 15 seconds
emilytrembeth 4:da63962bc0f1 268 }
noutram 0:90e393878517 269 }
noutram 0:90e393878517 270
emilytrembeth 5:60e116a1e913 271 void FunctionTime()
noutram 0:90e393878517 272 {
emilytrembeth 4:da63962bc0f1 273 while (true) {
emilytrembeth 4:da63962bc0f1 274 char lineBuffer[32];
emilytrembeth 4:da63962bc0f1 275 char *pointer;
emilytrembeth 4:da63962bc0f1 276 pointer = lineBuffer;
emilytrembeth 5:60e116a1e913 277 if (SW1 == 1) { //Set time on Putty
emilytrembeth 4:da63962bc0f1 278 // show initial message on console
emilytrembeth 4:da63962bc0f1 279 displayMessageOnConsole();
emilytrembeth 4:da63962bc0f1 280
emilytrembeth 4:da63962bc0f1 281 // get input from console
emilytrembeth 4:da63962bc0f1 282 getLineFromSerial(pointer, sizeof(lineBuffer));
emilytrembeth 4:da63962bc0f1 283
emilytrembeth 4:da63962bc0f1 284 myled = !myled;
emilytrembeth 4:da63962bc0f1 285 // update RTC based on input value
emilytrembeth 4:da63962bc0f1 286 updateRealTimeClock(pointer);
emilytrembeth 5:60e116a1e913 287 }
emilytrembeth 5:60e116a1e913 288 if (SW2 == 1) { // Set time with Buttons
emilytrembeth 5:60e116a1e913 289 pc.printf("Ready");
emilytrembeth 5:60e116a1e913 290 wait(0.5);
emilytrembeth 5:60e116a1e913 291 SetingTimeWithButtons ();
emilytrembeth 5:60e116a1e913 292 }
emilytrembeth 4:da63962bc0f1 293 while(true) {
emilytrembeth 4:da63962bc0f1 294 displayOnLcd();
emilytrembeth 4:da63962bc0f1 295 myled = !myled;
emilytrembeth 4:da63962bc0f1 296 Thread::wait(1000);
emilytrembeth 4:da63962bc0f1 297 }
noutram 0:90e393878517 298 }
emilytrembeth 4:da63962bc0f1 299
emilytrembeth 4:da63962bc0f1 300 }
noutram 0:90e393878517 301
noutram 0:90e393878517 302 //Main thread
noutram 0:90e393878517 303 int main() {
emilytrembeth 4:da63962bc0f1 304
emilytrembeth 5:60e116a1e913 305 lcd.printf("Select Time Option\n\n");
emilytrembeth 4:da63962bc0f1 306
emilytrembeth 4:da63962bc0f1 307 post();
noutram 0:90e393878517 308
emilytrembeth 4:da63962bc0f1 309 //Initialise the SD card
emilytrembeth 4:da63962bc0f1 310 if ( sd.init() != 0) {
emilytrembeth 4:da63962bc0f1 311 printf("Init failed \n");
emilytrembeth 4:da63962bc0f1 312 errorCode(FATAL);
emilytrembeth 4:da63962bc0f1 313 }
emilytrembeth 4:da63962bc0f1 314
emilytrembeth 4:da63962bc0f1 315 //Create a filing system for SD Card
emilytrembeth 4:da63962bc0f1 316 FATFileSystem fs("sd", &sd);
emilytrembeth 4:da63962bc0f1 317
emilytrembeth 4:da63962bc0f1 318 //Open to WRITE
emilytrembeth 4:da63962bc0f1 319 FILE* fp = fopen("/sd/test.csv","a");
emilytrembeth 4:da63962bc0f1 320 if (fp == NULL) {
emilytrembeth 4:da63962bc0f1 321 error("Could not open file for write\n");
emilytrembeth 4:da63962bc0f1 322 errorCode(FATAL);
emilytrembeth 4:da63962bc0f1 323 }
noutram 0:90e393878517 324
noutram 1:948bd552a2a2 325 // run threads
emilytrembeth 5:60e116a1e913 326 t1.start(FunctionSensor);
emilytrembeth 5:60e116a1e913 327 t2.start(FunctionTime);
emilytrembeth 4:da63962bc0f1 328
emilytrembeth 4:da63962bc0f1 329 }
noutram 0:90e393878517 330