Sunday, July 29, 2018

Changing the Local Name of Sparkfun's nrf52832 IOT Board

I had been playing with Sparkfun's nrf52832 IOT Board(there is no Chinese spoof version yet and I guess there will never be) ever since I found the IB003N-SHT to be defective. My goal is to develop a firmware using Arduino IDE to make the board function as Ibeacon/Eddystone and at the same be able to advertise sensor data in a different frame. I am still familiarizing my self how to make things done and am currently trying make a prototype whether it is possible to change beacon parameters without hard-resetting the device.

I have so far succeeded. My initial test was to change the local name which a very common function and I have not made it to function as a beacon. I am taking it 1 step at a time. I used the sample program provided by Sparkfun on their website which is the Ble Blink Example. I added the following features:

  1. Added a new Service to change the local name
  2. Added a characteristics under the new Service
  3. When the value on the said characteristic change, it will change the local name and reset the BLE.
The logic is very much the same as the original program when turning on/off the LED connected at pin 7. And here is the source code :
// Import libraries (BLEPeripheral depends on SPI)
#include <SPI.h>
#include <BLEPeripheral.h>

//////////////
// Hardware //
//////////////
#define LED_PIN    7 // LED on pin 7
#define LED_ACTIVE LOW // Pin 7 LED is active low
#define LED_DEFAULT LOW

///////////////////////
// BLE Advertisments //
///////////////////////
const char * localName = "LED Off"; //change the local name
BLEPeripheral blePeriph;
BLEService bleServ("1207");
BLEService chgName("1208");//added new service
BLECharCharacteristic ledChar("1207", BLERead | BLEWrite);
BLECharCharacteristic chgChar("1208", BLERead | BLEWrite);//added new characteristics
int vb =0;
void setup() 
{
  Serial.begin(115200); // Set up serial at 115200 baud
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, !LED_ACTIVE);

  setupBLE();
}

void loop() 
{
  blePeriph.poll();
//--
  if (ledChar.written())
  {
    int ledState = ledChar.value();
     if (ledState) {
      digitalWrite(LED_PIN, LED_ACTIVE);
     } else {
      digitalWrite(LED_PIN, !LED_ACTIVE);
    }
}
//--
//--This my added service when change this is triggered
  if (chgChar.written())
  {
    int chgState = chgChar.value();
    
    if (chgState) {
  
       blePeriph.end();
       setupBLE0();
     
    } else {
     
     blePeriph.end();
     setupBLE1();
    }
}
//--
}

void setupBLE()
{
  // Advertise name and service:
  blePeriph.setDeviceName(localName);
  blePeriph.setLocalName(localName);
  blePeriph.setAdvertisedServiceUuid(bleServ.uuid());

  // Add service
  blePeriph.addAttribute(bleServ);
  // Add characteristic
  blePeriph.addAttribute(ledChar);
  //my New Service and characteristic
  blePeriph.addAttribute(chgName);
  blePeriph.addAttribute(chgChar);

// Now that device6, service, characteristic are set up,
// initialize BLE:
  blePeriph.begin();
// Set led characteristic to default value:
  ledChar.setValue(!LED_ACTIVE);  
}
void setupBLE1()
{
 // Advertise name and service:
  blePeriph.setDeviceName("LED Off");
  blePeriph.setLocalName("LED Off");
 // initialize BLE:
  blePeriph.begin();
}
void setupBLE0()
{
  // Advertise name and service:
  blePeriph.setDeviceName(localName);
  blePeriph.setLocalName(localName);
// initialize BLE:
  blePeriph.begin();
}
And here is the video on how it is done(1208 is the new service visible in nRF Connect:

3 comments:

  1. “I jumped on an opportunity to purchase a rental property over the 4th of  weekend. Mr Lee was quick to respond and since this was my first time getting a loan to buy a rental property , he was able to help me walk through the loan process. It was a great experience working with a good and kind loan lender Mr Lee. I hopefully know very well if you are outta looking for loan to purchase a property or funding business purpose then Mr Lee will be able to help you with such process here his details WhatsApp +1-989-394-3740.   /  247officedept@gmail.com  !”  

    ReplyDelete
  2. Wow this is amazing post. Thanks for sharing the valuable content with us.
    Wigs for women
    best chairs mart

    ReplyDelete