Go Down

Topic: Unable to advertise multiple sensor readings via different LocalNames at once (Read 599 times) previous topic - next topic

mblaise0

Greetings! i need help with the intel CurieBLE library, I have a sketch with different sensors , i.e one to NOTIFY me on the battery of which am using the example from the Library for Battery Monitor and then I have another which is to NOTIFY me when a pin goes HIGH or LOW and I was yet to add more sensors before I got stocked. My issue now is, I am trying to send each of these reports independently with different Local names but i have been unable to achieve that. When ever i upload the sketch both sensor report are being captured under one Local Name. I am quite sure my logic should be wrong or am trying something not possible.  please this is very important to me as am very new to  arduino101(am using TinyTILE) and programming in general an example code or a correction to my code on how to achieve this would be well appreciated. Thanks in Anticipation. Below is an attachment to my code.


#include <SPI.h>
#include <CurieBLE.h>

//pin declarations

#define BATTERY_VOLTAGE_SENSE_PIN A0
#define POWER_AVAILABILITY_PIN 2

int oldBatteryLevel = 0;  // last battery level reading from analog input
long previousMillis = 0;  // last time the battery level was checked, in ms
int initialPowerAvailabilityState = 0;

BLEService batteryService("180F"); // BLE Battery Service
BLEService powerAvailableService("BD1CA63F-50E3-4A05-9294-0A1443CF5B72");

BLEUnsignedCharCharacteristic batteryLevelChar("2A19", BLERead | BLENotify);
BLEUnsignedCharCharacteristic powerAvailabilityChar("BD1CA63E-50E3-4A05-9294-0A1443CF5B72", BLERead | BLENotify);


void setup() {
  Serial.begin(9600);    // initialize serial communication
  pinMode(13, OUTPUT);   // initialize the LED on pin 13 to indicate when a central is connected

  // begin initialization
  BLE.begin();

  /* Set a local name for the BLE device
     This name will appear in advertising packets
     and can be used by remote devices to identify this BLE device
     The name can be changed but maybe be truncated based on space left in advertisement packet
  */
  BLE.setLocalName("PowerStatus");
  BLE.setLocalName("BatteryMonitor");

  BLE.setAdvertisedService(powerAvailableService);
  BLE.setAdvertisedService(batteryService);  // add the service UUID

  powerAvailableService.addCharacteristic(powerAvailabilityChar);
  batteryService.addCharacteristic(batteryLevelChar); // add the battery level characteristic

  BLE.addService(batteryService);   // Add the BLE Battery service
  BLE.addService(powerAvailableService);

  powerAvailabilityChar.setValue(initialPowerAvailabilityState);
  batteryLevelChar.setValue(oldBatteryLevel);   // initial value for this characteristic

  // start advertising
  BLE.advertise();
  BLE.advertise();
  Serial.println("Bluetooth device active, waiting for connections...");
}

void loop() {
 
  initialiseInterruptReading();
}


void initialiseInterruptReading(){
    // listen for BLE peripherals to connect:
  BLEDevice central = BLE.central();

  // if a central is connected to peripheral:
  if(central) {
      Serial.print("Connected to central: ");
      // print the central's MAC address:
      Serial.println(central.address());
      // turn on the LED to indicate the connection:
      digitalWrite(13, HIGH);
 
      // check the battery level every 200ms
      // as long as the central is still connected:
      while (central.connected()) {
        long currentMillis = millis();
        // if 200ms have passed, check the battery level:
        if (currentMillis - previousMillis >= 200) {
          previousMillis = currentMillis;

          updateBatteryLevel();
          GeneratorPowerAvailabilityStatus();
         
        }
    }
    // when the central disconnects, turn off the LED:
    digitalWrite(13, LOW);
    Serial.print("Disconnected from central: ");
    Serial.println(central.address());
  }
}



void updateBatteryLevel() {
  /* Read the current voltage level on the A0 analog input pin.
     This is used here to simulate the charge level of a battery.
  */
  int battery = analogRead(BATTERY_VOLTAGE_SENSE_PIN);
  int batteryLevel = map(battery, 0, 1023, 0, 100);

  if (batteryLevel != oldBatteryLevel) {      // if the battery level has changed
    Serial.print("Battery Level % is now: "); // print it
    Serial.println(batteryLevel);
    batteryLevelChar.setValue(batteryLevel);  // and update the battery level characteristic
    oldBatteryLevel = batteryLevel;           // save the level for next comparison
  }
}

void PowerAvailabilityStatus() {
    int powerAvailableStatus = digitalRead(POWER_AVAILABILITY_PIN);
    if (powerAvailableStatus == HIGH){
       Serial.print("power is Available on Generator1");
       powerAvailabilityChar.setValue(powerAvailableStatus);
       initialPowerAvailabilityState = powerAvailableStatus;
    }else{
      Serial.println("power is Unavailable on Generator1");
      powerAvailabilityChar.setValue(powerAvailableStatus);
      initialPowerAvailabilityState = powerAvailableStatus;
    }
}

Go Up