Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: CheckRTC LPS25H hts221
Fork of ELEC350-extended-referral by
main.cpp
00001 #include "mbed.h" 00002 #include "rtos.h" 00003 #include "hts221.h" 00004 #include "LPS25H.h" 00005 #include <sstream> 00006 #define READALL_OFF 1 00007 #define SETTIME_OFF 2 00008 #define SETT_OFF 4 00009 #define READ_OFF 7 00010 #define ALL_OFF 9 00011 00012 00013 00014 DigitalOut myled(LED1); 00015 I2C i2c2(I2C_SDA, I2C_SCL); 00016 Serial pc(USBTX, USBRX); 00017 float tempCelsius = 25.50; 00018 float humi = 55; 00019 int humiMax = 100; 00020 char buffer_str=0; 00021 uint32_t seconds = 0, minutes=0, hours=0; 00022 Ticker t; 00023 volatile static unsigned short sample16 = 0; 00024 void doSample1Hz(); 00025 int count = 0; 00026 char *buf; 00027 size_t sz; 00028 typedef struct { 00029 float temp; 00030 float press; 00031 float humid; 00032 } measurement; 00033 measurement buffer [120]; 00034 00035 LPS25H barometer(i2c2, LPS25H_V_CHIP_ADDR); 00036 HTS221 humidity(I2C_SDA, I2C_SCL); 00037 00038 void function1(); 00039 void function2(); 00040 void function3(); 00041 void function4(); 00042 00043 Thread t1; 00044 Thread t2; 00045 Thread t3; 00046 Thread t4; 00047 Thread t5; 00048 00049 osThreadId idMain; 00050 osThreadId id1; 00051 osThreadId id2; 00052 osThreadId id3; 00053 osThreadId id4; 00054 00055 00056 00057 void function1() 00058 { 00059 while (true){ 00060 char buffer [128]; 00061 scanf("%127s", buffer); 00062 string buffer_str(buffer); 00063 char date [32]; 00064 strftime(date, 32, "%Y/%m/%d %a", localtime(&seconds)); 00065 char time [32]; 00066 strftime(time, 32, "%H:%M:%S %p", localtime(&seconds)); 00067 00068 if(buffer_str=="READALL") { 00069 humidity.ReadTempHumi(&tempCelsius, &humi); // reads temperature and humity levels 00070 barometer.get(); // gets barometer readings 00071 // displays all the variables of time, date, temperature, humidity and all the barometric readings to the serial 00072 printf("%s, %s, %4.2fC , %3.1f%%, %6.1f, %4.1f\n\r", time, date, tempCelsius, humi, barometer.pressure(), barometer.temperature()); 00073 myled = 1; // LED is ON 00074 Thread::wait(200); // 200 ms NB 'Thread::wait(int d);' !!! d is in milliseconds! 00075 myled = 0; // LED is OFF 00076 t2.signal_set(READALL_OFF); 00077 } 00078 Thread::wait(200); 00079 00080 00081 } 00082 } 00083 void function2() 00084 { 00085 while (true){ 00086 Thread::signal_wait(READALL_OFF); 00087 char buffer [128]; 00088 scanf("%127s", buffer); 00089 string buffer_str(buffer); 00090 char date [32]; 00091 strftime(date, 32, "%Y/%m/%d %a", localtime(&seconds)); 00092 char time [32]; 00093 strftime(time, 32, "%H:%M:%S %p", localtime(&seconds)); 00094 if(buffer_str=="SETTIME") { 00095 set_time(1508268288); 00096 printf("time and date updated to %s %s\n\r", time, date); 00097 myled = 1; 00098 00099 t3.signal_set(SETTIME_OFF); 00100 } 00101 //Thread::wait(200); 00102 //Thread::wait(200); 00103 //myled = 0; 00104 //Thread::wait(100); 00105 } 00106 } 00107 00108 void function3() 00109 { 00110 while (true){ 00111 Thread::signal_wait(SETTIME_OFF); 00112 char buffer [128]; 00113 scanf("%127s", buffer); 00114 string buffer_str(buffer); 00115 char date [32]; 00116 strftime(date, 32, "%Y/%m/%d %a", localtime(&seconds)); 00117 char time [32]; 00118 strftime(time, 32, "%H:%M:%S %p", localtime(&seconds)); 00119 if(buffer_str=="SETT") { 00120 for (count = 0; count < 5; count++) { 00121 sz = snprintf(NULL, 0,"%s, %s, %4.2fC , %3.1f%%, %6.1f, %4.1f\n\r", time, date, tempCelsius, humi, barometer.pressure(), barometer.temperature()); 00122 buf = (char *)malloc(sz + 1); 00123 snprintf(buf, sz + 1, "%s, %s, %4.2fC , %3.1f%%, %6.1f, %4.1f\n\r", time, date, tempCelsius, humi, barometer.pressure(), barometer.temperature()); 00124 printf("%s\n\r",buf); 00125 printf("written to memory\n\r"); 00126 myled = 1; 00127 t4.signal_set(SETT_OFF); 00128 } 00129 //Thread::wait(200); 00130 //myled = 0; 00131 //Thread::wait(100); 00132 //wait(1); 00133 } 00134 } 00135 } 00136 void function4() 00137 { 00138 while (true){ 00139 Thread::signal_wait(SETT_OFF); 00140 char buffer [128]; 00141 scanf("%127s", buffer); 00142 string buffer_str(buffer); 00143 char date [32]; 00144 strftime(date, 32, "%Y/%m/%d %a", localtime(&seconds)); 00145 if(buffer_str=="READ") { 00146 printf("%s\n\r", buf); 00147 myled = 1; 00148 osSignalSet(idMain, ALL_OFF); 00149 } 00150 //Thread::wait(200); 00151 //myled = 0; 00152 //Thread::wait (100); 00153 } 00154 } 00155 00156 int main() 00157 { 00158 pc.baud(9600); //sets baud rate to 9600 00159 idMain = osThreadGetId(); 00160 t1.start(function1); 00161 t2.start(function2); 00162 t3.start(function3); 00163 t4.start(function4); 00164 id1 = t1.gettid(); 00165 id2 = t2.gettid(); 00166 id3 = t3.gettid(); 00167 id4 = t4.gettid(); 00168 time_t seconds = time(NULL); 00169 00170 humidity.init(); 00171 humidity.calib(); 00172 printf("Type SETTIME to set time and date [enter] \n\r"); 00173 printf("Type SETT to start sampling [ENTER]\n\r"); 00174 printf("Type DELETEALL to delete sensor data [enter]\n\r"); 00175 printf("Type READALL [enter]\n\r"); 00176 printf("Type READ to read sensor data in memory [ENTER]\n\r"); 00177 00178 00179 00180 00181 00182 while(1) { 00183 char buffer [128]; 00184 scanf("%127s", buffer); 00185 string buffer_str(buffer); 00186 osSignalWait(ALL_OFF,osWaitForever); 00187 00188 00189 if(buffer_str=="?") { 00190 printf("SOFT253 simple Temperature Humidity and Pressure Sensor Monitor\n\r"); 00191 printf("Using the X-NUCLEO-IKS01A1 shield and MBED Libraries\n\r"); 00192 } 00193 00194 00195 } 00196 00197 00198 }
Generated on Tue Jul 12 2022 21:43:41 by
1.7.2
