sandeepmistry / arduino-BLEPeripheral

An Arduino library for creating custom BLE peripherals with Nordic Semiconductor's nRF8001 or nR51822.
MIT License
464 stars 180 forks source link

blePeripheral.begin(); line hangs if using more than 1 characteristic #257

Open JMAC4790 opened 5 years ago

JMAC4790 commented 5 years ago

I'm attempting to create a sketch which will send the pressure readings from 3 sensors and the timestamp from a RTC DS3231 module (Using an arduino nano, adafruit nrf8001 and rtc ds3231). Without including the RTC module code, It works fine and i am able to send three sensor readings in 3 characteristics to a BLE device. The RTC module code also works fine on its own and i am able to read the timestamp on the serial monitor. However, with these two functions combined, the code gets hung on the blePeripheral.begin(); line if i have more than one characteristic.

I've included my code below;

`#include

include

include "RTClib.h"

include

define BLE_REQ 10

define BLE_RDY 2

define BLE_RST 9

DateTime now; char daysOfTheWeek[7][12] = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};

RTC_DS3231 rtc; char buf1[20];

BLEPeripheral blePeripheral = BLEPeripheral(BLE_REQ,BLE_RDY,BLE_RST); BLEService pressureReadingService = BLEService("BBB0");

BLEFloatCharacteristic pressure1Characteristic = BLEFloatCharacteristic("BBB1",BLERead | BLENotify); BLEDescriptor pressure1Descriptor = BLEDescriptor("2901", "kPa");

BLEFloatCharacteristic pressure2Characteristic = BLEFloatCharacteristic("BBB2",BLERead | BLENotify); BLEDescriptor pressure2Descriptor = BLEDescriptor("2901", "kPa");

BLEFloatCharacteristic pressure3Characteristic = BLEFloatCharacteristic("BBB3",BLERead | BLENotify); BLEDescriptor pressure3Descriptor = BLEDescriptor("2901", "kPa");

long previousMillis = 0; //Stores the last time sensor was read long interval = 1000; //Interval between sensor readings

void setup() { Serial.begin(57600); Wire.begin(); rtc.begin(); if(!rtc.begin()) { Serial.println("Couldn't find RTC Module"); while(1); }

if (rtc.lostPower()) { Serial.println("RTC lost power, let's set time"); rtc.adjust(DateTime(F(DATE),F(TIME))); }

//Comment following line out after initial upload so clock doesn't always readjust rtc.adjust(DateTime(F(DATE),F(TIME)));

Serial.println(F("Bluetooth Low Energy Sensor Reader"));

blePeripheral.setLocalName("Pressure Reader"); blePeripheral.setDeviceName("Pressure Reader"); blePeripheral.setAdvertisedServiceUuid(pressureReadingService.uuid()); blePeripheral.addAttribute(pressureReadingService);

blePeripheral.addAttribute(pressure1Characteristic); blePeripheral.addAttribute(pressure1Descriptor);

//Comment out following 2 characteristics and code will work as intended blePeripheral.addAttribute(pressure2Characteristic); blePeripheral.addAttribute(pressure2Descriptor);

blePeripheral.addAttribute(pressure3Characteristic); blePeripheral.addAttribute(pressure3Descriptor);

Serial.println("12"); blePeripheral.begin(); Serial.println("34"); } void loop() { DateTime now = rtc.now(); sprintf(buf1,"%02d:%02d:%02d %02d/%02d/%02d", now.hour(),now.minute(),now.second(),now.day(),now.month(),now.year());

blePeripheral.poll();

if (millis() - previousMillis > interval) { pollSensors(); previousMillis = millis(); Serial.print(F("Date/Time")); Serial.println(buf1); } }

void pollSensors() { float voltage1 = analogRead(A0); float pressure1 = voltage1*5/1023; //Change for correct calibration

float voltage2 = analogRead(A0); float pressure2 = voltage225/1023; //Change for correct calibration

float voltage3 = analogRead(A0); float pressure3 = voltage335/1023; //Change for correct calibration

if (!isnan(pressure1) && pressure1Characteristic.value() !=pressure1) { pressure1Characteristic.setValue(pressure1); Serial.print(F("Pressure 1: ")); Serial.println(pressure1); }

if (!isnan(pressure2) && pressure2Characteristic.value() !=pressure2) { pressure2Characteristic.setValue(pressure2); Serial.print(F("Pressure 2: ")); Serial.println(pressure2); }

if (!isnan(pressure3) && pressure3Characteristic.value() !=pressure3) { pressure3Characteristic.setValue(pressure3); Serial.print(F("Pressure 3: ")); Serial.println(pressure3); } }`

Is there any reason this would be happening?