keil5 stm32f407ve芯片利用esp8266模块连接知心天气获取天气信息的代码
时间: 2023-06-13 21:05:20 浏览: 388
以下是一个基于Keil5和STM32F407VE芯片,使用ESP8266模块连接到知心天气API获取天气信息的代码示例:
```c
#include "stm32f4xx.h"
#include <string.h>
#include <stdio.h>
#define SSID "your_SSID" // WiFi名称
#define PASSWORD "your_PASSWORD" // WiFi密码
#define API_KEY "your_API_KEY" // 知心天气API Key
#define CITY_ID "your_CITY_ID" // 城市ID
#define ESP8266_USART USART3
char esp8266RxBuffer[256]; // 接收缓冲区
uint8_t esp8266RxIndex = 0;
void USART3_IRQHandler(void) {
if (USART_GetITStatus(ESP8266_USART, USART_IT_RXNE) != RESET) {
char ch = USART_ReceiveData(ESP8266_USART);
esp8266RxBuffer[esp8266RxIndex++] = ch;
if (ch == '\n' && esp8266RxIndex > 2 && esp8266RxBuffer[esp8266RxIndex - 2] == '\r') {
esp8266RxBuffer[esp8266RxIndex - 2] = '\0';
}
}
}
void ESP8266_SendCommand(char *command) {
esp8266RxIndex = 0;
USART_SendData(ESP8266_USART, '\r');
USART_SendData(ESP8266_USART, '\n');
while (USART_GetFlagStatus(ESP8266_USART, USART_FLAG_TXE) == RESET);
while (*command) {
USART_SendData(ESP8266_USART, *command++);
while (USART_GetFlagStatus(ESP8266_USART, USART_FLAG_TXE) == RESET);
}
USART_SendData(ESP8266_USART, '\r');
USART_SendData(ESP8266_USART, '\n');
while (USART_GetFlagStatus(ESP8266_USART, USART_FLAG_TXE) == RESET);
}
void ESP8266_WaitResponse(char *response, uint32_t timeout) {
uint32_t startTime = HAL_GetTick();
while (HAL_GetTick() - startTime < timeout) {
if (strstr(esp8266RxBuffer, response)) {
return;
}
}
}
void ESP8266_Init(void) {
USART_InitTypeDef USART_InitStruct;
GPIO_InitTypeDef GPIO_InitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOB, &GPIO_InitStruct);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_USART3);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_USART3);
USART_InitStruct.USART_BaudRate = 115200;
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStruct.USART_Parity = USART_Parity_No;
USART_InitStruct.USART_StopBits = USART_StopBits_1;
USART_InitStruct.USART_WordLength = USART_WordLength_8b;
USART_Init(ESP8266_USART, &USART_InitStruct);
NVIC_InitStruct.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
NVIC_Init(&NVIC_InitStruct);
USART_ITConfig(ESP8266_USART, USART_IT_RXNE, ENABLE);
USART_Cmd(ESP8266_USART, ENABLE);
}
void ESP8266_ConnectWiFi(void) {
ESP8266_SendCommand("AT+CWJAP=\"" SSID "\",\"" PASSWORD "\"");
ESP8266_WaitResponse("OK", 5000);
}
void ESP8266_GetWeather(char *weather) {
char command[128];
sprintf(command, "AT+HTTPGET=\"http://api.thinkpage.cn/v3/weather/now.json?key=%s&location=%s&language=zh-Hans&unit=c\"", API_KEY, CITY_ID);
ESP8266_SendCommand(command);
ESP8266_WaitResponse("HTTP/1.1 200 OK", 10000);
char *p = strstr(esp8266RxBuffer, "{\"results\":[{");
if (p) {
strcpy(weather, p);
}
}
int main(void) {
char weather[1024];
ESP8266_Init();
ESP8266_ConnectWiFi();
ESP8266_GetWeather(weather);
printf("Weather: %s\r\n", weather);
while (1);
}
```
需要注意的是,该示例代码中的`your_SSID`、`your_PASSWORD`、`your_API_KEY`和`your_CITY_ID`需要替换为你自己的信息。此外,该示例代码中使用的是串口3连接ESP8266模块,如果你的连接方式不同,需要相应地修改代码。
阅读全文