目录
前言:
一、串口程序的编写
1、串口重定向
2、回调函数
二、WiFi模块发送AT指令
esp01s.c
esp01s.h
三、数据处理
初始化
1、cjson的使用
2、字符串提取有用信息
3、转成标准时间
4.任务处理
前面讲解了使用AT指令获取网络时间与cjson的解析数据,本章综合将时间显示到屏幕
int fputc(int ch,FILE *f)
{
//采用轮询方式发送1字节数据,超时时间设置为无限等待
HAL_UART_Transmit(&huart1,(uint8_t *)&ch,1,HAL_MAX_DELAY);
return ch;
}
int fgetc(FILE *f)
{
uint8_t ch;
// 采用轮询方式接收 1字节数据,超时时间设置为无限等待
HAL_UART_Receive( &huart1,(uint8_t*)&ch,1, HAL_MAX_DELAY );
return ch;
}
uint8_t rx_dat;
char rxdata[256];
uint8_t rx_p = 0,rx_ppre = 0;
//串口中断接收数据
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if(huart->Instance == USART1)
{
if(rx_p >= sizeof(rxdata)) rx_p = 0; //防止串口被刷爆
rxdata[rx_p ++] = rx_dat;
HAL_UART_Receive_IT(&huart1,&rx_dat,1);
}
}
#include "esp01s.h"
#include "usart.h"
uint8_t rx_dat;
char rxdata[256];
uint8_t rx_p = 0,rx_ppre = 0;
unsigned char Time_buff[100];
int fputc(int ch,FILE *f)
{
//采用轮询方式发送1字节数据,超时时间设置为无限等待
HAL_UART_Transmit(&huart1,(uint8_t *)&ch,1,HAL_MAX_DELAY);
return ch;
}
int fgetc(FILE *f)
{
uint8_t ch;
// 采用轮询方式接收 1字节数据,超时时间设置为无限等待
HAL_UART_Receive( &huart1,(uint8_t*)&ch,1, HAL_MAX_DELAY );
return ch;
}
//串口中断接收数据
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if(huart->Instance == USART1)
{
if(rx_p >= sizeof(rxdata)) rx_p = 0; //防止串口被刷爆
rxdata[rx_p ++] = rx_dat;
HAL_UART_Receive_IT(&huart1,&rx_dat,1);
}
}
//清空缓存
void esp01s_clear(void)
{
memset(rxdata,0,sizeof(rxdata));
rx_p = 0;
}
//esp01s等待接收完成 0成功 1失败
int esp01s_waitrecive(void)
{
if(rx_p == 0) //如果接收计数为0 则说明没有处于接收数据中,所以直接跳出,结束函数
return 1;
if(rx_p == rx_ppre) //如果上一次的值和这次相同,则说明接收完毕
{
rx_p = 0; //清0接收计数
return 0; //返回接收完成标志
}
rx_ppre = rx_p; //置为相同
return 1; //返回接收未完成标志
}
//esp01s发送指令 0成功 1失败
int esp01s_sendcmd(char *cmd,char *res)
{
unsigned char timeout = 250;
printf(cmd);
while(timeout --)
{
if(esp01s_waitrecive() == 0) //如果收到数据
{
if(strstr(rxdata,res) != NULL) //如果检索到关键词
{
esp01s_clear();
return 0;
}
}
HAL_Delay(10);
}
return 1;
}
//esp01s发送数据
void esp01s_senddata(char *data)
{
esp01s_clear(); //清空接收缓存
if(!esp01s_sendcmd("AT+CIPSEND\r\n",">")) //收到‘>’时可以发送数据
//printf(data);
{
/*发送请求数据*/
printf(data); //发送设备连接请求数据
HAL_GPIO_WritePin(led_blue_GPIO_Port,led_blue_Pin,GPIO_PIN_SET);
}
}
unsigned char *ESP8266_GetIPD_GET(unsigned short timeOut, uint8_t *buff) //这里我用了一个全局变量将esp8266buf储存到这个全局变量里面
{
do
{
HAL_Delay(5);
}
while(timeOut--);
strcpy((char*)buff, (char*)rxdata);
return buff;
}
#ifndef __ESP01S_H
#define __ESP01S_H
#include "main.h"
#include "string.h"
#include "stdio.h"
extern uint8_t rx_dat;
extern char rxdata[256];
extern uint8_t rx_p,rx_ppre;
extern unsigned char Time_buff[100];
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart);
void esp01s_clear(void);
int esp01s_waitrecive(void);
int esp01s_sendcmd(char *cmd,char *res);
void esp01s_senddata(char *data);
unsigned char *ESP8266_GetIPD_GET(unsigned short timeOut, uint8_t *buff);
#endif
esp01s_clear();
while(esp01s_sendcmd("+++",""));
while(esp01s_sendcmd("AT\r\n", "OK"));
while(esp01s_sendcmd("AT+RST\r\n", "OK"));
while(esp01s_sendcmd("AT+CWMODE=1\r\n", "OK"));
while(esp01s_sendcmd("AT+CIPMUX=0\r\n", "OK"));
while(esp01s_sendcmd("AT+CWJAP=\"meng\",\"20010131\"\r\n", "WIFI GOT IP"));
// while(esp01s_sendcmd("AT+CIPSTART=\"TCP\",\"api.seniverse.com\",80\r\n","CONNECT"));
while(esp01s_sendcmd("AT+CIPSTART=\"TCP\",\"api.pinduoduo.com\",80\r\n","CONNECT"));
while(esp01s_sendcmd("AT+CIPMODE=1\r\n", "OK"));
void test1_jiexi()
{
//解析
// char jsondata[] = "{\"name\":\"menglingjun\",\"age\":21}";
cJSON *json = NULL,*json_name = NULL,*json_age = NULL;
json = cJSON_Parse((char *)te);
//解析失败
if(!json)
{
}
else
{
json_age = cJSON_GetObjectItem(json,"server_time");
char *print1 = cJSON_Print(json_age);
// lcd_showstr(5,0,(uint8 *)print1,WHITE);
char *timestamp_start = strstr(print1,"17"); //字符串处理
char timestamp[11];
strncpy(timestamp, timestamp_start, 10);
timestamp[10] = '\0';
int timestamp_cnt =atoi(timestamp);
// sprintf(text,"%d",timestamp_cnt);
// lcd_showstr(5,1,(uint8 *)text,WHITE);
struct tm *timeinfo = localtime(×tamp_cnt);
sprintf(text,"%d-%d-%d",timeinfo->tm_year + 1900,timeinfo->tm_mon + 1,timeinfo->tm_mday);
lcd_showstr(5,2,(uint8 *)text,WHITE);
sprintf(text,"%d:%d:%d",timeinfo->tm_hour+8,timeinfo->tm_min,timeinfo->tm_sec);
lcd_showstr(10,3,(uint8 *)text,WHITE);
free(print1);
// 类型是数字
// if(json_age->type == cJSON_Number)
// {
// // printf("age is %d\n",json_age->valueint);
// sprintf(text,"%ld",json_age->valueint);
// lcd_showstr(5,2,(uint8 *)text,WHITE);
// u8g2_DrawStr(&u8g2,20,20,text);
// }
// json_name = cJSON_GetObjectItem(json,"name");
// //类型是字符串
// if(json_name->type == cJSON_String)
// {
// //printf("name is %s\n",json_age->valuestring);
// lcd_showstr(0,1,(uint8 *)json_name->valuestring,WHITE);
// }
//释放内存
cJSON_Delete(json);
}
}
char *timestamp_start = strstr(print1,"17"); //字符串处理
char timestamp[11];
strncpy(timestamp, timestamp_start, 10);
timestamp[10] = '\0';
int timestamp_cnt =atoi(timestamp);
struct tm *timeinfo = localtime(×tamp_cnt);
sprintf(text,"%d-%d-%d",timeinfo->tm_year + 1900,timeinfo->tm_mon + 1,timeinfo->tm_mday);
lcd_showstr(5,2,(uint8 *)text,WHITE);
sprintf(text,"%d:%d:%d",timeinfo->tm_hour+8,timeinfo->tm_min,timeinfo->tm_sec);
lcd_showstr(10,3,(uint8 *)text,WHITE);
u8g2_ClearBuffer(&u8g2);
if(key[0].single_flag == 1)
{
key[0].single_flag = 0;
esp01s_senddata("GET http://api.pinduoduo.com/api/server/_stm\r\n");
// esp01s_senddata("GET https://api.seniverse.com/v3/weather/daily.json?key=S6H95GCCwULqmbSE8&location=weifang&language=en&unit=c&start=-1&days=5\r\n");
te = ESP8266_GetIPD_GET(200, Time_buff); //将串口数据取出来
esp01s_clear();//清除缓存数据
while(esp01s_sendcmd("+++", "")); /*退出透传模式,发送两次*/
}
// lcd_showstr(0,0,(uint8 *)rxdata,WHITE);
test1_jiexi();
u8g2_SendBuffer(&u8g2);
// lcd_clear(BLACK);