![](/media/cache/profiles/acab0116c354964a558e65bdd07ff047.jpg.50x50_q85.jpg)
Syggestions
Dependencies: CheckRTC LPS25H hts221
Fork of ELEC350-extended-referral2 by
main.cpp@40:426cd176ce80, 2017-11-16 (annotated)
- Committer:
- spanesar
- Date:
- Thu Nov 16 01:06:41 2017 +0000
- Revision:
- 40:426cd176ce80
- Parent:
- 39:504f5dc30a33
- Child:
- 41:ce3d6fb38d3b
tried creating a thread for the serial function but keep getting errors
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 | 39:504f5dc30a33 | 6 | //#include "CheckRTC.h" |
spanesar | 39:504f5dc30a33 | 7 | |
martinsimpson | 32:260a288be58f | 8 | |
martinsimpson | 32:260a288be58f | 9 | DigitalOut myled(LED1); |
martinsimpson | 32:260a288be58f | 10 | I2C i2c2(I2C_SDA, I2C_SCL); |
spanesar | 39:504f5dc30a33 | 11 | Serial pc(USBTX, USBRX); |
noutram | 35:af125862c33e | 12 | float tempCelsius = 25.50; |
noutram | 35:af125862c33e | 13 | float humi = 55; |
spanesar | 39:504f5dc30a33 | 14 | int humiMax = 100; |
spanesar | 40:426cd176ce80 | 15 | char buffer_str=0; |
spanesar | 39:504f5dc30a33 | 16 | uint32_t seconds = 0, minutes=0, hours=0; |
spanesar | 39:504f5dc30a33 | 17 | Ticker t; |
spanesar | 39:504f5dc30a33 | 18 | volatile static unsigned short sample16 = 0; |
spanesar | 39:504f5dc30a33 | 19 | void doSample1Hz(); |
spanesar | 39:504f5dc30a33 | 20 | int count = 0; |
spanesar | 39:504f5dc30a33 | 21 | char *buf; |
spanesar | 39:504f5dc30a33 | 22 | size_t sz; |
spanesar | 40:426cd176ce80 | 23 | typedef struct { |
spanesar | 40:426cd176ce80 | 24 | float temp; |
spanesar | 40:426cd176ce80 | 25 | float press; |
spanesar | 40:426cd176ce80 | 26 | float humid; |
spanesar | 40:426cd176ce80 | 27 | } measurement; |
spanesar | 40:426cd176ce80 | 28 | measurement buffer [120]; |
martinsimpson | 32:260a288be58f | 29 | |
noutram | 35:af125862c33e | 30 | LPS25H barometer(i2c2, LPS25H_V_CHIP_ADDR); |
noutram | 35:af125862c33e | 31 | HTS221 humidity(I2C_SDA, I2C_SCL); |
noutram | 35:af125862c33e | 32 | |
spanesar | 40:426cd176ce80 | 33 | void function1(); |
spanesar | 40:426cd176ce80 | 34 | void function2(); |
spanesar | 40:426cd176ce80 | 35 | void function3(); |
spanesar | 39:504f5dc30a33 | 36 | |
spanesar | 40:426cd176ce80 | 37 | Thread t1; |
spanesar | 40:426cd176ce80 | 38 | Thread t2; |
spanesar | 40:426cd176ce80 | 39 | Thread t3; |
spanesar | 40:426cd176ce80 | 40 | Thread t4; |
spanesar | 39:504f5dc30a33 | 41 | |
spanesar | 40:426cd176ce80 | 42 | osThreadId idMain; |
spanesar | 40:426cd176ce80 | 43 | osThreadId id1; |
noutram | 35:af125862c33e | 44 | |
spanesar | 40:426cd176ce80 | 45 | void function1() |
spanesar | 40:426cd176ce80 | 46 | { |
spanesar | 40:426cd176ce80 | 47 | char buffer [128]; |
spanesar | 40:426cd176ce80 | 48 | scanf("%127s", buffer); |
spanesar | 40:426cd176ce80 | 49 | string buffer_str(buffer); |
spanesar | 40:426cd176ce80 | 50 | |
spanesar | 40:426cd176ce80 | 51 | /*--------------If 'A' is pressed then all sensor data is diplayed---------------------*/ |
spanesar | 40:426cd176ce80 | 52 | if(buffer_str=="READALL") { |
spanesar | 40:426cd176ce80 | 53 | humidity.ReadTempHumi(&tempCelsius, &humi); // reads temperature and humity levels |
spanesar | 40:426cd176ce80 | 54 | barometer.get(); // gets barometer readings |
spanesar | 40:426cd176ce80 | 55 | // displays all the variables of time, date, temperature, humidity and all the barometric readings to the serial |
spanesar | 40:426cd176ce80 | 56 | printf("%s, %s, %4.2fC , %3.1f%%, %6.1f, %4.1f\n\r", time, date, tempCelsius, humi, barometer.pressure(), barometer.temperature()); |
spanesar | 40:426cd176ce80 | 57 | myled = 1; // LED is ON |
spanesar | 40:426cd176ce80 | 58 | Thread::wait(200); // 200 ms NB 'Thread::wait(int d);' !!! d is in milliseconds! |
spanesar | 40:426cd176ce80 | 59 | myled = 0; // LED is OFF |
spanesar | 40:426cd176ce80 | 60 | Thread::wait(100); // 100 ms |
spanesar | 40:426cd176ce80 | 61 | } |
spanesar | 40:426cd176ce80 | 62 | /*-------------- if 'T' is pressed then the time and date are set to the UTC below------------------------*/ |
spanesar | 40:426cd176ce80 | 63 | if(buffer_str=="SETTIME") { |
spanesar | 40:426cd176ce80 | 64 | set_time(1508268288); |
spanesar | 40:426cd176ce80 | 65 | printf("time and date updated to %s %s\n\r", time, date); |
spanesar | 40:426cd176ce80 | 66 | myled = 1; |
spanesar | 40:426cd176ce80 | 67 | Thread::wait(200); |
spanesar | 40:426cd176ce80 | 68 | myled = 0; |
spanesar | 40:426cd176ce80 | 69 | Thread::wait(100); |
spanesar | 40:426cd176ce80 | 70 | } |
spanesar | 40:426cd176ce80 | 71 | /*-------------if 's' is pressed, 5 sample of data are taken and stored to the RAM every second-------------------------*/ |
spanesar | 40:426cd176ce80 | 72 | if(buffer_str=="SETT") { |
spanesar | 40:426cd176ce80 | 73 | for (count = 0; count < 5; count++) { |
spanesar | 39:504f5dc30a33 | 74 | sz = snprintf(NULL, 0,"%s, %s, %4.2fC , %3.1f%%, %6.1f, %4.1f\n\r", time, date, tempCelsius, humi, barometer.pressure(), barometer.temperature()); |
spanesar | 39:504f5dc30a33 | 75 | buf = (char *)malloc(sz + 1); |
spanesar | 39:504f5dc30a33 | 76 | snprintf(buf, sz + 1, "%s, %s, %4.2fC , %3.1f%%, %6.1f, %4.1f\n\r", time, date, tempCelsius, humi, barometer.pressure(), barometer.temperature()); |
spanesar | 39:504f5dc30a33 | 77 | printf("%s\n\r",buf); |
spanesar | 39:504f5dc30a33 | 78 | printf("written to memory\n\r"); |
spanesar | 39:504f5dc30a33 | 79 | myled = 1; |
spanesar | 39:504f5dc30a33 | 80 | Thread::wait(200); |
spanesar | 39:504f5dc30a33 | 81 | myled = 0; |
spanesar | 39:504f5dc30a33 | 82 | Thread::wait(100); |
spanesar | 39:504f5dc30a33 | 83 | wait(1); |
spanesar | 40:426cd176ce80 | 84 | } |
spanesar | 40:426cd176ce80 | 85 | if(buffer_str=="READ") { |
spanesar | 39:504f5dc30a33 | 86 | printf("%s\n\r", buf); |
spanesar | 39:504f5dc30a33 | 87 | myled = 1; |
spanesar | 39:504f5dc30a33 | 88 | Thread::wait(200); |
spanesar | 39:504f5dc30a33 | 89 | myled = 0; |
spanesar | 39:504f5dc30a33 | 90 | Thread::wait (100); |
spanesar | 39:504f5dc30a33 | 91 | } |
noutram | 35:af125862c33e | 92 | } |
spanesar | 40:426cd176ce80 | 93 | } |
spanesar | 40:426cd176ce80 | 94 | |
spanesar | 40:426cd176ce80 | 95 | int main() |
spanesar | 40:426cd176ce80 | 96 | { |
spanesar | 40:426cd176ce80 | 97 | pc.baud(9600); //sets baud rate to 9600 |
spanesar | 40:426cd176ce80 | 98 | idMain = osThreadGetId(); |
spanesar | 40:426cd176ce80 | 99 | t1.start(function1); |
spanesar | 40:426cd176ce80 | 100 | id1 = t1.gettid(); |
spanesar | 40:426cd176ce80 | 101 | |
spanesar | 40:426cd176ce80 | 102 | time_t seconds = time(NULL); |
spanesar | 40:426cd176ce80 | 103 | char time [32]; |
spanesar | 40:426cd176ce80 | 104 | char date [32]; |
spanesar | 40:426cd176ce80 | 105 | strftime(time, 32, "%H:%M:%S %p", localtime(&seconds)); |
spanesar | 40:426cd176ce80 | 106 | strftime(date, 32, "%Y/%m/%d %a", localtime(&seconds)); |
spanesar | 40:426cd176ce80 | 107 | humidity.init(); |
spanesar | 40:426cd176ce80 | 108 | humidity.calib(); |
spanesar | 40:426cd176ce80 | 109 | printf("Type SETTIME to set time and date [enter] \n\r"); |
spanesar | 40:426cd176ce80 | 110 | printf("Type SETT to start sampling [ENTER]\n\r"); |
spanesar | 40:426cd176ce80 | 111 | printf("Type DELETEALL to delete sensor data [enter]\n\r"); |
spanesar | 40:426cd176ce80 | 112 | printf("Type READALL [enter]\n\r"); |
spanesar | 40:426cd176ce80 | 113 | printf("Type READ to read sensor data in memory [ENTER]\n\r"); |
spanesar | 40:426cd176ce80 | 114 | |
spanesar | 40:426cd176ce80 | 115 | |
spanesar | 40:426cd176ce80 | 116 | |
spanesar | 40:426cd176ce80 | 117 | |
spanesar | 40:426cd176ce80 | 118 | |
spanesar | 40:426cd176ce80 | 119 | while(1) { |
spanesar | 40:426cd176ce80 | 120 | char buffer [128]; |
spanesar | 40:426cd176ce80 | 121 | scanf("%127s", buffer); |
spanesar | 40:426cd176ce80 | 122 | string buffer_str(buffer); |
spanesar | 40:426cd176ce80 | 123 | |
spanesar | 40:426cd176ce80 | 124 | |
spanesar | 40:426cd176ce80 | 125 | if(buffer_str=="?") { |
spanesar | 40:426cd176ce80 | 126 | printf("SOFT253 simple Temperature Humidity and Pressure Sensor Monitor\n\r"); |
spanesar | 40:426cd176ce80 | 127 | printf("Using the X-NUCLEO-IKS01A1 shield and MBED Libraries\n\r"); |
spanesar | 40:426cd176ce80 | 128 | } |
spanesar | 40:426cd176ce80 | 129 | |
spanesar | 40:426cd176ce80 | 130 | |
spanesar | 40:426cd176ce80 | 131 | } |
spanesar | 40:426cd176ce80 | 132 | |
spanesar | 39:504f5dc30a33 | 133 | |
FairyMental | 34:62aae7d507e2 | 134 | } |