材料: STM32F103、ESP8266、串口 第三章介绍了利用串口调试助手+AT指令连接阿里云平台的,现在用代码去实现其功能,利用STM32的串口2跟WIFI模块进行数据通信即可。这里我实现的功能是:在阿里云平台添加一个bool开关的模型,发送开和关的命令去控制STM32上的LED灯的亮灭。创建阿里云平台的模型在第二章,可点击主页观看。 下面是STM32部分的代码: <---------------------wifi.h----------------------->
#ifndef __WIFI_H
#define __WIFI_H
#include "stm32f10x.h"
#include "usart.h"
#include "delay.h"
#include "string.h"
void WIFI_Init(void);
void WIFI_Rst(void);
void Setting_Connect_Work(char *type);
void Login_URL(void);
void WIFI_Connect(char *name,char *password);
void USER_Connect(void);
void Client_Connect(void);
void Connect_Aliyun_Server(void);
void Client_Subscribe(void);
void wait_OK(void);
#endif
<---------------------wifi.c----------------------->
#include "wifi.h"
void WIFI_Init(void)
{
WIFI_Rst();
Setting_Connect_Work("1");
wait_OK();
Login_URL();
wait_OK();
WIFI_Connect("\"YCF\"","\"ycf88888888\"");
wait_OK();
USER_Connect();
wait_OK();
Client_Connect();
wait_OK();
Connect_Aliyun_Server();
wait_OK();
Client_Subscribe();
wait_OK();
}
void WIFI_Rst(void)
{
USART1TxStr("重启模块...\r\n");
USART2TxStr("AT+RST\r\n");
Delay_Ms(1000);
Delay_Ms(1000);
Delay_Ms(1000);
Delay_Ms(1000);
Delay_Ms(1000);
Delay_Ms(1000);
CLR_Buf2();
Flag_usart2_receive_OK = 0;
}
void Setting_Connect_Work(char *type)
{
char wifi_mode_buf[13] = {"AT+CWMODE=x\r\n"};
wifi_mode_buf[10] = *type;
USART1TxStr("设置工作模式...\r\n");
USART2TxStr(wifi_mode_buf);
}
void Login_URL(void)
{
char login_url[100] = {"AT+CIPSNTPCFG=1,8,\"ntp1.aliyun.com\"\r\n"};
USART1TxStr("连接阿里云服务器...\r\n");
USART2TxStr(login_url);
}
void WIFI_Connect(char *name,char *password)
{
char wifi_connect_buf[100] = {"AT+CWJAP="};
strcat(wifi_connect_buf,name);
strcat(wifi_connect_buf,",");
strcat(wifi_connect_buf,password);
strcat(wifi_connect_buf,"\r\n");
USART1TxStr("连接已知WiFi...\r\n");
USART2TxStr(wifi_connect_buf);
}
void USER_Connect(void)
{
char user_connect_buf[200] = {"AT+MQTTUSERCFG=0,1,\"NULL\",\"C6T6&gavk88e3djY\",\"64678A30225AB875CA6A6C323DAA575A3B283AB7\",0,0,\"\"\r\n"};
USART1TxStr("用户设备连接...\r\n");
USART2TxStr(user_connect_buf);
}
void Client_Connect(void)
{
char client_connect_buf[100] = {"AT+MQTTCLIENTID=0,\"112233|securemode=3\\,signmethod=hmacsha1\\,timestamp=456|\"\r\n"};
USART1TxStr("连接客户端...\r\n");
USART2TxStr(client_connect_buf);
}
void Connect_Aliyun_Server(void)
{
char connect_server_buf[100] = {"AT+MQTTCONN=0,\"gavk88e3djY.iot-as-mqtt.cn-shanghai.aliyuncs.com\",1883,1\r\n"};
USART1TxStr("连接阿里云服务器...\r\n");
USART2TxStr(connect_server_buf);
}
void Client_Subscribe(void)
{
char client_subscribe_buf[100] = {"AT+MQTTSUB=0,\"/sys/gavk88e3djY/C6T6/thing/service/property/set\",1\r\n"};
USART1TxStr("客户端订阅消息...\r\n");
USART2TxStr(client_subscribe_buf);
}
void wait_OK(void)
{
while(!Flag_usart2_receive_OK);
Flag_usart2_receive_OK = 0;
CLR_Buf2();
}
<---------------------主函数部分----------------------->
#include <string.h>
#include "stm32f10x.h"
#include "led.h"
#include "delay.h"
#include "usart.h"
#include "wifi.h"
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
Usart1_Init(115200);
Usart2_Init(115200);
LED_Init();
Delay_Init();
USART1TxStr("this is wifi test!!!\r\n");
WIFI_Init();
while(1)
{
if(Flag_Usart2_Receive == 1)
{
Flag_Usart2_Receive = 0;
USART1TxStr(USART2_RX_BUF);
if(USART2_RX_BUF[156] == '1')
LED0_OFF;
if(USART2_RX_BUF[156] == '0')
LED0_ON;
CLR_Buf2();
}
Delay_Ms(1000);
}
}
<---------------------串口2中断接收处理----------------------->
void USART2_IRQHandler(void)
{
u8 Res;
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
Res =USART_ReceiveData(USART2);
USART2_RX_BUF[USART2_RX_STA]=Res;
if(USART2_RX_BUF[USART2_RX_STA] == 'K')
{
if(USART2_RX_BUF[USART2_RX_STA - 1] == 'O')
{
Flag_usart2_receive_OK = 1;
}
}
else
{
Flag_Usart2_Receive = 1;
}
USART2_RX_STA++;
if(USART2_RX_STA > (USART2_REC_LEN - 1))
USART2_RX_STA = 0;
}
}
实现效果如下:
|