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.


Thursday, June 28, 2018

Reading Temperature and Humidity on AppsFactory Beacons

AppsFactory Beacons have SHT20 High precision Temperature and Humidity sensor embedded in them. Reading the values from the beacons is not that easy that is why this post is written which will serve as guide. Temperature and Humidity is important as it will allow building owners discover patterns that can not be detected by ordinary humans not unless of course humans are so super intelligent that they may not need tools to predict or discover patterns which could suggest something is just about to happen.

There are two ways to read the sensor data which are as follows:

  1. can be read via GATT
  2. can be included in the beacon advertisement data.

Here is the detail:

1. Reading the Temperature and Humidity via GATT Service
The data can be accessed via GATT Service 0xFFB0 see table below:
The characteristic 0xFFB3 is used to enable the measured Teperature and Humidity on the Advertisement Data. The possible values are as follows(2 bytes):

  • 0x0 - Enable
  • 0x00 - Disable 
Other values maybe entered to specify the rate which it will be advertised, example if 0x0F was entered means the temp and humidity will be advertised every 30 seconds. 
2. Reading the Temperature and Humidity from Beacon Advertisement Data
The Temperature and Humidity is included at the last bytes of the major and minor of the Ibeacon frame.

After obtaining the value, the conversion formula to get the actual values are as follows:

Monday, June 25, 2018

Beacon Scanner App Updated

Remember the app I created to scan Ibeacons and list them on screen and wrote about it that it is possible to modify the app to scan EddyStones as well? To prove it, I have decided to modify it myself. In case you haven't read the previous posts, here are the links:
A Simple Ibeacon Scanner App in Android
An Update to the Ibeacon Scanner App for Android
The a small change I made is to change the name of device whether it is an Ibeacon or an EddyStone-UID.

Like I said, the file DeviceScanActivity.java needs to be modified to detect the 3 Eddystone frames, but I only modified it to scan the UID frame on for demo purposes. And here is the code I insterted:


This is the raw data taken from Ibeacon Detector App:

And here is the resulting app:


Sunday, June 10, 2018

How to Setup AppsFactory Beacons to Broadcast Ibeacon/EddyStone/TLM(Sensor Data)

AppsFactory Beacons Model A just arrived and has made me busy doing tests on each features that the beacons were design to have whether it really works. Doing the program was  not that easy and to see your design work as expected makes you happy.

One notable feature of the beacons is that it can be configured over the air using nRfConnect App for Android or IOS. And can be made to broadcast ibeacon, eddystone and sensor data at an alternating interval. the beacon is also capable of motion triggered broadcast but I haven't test this part yet.

BTW, the sensor data consists of acceleration and temperature data. I will this this part later.



I have designed the beacon to be configured at Bluetooth service 0xff70, and under this is characteristic 0x2a80. The possible values that can be enterered are the ff:
No advertisement: 0x01
iBeacon only: 0x02
Eddystone only:0x08
TLM only: 0x20
To broadcast all three frames : 0x02 + 0x08 + 0x20 = 0x2A

And here is the video:


Sunday, May 20, 2018

How To Make China to Be not Aggressive

With all the current buzz about China being too aggressive by militarizing the South China Sea which include the West Philippine Sea, the world can still make China not to be too aggressive. I think these are the best ways to do it:

1. Send all Chinese back to their homeland. Would you believe that it took China just a decade to overtake the Filipinos as the biggest Asian community in the USA alone? The Philippines took almost 5 decades to send 3M Pinoys to the US. This could have been possible because most Chinese found their way through Canada and then went to the US.

2. Ignore Chinese presence all over the world. Do not buy China made product. By doing this, Robinsons, SM, and other big mall operators will lose their commercial viability since 80% of their products come from China, not to mention the owners.

3. DO not be an opportunist. Just because someone is rich you will want to accept his marriage proposal. Just because you are poor and it is your chance to become rich is not an excuse that you will not be labled as opportunist.

4. Maintain closer relationship with your allies and rely more on locally produced goods. Stop importing too much, even if the locally made products are inferior in terms of quality or more expensive, always remember that if you buy China made products, you are killing your country. The country should support R&D in electric motors and batteries to make electric vehicles. We already know how to manufacture solar panels so we can already achieve energy sufficiency. By doing this we can cut our imports by 50% since most of our imports are gasoline and energy related products.

5. Convince the US to ban all imports/exports to China like weapons, electronics, raw materials, chemicals, machineries, etc. The world could be at worst killing itself if they keep on buying products from China.

6. Ban all tourists coming from China even if they come from Hong Kong, Macau, Singapore, Malaysia, or Taiwan. Chinese make up 20% of the population in South East Asia so be cautious about dealing with these countries. And even with this huge communities, fellow South East Asians should be aware of their presence and the future of their country. I think the local Chinese are also competing with Pinoys in obtaining US visas that is why it has become very to difficult to obtain US visa.

7. Ban Chinese ownership in strategic and important industries like Airline, Telecommunications, Broadcasting, Infrastructure Development. Look at what happened to the last purchased of MRT trains, until now those trains are not yet being used, the people behind those must be prosecuted and should be made liable and labled as terrorists.

8. You can not rely on the US as an ally since they have very healthy economic relationship with China and they look at the Philippines as a tiny country and of no significance to world affairs so why rely on US for military help? Why haven't Pinoys developed weapon systems that could have served as the source of the country's strength? It could not just have been more than that, the Government could have generated jobs. Or exported those weapons to support the local economy.