BG96 Module MQTT client example using X-NUCLEO-IKS01A2

Dependencies:   mbed X_NUCLEO_IKS01A2 NetworkSocketAPI MQTT

ClientExample

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

Warning

ANTENNA POSITION

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

/media/uploads/abci5961/jp6_open.jpg

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

/media/uploads/abci5961/home_button.jpg

You can import and compile this project in two ways:

1. Using the Online compiler. Just clicking here:

/media/uploads/abci5961/a2_import_online.jpg

Information

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

2. Exporting to 3rd party tools (IDE)

/media/uploads/abci5961/a2_import_online_2.jpg

Information

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:

/media/uploads/abci5961/a2_import.jpg

leave all aption unchanged and click on "Import"

After importing , the project is as image below:

/media/uploads/abci5961/a2_prj.jpg

Make shure that properly NUCLEO board is selected

/media/uploads/abci5961/board_set.jpg

Now, click on compile.

/media/uploads/abci5961/a2_compile_1.jpg

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

Information

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 play.internetofthings.ibmcloud.com
//#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 ".messaging.internetofthings.ibmcloud.com";     
#ifdef ORG_QUICKSTART
	#define ORG "quickstart"     								// connect to quickstart.internetofthings.ibmcloud.com/ 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 play.internetofthings.ibmcloud.com/ 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" 
#endif

2. APN name of your provider

// network credential
#define APN   "web.omnitel.it"  // 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
						pc.printf("\n");
						myled=1;
            if (publish(&client, &ipstack) != 0) { 
                myled=0;
                attemptConnect(&client, &ipstack);   // if we have lost the connection                
            } 
						//else 
						myled=0;
            loop_count = 0;
        }        

Warning

KNOW LIMITATION

  • 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.

BG96.cpp

  • 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

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

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

n1=0x00;
    n2=0x0b;
    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)));

Information

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)

/media/uploads/abci5961/vcom.jpg

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

/media/uploads/abci5961/vcom_set.jpg

log example

Here below details of log informations

/media/uploads/abci5961/a2_serial_log.jpg

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.

/media/uploads/abci5961/qstart.jpg

Now the Bluemix is waiting data from device

/media/uploads/abci5961/qstart_w.jpg


All wikipages