Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <CurieBle.h>
- BLEPeripheral blePeripheral; // Устройство BLE
- BLEService heartRateService("00000000000000000000000072646372"); // Сервис. 00000000000000000000000072646372 - ID сервиса, может быть использовано любое значение размером в 32 байта
- // Характеристика для посылки измерений
- BLECharacteristic heartRateChar("2A37", // стандартный 16-битный ID
- BLERead | BLENotify, 2); // BLERead | BLENotify говорит о том что характеристика будет оповещать всех клиентов о своем изменении // Характеристика занимает два байта (по спецификации) // https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml
- int oldHeartRate = 0; // переменная для того чтобы убедиться в том что не произойдет повторной отправки результатов
- long previousMillis = 0; // Переменная для контроля за частотой проверки данных
- void setup() {
- Serial.begin(9600);
- pinMode(13, OUTPUT); // инициализация лампочки на борде, которая будет зажигаться, когда к Genuino подключается другое устройство
- /* Задает имя для устройства*/
- blePeripheral.setLocalName("HeartRateSketch");
- blePeripheral.setAdvertisedServiceUuid(heartRateService.uuid()); //Включает уведомления о том что наше устройство вышло в сеть и обладает определенным сервисом
- blePeripheral.addAttribute(heartRateService); //добавляет сервис
- blePeripheral.addAttribute(heartRateChar); // Добавляет характеристике
- /* Включает BLE */
- blePeripheral.begin();
- Serial.println("Bluetooth device active, waiting for connections...");
- }
- void loop() {
- // Проверяет, подключился ли к нам кто то:
- BLECentral central = blePeripheral.central();
- // Если подключился:
- if (central) {
- Serial.print("Connected to central: ");
- // зажигает лампочке:
- digitalWrite(13, HIGH);
- // пока к нам подключен клиент
- //передаем ему значения пульсометра каждые 200мс:
- while (central.connected()) {
- long currentMillis = millis();
- if (currentMillis - previousMillis >= 200) {
- previousMillis = currentMillis;
- updateHeartRate();
- }
- }
- // при отключении от клиента выключает лампочке:
- digitalWrite(13, LOW);
- Serial.print("Disconnected from central: ");
- Serial.println(central.address());
- }
- }
- void updateHeartRate() {
- /*Читает данные с аналогового входа
- */
- int heartRate = analogRead(A0);
- if (heartRate != oldHeartRate) { // при изменении значения
- Serial.print("Heart Rate is now: ");
- Serial.println(heartRate);
- const unsigned char heartRateCharArray[2] = { 0, (char)heartRate };
- heartRateChar.setValue(heartRateCharArray, 2); // обновляет характеристику
- oldHeartRate = heartRate;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement