概述
#include "mqtt.h"
unsigned char GetDataFixedHead(unsigned char MesType,unsigned char DupFlag,unsigned char QosLevel,unsigned char Retain)
{
unsigned char dat = 0;
dat = (MesType & 0x0f) << 4;
dat |= (DupFlag & 0x01) << 3;
dat |= (QosLevel & 0x03) << 1;
dat |= (Retain & 0x01);
return dat;
}
void GetDataConnet(unsigned char *buff)//获取连接的数据包正确连接返回20 02 00 00
{
unsigned int i,len,lennum = 0;
unsigned char *msg;
buff[0] = GetDataFixedHead(MQTT_TypeCONNECT,0,0,0);
buff[2] = 0x00;
buff[3] = 0x04;
buff[4] = 'M';
buff[5] = 'Q';
buff[6] = 'T';
buff[7] = 'T';
buff[8] = 0x04;//协议级别 Protocol Level
buff[9] = 0 | (MQTT_StaCleanSession << 1) | (MQTT_StaWillFlag << 1)
| (MQTT_StaWillQoS << 3) | (MQTT_StaWillRetain << 5)
| (MQTT_StaPasswordFlag << 6) |(MQTT_StaUserNameFlag << 7);//连接标志
buff[10] = MQTT_KeepAlive >> 8;
buff[11] = MQTT_KeepAlive;
len = strlen(MQTT_ClientIdentifier);
buff[12] = len >> 8;
buff[13] = len;
msg = MQTT_ClientIdentifier;
for(i = 0;i
{
buff[14+i] = msg[i];
}
lennum += len;
if(MQTT_StaWillFlag)
{
len = strlen(MQTT_WillTopic);
buff[13 + lennum + 1] = len >> 8;
buff[13 + lennum + 2] = len;
lennum += 2;
msg = MQTT_WillTopic;
for(i = 0;i
{
buff[14+lennum+i] = msg[i];
}
lennum += len;
len = strlen(MQTT_WillMessage);
buff[12] = len >> 8;
buff[13] = len;
lennum += 2;
msg = MQTT_WillMessage;
for(i = 0;i
{
buff[14+lennum+i] = msg[i];
}
lennum += len;
}
if(MQTT_StaUserNameFlag)
{
len = strlen(MQTT_UserName);
buff[13 + lennum + 1] = len >> 8;
buff[13 + lennum + 2] = len;
lennum += 2;
msg = MQTT_UserName;
for(i = 0;i
{
buff[14+lennum+i] = msg[i];
}
lennum += len;
}
if(MQTT_StaPasswordFlag)
{
len = strlen(MQTT_Password);
buff[13 + lennum + 1] = len >> 8;
buff[13 + lennum + 2] = len;
lennum += 2;
msg = MQTT_Password;
for(i = 0;i
{
buff[14+lennum+i] = msg[i];
}
lennum += len;
}
buff[1] = 13 + lennum - 1;
}
void GetDataDisConnet(unsigned char *buff)//获取断开连接的数据包
{
buff[0] = 0xe0;
buff[1] = 0;
}
void GetDataPINGREQ(unsigned char *buff)//心跳请求的数据包成功返回d0 00
{
buff[0] = 0xc0;
buff[1] = 0;
}
/*
成功返回90 0x 00 Num RequestedQoS
*/
void GetDataSUBSCRIBE(unsigned char *buff,const char *dat,unsigned int Num,unsigned char RequestedQoS)//订阅主题的数据包 Num:主题序号 RequestedQoS:服务质量要求0,1或2
{
unsigned int i,len = 0,lennum = 0;
buff[0] = 0x82;
len = strlen(dat);
buff[2] = Num>>8;
buff[3] = Num;
buff[4] = len>>8;
buff[5] = len;
for(i = 0;i
{
buff[6+i] = dat[i];
}
lennum = len;
buff[6 + lennum ] = RequestedQoS;
buff[1] = lennum + 5;
}
void GetDataPUBLISH(unsigned char *buff,unsigned char dup, unsigned char qos,unsigned char retain,const char *topic ,const char *msg)//获取发布消息的数据包
{
unsigned int i,len=0,lennum=0;
buff[0] = GetDataFixedHead(MQTT_TypePUBLISH,dup,qos,retain);
len = strlen(topic);
buff[2] = len>>8;
buff[3] = len;
for(i = 0;i
{
buff[4+i] = topic[i];
}
lennum = len;
len = strlen(msg);
for(i = 0;i
{
buff[4+i+lennum] = msg[i];
}
lennum += len;
buff[1] = lennum + 2;
}
最后
以上就是知性茉莉为你收集整理的stm32搭建mqtt_基于STM32实现MQTT的全部内容,希望文章能够帮你解决stm32搭建mqtt_基于STM32实现MQTT所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复