![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
my coursework
Dependencies: CheckRTC LPS25H hts221
Fork of ELEC350-extended-referral1 by
main.cpp@48:614f8821ccca, 2017-11-23 (annotated)
- Committer:
- spanesar
- Date:
- Thu Nov 23 13:16:23 2017 +0000
- Revision:
- 48:614f8821ccca
- Parent:
- 47:12f18a4527d8
- Child:
- 49:a39eacfcd968
120 readings coming out as zero?
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jonathan Austin |
0:2757d7abb7d9 | 1 | #include "mbed.h" |
martinsimpson | 32:260a288be58f | 2 | #include "rtos.h" |
noutram | 35:af125862c33e | 3 | #include "hts221.h" |
martinsimpson | 32:260a288be58f | 4 | #include "LPS25H.h" |
spanesar | 40:426cd176ce80 | 5 | #include <sstream> |
spanesar | 41:ce3d6fb38d3b | 6 | #define READALL_OFF 1 |
spanesar | 41:ce3d6fb38d3b | 7 | #define SETTIME_OFF 2 |
spanesar | 41:ce3d6fb38d3b | 8 | #define SETT_OFF 4 |
spanesar | 43:a9682b81632b | 9 | #define READ_OFF 7 |
spanesar | 43:a9682b81632b | 10 | #define ALL_OFF 9 |
spanesar | 41:ce3d6fb38d3b | 11 | |
spanesar | 39:504f5dc30a33 | 12 | |
martinsimpson | 32:260a288be58f | 13 | |
martinsimpson | 32:260a288be58f | 14 | DigitalOut myled(LED1); |
martinsimpson | 32:260a288be58f | 15 | I2C i2c2(I2C_SDA, I2C_SCL); |
spanesar | 39:504f5dc30a33 | 16 | Serial pc(USBTX, USBRX); |
noutram | 35:af125862c33e | 17 | float tempCelsius = 25.50; |
noutram | 35:af125862c33e | 18 | float humi = 55; |
spanesar | 39:504f5dc30a33 | 19 | int humiMax = 100; |
spanesar | 40:426cd176ce80 | 20 | char buffer_str=0; |
spanesar | 39:504f5dc30a33 | 21 | uint32_t seconds = 0, minutes=0, hours=0; |
spanesar | 39:504f5dc30a33 | 22 | Ticker t; |
spanesar | 39:504f5dc30a33 | 23 | volatile static unsigned short sample16 = 0; |
spanesar | 39:504f5dc30a33 | 24 | void doSample1Hz(); |
spanesar | 39:504f5dc30a33 | 25 | int count = 0; |
spanesar | 39:504f5dc30a33 | 26 | char *buf; |
spanesar | 39:504f5dc30a33 | 27 | size_t sz; |
spanesar | 48:614f8821ccca | 28 | int oldest; |
spanesar | 46:794973f66ef6 | 29 | |
noutram | 47:12f18a4527d8 | 30 | typedef struct { |
noutram | 47:12f18a4527d8 | 31 | float temp; |
noutram | 47:12f18a4527d8 | 32 | float press; |
noutram | 47:12f18a4527d8 | 33 | float humid; |
noutram | 47:12f18a4527d8 | 34 | } measurement; |
spanesar | 46:794973f66ef6 | 35 | |
spanesar | 48:614f8821ccca | 36 | measurement buffer[120]; |
martinsimpson | 32:260a288be58f | 37 | |
noutram | 35:af125862c33e | 38 | LPS25H barometer(i2c2, LPS25H_V_CHIP_ADDR); |
noutram | 35:af125862c33e | 39 | HTS221 humidity(I2C_SDA, I2C_SCL); |
noutram | 35:af125862c33e | 40 | |
spanesar | 40:426cd176ce80 | 41 | void function1(); |
spanesar | 43:a9682b81632b | 42 | |
spanesar | 39:504f5dc30a33 | 43 | |
spanesar | 40:426cd176ce80 | 44 | Thread t1; |
noutram | 45:fac0d195fc65 | 45 | Thread tSample; |
spanesar | 39:504f5dc30a33 | 46 | |
spanesar | 40:426cd176ce80 | 47 | osThreadId idMain; |
spanesar | 40:426cd176ce80 | 48 | osThreadId id1; |
spanesar | 41:ce3d6fb38d3b | 49 | osThreadId id2; |
spanesar | 41:ce3d6fb38d3b | 50 | osThreadId id3; |
spanesar | 41:ce3d6fb38d3b | 51 | osThreadId id4; |
spanesar | 41:ce3d6fb38d3b | 52 | |
noutram | 45:fac0d195fc65 | 53 | Ticker ticker; |
noutram | 35:af125862c33e | 54 | |
spanesar | 40:426cd176ce80 | 55 | void function1() |
spanesar | 46:794973f66ef6 | 56 | { |
spanesar | 46:794973f66ef6 | 57 | //begin function |
spanesar | 43:a9682b81632b | 58 | while (true) {//begin while true |
spanesar | 48:614f8821ccca | 59 | char strbuffer [128]; |
spanesar | 48:614f8821ccca | 60 | scanf("%127s", strbuffer); |
spanesar | 48:614f8821ccca | 61 | string buffer_str(strbuffer); |
spanesar | 43:a9682b81632b | 62 | char date [32]; |
spanesar | 43:a9682b81632b | 63 | strftime(date, 32, "%Y/%m/%d %a", localtime(&seconds)); |
spanesar | 43:a9682b81632b | 64 | char time [32]; |
spanesar | 43:a9682b81632b | 65 | strftime(time, 32, "%H:%M:%S %p", localtime(&seconds)); |
spanesar | 43:a9682b81632b | 66 | |
spanesar | 43:a9682b81632b | 67 | if(buffer_str=="READALL") {//begin if buffer readall |
spanesar | 46:794973f66ef6 | 68 | |
spanesar | 43:a9682b81632b | 69 | // displays all the variables of time, date, temperature, humidity and all the barometric readings to the serial |
spanesar | 48:614f8821ccca | 70 | for (int i = 0; i <120; i++) { |
spanesar | 48:614f8821ccca | 71 | //printf("%s, %s, %4.2fC , %3.1f%%, %6.1f, %4.1f\n\r", time, date, tempCelsius, humi, buffer[i].humid, buffer[i].press, buffer[i].temp, barometer.temperature()); |
spanesar | 48:614f8821ccca | 72 | //measurement test=buffer[i]; |
spanesar | 48:614f8821ccca | 73 | printf("%f %f %f \n\r",buffer[i].humid, buffer[i].press, buffer[i].temp); |
spanesar | 48:614f8821ccca | 74 | } |
spanesar | 43:a9682b81632b | 75 | myled = 1; // LED is ON |
spanesar | 43:a9682b81632b | 76 | Thread::wait(200); // 200 ms NB 'Thread::wait(int d);' !!! d is in milliseconds! |
spanesar | 43:a9682b81632b | 77 | myled = 0; // LED is OFF |
spanesar | 43:a9682b81632b | 78 | |
spanesar | 46:794973f66ef6 | 79 | }//end if buffer readall |
spanesar | 46:794973f66ef6 | 80 | |
spanesar | 46:794973f66ef6 | 81 | |
spanesar | 43:a9682b81632b | 82 | |
spanesar | 43:a9682b81632b | 83 | |
spanesar | 46:794973f66ef6 | 84 | if(buffer_str=="SETTIME") {// begin if buffer settime |
spanesar | 46:794973f66ef6 | 85 | set_time(1508268288); //sets time in UTC |
spanesar | 46:794973f66ef6 | 86 | printf("time and date updated to %s %s\n\r", time, date); //prints date and time |
spanesar | 46:794973f66ef6 | 87 | myled = 1; //led on |
spanesar | 46:794973f66ef6 | 88 | Thread::wait(200); // wait 200ms |
spanesar | 46:794973f66ef6 | 89 | myled = 0; //led off |
spanesar | 46:794973f66ef6 | 90 | |
spanesar | 46:794973f66ef6 | 91 | }//end if buffer settime |
spanesar | 46:794973f66ef6 | 92 | |
spanesar | 46:794973f66ef6 | 93 | |
spanesar | 46:794973f66ef6 | 94 | |
spanesar | 46:794973f66ef6 | 95 | if(buffer_str=="SETT") { // begin if buffer sett |
spanesar | 43:a9682b81632b | 96 | for (count = 0; count < 5; count++) { |
noutram | 45:fac0d195fc65 | 97 | float temp = barometer.temperature(); |
noutram | 45:fac0d195fc65 | 98 | float pressure = barometer.pressure(); |
spanesar | 46:794973f66ef6 | 99 | |
noutram | 45:fac0d195fc65 | 100 | sz = snprintf(NULL, 0,"%s, %s, %4.2fC , %3.1f%%, %6.1f, %4.1f\n\r", time, date, tempCelsius, humi, pressure, temp); |
spanesar | 43:a9682b81632b | 101 | buf = (char *)malloc(sz + 1); |
noutram | 45:fac0d195fc65 | 102 | snprintf(buf, sz + 1, "%s, %s, %4.2fC , %3.1f%%, %6.1f, %4.1f\n\r", time, date, tempCelsius, humi, pressure, temp); |
spanesar | 43:a9682b81632b | 103 | printf("%s\n\r",buf); |
noutram | 45:fac0d195fc65 | 104 | free(buf); |
spanesar | 43:a9682b81632b | 105 | printf("written to memory\n\r"); |
spanesar | 43:a9682b81632b | 106 | myled = 1; |
spanesar | 43:a9682b81632b | 107 | Thread::wait(200); |
spanesar | 43:a9682b81632b | 108 | myled = 0; |
spanesar | 46:794973f66ef6 | 109 | |
spanesar | 43:a9682b81632b | 110 | } //end if buffer sett |
spanesar | 46:794973f66ef6 | 111 | |
spanesar | 46:794973f66ef6 | 112 | } |
spanesar | 43:a9682b81632b | 113 | |
spanesar | 46:794973f66ef6 | 114 | |
spanesar | 46:794973f66ef6 | 115 | |
spanesar | 44:ca34a376a17c | 116 | |
spanesar | 43:a9682b81632b | 117 | if(buffer_str=="READ") { //begin if buffer read |
spanesar | 43:a9682b81632b | 118 | printf("%s\n\r", buf); |
spanesar | 43:a9682b81632b | 119 | myled = 1; |
spanesar | 43:a9682b81632b | 120 | Thread::wait(200); |
spanesar | 43:a9682b81632b | 121 | myled = 0; |
spanesar | 46:794973f66ef6 | 122 | |
spanesar | 43:a9682b81632b | 123 | }//end if buffer read |
spanesar | 46:794973f66ef6 | 124 | |
noutram | 45:fac0d195fc65 | 125 | }//end while true |
noutram | 45:fac0d195fc65 | 126 | }//end function |
spanesar | 43:a9682b81632b | 127 | |
spanesar | 46:794973f66ef6 | 128 | void getSampleThread() |
spanesar | 46:794973f66ef6 | 129 | { |
spanesar | 46:794973f66ef6 | 130 | while (true) { |
spanesar | 46:794973f66ef6 | 131 | Thread::signal_wait(99); |
spanesar | 46:794973f66ef6 | 132 | //Get samples |
noutram | 47:12f18a4527d8 | 133 | measurement m; |
spanesar | 48:614f8821ccca | 134 | |
spanesar | 48:614f8821ccca | 135 | |
noutram | 47:12f18a4527d8 | 136 | float pressure = barometer.pressure(); |
spanesar | 48:614f8821ccca | 137 | m.press = pressure; |
spanesar | 43:a9682b81632b | 138 | |
noutram | 47:12f18a4527d8 | 139 | float temp; |
spanesar | 48:614f8821ccca | 140 | float humidityval; |
spanesar | 48:614f8821ccca | 141 | humidity.ReadTempHumi(&temp, &humidityval); // reads temperature and humity levels |
spanesar | 48:614f8821ccca | 142 | m.humid = humidityval; |
spanesar | 48:614f8821ccca | 143 | m.temp = temp; |
spanesar | 48:614f8821ccca | 144 | |
noutram | 47:12f18a4527d8 | 145 | buffer[oldest] = m; |
spanesar | 48:614f8821ccca | 146 | |
noutram | 47:12f18a4527d8 | 147 | //update oldest |
noutram | 47:12f18a4527d8 | 148 | oldest++; |
noutram | 47:12f18a4527d8 | 149 | //wrap back to zero if at the end |
spanesar | 48:614f8821ccca | 150 | if (oldest>120) { |
spanesar | 48:614f8821ccca | 151 | oldest = 0; |
spanesar | 48:614f8821ccca | 152 | } |
spanesar | 48:614f8821ccca | 153 | |
noutram | 47:12f18a4527d8 | 154 | //TO DO |
spanesar | 48:614f8821ccca | 155 | |
spanesar | 43:a9682b81632b | 156 | |
spanesar | 43:a9682b81632b | 157 | |
spanesar | 46:794973f66ef6 | 158 | } |
spanesar | 46:794973f66ef6 | 159 | } |
spanesar | 43:a9682b81632b | 160 | |
spanesar | 46:794973f66ef6 | 161 | void getSample() |
spanesar | 46:794973f66ef6 | 162 | { |
spanesar | 46:794973f66ef6 | 163 | tSample.signal_set(99); |
spanesar | 41:ce3d6fb38d3b | 164 | } |
spanesar | 41:ce3d6fb38d3b | 165 | |
spanesar | 46:794973f66ef6 | 166 | int main() |
spanesar | 46:794973f66ef6 | 167 | { |
spanesar | 46:794973f66ef6 | 168 | time_t seconds = time(NULL); |
spanesar | 46:794973f66ef6 | 169 | pc.baud(9600); //sets baud rate to 9600 |
spanesar | 46:794973f66ef6 | 170 | ticker.attach(getSample, 15); |
spanesar | 46:794973f66ef6 | 171 | tSample.start(getSampleThread); |
spanesar | 46:794973f66ef6 | 172 | idMain = osThreadGetId(); |
spanesar | 46:794973f66ef6 | 173 | t1.start(function1); |
spanesar | 46:794973f66ef6 | 174 | id1 = t1.gettid(); |
spanesar | 46:794973f66ef6 | 175 | humidity.init(); |
spanesar | 46:794973f66ef6 | 176 | humidity.calib(); |
spanesar | 46:794973f66ef6 | 177 | printf("Type SETTIME to set time and date [enter] \n\r"); |
spanesar | 46:794973f66ef6 | 178 | printf("Type SETT to start sampling [ENTER]\n\r"); |
spanesar | 46:794973f66ef6 | 179 | printf("Type DELETEALL to delete sensor data [enter]\n\r"); |
spanesar | 46:794973f66ef6 | 180 | printf("Type READALL [enter]\n\r"); |
spanesar | 46:794973f66ef6 | 181 | printf("Type READ to read sensor data in memory [ENTER]\n\r"); |
spanesar | 46:794973f66ef6 | 182 | |
spanesar | 46:794973f66ef6 | 183 | |
spanesar | 46:794973f66ef6 | 184 | while(1) { |
spanesar | 46:794973f66ef6 | 185 | char buffer [128]; |
spanesar | 46:794973f66ef6 | 186 | scanf("%127s", buffer); |
spanesar | 46:794973f66ef6 | 187 | string buffer_str(buffer); |
spanesar | 46:794973f66ef6 | 188 | osSignalWait(ALL_OFF,osWaitForever); |
spanesar | 46:794973f66ef6 | 189 | |
spanesar | 46:794973f66ef6 | 190 | } |
spanesar | 46:794973f66ef6 | 191 | } |
spanesar | 46:794973f66ef6 | 192 |