BG96 Module MQTT client example using X-NUCLEO-IKS01A2

Dependencies:   mbed X_NUCLEO_IKS01A2 NetworkSocketAPI MQTT


BG96 MQTT Client example

Board setup

Required boards:

  • X_NUCLEO_L476RG mcu board
  • X_NUCLEO_IKS01A2 sensors expansion board
  • AVNET RSR1157 NbIOT BG96 expansion board

here below how connect the required boards /media/uploads/abci5961/a2_hw_setup_1.jpg



Take care at antenna position (see image below). It's very important to avoid RF field interferences. /media/uploads/abci5961/antenna_1.jpg

IKS01A2 Hardware changes

Using this sensor board for this project, user must open the JP6 jumper between pin 5 and 6, as in image below


Connect ST-LINK USB port on NUCLEO_L476RG to PC, then open terminal to see application messages.

Import and compile

First of all, go to "Home" section


You can import and compile this project in two ways:

1. Using the Online compiler. Just clicking here:



Learn how to use the Online compiler reading Mbed Online Compiler quickstart guide.

2. Exporting to 3rd party tools (IDE)



Learn how to use the mbed exporting tools here .

Using online compiler

When you select "Import into Compiler" /media/uploads/abci5961/import_button1.jpg

the following popup will show:


leave all aption unchanged and click on "Import"

After importing , the project is as image below:


Make shure that properly NUCLEO board is selected


Now, click on compile.


In your default download folder you can find the binary file ... Drag and drop it into USB virtual mass storage of your test equipment boards. The program starts immediatly

Quick Configuration

main.cpp file


By default, the application is configured for BlueMix Quickstart. If you have your own BlueMix account, comment ORG_QUICKSTART macro, configure your connection as needed then recompile the project

1. BlueMix connection configuration macros:

#define ORG_QUICKSTART         							// comment to connect to
//#define SUBSCRIBE              							// uncomment to subscribe to broker msgs (not to be used with IBM broker) 

 // Configuration values needed to connect to IBM IoT Cloud
#define BROKER_URL "";     
	#define ORG "quickstart"     								// connect to For a registered connection, replace with your org 
	#define ID ""
	#define AUTH_TOKEN ""
	#define DEFAULT_TYPE_NAME "iotsample-mbed-Nucleo"
	#define TOPIC  "iot-2/evt/status/fmt/json" 
#else   // not def ORG_QUICKSTART
	#define ORG "pvko17"             						// connect to For a registered connection, replace with your org
	#define ID "testtype_112233445566"       		// For a registered connection, replace with your id
	#define AUTH_TOKEN "testtype_112233445566"	// For a registered connection, replace with your auth-token
	#define DEFAULT_TYPE_NAME "TestType"
	#define TOPIC   "iot-2/type/TestType/id/testtype_112233445566/evt/status/fmt/json" 

2. APN name of your provider

// network credential
#define APN   ""  // apn definition's
#define PASSW  ""
#define USNAME ""

3. Publishing loop

Every 30 seconds the client publish last sensor's data (see loop_count variable)

        if (++loop_count == 60)
						// Publish a message every 30 second
            if (publish(&client, &ipstack) != 0) { 
                attemptConnect(&client, &ipstack);   // if we have lost the connection                
            loop_count = 0;



  • BG96 module, after about one hour of work, fails send data and needs to be restarted . This issue will be further investigated and verified.
  • Firmware release 1.0a is able to detect socket sending failure and make a module restart ONLY after several retries of sending data.


  • Device ID settings:

a) User defined (macro ID_ON_FLASH = 1)

#include "BG96.h"
#include "mbed_debug.h"

//device id settings
#define ID_ON_FLASH		0

const uint8_t DEVICE_ID[6] = {0x00,0x0b,0x57,0x55,0xdb,0x45};

b) Derived from BG96 module IMEI code (macro ID_ON_FLASH = 0 -> see getMACAddress() function)

    n3=(uint8_t)(((sn[4+IMEI_OFFS]&0x0f)<<4 | (sn[5+IMEI_OFFS]&0x0f)) + ((sn[12+IMEI_OFFS]&0x0f)<<4));
    n4=(uint8_t)(((sn[6+IMEI_OFFS]&0x0f)<<4 | (sn[7+IMEI_OFFS]&0x0f)) + (uint8_t)((sn[0+IMEI_OFFS]&0x0f)<<4 | (sn[1+IMEI_OFFS]&0x0f)));
    n5=(uint8_t)(((sn[8+IMEI_OFFS]&0x0f)<<4 | (sn[9+IMEI_OFFS]&0x0f)) + ((sn[13+IMEI_OFFS]&0x0f)));
    n6=(uint8_t)(((sn[10+IMEI_OFFS]&0x0f)<<4 | (sn[11+IMEI_OFFS]&0x0f)) + (uint8_t)((sn[2+IMEI_OFFS]&0x0f)<<4 | (sn[3+IMEI_OFFS]&0x0f)));


By default, the macro ID_ON_FLASH is set to 0

Serial log

Using a PC terminal software (such as RealTerm, Hyperterm) you can see a log of work in progress. To make and see it:

  • connect properly USB port (for example COM11)


  • open a com 115200 baud, 8 bit, 1 stop, no parity, no handshake (here example using RealTerm)


log example

Here below details of log informations


Using IBM Quickstart

To connect and register your device to IBM Quickstart click here (no registration needed)

Mark the IBM's terms and after insert the device ID of your board and . Then click on GO button.


Now the Bluemix is waiting data from device


All wikipages