Monday, July 30, 2018

Make Sparkfun's nrf52832 IOT Board an Ibeacon

Like I said, the nrf52832 breakout board can be programmed to become an Ibeacon using the blepheripheral library, in fact it has a sample sketch. But I think the sample sketch is not applicable to what I am trying to achieve so, I decided not to use the sample sketch. The main reason is that it needs an interrupt and a timer just to change the frame from ibeacon to eddystone, so it is not good.

But, using the included ibeacon library as guide, it enabled me to understand how to turn the device into ibeacon. I used Ibeacon Detector app to capture the data packets or advertising packets, and used the captured data to change the blepheripheral manufurerdata and that's it. I just turned it into ibeacon.

Here is the sample ibeacon advertising packet:


This is the manufacturerdata I used:
0x4c, 0x00, 0x02, 0x15, 0xa1, 0x96, 0xc8, 0x76, 0xde, 0x8c, 0x4c, 0x47, 0xab, 0x5a, 0xd7, 0xaf, 0xd5, 0xae, 0x71, 0x27, 0x00, 0x12, 0x00, 0x25, 0xcb, 0x5c

The breakdown is as follows:
0x4c 0x00 = Apple Company
0x02 0x15 = Ibeacon Identifier
0xa1 0x96 0xc8 0x76 0xde 0x8c 0x4c 0x47 0xab 0x5a 0xd7 0xaf 0xd5 0xae 0x71 0x27 =UUID
0x00 0x12 = Major
0x00 0x25 = Minor
0xcb 0x5c = Transmitted Power(-53dBm)

Here is the complete sketch:
#include <SPI.h>
#include <BLEPeripheral.h>

const char * localName  = "LED On"; 
BLEPeripheral blePeriph();

void setup() {
  // put your setup code here, to run once:
Serial.begin(115200);
  blePeriph.setDeviceName(localName);
  blePeriph.setLocalName(localName);
  blePeriph.setTxPower(measuredPower);
  const unsigned char  manufacturerData[26] = {0x4c, 0x00, 0x02, 0x15, 0xa1, 0x96, 0xc8, 0x76, 0xde, 0x8c, 0x4c, 0x47, 0xab, 0x5a, 0xd7, 0xaf, 0xd5, 0xae, 0x71, 0x27, 0x00, 0x12, 0x00, 0x25, 0xcb, 0x5c};
  unsigned int lng         = 26;
  blePeriph.setManufacturerData(manufacturerData, lng);
  blePeriph.begin();
  
}

void loop() {
  // put your main code here, to run repeatedly:
 blePeriph.poll();
}

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:

Friday, July 27, 2018

IB003N-SHT Honest Review

Not long ago, I bought this beacons from Alibaba. What attracted me most and main consideration of choosing this beacons against a number of China made beacons are the following:


  1. It can broadcast 3 frames(Ibeacon, eddystone(URL or UID) and Accelerometer)
  2. It has a specialize temperature sensor
  3. It has buzzer

For some reasons, the Chinese seller had delayed the delivery considering that I paid expensive shipping charge($30) but the delivery time took almost a month which is just like having it shipped for free via postal mail(small packets) but I didn't mind it since the product I bought are really loaded with tons features.

Ok the item arrived and my initial testing which is to check whether it really can broadcast the 3 frames and it worked. Days later, when I tried to check the data being broadcasted and verify if it really has all the goodies and to my surprised, I found that it is not what they claimed it be which really had disappointed me. Here are my actual findings:

  1. Ibeacon and Temperature Sensor issue. According to their documentation, the temperature readings are being broadcasted at the major and minor (least significant bits). During my test, I could not find the major value and the minor value turns out to be the least significant bit. I reported this issue to the Chinese seller and after a few days of discussion, they have admitted that the major value is unusable. They did not mentioned this in the technical documentation they have provided nor on their advertisement. Another issue I found is that most of the time, the temperature reading is 0 which is weird and I found this to be a hardware defect.
  2. Eddystone issue. The data being broadcasted is not Eddystone standard compliant. The main identifer of Eddystone "AAFE" is missing, I have confirmed this because even nRFConnect can not identify the frame as Eddystone. Their counting of UID begins at the "02" byte which is absolutely wrong. The transmittion power is missing. And worst is that when I tried to populate the characteristics "FFD02", the Eddystone frame goes blank which stunned or shocked me.
  3. Buzzer Issue. 20dB is very loud, and based on their technical documentaion, it can be adjusted through GATT. But during my testing, the loudness could not be adjusted. With that loudness, I could hardly hear it.
After I found these defects, I stopped the testing of other features. I have already concluded that the IB003N-SHT is full of defects. I tried to negotiate with the Chinese seller by returning the product to them due to the above mentioned defects, but I got this reply from them:

"Our products have no defects, but if you are not satisfied, you may return the items to us. But pls do not include the batteries. We will deduct $2 for each item for the batteries. We will test each items after receiving them from you and we find no defects, we will refund your money. Shipping and delivery charge will be deducted."

I think this is not what I have expected from them, with this transaction, I would suffer $127 loss which way too much.