今天給大家介紹一下小程序配網(wǎng)工具-安信可 IoT,近期剛發(fā)布了新的版本,新增了服務(wù)器地址配置的功能。這個(gè)新功能,給我們帶來了不少便利,特別是 HomeAssistant 固件使用這一塊。
在2024 年 7 月 17 號(hào)之前,安信可 IoT 小程序只能提供配網(wǎng)程序,用戶能夠利用藍(lán)牙功能給 Wi-Fi 模塊進(jìn)行無線網(wǎng)絡(luò)配置,但是隨著 DIY 作品不斷地更新,為了更加方便地使用固件,涉及 TCP 服務(wù)器和 MQTT 服務(wù)器地址配置只能通過 AT 指令來實(shí)現(xiàn),在某些應(yīng)用場(chǎng)合根本無法實(shí)現(xiàn)。
所以,這次讓前端工程師加了這個(gè)功能,在配網(wǎng)的時(shí)候,可以選擇配置服務(wù)器地址。
01新功能使用說明
02數(shù)據(jù)格式
小程序下發(fā)的服務(wù)器配置數(shù)據(jù)是以 json 格式下發(fā):
{ "server_type": { "addr":"0.0.0.0", "port":"xxxxx" } }
字段名說明示例server_type服務(wù)器類型"tcp"addr服務(wù)器地址,支持 IPV4 地址和域名"192.168.1.1"port服務(wù)器端口號(hào)1883
例如:下發(fā)一個(gè)TCP 服務(wù)器,地址為:192.168.3.1,端口號(hào)為:8888,則數(shù)據(jù)為:
{ "tcp": { "addr":"192.168.3.1", "port":"8888" } }
03數(shù)據(jù)接收
數(shù)據(jù)接收就是設(shè)備端的了,但是前提是要使用 Blufi 配網(wǎng)功能,在 Ai-WB2 的 SDK(Ai-Thinker-WB2)當(dāng)中,有 Blufi 的 demo。
因?yàn)樾〕绦蛳掳l(fā)的數(shù)據(jù)是通過 Blufi 的custom data(自定義數(shù)據(jù))接口下發(fā),在 blufo demo 當(dāng)中
的AXK_BLUFI_EVENT_RECV_CUSTOM_DATA事件,可以看到下發(fā)的服務(wù)器地址數(shù)據(jù):
04數(shù)據(jù)解析
既然是 json 數(shù)據(jù),那只要引用 cJSON.h 頭文件,就能實(shí)現(xiàn)對(duì)服務(wù)器地址和端口號(hào)的解析,下面寫了函數(shù),大家做個(gè)參考
解析服務(wù)器地址
static char data_buff[128] = { 0 }; char* get_ip_addr_from_custom_data(const char* server_type, const char* custom_data) { if (custom_data==NULL) { blog_error("custom_data is NULL"); return NULL; } char* cjson_root = custom_data; cJSON* root = cJSON_Parse(cjson_root); if (root==NULL) { blog_error("%s is't json data", cjson_root); cJSON_Delete(root); return NULL; } cJSON* add_type = cJSON_GetObjectItem(root, server_type); if (add_type==NULL) { blog_error("%s not "%s" project ", cjson_root,server_type); cJSON_Delete(root); return NULL; } cJSON* addr = cJSON_GetObjectItem(add_type, "addr"); if (addr==NULL) { blog_error("%s not "addr" project ", cjson_root); cJSON_Delete(root); return NULL; } memset(data_buff, 0, 128); strcpy(data_buff, addr->valuestring); cJSON_Delete(root); return data_buff; }
調(diào)用示例:
char* addr=get_ip_addr_from_custom_data("tcp",custom_data); printf("addr=%srn",addr);
解析端口號(hào)
uint16_t get_port_from_custom_data(const char* server_type, const char* custom_data) { if (custom_data==NULL) { blog_error("custom_data is NULL"); return NULL; } char* cjson_root = custom_data; cJSON* root = cJSON_Parse(cjson_root); if (root==NULL) { blog_error("%s is't json data", cjson_root); cJSON_Delete(root); return NULL; } cJSON* add_type = cJSON_GetObjectItem(root, server_type); if (add_type==NULL) { blog_error("%s not "%s" project ", cjson_root,server_type); cJSON_Delete(root); return NULL; } cJSON* port_p = cJSON_GetObjectItem(add_type, "port"); if (port_p==NULL) { blog_error("%s not "port" project ", cjson_root); cJSON_Delete(root); return NULL; } uint16_t port = atoi(port_p->valuestring); cJSON_Delete(root); return port; }
調(diào)用示例:
uint16_t port=get_port_from_custom_data("tcp",custom_data); printf("port=%drn",port);
這些程序流程我就不解釋,太簡(jiǎn)單了,不懂的話,自己去學(xué)一下 cjson 的數(shù)據(jù)解析。
審核編輯 黃宇
-
智能家居
+關(guān)注
關(guān)注
1930文章
9614瀏覽量
186597 -
配網(wǎng)
+關(guān)注
關(guān)注
0文章
158瀏覽量
9112 -
IOT
+關(guān)注
關(guān)注
187文章
4231瀏覽量
197764 -
安信可
+關(guān)注
關(guān)注
0文章
162瀏覽量
4053
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論