Переделал под свой датчик bme280 и под свою платку ble400
Dependencies: mbed BLE_API nRF51822
main.cpp
00001 #include "mbed.h" 00002 #include "BLE.h" 00003 #include "WeatherService.h" 00004 #include "bme280.h" 00005 #define I2C_SDA P0_0 00006 #define I2C_SCL P0_1 00007 00008 float t; 00009 float pr; 00010 float h; 00011 00012 uint8_t address = 0x78; 00013 00014 I2C i2c(I2C_SDA, I2C_SCL); 00015 00016 BLE ble; 00017 00018 DigitalOut okLED(LED1); 00019 DigitalOut errLED(LED2); 00020 DigitalOut instrumentsPower(P0_22); 00021 00022 BME280 bme280; 00023 00024 const static char DEVICE_NAME[] = "Weather Station"; 00025 00026 static const uint16_t serviceList[] = { 00027 GattService::UUID_ENVIRONMENTAL_SERVICE 00028 }; 00029 00030 static volatile bool triggerSensorPolling = false; 00031 00032 //bme280 00033 void i2cWrite(uint8_t i2c_address, uint8_t *p_data, uint8_t data_size, uint8_t repeated_start) 00034 { 00035 // mbed uses 8-bit addresses, always confusing. 00036 i2c.write(i2c_address<<1,(const char *)p_data,data_size,repeated_start); 00037 } 00038 00039 void i2cRead(uint8_t i2c_address, uint8_t *p_data, uint8_t data_size) 00040 { 00041 // mbed uses 8-bit addresses, always confusing. 00042 i2c.read(i2c_address<<1,(char *)p_data,data_size); 00043 } 00044 00045 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) 00046 { 00047 /* Restart Advertising on disconnection*/ 00048 ble.gap().startAdvertising(); 00049 } 00050 00051 void blink(void) 00052 { 00053 // statusLED = !statusLED; 00054 triggerSensorPolling = true; 00055 } 00056 00057 void updateFromBME280() 00058 { 00059 bme280.read(); 00060 t = bme280.temperature(); 00061 // float p = bme280.pressure()/100; 00062 // pr = p*0.750062; 00063 pr = bme280.pressure(); 00064 h = bme280.humidity(); 00065 } 00066 00067 00068 int main(void) 00069 { 00070 okLED = 0; 00071 errLED = 0; 00072 00073 Ticker ticker; 00074 ticker.attach(blink, 5); 00075 00076 ble.init(); 00077 00078 bme280.begin(BME280_I2C_ADDRESS1); 00079 // Configure for test purposes. 00080 bme280.writeConfigRegister(BME280_STANDBY_500_US,BME280_FILTER_OFF,0); 00081 bme280.writeControlRegisters(BME280_OVERSAMPLING_1X,BME280_OVERSAMPLING_1X,BME280_OVERSAMPLING_1X,BME280_MODE_NORMAL); 00082 00083 00084 ble.gap().onDisconnection(disconnectionCallback); 00085 00086 /* Setup weather service. */ 00087 WeatherService weatherService(ble); 00088 00089 /* setup advertising */ 00090 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); 00091 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)serviceList, sizeof(serviceList)); 00092 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::UNKNOWN); 00093 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); 00094 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00095 ble.gap().setAdvertisingInterval(1000); /* 1000ms */ 00096 ble.gap().startAdvertising(); 00097 00098 while (true) { 00099 if (triggerSensorPolling && ble.getGapState().connected) { 00100 okLED = 1; 00101 triggerSensorPolling = false; 00102 00103 instrumentsPower = 1; 00104 wait(1); 00105 updateFromBME280(); 00106 instrumentsPower = 0; 00107 00108 weatherService.updateTemperature(t); 00109 weatherService.updatePressure(pr); 00110 weatherService.updateHumidity(h); 00111 00112 } else { 00113 okLED = 0; 00114 ble.waitForEvent(); 00115 } 00116 } 00117 }
Generated on Wed Jul 20 2022 04:44:43 by
1.7.2