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 ZigBee devices on Dusun's gateways

How to implement an application to ‘talk’ with ZigBee sensors and devices on Dusun gateways will be presented here.

2. System block diagram

An IoT gateway aggregates sensor data, translates between sensor protocols, processes sensor data before sending it onward and more. The Dusun gateways supports most connectivity protocols including BLE(4.x/5.0/Mesh), ZigBee 1.2/3.0, Z-Wave, TCP/UDP, etc. How to implement an application to ‘talk’ with ZigBee sensors and devices on Dusun gateways will be presented here.

1. Product Feature Summary

 

System:

  • OS: Linux@ OpenWrt

  • Processor: MTK7620A (MIPS24KEc(580MHZ))

  • RAM: 128MB

  • Flash: 64MB

Wireless protocol:

  • LTE-M1

  • Wi-Fi

  • Zigbee3.0

  • Bluetooth

  • Z-Wave

  • Ethernet

  • WLAN

  • LAN

2. System block diagram

 

Figure 1. Dusun Gateway system architecture

Dusun’s gateways run Linux OpenWrt system. UBUS is a message bus in OpenWrt. Its main function is to realize information exchange between different applications. When UBUS starts, it runs the ubusd process in the background, which listens for a UNIX socket to communicate with other applications. Other applications can communicate with libubox based on the interface provided by libubox (or implemented by themselves). Figure 1 shows the simplified gateway OpenWrt system architecture.

3.System Configuration

 

Figure 2. gateway→PC connection diagram

The gateway can be configured following these steps:

  1) Connect the gateway to the PC and power up, according to figure 2;

  2) Open a web browser on PC, Input Gateway IP Address: 192.168.66.1;Enter the username and Password (Username: root Password: root),login to the gateway; 

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

4) Configure the network at the network sub-memu.

5) Change other settings, such as ZigBee 3.0 and MQTT settings.

 

4. Getting ZigBee sensors/devices data from UBUS

Dusun has provided a library (please see the attached code files) in which a set of APIs can be used to fetch data from the ZigBee sensors/devices. The API library includes the static link library (librbsdk.a) and a corresponding header file (rbsdk.h). The API functions can be seen from the header file (rbsdk.h). Some core functions are listed below:

/*Function to initialize the SDK. */

  int rbsdk_init(void *unused, int (*msgcb)(char *src, char *dest, char *msg));

/*Function to get the SDK version. */

  int rbsdk_vers(char *version);

/*A General Call Functions. */

  int rbsdk_call(char *mac, char *attr, int setOrget, void *jvalue);

  /*Function to Set the Message Callback. */

  int rbsdk_dev_msgcb_set(stDevMsgCb_t *dmc); 

  /*Function to Get Device list. */

  int rbsdk_list_dev();

/*Functoin to Send adding device Request. */

  int rbsdk_add_dev(char *mac, char *type);

  /*Function to Send Deleting Device Request. */

  int rbsdk_del_dev(char *mac); 

  /*Function to Send Add Lock PassWord Request. */

  int rbsdk_dev_lock_add_pass(char *mac, int type, int id, int passVal1, int passVal2, int startTime, int endTime, int suspend);  

/*Function to Send Delete Lock Password Request. */

  int rbsdk_dev_lock_del_pass(char *mac, int type, int id);  

/*Function to Send Modify Lock Password Request. */

  int rbsdk_dev_lock_mod_pass(char *mac, int type, int id, int passVal1, int passVal2, int startTime, int endTime, int suspend);

  /*Function to Send Clear Lock Password Request. */

  int rbsdk_dev_lock_clr_pass(char *mac, int type);

  /*Function to Send Set Dynamic Password Seed Request. */

  int rbsdk_dev_lock_set_seed(char *mac, int id, int seed, int interval, int startTime, int endTime);

  /*Function To Send Light OnOff Request. */

  int rbsdk_dev_light_onoff(char *mac, int onoff);

  /*Function to Send Window Controller's Open Request. */

  int rbsdk_dev_winctr_open(char *mac);

  /*Function to Send Window Controller's Stop Request. */

  int rbsdk_dev_winctr_stop(char *mac);

  /*Function to Send Window Controller's Close Request.*/

  int rbsdk_dev_winctr_close(char *mac);

  /*Function to Send Air conditioner Onoff Request.*/

  int rbsdk_dev_air_onoff(char *mac, int onoff);

  /*Function to Send Air conditioner Mode Request.*/

  int rbsdk_dev_air_mode(char *mac, int mode);

  /*Function to Send Air conditioner Fan Request. */

  int rbsdk_dev_air_fan(char *mac, int fan);

/*Function to Send Air conditioner temp Request. */

  int rbsdk_dev_air_temp(char *mac, int temp_mode, int dir, int temp_delt); 

/*A callback function for pushing the sensor command data reported by the sensor using a customized cluster to the application*/
  int (*rpt_cmd)(char *mac, int ep, int cluster, int cmd, char *buf, int len);

/*A callback function for pushing the sensor attribute data reported by the sensor using a customized cluster to the application*/     
 
int (*rpt_attr)(char *mac, int ep, int cluster, int attr, char *buf, int len);

  /*A function for sending the command data to the sensor using a customized cluster */

  int rbsdk_zcl_cmd(char *mac, int ep, int cluster, int cmdid, char *data, int len);
/*A function for sending the attribute data to the sensor using a customized cluster */
 
int rbsdk_set_attr(char *mac, int ep, int cluster, int attrid, char *data, int len);
/*A function for sending the get atrribute command to the sensor using a customized cluster */
  int rbsdk_get_attr(char *mac, int ep, int cluster, int attrid);

5. A sample: communicate with a door ZigBee sensor using the APIs

 

The code below shows how to use the provided APIs to get a Dusun door contact sensor data. First, we should init the SDK by calling rbsdk_init(NULL, NULL); Then, we write four callback functions, and fill a stDevMsgCb struct which is used by rbsdk_dev_msgcb_set to set the callback functions. We will get the data when the callback functions are invoking. For example, the rpt_door function can print the door status reported. If your sensor report data using a customized ZigBee cluster or customized data format, you should write the rpt_cmd and rpt_attr callback functions in which you can get the sensor data to be processed.

#include <stdio.h>

#include <stdlib.h>

#include "rbsdk.h"

int rpt_dev_added(char *mac, char *ModelStr, char *model, int type, int battery) {

printf("[%s] - %s added\n", __func__, mac);

return 0;

}

int rpt_dev_deled(char *mac) {

printf("[%s] - %s deleted\n", __func__, mac);

return 0;

}

int rpt_dev_online(char *mac, int online) {

printf("[%s] - %s online : %d\n", __func__, mac, online);

return 0;

}

int rpt_door(char *mac, int door) {

printf("[%s] - %s door status:%d\n", __func__, mac, door);

return 0;

}

 

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

 

rbsdk_init(NULL, NULL);

 

char sver[32];

rbsdk_vers(sver);

printf("rbsdk version : %s\n", sver);

 

stDevMsgCb_t dmc = {

.rpt_dev_added = rpt_dev_added,

.rpt_dev_deled = rpt_dev_deled,

.rpt_dev_online = rpt_dev_online,

.rpt_dev_battery = NULL,

.rpt_dev_lowpower = NULL,

.rpt_temp = NULL,

.rpt_humi = NULL,

.rpt_door = rpt_door,

.rpt_pir = NULL,

.rpt_leak = NULL,

.ret_lock_password_add = NULL,

.ret_lock_password_del = NULL,

.ret_lock_password_mod= NULL,

.ret_lock_password_clr = NULL,

.ret_lock_dynamic_seed = NULL,

.rpt_lock_checkrecord = NULL,

.rpt_lock_system_locked= NULL,

.rpt_smoke = NULL,

.rpt_light_onoff = NULL,

.rpt_winctr_status = NULL,

.rpt_winctr_percent = NULL,

        .rpt_cmd             = NULL,

.rpt_attr              = NULL,

};

rbsdk_dev_msgcb_set(&dmc);

 

while (1) {

sleep (1);

}

return 0;

}

6. Run the door 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-0.9.33.2. Linux-x86_64.tar.bz2. 4. Decompress the downloaded OpenWrt Toolchain to a local folder (E.g.: home/software/OpenWrt-SDK).

2) Compiled the attached code files.

Copy the above code files to the Linux PC and decompress it to a folder (E.g.: home/software/test). Open ./test/example/make folder, and edit the arch.mk 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

Figure 4.  the test application compilation

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

cd /home/software/test/example

make

cd build

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

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

 

There are some ways one can do it. 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 /home/software/test/example/test root@192.168.66.1:/user/bin

 Then remote login to the gateway using SSH commands or SSH client (windows: putty or SecureCrt) and run the copied bin file. The login password can be revised following the configuration steps in the above sections.

4) Joining the sensor to the gateway. 

 

Open a door contact sensor and press the ZigBee pairing button on the sensor for about 3 seconds (figure 5). Then run a command in the SSH terminal (after the pc has connected the gateway through SSH):

dusun_ucmd.sh permit 1


The API function rbsdk_add_dev can be used in the application to allow the ZigBee device to join ZigBee network. This will start the ZigBee pairing procedure and connect the sensor to the gateway. Finally, we can see the report messages appearing in the console (Figure 6). When the sensor is connected to the gateway, the rpt_dev_added function is invoked; When the communication begins, the rpt_dev_online function is invoked; When the sensor reports the door status, the rpt_door function is invoked and the door status is printed. If your sensor has a customized ZigBee cluster, the rpt_cmd and rpt_attr should be used to get the data.

Figure 5.  the Door Sensor and Dusun Gateway for testing

Figure 6.  the printed messages when report callback functions invoked

Download this Document

​Developing an application communicating with ZigBee devices on Dusun's gateways

Ask for More IoT Resources

Talk to our experts in IoT.

Speed up the process in your IoT projects.