一、环境介绍
单片机采用: STM32F103C8T6
上网方式: 采用ESP8266,也可以使用其他设备代替,只要支持TCP协议即可。比如:GSM模块、有线网卡等。
开发软件: keil5
硬件连接功能: ESP8266接在STM32的串口3上。通过AT指令与ESP8266进行通信。
阿里云物联网平台提供安全可靠的设备连接通信能力,支持设备数据采集上云,规则引擎流转数据和云端数据下发设备端。此外,也提供方便快捷的设备管理能力,支持物模型定义,数据结构化存储,和远程调试、监控、运维。
阿里云物联网平台是一个集成了设备管理、数据安全通信和消息订阅等能力的一体化平台。向下支持连接海量设备,采集设备数据上云;向上提供云端API,服务端可通过调用云端API将指令下发至设备端,实现远程控制。

二、实现功能
接下来通过阿里云物联网服务器实现STM32+ESP8266设备数据远程上传、下发,实现数据交互。
在当前使用的开发板上有4盏LED灯、一个蜂鸣器、4个按键。
实现步骤阿里云官方提供了很详细的文档和对应的SDK,可以参考一下。
文档地址:https://iot.console.aliyun.com/lk/document)






三、阿里云物联网服务器创建步骤


说明:如果没有账号的话,先点击网页右上角,注册一个账号,并完成实名认证再继续下一步。


产品名称根据自己情况填写。



设备信息根据自己情况填写。








下面参数根据自己情况填写。



























四、向服务器上传的数据效果
完成网页端服务器的创建之后,下面使用STM32开发板按下按键通过ESP8266将烟雾传感器数据上传到阿里云服务器。
如果连接成功的话,网页会显示在线状态。

上传的数据可以在这里查看。


五、STM32端的MQTT协议核心代码
代码是标准的MQTT协议代码,实现过程可以参考MQTT协议官方文档。

5.1 mqtt.c代码
#include "aliyun_mqtt.h"
char MQTT_ClientID[100];
char MQTT_UserName[100];
char MQTT_PassWord[100];
u8 *mqtt_rxbuf;
u8 *mqtt_txbuf;
u16 mqtt_rxlen;
u16 mqtt_txlen;
u8 _mqtt_txbuf[256];
u8 _mqtt_rxbuf[256];
typedef enum
{
M_RESERVED1 =0 ,
M_CONNECT ,
M_CONNACK ,
M_PUBLISH ,
M_PUBACK ,
M_PUBREC ,
M_PUBREL ,
M_PUBCOMP ,
M_SUBSCRIBE ,
M_SUBACK ,
M_UNSUBSCRIBE ,
M_UNSUBACK ,
M_PINGREQ ,
M_PINGRESP ,
M_DISCONNECT ,
M_RESERVED2 ,
}_typdef_mqtt_message;
const u8 parket_connetAck[] = {0x20,0x02,0x00,0x00};
const u8 parket_disconnet[] = {0xe0,0x00};
const u8 parket_heart[] = {0xc0,0x00};
const u8 parket_heart_reply[] = {0xc0,0x00};
const u8 parket_subAck[] = {0x90,0x03};
void Aliyun_LoginInit(char *ProductKey,char *DeviceName,char *DeviceSecret)
{
sprintf(MQTT_ClientID,"%s|securemode=3,signmethod=hmacsha1|",DeviceName);
sprintf(MQTT_UserName,"%s&%s",DeviceName,ProductKey);
sprintf(MQTT_PassWord,"%s","ebc042f42a9d73ba9ead8456b652e7756895b79d");
}
void MQTT_Init(void)
{
mqtt_rxbuf = _mqtt_rxbuf;
mqtt_rxlen = sizeof(_mqtt_rxbuf);
mqtt_txbuf = _mqtt_txbuf;
mqtt_txlen = sizeof(_mqtt_txbuf);
memset(mqtt_rxbuf,0,mqtt_rxlen);
memset(mqtt_txbuf,0,mqtt_txlen);
MQTT_Disconnect();
delay_ms(100);
MQTT_Disconnect();
delay_ms(100);
}
u8 MQTT_Connect(char *ClientID,char *Username,char *Password)
{
u8 i,j;
int ClientIDLen = strlen(ClientID);
int UsernameLen = strlen(Username);
int PasswordLen = strlen(Password);
int DataLen;
mqtt_txlen=0;
DataLen = 10 + (ClientIDLen+2) + (UsernameLen+2) + (PasswordLen+2);
mqtt_txbuf[mqtt_txlen++] = 0x10;
do
{
u8 encodedByte = DataLen % 128;
DataLen = DataLen / 128;
if ( DataLen > 0 )
encodedByte = encodedByte | 128;
mqtt_txbuf[mqtt_txlen++] = encodedByte;
}while ( DataLen > 0 );
mqtt_txbuf[mqtt_txlen++] = 0;
mqtt_txbuf[mqtt_txlen++] = 4;
mqtt_txbuf[mqtt_txlen++] = 'M';
mqtt_txbuf[mqtt_txlen++] = 'Q';
mqtt_txbuf[mqtt_txlen++] = 'T';
mqtt_txbuf[mqtt_txlen++] = 'T';
mqtt_txbuf[mqtt_txlen++] = 4;
mqtt_txbuf[mqtt_txlen++] = 0xc2;
mqtt_txbuf[mqtt_txlen++] = 0;
mqtt_txbuf[mqtt_txlen++] = 100;
mqtt_txbuf[mqtt_txlen++] = BYTE1(ClientIDLen);
mqtt_txbuf[mqtt_txlen++] = BYTE0(ClientIDLen);
memcpy(&mqtt_txbuf[mqtt_txlen],ClientID,ClientIDLen);
mqtt_txlen += ClientIDLen;
if(UsernameLen > 0)
{
mqtt_txbuf[mqtt_txlen++] = BYTE1(UsernameLen);
mqtt_txbuf[mqtt_txlen++] = BYTE0(UsernameLen);
memcpy(&mqtt_txbuf[mqtt_txlen],Username,UsernameLen);
mqtt_txlen += UsernameLen;
}
if(PasswordLen > 0)
{
mqtt_txbuf[mqtt_txlen++] = BYTE1(PasswordLen);
mqtt_txbuf[mqtt_txlen++] = BYTE0(PasswordLen);
memcpy(&mqtt_txbuf[mqtt_txlen],Password,PasswordLen);
mqtt_txlen += PasswordLen;
}
for(i=0;i<10;i++)
{
memset(mqtt_rxbuf,0,mqtt_rxlen);
MQTT_SendBuf(mqtt_txbuf,mqtt_txlen);
for(j=0;j<10;j++)
{
delay_ms(50);
if(USART3_RX_FLAG)
{
USART3_RX_BUFFER[USART3_RX_CNT]='\0';
sprintf((char *)mqtt_rxbuf,"%s",USART3_RX_BUFFER);
USART3_RX_FLAG=0;
USART3_RX_CNT=0;
}
if(mqtt_rxbuf[0]==parket_connetAck[0] && mqtt_rxbuf[1]==parket_connetAck[1])
{
return 0;
}
}
}
return 1;
}
u8 MQTT_SubscribeTopic(char *topic,u8 qos,u8 whether)
{
u8 i,j;
mqtt_txlen=0;
int topiclen = strlen(topic);
int DataLen = 2 + (topiclen+2) + (whether?1:0);
if(whether)mqtt_txbuf[mqtt_txlen++] = 0x82;
else mqtt_txbuf[mqtt_txlen++] = 0xA2;
do
{
u8 encodedByte = DataLen % 128;
DataLen = DataLen / 128;
if ( DataLen > 0 )
encodedByte = encodedByte | 128;
mqtt_txbuf[mqtt_txlen++] = encodedByte;
}while ( DataLen > 0 );
mqtt_txbuf[mqtt_txlen++] = 0;
mqtt_txbuf[mqtt_txlen++] = 0x01;
mqtt_txbuf[mqtt_txlen++] = BYTE1(topiclen);
mqtt_txbuf[mqtt_txlen++] = BYTE0(topiclen);
memcpy(&mqtt_txbuf[mqtt_txlen],topic,topiclen);
mqtt_txlen += topiclen;
if(whether)
{
mqtt_txbuf[mqtt_txlen++] = qos;
}
for(i=0;i<10;i++)
{
memset(mqtt_rxbuf,0,mqtt_rxlen);
MQTT_SendBuf(mqtt_txbuf,mqtt_txlen);
for(j=0;j<10;j++)
{
delay_ms(50);
if(USART3_RX_FLAG)
{
USART3_RX_BUFFER[USART3_RX_CNT]='\0';
strcpy((char *)mqtt_rxbuf,(char*)USART3_RX_BUFFER);
USART3_RX_FLAG=0;
USART3_RX_CNT=0;
}
if(mqtt_rxbuf[0]==parket_subAck[0] && mqtt_rxbuf[1]==parket_subAck[1])
{
return 0;
}
}
}
return 1;
}
u8 MQTT_PublishData(char *topic, char *message, u8 qos)
{
int topicLength = strlen(topic);
int messageLength = strlen(message);
static u16 id=0;
int DataLen;
mqtt_txlen=0;
if(qos) DataLen = (2+topicLength) + 2 + messageLength;
else DataLen = (2+topicLength) + messageLength;
mqtt_txbuf[mqtt_txlen++] = 0x30;
do
{
u8 encodedByte = DataLen % 128;
DataLen = DataLen / 128;
if ( DataLen > 0 )
encodedByte = encodedByte | 128;
mqtt_txbuf[mqtt_txlen++] = encodedByte;
}while ( DataLen > 0 );
mqtt_txbuf[mqtt_txlen++] = BYTE1(topicLength);
mqtt_txbuf[mqtt_txlen++] = BYTE0(topicLength);
memcpy(&mqtt_txbuf[mqtt_txlen],topic,topicLength);
mqtt_txlen += topicLength;
if(qos)
{
mqtt_txbuf[mqtt_txlen++] = BYTE1(id);
mqtt_txbuf[mqtt_txlen++] = BYTE0(id);
id++;
}
memcpy(&mqtt_txbuf[mqtt_txlen],message,messageLength);
mqtt_txlen += messageLength;
MQTT_SendBuf(mqtt_txbuf,mqtt_txlen);
return mqtt_txlen;
}
void MQTT_SentHeart(void)
{
MQTT_SendBuf((u8 *)parket_heart,sizeof(parket_heart));
}
void MQTT_Disconnect(void)
{
MQTT_SendBuf((u8 *)parket_disconnet,sizeof(parket_disconnet));
}
void MQTT_SendBuf(u8 *buf,u16 len)
{
USARTx_DataSend(USART3,buf,len);
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.
- 138.
- 139.
- 140.
- 141.
- 142.
- 143.
- 144.
- 145.
- 146.
- 147.
- 148.
- 149.
- 150.
- 151.
- 152.
- 153.
- 154.
- 155.
- 156.
- 157.
- 158.
- 159.
- 160.
- 161.
- 162.
- 163.
- 164.
- 165.
- 166.
- 167.
- 168.
- 169.
- 170.
- 171.
- 172.
- 173.
- 174.
- 175.
- 176.
- 177.
- 178.
- 179.
- 180.
- 181.
- 182.
- 183.
- 184.
- 185.
- 186.
- 187.
- 188.
- 189.
- 190.
- 191.
- 192.
- 193.
- 194.
- 195.
- 196.
- 197.
- 198.
- 199.
- 200.
- 201.
- 202.
- 203.
- 204.
- 205.
- 206.
- 207.
- 208.
- 209.
- 210.
- 211.
- 212.
- 213.
- 214.
- 215.
- 216.
- 217.
- 218.
- 219.
- 220.
- 221.
- 222.
- 223.
- 224.
- 225.
- 226.
- 227.
- 228.
- 229.
- 230.
- 231.
- 232.
- 233.
- 234.
- 235.
- 236.
- 237.
- 238.
- 239.
- 240.
- 241.
- 242.
- 243.
- 244.
- 245.
- 246.
- 247.
- 248.
- 249.
- 250.
- 251.
- 252.
- 253.
- 254.
- 255.
- 256.
- 257.
- 258.
- 259.
- 260.
- 261.
- 262.
- 263.
- 264.
- 265.
- 266.
- 267.
- 268.
- 269.
- 270.
- 271.
- 272.
- 273.
- 274.
- 275.
- 276.
- 277.
- 278.
- 279.
- 280.
- 281.
- 282.
- 283.
- 284.
- 285.
- 286.
- 287.
- 288.
- 289.
- 290.
- 291.
- 292.
- 293.
- 294.
- 295.
- 296.
- 297.
- 298.
- 299.
- 300.
- 301.
5.2 mqtt.h代码
5.3 main.c代码
六、代码参数解释
6.1 设备证书与发布订阅主题

设备证书在创建设备时保存过,如果没有保存可以在下面的页面里查看对应的值。

订阅的主题在下面页面可以看到。
SET,GET,POST,ERR。 SET 用于设置(一般由单片机端使用), GET 用于获取(一般由 APP 端使用), Post 用于回复机制, ERR 用于错误。
作为单片机端用的最多的两个 TOPIC 就是 SET 与 POST

6.2 MQTT登录的密码、ID、用户名、端口号、域名
MQTT标准的3个参数格式在官方文档有介绍:使用MQTT.fx接入物联网平台 - 阿里云物联网平台 - 阿里云



密码的组成格式:

6.3 上传数据

这是上传数据的格式:
第一个参数是 method:后面所跟的参数可以由物模型看到。
第二个参数id : 因为云端会连接很多个用户,所以他所下发的数据会有一个ID 编号,我们这里任意值都行,我用的 0000000001,这里有要注意的,这个 ID 是多少不重要但是位数一定不能少。
第三个是 params:表示上传的具体数据,根据自己云端订阅的类型上传。
第四个是版本号:可以根据自己实际版本填。
7. 总结
这篇文章介绍阿里云物联网平台的基本使用流程,创建产品、创建设备,利用MQTT客户端登录模拟进行数据传输,最终利用STM32+ESP8266连接阿里云物联网平台,完成数据传输。
【本文正在参加物联网有奖征文活动】,活动链接:https://ost.51cto.com/posts/14758
楼主的文章一如既往的详细。