BLE test with temp
Dependencies: BLE_API X_NUCLEO_IDB0XA1 X_NUCLEO_IKS01A2 mbed
Fork of BLE_HeartRate_IDB0XA1 by
main.cpp
00001 #include "mbed.h" 00002 #include "BLE.h" 00003 #include "XNucleoIKS01A2.h" 00004 00005 #if 1 //1:シリアルモニタ表示, 0:非表示 00006 Serial pc(USBTX, USBRX); 00007 #define DEBUG(...) { pc.printf(__VA_ARGS__); } 00008 #else 00009 #define DEBUG(...) 00010 #endif 00011 00012 /* Instantiate the expansion board */ 00013 static XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(D14, D15, D4, D5); 00014 static HTS221Sensor *hum_temp = mems_expansion_board->ht_sensor; 00015 00016 float value1, value2; 00017 00018 DigitalOut led(LED1); 00019 Ticker ticker; 00020 BLE ble; 00021 const static char DEVICE_NAME[] = "CD1"; 00022 static Gap::ConnectionParams_t connectionParams; 00023 uint16_t uuid_list[] = {GattService::UUID_DEVICE_INFORMATION_SERVICE}; 00024 uint8_t msg = 0; 00025 uint16_t msg16 = 0; 00026 uint8_t msg_low = 0; 00027 uint8_t msg_high = 0; 00028 uint8_t error_flag = 0; 00029 00030 //ここからサービスとキャラクタリスティックの用意 00031 static const uint8_t UUID_CHAR_DATA[] = {0xFF,0xCF,0xFC,0xCC,0xFF,0xCF,0xFC,0xCC,0xFF,0xCF,0xFC,0xCC,0xFF,0xCF,0xFC,0xCC};//なんでもいい 00032 GattCharacteristic customCharastic(UUID_CHAR_DATA, (uint8_t *)&msg16, sizeof(msg16), sizeof(msg16), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); 00033 GattCharacteristic *customChars[] = {&customCharastic}; 00034 GattService customService(GattService::UUID_DEVICE_INFORMATION_SERVICE, customChars, sizeof(customChars) / sizeof(GattCharacteristic *)); 00035 00036 //BLE接続したら呼ばれるやつ 00037 void connectionCallback(const Gap::ConnectionCallbackParams_t *params) { 00038 DEBUG("Connected!\n\r"); 00039 ble.getPreferredConnectionParams(&connectionParams); 00040 connectionParams.minConnectionInterval = 7.5; 00041 connectionParams.maxConnectionInterval = 10; 00042 connectionParams.slaveLatency = 0; 00043 if (ble.gap().updateConnectionParams(params->handle, &connectionParams) != BLE_ERROR_NONE) { 00044 DEBUG("Failed to update\n\r"); 00045 } 00046 } 00047 00048 //BLE切断したら呼ばれるやつ 00049 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) { 00050 DEBUG("Disconnected!\n\r"); 00051 ticker.detach(); 00052 ble.gap().startAdvertising(); 00053 } 00054 00055 //周期的に呼ばれるやつ 00056 void tickerCallback() { 00057 DEBUG("update\n\r"); 00058 hum_temp->get_temperature(&value1); 00059 msg_high = (uint8_t)value1; 00060 msg_low = (uint8_t) ((value1 - msg_high) *255); 00061 msg16 = msg_high << 8 | msg_low ; 00062 DEBUG("msg = 0x%x\n\r", msg16); 00063 error_flag = ble.updateCharacteristicValue(customCharastic.getValueAttribute().getHandle(), (uint8_t *)&msg16 , sizeof(msg16)); 00064 DEBUG("error = %d\n\r", error_flag); 00065 } 00066 00067 //通知を開始するやつ 00068 void updatesEnabledCallback(Gap::Handle_t handle) { 00069 led = 1; 00070 ticker.attach(&tickerCallback, 10); 00071 DEBUG("Notification is enabled\n\r"); 00072 } 00073 00074 //通知を停止するやつ 00075 void updatesDisabledCallback(Gap::Handle_t handle) { 00076 led = 0; 00077 ticker.detach(); 00078 DEBUG("Notification is disabled\n\r"); 00079 } 00080 00081 void printMacAddress() 00082 { 00083 /* Print out device MAC address to the console*/ 00084 Gap::AddressType_t addr_type; 00085 Gap::Address_t address; 00086 BLE::Instance().gap().getAddress(&addr_type, address); 00087 printf("DEVICE MAC ADDRESS: "); 00088 for (int i = 5; i >= 1; i--){ 00089 printf("%02x:", address[i]); 00090 } 00091 printf("%02x\r\n", address[0]); 00092 } 00093 00094 00095 int main(void) { 00096 uint8_t id; 00097 00098 DEBUG("start\n\r"); 00099 00100 hum_temp->enable(); 00101 hum_temp->read_id(&id); 00102 printf("HTS221 humidity & temperature = 0x%X\r\n", id); 00103 DEBUG("Initialize\n\r"); 00104 ble.init(); 00105 00106 DEBUG("Setup the event handlers\n\r"); 00107 ble.gap().onConnection(connectionCallback); 00108 ble.gap().onDisconnection(disconnectionCallback); 00109 ble.onUpdatesEnabled(updatesEnabledCallback); 00110 ble.onUpdatesDisabled(updatesDisabledCallback); 00111 00112 DEBUG("Advertising payload\n\r"); 00113 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); 00114 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid_list, sizeof(uuid_list)); 00115 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof((const uint8_t *)DEVICE_NAME)); 00116 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00117 ble.gap().setAdvertisingInterval(160); //100ms; 00118 ble.gap().startAdvertising(); 00119 00120 DEBUG("Add service\n\r"); 00121 ble.gattServer().addService(customService); 00122 00123 printMacAddress(); 00124 00125 while (true) { 00126 ble.waitForEvent(); 00127 } 00128 }
Generated on Mon Aug 1 2022 21:34:53 by
1.7.2
