一、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);
}