我是靠谱客的博主 慈祥服饰,这篇文章主要介绍NBIOT专栏之BC28与STM32F103单片机串口通信连接阿里云系列文章目录前言正文,现在分享给大家,希望可以做个参考。

系列文章目录

创思通信BC28核心板+STM32F系列单片机开发记录博客
1.测试BC28模块
2.串口助手MQTT连接阿里云物联网平台接发数据


前言

前面记录了BC28使用串口调试助手连接阿里云的过程,最头疼的还是使用单片机连接阿里云,因为代码是真的让人头疼!还好,经过不断的找源码,测试代码,修改代码,终于搞好了!下面跟大家分享一下!

正文

1.串口初始化

BC28模块和单片机是通过串口发送AT指令的!所以需要进行串口初始化!我使用野火指南者的板子,这里我使用的是串口1进行串口打印,串口3进行单片机与BC28的通信!
这里主要说一下我的串口3的两个引脚是B10和B11,B10作为单片机的接收端连接BC28的发送端(Tx),B11作为单片机的发送端连接BC28的接收端(Rx),下面是我的串口3接收BC28模块返回来的数据,存放在 RxBuffer[]数组中。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
void USART3_IRQHandler(void) //串口3中断服务程序 { char Res; //接收模块返回来的数据 if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) { /接收模块返回来的数据 Res=USART_ReceiveData(USART3); RxBuffer[RxCounter++] =USART_ReceiveData(USART3); } }

2.打开BC28模块
向BC28发送AT指令,如果没有响应再打开BC28。

复制代码
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
void OPEN_BC28(void) { char *strx; printf("ATrn"); delay_ms(300); strx=strstr((const char*)RxBuffer,(const char*)"OK");//返回OK printf("ATrn"); delay_ms(300); strx=strstr((const char*)RxBuffer,(const char*)"OK");//返回OK if(strx==NULL) { PWRKEY=1;//拉低 delay_ms(300); delay_ms(300); delay_ms(300); delay_ms(300); PWRKEY=0;//拉高正常开机 } printf("ATrn"); delay_ms(300); strx=strstr((const char*)RxBuffer,(const char*)"OK");//返回OK if(strx==NULL)//如果设备休眠了,就复位模块 { RESET=1;//拉低 delay_ms(300); delay_ms(300); RESET=0;//复位模块 } printf("ATE0&Wrn"); //关闭回显 delay_ms(300); printf("AT+QMTDISC=0rn");//关闭连接 delay_ms(300); printf("AT+QMTCLOSE=0rn"); delay_ms(300); }

3.BC28初始化
BC28的初始化,也是单片机通过串口3向BC28发送初始化AT指令。
其中使用printf()函数发送指令,利用strstr函数将接收到的响应数据与正常应该返回的数据做比较,不一致返回null,程序就进入循环中发送命令,一直到和规定数据一致。

复制代码
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
//RxBuffer[255]存放单片机接收BC28发来的数据,缓冲区 extern unsigned char RxBuffer[255],RxCounter; //清空缓存区 void Clear_Buffer(void)//清空缓存 { u8 i; Uart1_SendStr(RxBuffer); for(i=0;i<100;i++) RxBuffer[i]=0;//缓存 RxCounter=0; } void BC28_Init(void) { printf("ATrn"); delay_ms(300); strx=strstr((const char*)RxBuffer,(const char*)"OK");//返回OK Clear_Buffer(); while(strx==NULL) { Clear_Buffer(); printf("ATrn"); delay_ms(300); strx=strstr((const char*)RxBuffer,(const char*)"OK");//返回OK } printf("AT+CFUN=1rn");//获取卡号,类似是否存在卡的意思,比较重要。 delay_ms(300); printf("AT+CIMIrn");//获取卡号,类似是否存在卡的意思,比较重要。 delay_ms(300); strx=strstr((const char*)RxBuffer,(const char*)"460");//返460,表明识别到卡了 Clear_Buffer(); while(strx==NULL) { Clear_Buffer(); printf("AT+CIMIrn");//获取卡号,类似是否存在卡的意思,比较重要。 delay_ms(300); strx=strstr((const char*)RxBuffer,(const char*)"460");//返回OK,说明卡是存在的 } printf("AT+CGATT=1rn");//激活网络,PDP delay_ms(300); strx=strstr((const char*)RxBuffer,(const char*)"OK");//返OK Clear_Buffer(); printf("AT+CGATT?rn");//查询激活状态 delay_ms(300); strx=strstr((const char*)RxBuffer,(const char*)"+CGATT:1");//返1 Uart1_SendStr((char*)RxBuffer); Clear_Buffer(); Uart1_SendStr("CGATT。。。。。。"); while(strx==NULL) { Clear_Buffer(); printf("AT+CGATT?rn");//获取激活状态 delay_ms(300); strx=strstr((const char*)RxBuffer,(const char*)"+CGATT:1");//返回1,表明注网成功 } printf("AT+CESQrn");//查看获取CSQ值 delay_ms(300); strx=strstr((const char*)RxBuffer,(const char*)"+CESQ");//返回CSQ if(strx) { BC26_Status.CSQ=(strx[7]-0x30)*10+(strx[8]-0x30);//获取CSQ if((BC26_Status.CSQ==99)||((strx[7]-0x30)==0))//说明扫网失败 { while(1) { BC26_Status.netstatus=0; Uart1_SendStr("信号搜索失败,请查看原因!rn"); RESET=1;//拉低 delay_ms(300); delay_ms(300); RESET=0;//复位模块 delay_ms(300);//没有信号就复位 } } else { BC26_Status.netstatus=1; } } Clear_Buffer(); }

4.连接阿里云物联网平台

(1)首先要宏定义阿里云平台的三元组信息。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#define ProductKey "a1tW6nsMu2w" //²úÆ·KEY #define DeviceName "BC28" // #define DeviceSecret "9ef288679f79069558bbf3d82da90931" // #define PubTopic "/sys/a1tW6nsMu2w/BC28/thing/event/property/post" #define SubTopic "/sys/a1tW6nsMu2w/BC28/thing/service/property/set"

(2)连接阿里云物联网平台

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void MQTT_Init(void) { printf("AT+QMTCFG="aliauth",0,"%s","%s","%s"rn",ProductKey,DeviceName,DeviceSecret); delay_ms(300); printf("AT+QMTOPEN=0,"139.196.135.135",1883rn");//通过TCP方式去连接MQTT阿里云服务器 delay_ms(300); strx=strstr((const char*)RxBuffer,(const char*)"+QMTOPEN: 0,0");//看下返回状态 while(strx==NULL) { strx=strstr((const char*)RxBuffer,(const char*)"+QMTOPEN: 0,0");//确认返回值正确 } Clear_Buffer(); printf("AT+QMTCONN=0,"client"rn");//去登录MQTT服务器,名称随意 delay_ms(300); strx=strstr((const char*)RxBuffer,(const char*)"+QMTCONN: 0,0,0");//看下返回状态 while(strx==NULL) { strx=strstr((const char*)RxBuffer,(const char*)"+QMTCONN: 0,0,0");//看下返回状态 } Clear_Buffer(); }

(3)向阿里云发送数据

复制代码
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
void aliyunMQTT_PUBdata(u8 temp,u8 humi) { u8 t_payload[200],len; printf("AT+QMTPUB=0,0,0,0,"%s"rn",PubTopic);//发布主题 delay_ms(300); len=Mqttaliyun_Savedata(t_payload); t_payload[len]=0; printf("%s",t_payload); while((USART3->SR&0X40)==0);//循环发送,直到发送完? USART3->DR = (u8) 0x1A; delay_ms(100); strx=strstr((const char*)RxBuffer,(const char*)"+QMTPUB: 0,0,0");//看下返回状态 while(strx==NULL) { strx=strstr((const char*)RxBuffer,(const char*)"+QMTPUB: 0,0,0");//看下返回状态 } Clear_Buffer(); } //生成JSON格式数据 u8 Mqttaliyun_Savedata(u8 *t_payload,) { int err; uint16_t pkt_id = 1; char led1status1,led1status2; char json[]="{params:{RoomHumidity:5.8}}"; char t_json[200]; int payload_len; unsigned short json_len; sprintf(t_json, json, temp, humi); payload_len = strlen(t_json)/sizeof(char); json_len = strlen(t_json)/sizeof(char); memcpy(t_payload, t_json, json_len); return json_len; }
整个工程的代码下载链接:[点击下载](https://download.csdn.net/download/JIANGYINGH/85203136)

最后

以上就是慈祥服饰最近收集整理的关于NBIOT专栏之BC28与STM32F103单片机串口通信连接阿里云系列文章目录前言正文的全部内容,更多相关NBIOT专栏之BC28与STM32F103单片机串口通信连接阿里云系列文章目录前言正文内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(66)

评论列表共有 0 条评论

立即
投稿
返回
顶部