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: BLE_API BMP180 mbed nRF51822
Fork of BMP180_example by
main.cpp
00001 #include <stdio.h> 00002 #include "mbed.h" 00003 #include "BMP180.h" 00004 00005 #include "ble/BLE.h" 00006 #include "Servo.h" 00007 #include "GattCallbackParamTypes.h" 00008 00009 #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */ 00010 #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */ 00011 #define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */ 00012 00013 #define TXRX_BUF_LEN 20 00014 00015 #define DIGITAL_OUT_PIN P0_17 //D7 00016 #define DIGITAL_IN_PIN P0_5 //A4 00017 #define PWM_PIN P0_16 //D6 00018 #define SERVO_PIN P0_14 //D10 00019 #define ANALOG_IN_PIN P0_6 //A5 00020 00021 //Start Add by Ax 2016-4-23 00022 #include "BMP180.h" 00023 I2C i2c(P0_17, P0_18); 00024 BMP180 bmp180(&i2c); 00025 00026 //#define DIGITAL_OUT_BlueLed_PIN P0_20 //Blue LED P0_19 00027 //#define DIGITAL_OUT_RedLed_PIN P0_19 //Red LED P0_20 00028 #define DIGITAL_IN_IsNoPeopleInfront_PIN P0_23 //Tx Pin to check if people infront 00029 00030 //#define DIGITAL_IN_IsNoPeopleInfront_PIN P0_23 //Tx Pin to check if people infront 00031 //#define DIGITAL_IN_IsNoPeopleInfront_PIN P0_23 //Tx Pin to check if people infront 00032 //#define DIGITAL_IN_IsNoPeopleInfront_PIN P0_23 //Tx Pin to check if people infront 00033 00034 float arrHistoryEnvironment[2] = {0,0};//total working hours,total rest hours 00035 float arrCurrentEnvironment[3] = {0,0,0};//current working hours,current rest hours,current temperature 00036 float stdWorkHours=60;//60 seconds for testing 00037 float stdRestHours=15;//15 seconds for testing 00038 float stdTemperature=18;//18 degree for testing 00039 int checkCounter=2; 00040 00041 DigitalIn IsNoPeopleInfront(DIGITAL_IN_IsNoPeopleInfront_PIN); 00042 //DigitalOut Blue_Led_SET(DIGITAL_OUT_BlueLed_PIN); 00043 //DigitalOut Red_Led_SET(DIGITAL_OUT_RedLed_PIN); 00044 00045 //End Add by Ax 2016-4-23 00046 00047 BLE ble; 00048 00049 DigitalOut LED_SET(DIGITAL_OUT_PIN); 00050 DigitalIn BUTTON(DIGITAL_IN_PIN); 00051 PwmOut PWM(PWM_PIN); 00052 AnalogIn ANALOG(ANALOG_IN_PIN); 00053 Servo MYSERVO(SERVO_PIN); 00054 00055 Serial pc(USBTX, USBRX); 00056 00057 static uint8_t analog_enabled = 0; 00058 static uint8_t old_state = 0; 00059 00060 // The Nordic UART Service 00061 static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; 00062 static const uint8_t uart_tx_uuid[] = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; 00063 static const uint8_t uart_rx_uuid[] = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; 00064 static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71}; 00065 00066 uint8_t txPayload[TXRX_BUF_LEN] = {0,}; 00067 uint8_t rxPayload[TXRX_BUF_LEN] = {0,}; 00068 00069 //static uint8_t rx_buf[TXRX_BUF_LEN]; 00070 //static uint8_t rx_len=0; 00071 00072 00073 GattCharacteristic txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); 00074 00075 GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); 00076 00077 GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic}; 00078 00079 GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *)); 00080 00081 00082 00083 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) 00084 { 00085 pc.printf("Disconnected \r\n"); 00086 pc.printf("Restart advertising \r\n"); 00087 ble.startAdvertising(); 00088 } 00089 00090 void WrittenHandler(const GattWriteCallbackParams *Handler) 00091 { 00092 uint8_t buf[TXRX_BUF_LEN]; 00093 uint16_t bytesRead, index; 00094 00095 if (Handler->handle == txCharacteristic.getValueAttribute().getHandle()) 00096 { 00097 ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead); 00098 memset(txPayload, 0, TXRX_BUF_LEN); 00099 memcpy(txPayload, buf, TXRX_BUF_LEN); 00100 00101 for(index=0; index<bytesRead; index++) 00102 pc.putc(buf[index]); 00103 00104 if(buf[0] == 0x01) 00105 { 00106 if(buf[1] == 0x01) 00107 LED_SET = 1; 00108 else 00109 LED_SET = 0; 00110 } 00111 else if(buf[0] == 0xA0) 00112 { 00113 if(buf[1] == 0x01) 00114 analog_enabled = 1; 00115 else 00116 analog_enabled = 0; 00117 } 00118 else if(buf[0] == 0x02) 00119 { 00120 float value = (float)buf[1]/255; 00121 PWM = value; 00122 } 00123 else if(buf[0] == 0x03) 00124 { 00125 MYSERVO.write(buf[1]); 00126 } 00127 else if(buf[0] == 0x04) 00128 { 00129 analog_enabled = 0; 00130 PWM = 0; 00131 MYSERVO.write(0); 00132 LED_SET = 0; 00133 old_state = 0; 00134 } 00135 00136 } 00137 } 00138 /* 00139 void uartCB(void) 00140 { 00141 while(pc.readable()) 00142 { 00143 rx_buf[rx_len++] = pc.getc(); 00144 if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n') 00145 { 00146 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len); 00147 pc.printf("RecHandler \r\n"); 00148 pc.printf("Length: "); 00149 pc.putc(rx_len); 00150 pc.printf("\r\n"); 00151 rx_len = 0; 00152 break; 00153 } 00154 } 00155 } 00156 */ 00157 void m_status_check_handle(void) 00158 { 00159 uint8_t buf[6]; 00160 //float buf[6]; 00161 if (analog_enabled) // if analog reading enabled 00162 { 00163 // Read and send out 00164 //float s = ANALOG; 00165 //uint16_t value = s*1024; 00166 //buf[0] = (0x0B); 00167 //buf[1] = (value >> 8); 00168 //buf[2] = (value); 00169 //ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); 00170 00171 //float s = 4; 00172 //uint16_t value = s*1024; 00173 //buf[0] = (0x0B); 00174 //buf[1] = 4.5; 00175 //buf[2] = 18; 00176 00177 buf[0] = (0x0B);//Result ID 00178 00179 buf[1] = arrHistoryEnvironment[0];//Total Work Hours 00180 //uint16_t value = arrHistoryEnvironment[0]*1024; 00181 //buf[1] = (value>>8);//Total Work Hours 00182 00183 buf[2] = arrHistoryEnvironment[1];//Total Rest Hours 00184 //value=arrHistoryEnvironment[1]*1024; 00185 //buf[2] = (value>>8);//Total Rest Hours 00186 00187 buf[3] = arrCurrentEnvironment[0];//Current Work Hours 00188 //value=arrCurrentEnvironment[0]*1024; 00189 //buf[3] = (value>>8);//Current Work Hours 00190 00191 buf[4] = arrCurrentEnvironment[1];//Current Rest Hours 00192 //value=arrCurrentEnvironment[1]*1024; 00193 //buf[4] = (value>>8);//Current Rest Hours 00194 00195 buf[5] = arrCurrentEnvironment[2];//Current Temperature 00196 //value=arrCurrentEnvironment[2]*1024; 00197 //buf[5] = (value>>8);//Current Temperature 00198 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 6); 00199 } 00200 00201 // If digital in changes, report the state 00202 if (BUTTON != old_state) 00203 { 00204 old_state = BUTTON; 00205 00206 if (BUTTON == 1) 00207 { 00208 buf[0] = (0x0A); 00209 buf[1] = (0x01); 00210 buf[2] = (0x00); 00211 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); 00212 } 00213 else 00214 { 00215 buf[0] = (0x0A); 00216 buf[1] = (0x00); 00217 buf[2] = (0x00); 00218 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); 00219 } 00220 } 00221 } 00222 00223 //Start Add by Ax 2016-4-23 00224 void GetTemperature() 00225 { 00226 while(1) { 00227 if (bmp180.init() != 0) { 00228 printf("Error communicating with BMP180\n"); 00229 } else { 00230 printf("Initialized BMP180\n"); 00231 break; 00232 } 00233 wait(1); 00234 } 00235 00236 bmp180.startTemperature(); 00237 wait_ms(5); // Wait for conversion to complete 00238 float temp; 00239 while(1) 00240 { 00241 if(bmp180.getTemperature(&temp) != 0) { 00242 printf("Error getting temperature\n"); 00243 continue; 00244 } 00245 else 00246 break; 00247 } 00248 arrCurrentEnvironment[2]=temp; 00249 //bmp180.startPressure(BMP180::ULTRA_LOW_POWER); 00250 //wait_ms(10); // Wait for conversion to complete 00251 //int pressure; 00252 //if(bmp180.getPressure(&pressure) != 0) { 00253 // printf("Error getting pressure\n"); 00254 // continue; 00255 //} 00256 00257 //printf("Pressure = %d Pa Temperature = %f C\n", pressure, temp); 00258 //wait(1); 00259 } 00260 00261 void CheckEnvironment() 00262 { 00263 //Blue_Led_SET=1;//Enable bule LED means start to check 00264 //Red_Led_SET=0;//Disable RED LED means start to check 00265 for(int i=0;i<checkCounter;i++) 00266 { 00267 if(!IsNoPeopleInfront) 00268 { 00269 //Red_Led_SET=0; 00270 //LED_R = 1; 00271 //LED_R = 0; 00272 //wait(1); 00273 //if check many times there's no people 00274 if(i==checkCounter-1) 00275 { 00276 //if no people there, Add rest hours e.g. 2 00277 arrCurrentEnvironment[1]=arrCurrentEnvironment[1]+1; 00278 arrHistoryEnvironment[1]=arrHistoryEnvironment[1]+1; 00279 00280 //if rest enough time, rest working hour and rest hours 00281 if(arrCurrentEnvironment[1]>stdRestHours) 00282 { 00283 arrCurrentEnvironment[0]=0; 00284 arrCurrentEnvironment[1]=0; 00285 } 00286 } 00287 } 00288 else 00289 { 00290 //if people is siting there, Add working hours e.g. 2 00291 arrCurrentEnvironment[0]=arrCurrentEnvironment[0]+1; 00292 arrHistoryEnvironment[0]=arrHistoryEnvironment[0]+1; 00293 arrCurrentEnvironment[1]=0;//Rest hours set to 0 00294 //if working enough time, warn user 00295 if(arrCurrentEnvironment[0]>stdWorkHours) 00296 { 00297 //Red_Led_SET=1; 00298 //Blue_Led_SET=0; 00299 } 00300 /* 00301 arrCurrentEnvironment; 00302 float arrHistoryEnvironment[2] = {0,0};//total working hours,total rest hours 00303 float arrCurrentEnvironment[3] = {0,0,0};//current working hours,current rest hours,current temperature 00304 float stdWorkHours=60;//60 seconds for testing 00305 float stdRestHours=15;//15 seconds for testing 00306 float stdTemperature=18;//18 degree for testing 00307 */ 00308 } 00309 wait(1); 00310 } 00311 00312 GetTemperature(); 00313 } 00314 //End Add by Ax 2016-4-23 00315 00316 int main(void) 00317 { 00318 Ticker ticker; 00319 ticker.attach_us(m_status_check_handle, 200000); 00320 00321 ble.init(); 00322 ble.onDisconnection(disconnectionCallback); 00323 ble.onDataWritten(WrittenHandler); 00324 00325 pc.baud(9600); 00326 pc.printf("SimpleChat Init \r\n"); 00327 00328 //pc.attach( uartCB , pc.RxIrq); 00329 00330 // setup advertising 00331 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); 00332 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00333 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, 00334 (const uint8_t *)"Biscuit", sizeof("Biscuit") - 1); 00335 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, 00336 (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid)); 00337 // 100ms; in multiples of 0.625ms. 00338 ble.setAdvertisingInterval(160); 00339 00340 ble.addService(uartService); 00341 00342 ble.startAdvertising(); 00343 00344 pc.printf("Advertising Start \r\n"); 00345 00346 while(1) 00347 { 00348 ble.waitForEvent(); 00349 //Check Temperature & People sitting time 00350 CheckEnvironment(); 00351 //wait(1); 00352 //Red_Led_SET=0; 00353 //Blue_Led_SET=0; 00354 00355 } 00356 }
Generated on Tue Jul 19 2022 01:04:10 by
1.7.2
