Taking C programming language as an example, this Demo introduces the implementation of device access under Linux.
Make sure that Linux environment, CMake project build tool (minimum required version 2.8) and GCC compiler have been installed properly on your local machine. Ubuntu 14.04.3 is used in this example.
This Demo depends on the following third-party library. Please install it properly. In this example, the default installation path is
/usr/local/:
openssl-OpenSSl_1_0_2q, paho.mqtt.c-1.3.0 (turn on the PAHO_WITH_SSl option when compiling paho)
Please make sure that products and devices have been created correctly.
Log in to the Linux virtual machine.。
Download for Demo Projects。
Unzip a file using the command. After that, you can see that the OneNET-MQTT-demo-c folder is added locally in Linux. The file details are as follows:
OneNET-MQTT-demo-c
|- cert #directory where mqtts certificate is located
|- certificate.pem
|- CmakeLists.txt #build cmake project
|- busi.h #user-specific business loop and function declaration
|- call-back.h #user-defined connection lost and downlink data callback declaration
|- mqtts-client.h #mqtts-client header file
|- utils.h #base64 codec, hmac_sha1, url_encode function declaration
|- token.h #function declaration for calculating authentication token
|- types.h #topic and dp payload format declaration
|- mqtts-sample.c #main program
|- mqtts-client.c #mqtts-client client create, destroy, subscribe function implementations
|- token.c #authentication token calculation function implementation
|- call-back.c #connection lost, downlink data callback implementation
|- busi.c #user-specific business implementation
|- utils.c #base64 codec, hmac_sha1, url_encode function implementation
|- types.c #topic and dp payload format definition
Before communicating with the IoT platform using a sample code, you should modify the device parameters in mqtts-samples.c
to the device information that you have successfully created, save it, and exit.
Product ID, Device Name and Device Key will be written, as shown below.
/*product to which your device belongs*/
const char *pid = "your product ID";
/*device name*/
const char *device_name = "your device name";
/*device-level access key*/
const char *device_access_key ="your device Key";
Users can customize the token validity period (i.e. expiration time) according to their needs. In this example, it is 2019-12-31 by default.
/*token expiration time. Please calculate the token expiration time according to actual needs. In this example, it is from 1970-1-1 to 2019-12-31, in seconds.*/
int64_t expire_time = 1577721600;
After modification, execute cmake command in a sample code directory to generate a makefile.
cmake .
Compile a sample program based on make instructions.
make clean
make
The generated sample program mqtts-sample
is stored in the current directory.
./mqtts-sample
[datapoint] receive dp accepted response from OneNET...
payload
field contains the sent command that is converted into hexadecimal format.[cmd] receive cmd from OneNET
cmduuid:59aba784-ee98-4620-84b0-b8744fd4ad74
payload:54 68 69 73 69 73 61 74 65 73 74 6D 65 73 73 61 67 65 2E
[cmd] OneNET accept the cmd[59aba784-ee98-4620-84b0-b8744fd4ad74] response...