一、RTL8710主要AT指令
1、ATSR:模块重启
2、ATSE=1:开启回显
3、ATPW=1:station模式
4、ATPN=ssid,password,,:连接到AP
5、ATPK=1:设置自动接收
6、ATPC=0,v1.yiketianqi.com,80:与网站建立TCP连接
7、ATPT=125,1:GET /api?unescape=1&version=v61&appid=此处替换成自己的ID&appsecret=此处替换成自己的key HTTP/1.1\r\nHost: v1.yiketianqi.com:80\r\nConnection: close\r\n
向天气API网站发送调用API的url请求,以获取天气信息
二、PY32F003+RTL8710(AT) 实现
main.c
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "string.h"
#include "Lcd_Driver.h"
#include "LCD_calculate.h"
#include "rtl8710ATcmd.h"/* Private define ------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef UartHandle;
uint8_t aTxBuffer[12] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
uint8_t aRxBuffer[2048] = {0xff};
uint8_t recvByte;
int rx_count=0; //接收计数
uint8_t dataReceived=0;/* Private user code ---------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/void RTL8710_USART_Config(void)
{UartHandle.Instance = USART2; //AF9:PA0(TX),PA1(RX)UartHandle.Init.BaudRate = 38400;UartHandle.Init.WordLength = UART_WORDLENGTH_8B;UartHandle.Init.StopBits = UART_STOPBITS_1;UartHandle.Init.Parity = UART_PARITY_NONE;UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;UartHandle.Init.Mode = UART_MODE_TX_RX;if (HAL_UART_Init(&UartHandle) != HAL_OK){APP_ErrorHandler();}
}/**
*
*
*/
void RTL8710_UART_Send(uint8_t *str)
{uint8_t ch;while(*str!=0){ch=*str;HAL_UART_Transmit(&UartHandle, (uint8_t *)&ch, 1, 200);str++;}
}uint8_t* RTL8710_UART_Recieve()
{uint8_t ch;int i=0;if(HAL_UART_Receive(&UartHandle, (uint8_t *)&ch, 1, 1000)==HAL_OK){aRxBuffer[i++]=ch;}aRxBuffer[i]='\0';return aRxBuffer;
}/*处理接收buffer的信息
*/
void processRxBuffer(void)
{printf("接收到的信息:[%s]\r\n",aRxBuffer);extract_all_from_json(aRxBuffer);dataReceived=0;
}
/*** @brief 应用程序入口函数.* @retval int*/
int main(void)
{/* systick初始化 */HAL_Init();Lcd_Init();Lcd_Clear(RED); //清屏/* USART初始化 */RTL8710_USART_Config();HAL_Delay(1000); /* DEBUG USART初始化 */BSP_USART_Config();HAL_Delay(1000);/*通过中断方式接收数据*/if (HAL_UART_Receive_IT(&UartHandle, (uint8_t *)&recvByte, 1) != HAL_OK){APP_ErrorHandler();}RTL8710_UART_Send((uint8_t*)"ATSR\r\n"); //restartHAL_Delay(2000); printf("ATSR\r\n");Gui_DrawFont_GBK16(0,0,WHITE,RED,"ATSR");RTL8710_UART_Send((uint8_t*)"ATSE=1\r\n"); //0关闭回显HAL_Delay(2000); printf("ATSE=1\r\n");Gui_DrawFont_GBK16(0,0,WHITE,RED,"ATSE");RTL8710_UART_Send((uint8_t*)"ATPW=1\r\n"); //1:station ;2:apHAL_Delay(1000); printf("ATPW=1\r\n"); //stationGui_DrawFont_GBK16(0,0,WHITE,RED,"ATPW=1");RTL8710_UART_Send((uint8_t*)"ATPN=lion,sujingliang,,\r\n"); HAL_Delay(10000); printf("ATPN=\"ssid\",password,,\r\n"); //连接到wifi APGui_DrawFont_GBK16(0,0,WHITE,RED,"CONNECT AP");createTCPClient();RTL8710_UART_Send((uint8_t*)"ATPK=1\r\n");HAL_Delay(1000); printf("ATPK=1\r\n"); //建立自动接收Gui_DrawFont_GBK16(0,0,WHITE,RED,"ATPK=1");getDataFromApi();/*通过中断方式发送数据*/processRxBuffer();while (1){/* 延时500ms */HAL_Delay(500); }
}/*** @brief USART错误回调执行函数* @param huart:USART句柄* @retval 无*/
void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
{if((huart==&UartHandle)&&(huart->ErrorCode!=HAL_UART_ERROR_NONE)){printf("Uart Error, ErrorCode = %d\r\n", huart->ErrorCode);if(__HAL_UART_GET_FLAG(huart,UART_FLAG_ORE) != RESET) {__HAL_UART_CLEAR_OREFLAG(huart);while(HAL_UART_Receive_IT(&UartHandle, &recvByte, 1)!=HAL_OK){UartHandle.RxState = HAL_UART_STATE_READY;__HAL_UNLOCK(&UartHandle);}}}
}/*** @brief USART发送回调执行函数* @param huart:USART句柄* @retval 无*/
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *UartHandle)
{if (HAL_UART_Receive_IT(UartHandle, (uint8_t *)aRxBuffer, 12) != HAL_OK){APP_ErrorHandler();}
}/*** @brief USART接收回调执行函数* @param huart:USART句柄* @retval 无*/
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *Handle)
{/*通过中断方式接收数据*/if(Handle->Instance==USART2){printf("%c", recvByte);if(rx_count<2047){if(dataReceived==0){aRxBuffer[rx_count++]=recvByte; //rx_count>100丢弃}}if((0x0A==aRxBuffer[rx_count-1])&&(0X0D==aRxBuffer[rx_count-2])){aRxBuffer[rx_count]='\0';if(strstr((char*)aRxBuffer,"{\"cityid\":")){dataReceived=1;}rx_count=0;}while(HAL_UART_Receive_IT(&UartHandle, &recvByte, 1)!=HAL_OK){UartHandle.RxState = HAL_UART_STATE_READY;__HAL_UNLOCK(&UartHandle);}}
}/*** @brief 错误执行函数* @param 无* @retval 无*/
void APP_ErrorHandler(void)
{/* 无限循环 */while (1){}
}#ifdef USE_FULL_ASSERT
/*** @brief 输出产生断言错误的源文件名及行号* @param file:源文件名指针* @param line:发生断言错误的行号* @retval 无*/
void assert_failed(uint8_t *file, uint32_t line)
{/* 用户可以根据需要添加自己的打印信息,例如: printf("Wrong parameters value: file %s on line %d\r\n", file, line) *//* 无限循环 */while (1){}
}
#endif /* USE_FULL_ASSERT *//************************ (C) COPYRIGHT Puya *****END OF FILE******************/
#include <main.h>
#include <string.h>
#include "rtl8710ATcmd.h"
#include "Lcd_Driver.h"
#include "LCD_calculate.h"struct CITY_WEATHER{char date[11];char city[20];char wea[5];char tem[6];
}city_weather;extern void RTL8710_UART_Send(uint8_t *str);void createTCPClient(void)
{RTL8710_UART_Send((uint8_t*)"ATPC=0,v1.yiketianqi.com,80\r\n"); Gui_DrawFont_GBK16(0,0,WHITE,RED,"createTCPClient");HAL_Delay(2000);
}void getDataFromApi(void)
{RTL8710_UART_Send((uint8_t*)"ATPT=125,1:GET /api?unescape=1&version=v61&appid=17769781&appsecret=5IbudTJx HTTP/1.1\r\nHost: v1.yiketianqi.com:80\r\nConnection: close\r\n\r\n"); Gui_DrawFont_GBK16(0,0,WHITE,RED,"getDataFromApi");HAL_Delay(5000); //printf("ATPT=125,1:GET /api?unescape=1&version=v61&appid=17769781&appsecret=5IbudTJx HTTP/1.1\r\nHost: v1.yiketianqi.com:80\r\nConnection: close\r\n\r\n");
}void extract_item_from_json(const char *json_str,char *itemname,char *target)
{char *start = strstr(json_str, itemname); // 查找"cityEn":" char *end;size_t len=20;char item[20]; // +1 for null terminatorif (start != NULL) { start += strlen(itemname); // 跳过itemname end = strchr(start, '"'); // 查找下一个双引号 if (end != NULL) { // 提取 len = end - start; strncpy(item, start, len); item[len] = '\0'; // 添加字符串结束符 printf("Item: %s\n", item); strncpy(target, item, len);target[len] = '\0'; // 添加字符串结束符 } } else { printf("Item[%s] not found in the JSON string.\n",itemname); }
}void extract_all_from_json(const char *json_str)
{extract_item_from_json(json_str,"\"cityEn\":\"",city_weather.city);extract_item_from_json(json_str,"\"tem\":\"",city_weather.tem);extract_item_from_json(json_str,"\"date\":\"",city_weather.date);Lcd_Clear(BLACK); Gui_DrawFont_GBK16(0,0,WHITE,BLACK," Weather ");Gui_DrawFont_GBK16(2,20,BLUE,BLACK,"City:");Gui_DrawFont_GBK16(45,20,YELLOW,BLACK,city_weather.city);Gui_DrawFont_GBK16(2,40,BLUE,BLACK,"Temp:");Gui_DrawFont_GBK16(45,40,YELLOW,BLACK,city_weather.tem);Gui_DrawFont_GBK16(2,60,BLUE,BLACK,"Date:");Gui_DrawFont_GBK16(45,60,YELLOW,BLACK,city_weather.date);
}