Version 8, working version with Alix, sams and ollies code. Displays time, date and sensor info onto terminal, LCD and networking, and saves onto SD card.
Dependencies: BMP280 ELEC350-Practicals-FZ429 TextLCD BME280 ntp-client
SDcard.hpp@12:4c7eaac8ceef, 2018-12-31 (annotated)
- Committer:
- Alix955
- Date:
- Mon Dec 31 19:20:22 2018 +0000
- Revision:
- 12:4c7eaac8ceef
Version 8, integration of Alix & Sams work with older(?) version of ollies. Displays time, date and all sensor information onto LCD, Terminal and Networking, and saves sensor info to SD card.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Alix955 | 12:4c7eaac8ceef | 1 | #ifndef _SDCARD_ |
Alix955 | 12:4c7eaac8ceef | 2 | #define _SDCARD_ |
Alix955 | 12:4c7eaac8ceef | 3 | #include "mbed.h" |
Alix955 | 12:4c7eaac8ceef | 4 | #include "SDBlockDevice.h" |
Alix955 | 12:4c7eaac8ceef | 5 | #include "FATFileSystem.h" |
Alix955 | 12:4c7eaac8ceef | 6 | #include "sample_hardware.hpp" |
Alix955 | 12:4c7eaac8ceef | 7 | |
Alix955 | 12:4c7eaac8ceef | 8 | |
Alix955 | 12:4c7eaac8ceef | 9 | |
Alix955 | 12:4c7eaac8ceef | 10 | class SDcard |
Alix955 | 12:4c7eaac8ceef | 11 | |
Alix955 | 12:4c7eaac8ceef | 12 | { |
Alix955 | 12:4c7eaac8ceef | 13 | |
Alix955 | 12:4c7eaac8ceef | 14 | private: |
Alix955 | 12:4c7eaac8ceef | 15 | float temp; //current temperature of sensor |
Alix955 | 12:4c7eaac8ceef | 16 | float pressure; //current pressure of sensor |
Alix955 | 12:4c7eaac8ceef | 17 | float fLDR; //current light level from LDR |
Alix955 | 12:4c7eaac8ceef | 18 | |
Alix955 | 12:4c7eaac8ceef | 19 | void update_temp(double t) //use this function to update the current temperature value |
Alix955 | 12:4c7eaac8ceef | 20 | { |
Alix955 | 12:4c7eaac8ceef | 21 | temp = t; |
Alix955 | 12:4c7eaac8ceef | 22 | } |
Alix955 | 12:4c7eaac8ceef | 23 | |
Alix955 | 12:4c7eaac8ceef | 24 | void update_pressure(double p) //use this function to update the current pressure value |
Alix955 | 12:4c7eaac8ceef | 25 | { |
Alix955 | 12:4c7eaac8ceef | 26 | pressure = p; |
Alix955 | 12:4c7eaac8ceef | 27 | } |
Alix955 | 12:4c7eaac8ceef | 28 | |
Alix955 | 12:4c7eaac8ceef | 29 | void update_LDR(double L) |
Alix955 | 12:4c7eaac8ceef | 30 | { |
Alix955 | 12:4c7eaac8ceef | 31 | fLDR = L; |
Alix955 | 12:4c7eaac8ceef | 32 | } |
Alix955 | 12:4c7eaac8ceef | 33 | |
Alix955 | 12:4c7eaac8ceef | 34 | |
Alix955 | 12:4c7eaac8ceef | 35 | |
Alix955 | 12:4c7eaac8ceef | 36 | public: |
Alix955 | 12:4c7eaac8ceef | 37 | EventQueue SDcard_Queue; |
Alix955 | 12:4c7eaac8ceef | 38 | |
Alix955 | 12:4c7eaac8ceef | 39 | SDcard(){ //constructor, |
Alix955 | 12:4c7eaac8ceef | 40 | |
Alix955 | 12:4c7eaac8ceef | 41 | } |
Alix955 | 12:4c7eaac8ceef | 42 | |
Alix955 | 12:4c7eaac8ceef | 43 | |
Alix955 | 12:4c7eaac8ceef | 44 | |
Alix955 | 12:4c7eaac8ceef | 45 | ~SDcard(){ //Deconstructor, |
Alix955 | 12:4c7eaac8ceef | 46 | |
Alix955 | 12:4c7eaac8ceef | 47 | } |
Alix955 | 12:4c7eaac8ceef | 48 | |
Alix955 | 12:4c7eaac8ceef | 49 | |
Alix955 | 12:4c7eaac8ceef | 50 | |
Alix955 | 12:4c7eaac8ceef | 51 | |
Alix955 | 12:4c7eaac8ceef | 52 | void update_sensor_info(sample_message msg) //updates all current sensor information, this is called by a ticker every 5 seconds to read from the mailbox |
Alix955 | 12:4c7eaac8ceef | 53 | { |
Alix955 | 12:4c7eaac8ceef | 54 | update_temp(msg.temp); // Include message class passing of data |
Alix955 | 12:4c7eaac8ceef | 55 | update_pressure(msg.pressure); |
Alix955 | 12:4c7eaac8ceef | 56 | update_LDR(msg.ldr); |
Alix955 | 12:4c7eaac8ceef | 57 | } |
Alix955 | 12:4c7eaac8ceef | 58 | |
Alix955 | 12:4c7eaac8ceef | 59 | |
Alix955 | 12:4c7eaac8ceef | 60 | |
Alix955 | 12:4c7eaac8ceef | 61 | |
Alix955 | 12:4c7eaac8ceef | 62 | void Save_Data() { |
Alix955 | 12:4c7eaac8ceef | 63 | |
Alix955 | 12:4c7eaac8ceef | 64 | |
Alix955 | 12:4c7eaac8ceef | 65 | // initalising the SD card |
Alix955 | 12:4c7eaac8ceef | 66 | if ( sd.init() != 0) { |
Alix955 | 12:4c7eaac8ceef | 67 | printf("Init failed \n"); |
Alix955 | 12:4c7eaac8ceef | 68 | errorCode(FATAL); |
Alix955 | 12:4c7eaac8ceef | 69 | } |
Alix955 | 12:4c7eaac8ceef | 70 | |
Alix955 | 12:4c7eaac8ceef | 71 | //Create a filing system for SD Card |
Alix955 | 12:4c7eaac8ceef | 72 | |
Alix955 | 12:4c7eaac8ceef | 73 | FATFileSystem fs("sd", &sd); |
Alix955 | 12:4c7eaac8ceef | 74 | |
Alix955 | 12:4c7eaac8ceef | 75 | FILE* fp = fopen("/sd/SensorData.csv","a"); |
Alix955 | 12:4c7eaac8ceef | 76 | |
Alix955 | 12:4c7eaac8ceef | 77 | if (fp == NULL) { |
Alix955 | 12:4c7eaac8ceef | 78 | error("Could not open file for write\n"); |
Alix955 | 12:4c7eaac8ceef | 79 | errorCode(FATAL); |
Alix955 | 12:4c7eaac8ceef | 80 | } |
Alix955 | 12:4c7eaac8ceef | 81 | |
Alix955 | 12:4c7eaac8ceef | 82 | //Storing sensor data in csv file |
Alix955 | 12:4c7eaac8ceef | 83 | |
Alix955 | 12:4c7eaac8ceef | 84 | fprintf(fp, " Temperature(C) , %4.2f , Pressure(mbar) , %4.2f , Lux , %4.2f \n", temp , pressure , fLDR ); |
Alix955 | 12:4c7eaac8ceef | 85 | |
Alix955 | 12:4c7eaac8ceef | 86 | //Close the file |
Alix955 | 12:4c7eaac8ceef | 87 | fclose(fp); |
Alix955 | 12:4c7eaac8ceef | 88 | |
Alix955 | 12:4c7eaac8ceef | 89 | //Close down SD card |
Alix955 | 12:4c7eaac8ceef | 90 | sd.deinit(); |
Alix955 | 12:4c7eaac8ceef | 91 | |
Alix955 | 12:4c7eaac8ceef | 92 | errorCode(OK); |
Alix955 | 12:4c7eaac8ceef | 93 | |
Alix955 | 12:4c7eaac8ceef | 94 | temp = 0; |
Alix955 | 12:4c7eaac8ceef | 95 | pressure = 0; |
Alix955 | 12:4c7eaac8ceef | 96 | fLDR = 0; |
Alix955 | 12:4c7eaac8ceef | 97 | |
Alix955 | 12:4c7eaac8ceef | 98 | } |
Alix955 | 12:4c7eaac8ceef | 99 | }; |
Alix955 | 12:4c7eaac8ceef | 100 | // creating the instance SD of the class SDcard |
Alix955 | 12:4c7eaac8ceef | 101 | |
Alix955 | 12:4c7eaac8ceef | 102 | |
Alix955 | 12:4c7eaac8ceef | 103 | SDcard m_oSD; |
Alix955 | 12:4c7eaac8ceef | 104 | |
Alix955 | 12:4c7eaac8ceef | 105 | #endif |