Anda dapat menambahkan Link SDK ke proyek pengembangan yang sudah ada dan mengompilasinya bersama. Contoh berikut memandu Anda melalui proses ini.
Deskripsi contoh
Contoh ini mencakup program uji bernama hello.c yang mencetak Hello World!, serta sebuah makefile untuk mengompilasinya. Anda juga dapat mengunduh potongan kode hello.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
printf("Hello World!\n\r");
return(0);
}
Makefile berisi konten berikut:
PROG_FILE := hello.c mqtt_basic_demo.c
PROG_OBJS := $(patsubst %.c,%.o,$(PROG_FILE))
PROG = hello
SDK_ROOT = $(shell pwd)/LinkSDK
SDK_INC = -I$(SDK_ROOT)/output/include/
SDK_LIB = $(SDK_ROOT)/output/lib/libaiot.a -lpthread
all:prepare $(PROG_OBJS)
$(CC) $(CFLAGS) -o $(PROG) $(PROG_OBJS) $(SDK_INC) $(SDK_LIB)
prepare:
make -C $(SDK_ROOT)
./%.o: %.c
$(CC) -o $@ -c $< $(CFLAGS) $(SDK_INC)
clean:
make -C LinkSDK clean
rm -f *.o $(PROG) $(SDK_OBJS)
Porting SDK
-
Dapatkan SDK.
Dapatkan SDK dari jalur yang disediakan di Obtain an SDK atau . Konfigurasikan SDK dengan pengaturan berikut:
-
Atur Device OS ke Linux.
-
Atur Protocol to connect to IoT Platform ke MQTT.
-
Atur Data Encryption ke TLS-CA.
-
Atur Device Authentication Method ke Device Secret.
Jangan pilih fitur Advanced Features apa pun. Klik Generate untuk mengunduh SDK.
-
-
Lakukan porting SDK.
Setelah Anda mengunduh dan mengekstrak SDK, salin folder LinkSDK ke direktori yang berisi
hello.c, seperti yang ditunjukkan di bawah ini:$ ls -l -rwxrwxrwx 1 root root 183 May 11 19:45 hello.c drwxrwxrwx 1 root root 4096 May 11 17:28 LinkSDK -rwxrwxrwx 1 root root 672 May 11 19:49 makefile -
Salin dan modifikasi contoh MQTT.
Salin
LinkSDK/demos/mqtt_basic_demo.cke direktori yang berisihello.c. Dimqtt_basic_demo.c, ubah nama fungsimainmenjadisdk_test:/* TODO: Ganti dengan kredensial perangkat Anda. */ char *product_key = "${YourProductKey}"; char *device_name = "${YourDeviceName}"; char *device_secret = "${YourDeviceSecret}"; /* TODO: Ganti dengan titik akhir instans Anda. */ char *mqtt_host = "${YourInstanceId}.mqtt.iothub.aliyuncs.com"; int sdk_test(int argc, char *argv[]) { int32_t res = STATE_SUCCESS; void *mqtt_handle = NULL; char *url = "iot-as-mqtt.cn-shanghai.aliyuncs.com"; /* Akhiran nama domain untuk Alibaba Cloud IoT Platform di Wilayah Tiongkok (Shanghai). */ ... } -
Modifikasi makefile untuk menyertakan SDK.
Modifikasi makefile agar mengompilasi kode sumber SDK dan file
mqtt_basic_demo.cyang telah disalin. Anda juga dapat mengunduh potongan kode sdk_test.PROG_FILE := hello.c mqtt_basic_demo.c PROG_OBJS := $(patsubst %.c,%.o,$(PROG_FILE)) PROG = hello SDK_ROOT = $(shell pwd)/LinkSDK SDK_DIR = $(SDK_ROOT)/core $(SDK_ROOT)/core/sysdep $(SDK_ROOT)/core/utils $(SDK_ROOT)/portfiles/aiot_port $(SDK_ROOT)/external $(SDK_ROOT)/external/mbedtls/library SDK_INC = -I$(SDK_ROOT)/external/mbedtls/include $(foreach dir, $(SDK_DIR), -I$(dir) ) SDK_FILES = $(foreach dir, $(SDK_DIR), $(wildcard $(dir)/*.c)) SDK_OBJS = $(patsubst %.c,%.o,$(SDK_FILES)) SDK_LIBS = -lpthread CFLAGS += $(SDK_INC) main:$(PROG_OBJS) $(SDK_OBJS) $(CC) $(CFLAGS) -o $(PROG) $(PROG_OBJS) $(SDK_OBJS) $(SDK_LIBS) clean: rm -f *.o $(PROG) $(SDK_OBJS)Perhatikan perubahan berikut:
-
SDK_DIR harus berisi folder untuk fitur yang digunakan perangkat Anda. Jika Anda memilih fitur advanced, folder yang sesuai akan muncul di direktori
LinkSDK/components. Anda harus menambahkan folder-folder tersebut ke SDK_DIR. -
Contoh ini menggunakan Transport Layer Security (TLS) untuk mengenkripsi data. Oleh karena itu, library mbedtls yang disertakan dalam SDK juga dikompilasi. Variabel
SDK_INCmenentukan penyertaan direktoriexternal/mbedtls/include. -
Program yang dikompilasi dijalankan di Linux dan menggunakan library terkait thread. Oleh karena itu, variabel SDK_LIBS menentukan tautan ke library pthread.
-
-
Modifikasi hello.c untuk memanggil SDK.
Modifikasi
hello.cagar memanggil fungsisdk_testdarimqtt_basic_demo.cuntuk menginisialisasi SDK dan menghubungkan perangkat ke Alibaba Cloud IoT Platform.#include <stdio.h> #include <stdlib.h> /* Deklarasikan fungsi sdk_test. */ extern int sdk_test(int argc, char *argv[]); int main(int argc, char **argv) { printf("Hello World!\n\r"); /* Panggil fungsi sdk_test dalam demo SDK untuk menginisialisasi SDK dan terhubung ke Alibaba Cloud IoT Platform. */ sdk_test(0,NULL); return(0); } -
Untuk mengompilasi kode, jalankan
make. Untuk menjalankan program, jalankan./hello.Jalankan
makedi direktori yang berisihello.cuntuk mengompilasi program. Lalu jalankan./hellountuk mengeksekusinya. Jika berhasil, output berikut akan ditampilkan:Hello World! [1687781092.755][LK-0313] MQTT user calls aiot_mqtt_connect api, connect [1687781092.755][LK-032A] mqtt host: iot-****.mqtt.iothub.aliyuncs.com [1687781092.755][LK-0317] user name: demo****** establish tcp connection with server(host='iot-******.mqtt.iothub.aliyuncs.com', port=[443]) success to establish tcp, fd=3 local port: 52008 [1687781092.811][LK-1000] establish mbedtls connection with server(host='iot-****.mqtt.iothub.aliyuncs.com', port=[443]) [1687781092.844][LK-1000] success to establish mbedtls connection, (cost 45338 bytes in total, max used 48306 bytes) [1687781092.899][LK-0313] MQTT connect success in 137 ms AIOT_MQTTEVT_CONNECT heartbeat response