![](/media/cache/profiles/f0fcf351df4eb6786e9bb6fc4e2dee02.jpg.50x50_q85.jpg)
TYBLE16(BLE UART) module on Mbed system. Communicate with iPhone and send BME280 sensor data.
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 /* 00002 * Mbed Application program / Using Akizuki BLE Module AE-TYBLE16 00003 * on-board module : TAIYO YUDEN BLE 4.2 TYSA-B (EYSGJNAWY-WX) 00004 * 00005 * http://akizukidenshi.com/catalog/g/gK-12339/ 00006 * 00007 * Refernce document 00008 * https://www.yuden.co.jp/wireless_module/document/datareport2/jp/ 00009 * TY_BLE_EYSGJNAWY_WX_BriefDataReport_V1_3_20170925J.pdf 00010 * 00011 * Copyright (c) 2017 Kenji Arai / JH1PJL 00012 * http://www.page.sannet.ne.jp/kenjia/index.html 00013 * http://mbed.org/users/kenjiArai/ 00014 * Created: October 27th, 2017 00015 * Revised: October 30th, 2017 00016 */ 00017 00018 /* 00019 Tested condition 00020 mbed-os-5.6.3 & mbed Rev.154 00021 Nucleo-F446RE TYBLE16 /also F411RE & F042K6(only mbed & pin assign differ) 00022 PA_9 pin5 / P0.03(UART_RX) 00023 PA_10 pin6 / P0.01(UART_TX) 00024 pin7 / P0.02(UART_CTS) connected to pin8 / P0.00(UART_RTS) 00025 +3.3v pin14 / +V 00026 GND pin13 / GND 00027 Nucleo-F446RE BME280 00028 PB_9(SDA) SDI 00029 PB_8(SCL) SDK 00030 SDO = GND, CSB = +3.3V 00031 Communcation with iPhone 7 iOS 11.0.3 00032 TYs Terminal App 00033 https://itunes.apple.com/jp/app/tys-terminal-app/id1184640145?mt=8 00034 */ 00035 00036 // Include -------------------------------------------------------------------- 00037 #include "mbed.h" 00038 #include "BME280.h" 00039 00040 // Definition ----------------------------------------------------------------- 00041 #define TIME_OUT 5 // 5 sec 00042 #define ADT_TIME 15 // 15 sec 00043 00044 // Object/ Constructor -------------------------------------------------------- 00045 Serial pc(USBTX,USBRX); 00046 Serial tyble16(PA_9, PA_10); 00047 BME280 bme280(I2C_SDA, I2C_SCL); 00048 00049 // RAM ------------------------------------------------------------------------ 00050 bool state_connection = false; 00051 bool received_data = false; 00052 uint32_t cmd_timeout_cntr = ADT_TIME; 00053 Ticker t; 00054 00055 // ROM / Constant data -------------------------------------------------------- 00056 00057 // Function prototypes -------------------------------------------------------- 00058 void check_connection(void); 00059 void parse_input(char *buf); 00060 00061 //------------------------------------------------------------------------------ 00062 // Control Program 00063 //------------------------------------------------------------------------------ 00064 // special command for BME280 data collection 00065 bool excute_command(char *buf) 00066 { 00067 char c = buf[1]; 00068 switch (c){ 00069 case 'a': // All data 00070 case 'A': 00071 tyble16.printf("Pressure: %04.2f hPa\r\n", 00072 bme280.getPressure()); 00073 tyble16.printf("Temperature: %2.2f degC\r\n", 00074 bme280.getTemperature()); 00075 tyble16.printf("Humidity: %2.2f %%\r\n", 00076 bme280.getHumidity()); 00077 break; 00078 case 'p': // Pressure 00079 case 'P': 00080 tyble16.printf("Pressure: %04.2f hPa\r\n", 00081 bme280.getPressure()); 00082 break; 00083 case 't': // Temperature 00084 case 'T': 00085 tyble16.printf("Temperature: %2.2f degC\r\n", 00086 bme280.getTemperature()); 00087 break; 00088 case 'h': // Humidity 00089 case 'H': 00090 tyble16.printf("Humidity: %2.2f %%\r\n", 00091 bme280.getHumidity()); 00092 break; 00093 case '?': // Help 00094 tyble16.printf("Command syntax\r\n"); 00095 tyble16.printf("@a All of data\r\n"); 00096 tyble16.printf("@p Pressure\r\n"); 00097 tyble16.printf("@t Temperature\r\n"); 00098 tyble16.printf("@h Humidity\r\n"); 00099 break; 00100 default: // no available command 00101 tyble16.printf("ommand not available, hit '@?'\r\n"); 00102 break; 00103 } 00104 return true; 00105 } 00106 00107 int main() 00108 { 00109 static char rcv_bf[128]; 00110 static uint8_t n = 0; 00111 char c = 0; 00112 00113 pc.printf("\r\n\r\nApplication for AE-TYBLE16 Module "); 00114 pc.printf(" Peripheral(Sever) side\r\n"); 00115 pc.printf(" created on %s %s\r\n", __DATE__, __TIME__); 00116 tyble16.printf("I'm TYBLE16 module!\r\n"); 00117 // warming up the sensor 00118 for (int n = 0; n < 6; n++){ 00119 bme280.getPressure(); 00120 bme280.getTemperature(); 00121 bme280.getHumidity(); 00122 } 00123 t.attach(check_connection, 1); 00124 while(true){ 00125 while(tyble16.readable()){ 00126 //----- data receive from Client ----------------------------------- 00127 received_data = true; 00128 c = tyble16.getc(); // received data from client 00129 pc.putc(c); // show to console 00130 rcv_bf[n++] = c; // save int buf 00131 if (c == '\n'){ // end one line 00132 pc.putc('\r'); 00133 rcv_bf[n] = 0; 00134 if (n >3){ 00135 parse_input(rcv_bf); // ckeck command or not 00136 /*for (int i=0;; i++){ 00137 if (rcv_bf[i] == 0){break;} 00138 pc.printf("0x%x,", rcv_bf[i]); 00139 }*/ 00140 if ((rcv_bf[0] == '@') && (rcv_bf[2] == '\r')){ 00141 excute_command(rcv_bf); // BME280 data output 00142 } 00143 } 00144 n = 0; // Clear buffer 00145 } 00146 } 00147 while(pc.readable()){ 00148 //----- data send to Client ---------------------------------------- 00149 char c = pc.getc(); 00150 tyble16.putc(c); 00151 pc.putc(c); // echo back 00152 if(c == '\r'){ // if CR then put LF 00153 tyble16.putc('\n'); 00154 pc.putc('\n'); 00155 } 00156 } 00157 } 00158 } 00159 00160 // Check Response Events 00161 void parse_input(char *buf) 00162 { 00163 if (strstr(buf, "CON")){ // Connection successful 00164 state_connection = true; 00165 } else if (strstr(buf, "DCO")){ // Disconnect 00166 state_connection = false; 00167 } else if (strstr(buf, "ADT")){ // Advertising Timeout 00168 state_connection = false; 00169 cmd_timeout_cntr = 0; 00170 } else if (strstr(buf, "NAK")){ // Failed 00171 state_connection = false; 00172 if (cmd_timeout_cntr == 0){ 00173 // Both Advertising Start. Connectable and Discoverable. 00174 tyble16.printf("BCD3\r\n"); // send Advertising command 00175 cmd_timeout_cntr = ADT_TIME; 00176 } 00177 } 00178 } 00179 00180 // Every one second, check communication status 00181 void check_connection(void) 00182 { 00183 static int8_t counter = 0; 00184 00185 if ((received_data == false) && (state_connection == false)){ 00186 if (++counter >= TIME_OUT){ 00187 tyble16.putc('\n'); 00188 counter = 0; 00189 } 00190 } else { 00191 counter = 0; 00192 } 00193 if (cmd_timeout_cntr != 0){ 00194 --cmd_timeout_cntr; 00195 } 00196 received_data = false; 00197 }
Generated on Tue Jul 12 2022 18:09:43 by
![doxygen](doxygen.png)