龙鳞铁碎牙 发表于 2025-7-25 09:48

【STM32U385RG 测评】——5.获取国内城市天气预报

本帖最后由 龙鳞铁碎牙 于 2025-7-25 10:20 编辑

#每日话题# #申请原创# 手头上有一块乐鑫科技的ESP8266,空置了好久了,上周收到STM32U385RG开发板,今天突然想起来这个ESP8266还有服役的机会,于是就出来了这篇文章获取国内城市天气预报。
下面是板子连接图如下:

按照上图连接好ESP8266和STM32U385RG开发板就可以。
下面开始进入正题。
1.创建cubemx工程

2.配置好时钟96MHZ

3.配置好串口,串口1用来调试,串口5用来和ESP8266通信

4.生成代码

5.打开KEIL工程

6.修改核心代码
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file         : main.c
* @brief          : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2025 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "adc.h"
#include "usart.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "ESP8266.h"
#include "cJSON.h"


/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
char TX_Data[]="GET https://api.seniverse.com/v3/weather/now.json?key=SfobYEEioM5L6dRce&location=shanghai&language=zh-Hans&unit=c\r\n";
char Data[]="AT+CIPSEND\r\n";

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
#define TEMP_AVG_SLOPE 0.0025//2.5mV/°C
#define TEMP_V25 0.76//752mV    Voltage at 30° C (± 1 ° C)
#define TEMP_VSENSE 3.3/4096

uint32_t temperature_adc_value = 0;
float temperature_in_c = 0.0;

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{
        temperature_adc_value = HAL_ADC_GetValue(hadc);
        temperature_in_c = ((TEMP_VSENSE * temperature_adc_value - TEMP_V25) / TEMP_AVG_SLOPE) + 25;
        HAL_ADC_Start_IT(&hadc1);
}

/* USER CODE END 0 */

/**
* @briefThe application entry point.
* @retval int
*/
int main(void)
{

/* USER CODE BEGIN 1 */
char *dataPtr = NULL;


unsigned char           wifi_flag = 0;
char                                  ipaddr;
char                                  gateway;
uint32_t                          latest_time = 0;
int                                  rv = -1;

/* USER CODE END 1 */

/* MCU Configuration--------------------------------------------------------*/

/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();

/* USER CODE BEGIN Init */

/* USER CODE END Init */

/* Configure the system clock */
SystemClock_Config();

/* USER CODE BEGIN SysInit */

/* USER CODE END SysInit */

/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_UART5_Init();
MX_USART1_UART_Init();
MX_ADC1_Init();
/* USER CODE BEGIN 2 */

printf("很高兴使用STM32U385RG开发板********\r\n");
printf("串口输出打印 波特率115200\r\n\r\n");



#if 1
        printf("\r\nESP8266城市天气预报 \r\n\r\n");

        ESP8266_Init();
        printf("INFO:EnableESP8266_Init successfully ==================初始化完成\r\n");

        while(ESP8266_SendCmd("AT+CIPSTART=\"TCP\",\"api.seniverse.com\",80\r\n", "CONNECT\r\n"));
       
        //while(ESP8266_SendCmd("AT+CIPSTART=\"TCP\",\"api.openweathermap.org\",80\r\n", "OK"));
       
        while(ESP8266_SendCmd("AT+CIPMODE=1\r\n", "OK"));

       

        ESP8266_SendData(TX_Data,strlen(TX_Data));



       
        HAL_Delay(500);

        printf("%d\r\n",esp8266_cnt);
        printf("%d\r\n",strlen(esp8266_buf));
       

        printf("原始天气数据\r\n");
        printf("*****************************************************************************\r\n");
        printf("%s\r\n",esp8266_buf);
        printf("*****************************************************************************\r\n");

       

        //***********************************JSON解析数据
        cJSON *json;
        cJSON *Array_obj1;
        cJSON *Array_obj2;
        cJSON *Array_obj3;
        cJSON *Array_obj4;
        cJSON *Array_obj5;
        cJSON *Array_obj6;
        cJSON *Array_obj7;
        cJSON *Array_obj8;
        cJSON *Array;
       
        json = cJSON_Parse((const char *) esp8266_buf );
        Array_obj1=cJSON_GetObjectItem(json,"results");
       
        int size=cJSON_GetArraySize(Array_obj1);
        //printf("\n%d",size);

        printf("\r\nCJSON解析后的数据\r\n");
        printf("=============================================================================\r\n");
        if(json == NULL)
        printf("\njson fmt error:%s\n.", cJSON_GetErrorPtr());
        else
        {
                Array=cJSON_GetArrayItem(Array_obj1,0);
                Array_obj2=cJSON_GetObjectItem(Array,"now");
                Array_obj3=cJSON_GetObjectItem(Array_obj2,"code");
                Array_obj4=cJSON_GetObjectItem(Array_obj2,"temperature");
                Array_obj5=cJSON_GetObjectItem(Array_obj2,"text");
                Array_obj6=cJSON_GetObjectItem(Array,"location");
                Array_obj7=cJSON_GetObjectItem(Array_obj6,"name");
                Array_obj8=cJSON_GetObjectItem(Array,"last_update");
                printf("城市: %s       ",Array_obj7->valuestring);
                printf("天气: %s       ",Array_obj5->valuestring);
                //printf("\r\n%s\r\n",Array_obj3->valuestring);                                 //天气代码
                printf("气温: %s度          ",Array_obj4->valuestring);                                //气温
                printf("时间: %s\r\n",Array_obj8->valuestring);
        printf("=============================================================================\r\n");
        }

        cJSON_Delete(json);//释放json



       
        ESP8266_Send("+++");


#endif


/* USER CODE END 2 */

/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}

/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

/** Enable Epod Booster
*/
if (HAL_RCCEx_EpodBoosterClkConfig(RCC_EPODBOOSTER_SOURCE_MSIS, RCC_EPODBOOSTER_DIV1) != HAL_OK)
{
    Error_Handler();
}
if (HAL_PWREx_EnableEpodBooster() != HAL_OK)
{
    Error_Handler();
}

/** Configure the main internal regulator output voltage
*/
if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
{
    Error_Handler();
}

/** Set Flash latency before increasing MSIS
*/
__HAL_FLASH_SET_LATENCY(FLASH_LATENCY_2);

/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSIS;
RCC_OscInitStruct.MSISState = RCC_MSI_ON;
RCC_OscInitStruct.MSISSource = RCC_MSI_RC0;
RCC_OscInitStruct.MSISDiv = RCC_MSI_DIV1;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
    Error_Handler();
}

/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
                              |RCC_CLOCKTYPE_PCLK3;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSIS;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB3CLKDivider = RCC_HCLK_DIV1;

if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
    Error_Handler();
}
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
* @briefThis function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @briefReports the name of the source file and the source line number
*         where the assert_param error has occurred.
* @paramfile: pointer to the source file name
* @paramline: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
   ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */




默认修改城市为广州
char TX_Data[]="GET https://api.seniverse.com/v3/weather/now.json?key=SfobYEEioM5L6dRce&location=guangzhou&language=zh-Hans&unit=c\r\n";


编译代码,烧录到STM32U385RG板子,


打开串口调试助手,查看打印数据

{"results":[{"location":{"id":"WS0E9D8WN298","name":"广州","country":"CN","path":"广州,广州,广东,中国","timezone":"Asia/Shanghai","timezone_offset":"+08:00"},"now":{"text":"晴","code":"0","temperature":"32"},"last_update":"2025-07-25T10:02:17+08:00"}]}

打开手机,查看广州天气预报

可以看到气温非常的准确


下面再来换一个城市,选择北京,

char TX_Data[]="GET https://api.seniverse.com/v3/weather/now.json?key=SfobYEEioM5L6dRce&location=beijing&language=zh-Hans&unit=c\r\n";


烧录到板子,查看串口助手

{"results":[{"location":{"id":"WX4FBXXFKE4F","name":"北京","country":"CN","path":"北京,北京,中国","timezone":"Asia/Shanghai","timezone_offset":"+08:00"},"now":{"text":"小雨","code":"13","temperature":"25"},"last_update":"2025-07-25T10:07:29+08:00"}]}
打开手机,查看北京天气预报

捉虫天师 发表于 2025-7-25 11:55

之间用ESP8266本身就可以实现所有功能啊。
页: [1]
查看完整版本: 【STM32U385RG 测评】——5.获取国内城市天气预报