简介:
本章节以嵌入式芯片设备采用http协议接入本IOT中继平台为例程。 设备采用http协议接入本IOT中继系统平台,主要实现从接入设备上报物模型属性数据。 设备侧根据业务需要周期性调用此接口向平台上报设备属性数据,例如:间隔10分钟上报设备温度。
针对嵌入式设备, 上报设备属性数据报文由json字符串组成,数据报文对json字符串base64加密传输(本例程以stm32开发版+W5500以太网模块设备接入为例)。
设备接入提交报文
设备接入认证与数据json字符串格式如下:
{
"secretKey": "44080000001111000017,134xxxxxxxx,xxxxxx",
"protertiesData": [
{ "propertiesId": "P_1697248637282", "dataValue": "36" },
{ "propertiesId": "P_1697250944699", "dataValue": "55" },
。。。。。。
]
}参数说明:
| 参数 | 说明 |
|---|---|
| secretKey | 设备认证secretKey,由设备ID,登录账号,密钥加逗号拼接组成。 "44080000001111000017“为接入设备ID标识值 ”134xxxxxxxx“为IOT设备接入设置的连接账号 (此设备采用http协议接入,在IOT设备接入配置中设置的设备连接账号)。 ”xxxxxx"为 IOT设备接入设置的连接口令 (此设备采用http协议接入,在IOT设备接入配置中设置的设备连接口令)。 |
| protertiesData | 为属性数据集合标识,固定字符; |
| propertiesId | 为物模型属性标识,固定字符; “P_1697248637282”、“P_1697250944699” 为在本iot平台设备建模配置的物模型属性ID标识值,根据平台设置替换; |
| dataValue | 为物模型属性数据值标识,固定字符; "“36” 为您要上报的属性实际数值,例如温度36 度(设备的属性值为您程序开发中获取,获取后替换); “55” 为您要上报的属性实际数值,例如湿度55 度(设备的属性值为您程序开发中获取,获取后替换); |
上报属性报文URL: "/httpService/UpPropertiesData"
Request Method:POST
设备接入提交报文
设备接入认证与数据json字符串格式如下:
{
"secretKey": "44080000001111000017,134xxxxxxxx,xxxxxx",
"eventId": "E_1697248913306",
"paramsList": [
{ "paramsId": "aaaaaa", "dataValue": "36" },
{ "paramsId": "bbbbbb", "dataValue": "45" },
。。。。。。
]
}参数说明:
| 参数 | 说明 |
|---|---|
| secretKey | 设备认证secretKey,由设备ID,登录账号,密钥加逗号拼接组成。 "44080000001111000017“为接入设备ID标识值 ”134xxxxxxxx“为IOT设备接入设置的连接账号 (此设备采用http协议接入,在IOT设备接入配置中设置的设备连接账号)。 ”xxxxxx"为 IOT设备接入设置的连接口令 (此设备采用http协议接入,在IOT设备接入配置中设置的设备连接口令)。 |
| eventId | 为物模型事件标识,固定字符; “E_1697248913306” 为您在本iot平台设备建模配置的物模型事件ID标识值,根据平台设置替换; |
| paramsList | 为上报事件附带的属性数据集合标识,固定字符; |
| paramsId | 为附带的属性数据值标识,固定字符; “aaaaaa” “bbbbbb” 为附带的属性数据值,根据平台设置替换; |
| dataValue | 为物模型属性数据值标识,固定字符; “36” 为您要上报的属性实际数值,例如温度36 度(设备的属性值为您程序开发中获取,获取后替换); “45” 为您要上报的属性实际数值,45 (设备的属性值为您程序开发中获取,获取后替换); |
上报设备事件报文URL: "/httpService/UpEventData"
Request Method: POST
/*******************************************************************************
* @file http协议上报设备属性或设备事件数据 Template ../main.c
* @author txb0727
* @version V1.0.0
* @date 2023-12-10
* @brief Main program body
******************************************************************************
* @attention
* 本范例用于嵌入式开发版采用http协议上报设备属性或设备事件数据,仅供参考
* 本范例硬件为stm32f103开发板,通讯模块为W5500以太网通讯模块
*
* © COPYRIGHT 2023 txb0727. ******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include #include "mcu_init.h"
#include "config.h"
#include "device.h"
#include "spi2.h"
#include "socket.h"
#include "w5500.h"
#include "at24c16.h"
#include "util.h"
#include "dhcp.h"
#include "string.h"
#include #include "http_client.h"
/**********************
* 上报设备属性数据报文由此样例json字符串组成, 数据报文对json字符串base64加密传输
* 上报设备属性数据json字符串样例:
* "{\"secretKey\":\"44080000001111000017,13437156569,123456\",\"protertiesData\":[{\"propertiesId\":\"P_1697248637282\",\"dataValue\":\"36\"},{\"propertiesId\":\"P_1697250944699\",\"dataValue\":\"55\"},............{\"propertiesId\":\"P_1697250944999\",\"dataValue\":\"55\"}]}"
* json字符串内容说明:json字符串包含两部分,一是设备认证secretKey,由设备ID,登录账号,密钥加逗号拼接组成;
* 二是设备属性数据集合,可以多个属性数据
* 样例中 "secretKey" 为设备认证secretKey标识, "44080000001111000017" 为在本iot平台添加的设备ID; "13437156569" 为您在本iot平台设备接入配置设置的账户; "123456" 为您在本iot平台设备接入配置设置的接入口令;
* "protertiesData"为属性数据集合标识; "propertiesId"为物模型属性标识,固定字符;
* "P_1697248637282"、"P_1697250944699" 为您在本iot平台设备建模配置的物模型属性ID标识值,根据平台设置替换;
* "dataValue" 为物模型属性数据值标识,固定字符;
* "36" 为您要上报的属性实际数值,例如温度36 度(设备的属性值为您程序开发中获取,获取后替换);
* "55"为您要上报的属性实际数值,例如湿度55 度(设备的属性值为您程序开发中获取,获取后替换);
*
* ***************/
const char *uploadPropertiesData="{\"secretKey\":\"44080000001111000017,13437156569,123456\",\"protertiesData\":[{\"propertiesId\":\"P_1697248637282\",\"dataValue\":\"36\"},{\"propertiesId\":\"P_1697250944699\",\"dataValue\":\"55\"}]}";
/**********************
* 上报设备事件数据报文由此样例json字符串组成, 数据报文对json字符串base64加密传输
* 上报设备事件数据json字符串样例:
* "{\"secretKey\":\"44080000001111000017,13437156569,123456\",\"eventId\":\"E_1697248913306\",\"paramsList\":[{\"paramsId\":\"aaaaaa\",\"dataValue\":\"36\"},{\"paramsId\":\"bbbbbb\",\"dataValue\":\"45\"},.......{\"paramsId\":\"bbbbbb\",\"dataValue\":\"45\"}]}"
* json字符串内容说明:json字符串包含两部分,一是设备认证secretKey,由设备ID,登录账号,密钥加逗号拼接组成;
* 二是设备事件数据集合,用于上传事件发生时上报设备的属性数据,可以多个也可以为空
* 样例中 "secretKey" 为设备认证secretKey标识, "44080000001111000017" 为在本iot平台添加的设备ID; "13437156569" 为您在本iot平台设备接入配置设置的账户; "123456" 为您在本iot平台设备接入配置设置的接入口令;
* "eventId"为物模型事件标识,固定字符;
* "E_1697248913306" 为您在本iot平台设备建模配置的物模型事件ID标识值,根据平台设置替换;
* "paramsList" 为上报事件附带的属性数据集合标识,固定字符;
* "paramsId" 为附带的属性数据值标识,固定字符;
* "aaaaaa" "bbbbbb" 为附带的属性数据值,根据平台设置替换;
* "dataValue" 为物模型属性数据值标识,固定字符;
* "36" 为您要上报的属性实际数值,例如温度36 度(设备的属性值为您程序开发中获取,获取后替换);
* "45"为您要上报的属性实际数值,例如湿度45 度(设备的属性值为您程序开发中获取,获取后替换);
*
* ***************/
const char *uploadEventData="{\"secretKey\":\"44080000001111000017,13437156569,123456\",\"eventId\":\"E_1697248913306\",\"paramsList\":[{\"paramsId\":\"aaaaaa\",\"dataValue\":\"36\"},{\"paramsId\":\"bbbbbb\",\"dataValue\":\"45\"}]}";
uint16 local_port = 5000; //设置开发板本地端口
uint8 remote_ip[4] = {192, 168, 0, 105}; //连接服务器IP,即为iot平台IP
uint16 remote_port = 18080; //连接服务器端口,即为iot平台端口
int main()
{
RCC_Configuration(); /* 配置单片机系统时钟*/
NVIC_Configuration();/* 配置嵌套中断向量*/
Systick_Init(72);/* 初始化Systick工作时钟*/
GPIO_Configuration();/* 配置GPIO*/
Timer_Configuration();/*定时器初始化*/
USART1_Init(); /*初始化串口通信:115200@8-n-1*/
at24c16_init();/*初始化eeprom*/
printf("W5500 EVB initialization over.\r\n");
Reset_W5500();/*硬重启W5500*/
WIZ_SPI_Init();/*初始化SPI接口*/
printf("W5500 initialized!\r\n");
set_default();
init_dhcp_client();
while(1)
{
if(DHCP_run()){
//执行http client主函数,上报属性数据
do_upload_properties_data(uploadPropertiesData,local_port,remote_ip,remote_port);
//执行http client主函数,上报设备事件数据,根据业务情况触发调用
// do_upload_event_data(uploadEventData,local_port,remote_ip,remote_port);
}else{
DHCP_run();
};
}
}
/******************* (C) COPYRIGHT 2023 txb0727 ****** END OF FILE ****/#include #include #include #include "http_client.h"
#include "w5500.h"
#include "socket.h"
#include "util.h"
#include "base64.h"
/***************************************************************************
*@brief 执行http client主函数,上报属性数据
*@param uploadPropertiesData 为上传的属性数据json字符串
*@param local_port 为自定义开发版端口
*@param remote_ip 为iot平台IP
*@param remote_port 为iot平台端口
*@return 无
****************************************************************************/
void do_upload_properties_data(const char *uploadPropertiesData,uint16 local_port,uint8 remote_ip[4] ,uint16 remote_port)
{
switch (getSn_SR(SOCK_LOGIN))
{
case SOCK_CLOSED:
socket(SOCK_LOGIN, Sn_MR_TCP, local_port++, Sn_MR_ND);
printf("SOCK_CLOSED \r\n");
break;
case SOCK_INIT:
connect(SOCK_LOGIN, remote_ip, remote_port);
printf("SOCK_INIT \r\n");
break;
case SOCK_ESTABLISHED:
if (getSn_IR(SOCK_LOGIN)&Sn_IR_CON)
{
printf("TCP established \r\n");
setSn_IR(SOCK_LOGIN, Sn_IR_CON);
}
char http_request_body[512];
char *str=(char *)malloc(128);
char secretbody[320]={0};
char secretbodyvalue[300]={0}; //base64加密:后的报文内容
int secretbodyvaluelen;
// printf("base64加密前: %s\r\n",uploadPropertiesData);
base64_encode(uploadPropertiesData,strlen(uploadPropertiesData), secretbodyvalue,&secretbodyvaluelen); //对上传报文base64加密
// printf("base64加密: %s secretbodyvaluelen: %d \r\n\r\n",secretbodyvalue,secretbodyvaluelen);
memset(http_request_body, 0, 512);
strcat(http_request_body, "POST /httpService/UpPropertiesData HTTP/1.1\r\n"); // /httpService/UpPropertiesData 为向iot平台提交数据数据地址
strcat(http_request_body, "Connection:Keep-Alive\r\n");
strcat(http_request_body, "Host: 192.168.0.105:18080\r\n"); //字符串内“192.168.0.105:18080”为iot平台的连接IP和端口
strcat(http_request_body, "Accept: */*\r\n");
strcat(http_request_body, "Content-Type:application/x-www-form-urlencoded; charset=UTF-8\r\n");
strcat(http_request_body, "Content-Length: ");
sprintf(secretbody, "%s", "secretbody=");
strcat(secretbody, secretbodyvalue);
sprintf(str, "%d", strlen(secretbody));
// printf("secretbody:%s strlen(secretbody):%d \r\n",secretbody,strlen(secretbody));
strcat(http_request_body, str);
strcat(http_request_body, "\r\n\r\n");
//secretkey的值为post的数据
strcat(http_request_body, secretbody);
strcat(http_request_body, "\r\n\r\n");
printf("发送请求体:%s\r\n",http_request_body);
send(SOCK_LOGIN, (u_char *)http_request_body, strlen(http_request_body));
//释放内存空间
free(str);
free(secretbody);
free(secretbodyvalue);
free(http_request_body);
Delay_s(60); //延时60秒,根据业务要求的上报时间确定
break;
case SOCK_CLOSE_WAIT:
close(SOCK_LOGIN);
printf("SOCK_CLOSE_WAIT. \r\n");
break;
}
}
/******************************************************
*@brief 执行http client主函数,上报设备事件数据
*@param uploadEventData 为上传的设备事件数据json字符串
*@param local_port 为自定义开发版端口
*@param remote_ip 为iot平台IP
*@param remote_port 为iot平台端口
*@return 无
********************************************************/
void do_upload_event_data(const char *uploadEventData,uint16 local_port,uint8 remote_ip[4] ,uint16 remote_port)
{
switch (getSn_SR(SOCK_LOGIN))
{
case SOCK_CLOSED:
socket(SOCK_LOGIN, Sn_MR_TCP, local_port++, Sn_MR_ND);
printf("SOCK_CLOSED \r\n");
break;
case SOCK_INIT:
connect(SOCK_LOGIN, remote_ip, remote_port);
printf("SOCK_INIT \r\n");
break;
case SOCK_ESTABLISHED:
if (getSn_IR(SOCK_LOGIN)&Sn_IR_CON)
{
printf("TCP established \r\n");
setSn_IR(SOCK_LOGIN, Sn_IR_CON);
}
char http_request_body[512];
char *str=(char *)malloc(128);
char secretbody[320]={0};
char secretbodyvalue[300]={0}; //base64加密:后的报文内容
int secretbodyvaluelen;
// printf("base64加密前: %s\r\n",uploadEventData);
base64_encode(uploadEventData,strlen(uploadEventData), secretbodyvalue,&secretbodyvaluelen); //对上传报文base64加密
// printf("base64加密: %s secretbodyvaluelen: %d \r\n\r\n",secretbodyvalue,secretbodyvaluelen);
memset(http_request_body, 0, 512);
strcat(http_request_body, "POST /httpService/UpEventData HTTP/1.1\r\n");// /httpService/UpEventData 为向iot平台提交设备事件地址
strcat(http_request_body, "Connection:Keep-Alive\r\n");
strcat(http_request_body, "Host: 192.168.0.105:18080\r\n"); //字符串内“192.168.0.105:18080”为iot平台的连接IP和端口
strcat(http_request_body, "Accept: */*\r\n");
strcat(http_request_body, "Content-Type:application/x-www-form-urlencoded; charset=UTF-8\r\n");
strcat(http_request_body, "Content-Length: ");
sprintf(secretbody, "%s", "secretbody=");
strcat(secretbody, secretbodyvalue);
sprintf(str, "%d", strlen(secretbody));
// printf("secretbody:%s strlen(secretbody):%d \r\n",secretbody,strlen(secretbody));
strcat(http_request_body, str);
strcat(http_request_body, "\r\n\r\n");
//secretkey的值为post的数据
strcat(http_request_body, secretbody);
strcat(http_request_body, "\r\n\r\n");
printf("发送请求体:%s\r\n",http_request_body);
send(SOCK_LOGIN, (u_char *)http_request_body, strlen(http_request_body));
//释放内存空间
free(str);
free(secretbody);
free(secretbodyvalue);
free(http_request_body);
// Delay_s(2);
break;
case SOCK_CLOSE_WAIT:
close(SOCK_LOGIN);
printf("SOCK_CLOSE_WAIT. \r\n");
break;
}
} Http协议接入STM例程打包源码进入IOT中继宝盒主操作界面打开“IOT设备接口”窗口,选择对应的设备–设备接入端接口中对应的协议接入样例中下载。
长按关注宜联科技公众号