IoT Resources Library

Including IoT development kits, implementation reference documents, and the newest advances of the internet of things. To help more developers do more flexible applications on their IoT projects.

Developing an application communicating with BLE 5 devices on Dusun’s gateways

As the Dusun gateway is running based on the Linux system, how to implement an application over Bluez stack to ‘talk’ with BLE 5 devices on Dusun gateways will be presented here.

Bluetooth Low Energy (BLE) is intended to provide considerably reduced power consumption and cost while maintaining a similar communication range with Bluetooth. BLE is suitable for IoT applications. Many operating systems support BLE and thus BLE is very popular. The Dusun IoT gateways support most connectivity protocols including BLE(4.x/5.0/Mesh), ZigBee 1.2/3.0, Z-Wave, TCP/UDP, etc. Users can connect the gateway with various BLE devices. As the Dusun gateway is running based on the Linux system, how to implement an application over Bluez stack to ‘talk’ with BLE devices on Dusun gateways will be presented here.

1. Product Feature Summary



  • OS: Linux@ OpenWrt

  • Processor: MTK7620A (MIPS24KEc(580MHZ))

  • RAM: 128MB

  • Flash: 64MB

Wireless protocol:

  • LTE-M1

  • Bluetooth Low Energy

  • Wi-Fi

  • Zigbee3.0

  • Z-Wave


  • WLAN

  • LAN

2. System block diagram

Dusun Gateway system architecture.png

Figure 1. Dusun Gateway system architecture

Dusun’s gateways run the Linux OpenWrt system. As depicted in figure 1, Linux supports BLE protocol through running the Bluez stack. BlueZ provides support for the core Bluetooth layers and protocols. It is flexible, efficient and uses a modular implementation. The BlueZ stack supports all core Bluetooth protocols and layers now. Users can write BLE applications using Bluez APIs to manipulate BLE 5 devices.

3. System Configuration


The Dusun gateways are configured through a web service which means the gateways should be connected with a computer first. The Dusun gateway can be connected through wired connection and WIFI connection.

3.1 Gateway connection

  • Wired connection

The two RJ45 ports gateway includes a WLAN port and a LAN ports. The wired connection of this type gateway is shown below figure 2-A. Users can use the gateway default LAN IP address: to connect it.  

System configuration.jpg

Figure 2-A Direct connection

The one RJ45 port gateway includes a WLAN port only. The wired connection of this type gateway is shown in Figure 2-B. A router is used to ensure the gateway and the PC are in the same local area network. Users should log into the router to check the gateway’s IP address for connecting it.

Connection via a router.png

Figure 2-B Connection via a router

After the above physical connection is completed, please open a web browser on PC, Input Gateway IP Address;Enter the username and Password(default Username: root Password: root),login to the gateway.

WiFi Manage-username.png
  • WIFI connection

The Dusun gateway supports WIFI connection to login it, but WIFI is not enabled default. The WIFI should be enabled first if user want to use WIFI to connect a gateway. Users should login the gateway through the above wired connection to enable the WIFI module. After the connection is established, please follow the below steps to enable WIFI:

  1. Login to the gateway (see above figure);

  2. Select Dusun > Wifi Manage.

WiFi Manage.png

3. Enable Device Radio0 and configure Wifi Interface Wlan0(Wifi Mode:ap, Wifi SSID: Input your own; WIFI Encryption: PSK2 or others; WIFI key: your password)

WiFi Manage-1.png

  4. Click the Save & Apply button, and waiting the changes to be applied.

WiFi Manage-save-apply.png

 5. Then we can connect the WIFI (for this case: Dusun123) and connect the gateway through WIFI.

WiFi Manage-connect-gateway.png
WiFi Manage-username.png

3.2 Gateway configuration

After the connection between the gateway and computer is established, the gateway can be configured following these steps:

  1. Open a web browser on PC, input the gateway IP address, enter the username and Password (default username: root password: root),login to the gateway; 

  2. Make system configuration at the system menu, the timezone, password and others can be revised here.

Gateway configuration-1.png

 3. Configure network at the network sub-memu.

Gateway configuration-network.png

 4. Configure other settings according to users’ needs.

When the configuration is completed please reboot the gateway. Then users can login to the gateway by inputting the following ssh commands in a terminal, and then input your password. IP_addr is the IP addr which user configured above. The default IP is The below figure shows the login scene.


ssh root@IP_addr

Gateway configuration-ssh-root.png

4. API for BLE5 software development

Dusun BLE gateway implemented EFR32BG21 BLE5 chip from silicon lab. In order to simplify the programming procedures, we provide a detailed API document seen from attached file for all kinds of implementation, some core function is listed below,

/*Enhance the security of a connection to current security requirements*/


/* set the default Bluetooth connection parameters*/


/* connect an advertising device with the specified initiating PHY*/


/*enable or disable the notifications and indications being sent from a remote GATT server*/


/*write the value of a characteristic in a remote GATT database*/


/*start a software timer*/


/*end a current GAP procedure*/


/*start the GAP discovery procedure to scan for advertising devices on the specified scanning PHY, that is to perform a device discovery*/


As for other function required, please check about the ble5 api file.

5. A sample program: read data from an oximeter BLE sensor

We have provided a sample application to get the BLE oximeter sensor data. To be more specific, the function of scan, connect and read data is called in app.c file. The main function in the main.c is shown as below.


int main(int argc, char* argv[])


  struct gecko_cmd_packet* evt;

  pthread_t tid1;


  /* Initialize BGLIB with our output function for sending messages. */

  BGLIB_INITIALIZE_NONBLOCK(on_message_send, uartRx, uartRxPeek);


  /* Initialise serial communication as non-blocking. */

  if (appSerialPortInit(100) < 0) {

    printf("Non-blocking serial port init failure\n");



  if (pthread_create(&tid1, NULL, (void*)bootdaemon, "new thread:") != 0) {

        printf("pthread_create error.");





  // Flush std output



  DEBUG_INFO("Starting up...\n");

  DEBUG_INFO("Resetting NCP target...\n");


  /* Reset NCP to ensure it gets into a defined state.

   * Once the chip successfully boots, gecko_evt_system_boot_id event should be received. */


  // Flush std output



  while (1) {

    /* Check for stack event. */

    evt = gecko_peek_event();

    /* Run application and event handler. */




  return -1;


The current event is received by the variant evt, according to different event the function appHandleEvents function would call corresponding command. The function appHandleEvents is the main framework to schedule different function, the main part of this function in file app.c is listed below,

switch (BGLIB_MSG_ID(evt->header)) {

    case gecko_evt_system_boot_id:



      appBooted = true;

      DEBUG_SUCCESS("System booted.");


     if ( CONF_get()->remote_address_set ) {

        DEBUG_INFO("Connecting devices.\n ");

        gecko_cmd_hardware_set_soft_timer(SW_TIMER_20_MS, EVT_CONNECT, 1);

      } else {


        struct gecko_msg_le_gap_set_discovery_timing_rsp_t * rsp_t;


        rsp_t=gecko_cmd_le_gap_set_discovery_timing(le_gap_phy_1m, 40, 32);


        if (rsp_t->result) {

            ERROR_EXIT("Error, setting scanning parameters failed,%x", rsp_t->result);



        printf("Scanning for devices %x.\n", rsp_t->result);

        gecko_cmd_hardware_set_soft_timer(SW_TIMER_100_MS, EVT_SCANNING, 0);

        gecko_cmd_le_gap_start_discovery(le_gap_phy_1m, le_gap_discover_generic);





    case gecko_evt_le_connection_closed_id:

      DEBUG_SUCCESS("Connection closed.\n");



      /* Restart general advertising and re-enable connections after disconnection. */

      gecko_cmd_le_gap_start_advertising(0, le_gap_general_discoverable, le_gap_connectable_scannable);


      gecko_cmd_hardware_set_soft_timer(SW_TIMER_100_MS, EVT_SCANNING, 0);

      gecko_cmd_le_gap_start_discovery(le_gap_phy_1m, le_gap_discover_generic);



    case gecko_evt_le_connection_opened_id:

      DEBUG_SUCCESS("Connection Success.\n");





    case gecko_evt_gatt_service_id:



      print_data(evt->, evt->data.evt_gatt_service.uuid.len);printf("\n");



            service_handle = evt->data.evt_gatt_service.service;

            DEBUG_SUCCESS("find service:%d",service_handle);




    case gecko_evt_gatt_characteristic_id:

       //struct gecko_msg_gatt_characteristic_evt_t * cha_t;


       DEBUG_INFO("character %d- %d- ",evt->data.evt_gatt_characteristic.connection,evt->data.evt_gatt_characteristic.characteristic);

       print_data(evt->, evt->data.evt_gatt_characteristic.uuid.len);printf("\n");



            rxcharservice_handle = evt->data.evt_gatt_characteristic.characteristic;

            DEBUG_SUCCESS("find service:%d",rxcharservice_handle);





    case gecko_evt_gatt_procedure_completed_id:




    case gecko_evt_gatt_characteristic_value_id:


      if (evt->data.evt_gatt_characteristic_value.characteristic == rxcharservice_handle ) {

           print_notifydata(evt->, evt->data.evt_gatt_characteristic_value.value.len);





    case gecko_evt_le_gap_scan_response_id:



    case gecko_evt_hardware_soft_timer_id:






With different events reported to this function, it would invoke different functions. For example, if the program get a event with event id gecko_evt_le_gap_scan_response_id, it would call the SCAN_Process_scan_response  function and start the scanning process.

For the complete code, you can find it in the attached file named BLE5_oximeter.

If you would prefer the code with uploading data to mqtt broker function, you can view the code BLE5withpaho.

6. Run the oximeter sensor sample on the gateway


1) Get the Openwork toolchain and copy it to a Linux PC. 

Get the cross compile OpenWrt-Toolchain from Dusun, whose name is openwrt-sdk-ramips-mt7620_gcc-4.8-linaro_uClibc- Linux-x86_64.tar.bz2. Decompress the downloaded OpenWrt Toolchain to a local folder (E.g.: home/bc/ OpenWrt-SDKCHAOSCALMER).

2) Compiled the attached code files.


Copy the above code file to the Linux PC and decompress it to a folder (E.g.: home/bc/BLE5). Open ./BLE5 folder, and edit the makefile file to revise the CROSSTOOLDIR to the OpenWrt toolchain directory you created above. This is depicted in Figure 3.


Figure 3 change the CROSSTOODIR path

Then open a terminal on the Linux PC, and type the following commands:

cd BLE5_oximeter ;


Finally, the test bin file which can be run in the gateway has been compiled. (Figure 4).

test application compilation.png

Figure 4. the test application compilation

3) Copy the compiled test bin file into the gateway and run it. 

There are several ways to do it, one of them is introduced below. Under Linux PC, you can use SCP command (scp local_file remote_username @remote_ip:remote_folder) to do it. Make sure the gateway is connected to the same router with PC, then run the following commands:

scp test root@