//单片机头文件
#include "stm32f4xx.h"                  // Device header
#include "common.h"      
#include "LCD.h"  

//网络设备驱动
#include "esp8266.h"

//硬件驱动

#include "usart.h"
#include "net_time.h"
//C库
#include <string.h>
#include <stdio.h>

//时间端口号
#define TIME_PORTNUM			"80"
//时间服务器IP
#define TIME_SERVERIP			"api.k780.com"  

#define WIFItime_SSID 						"hzxy"								//	WIFI的名称 必须用2.4G的wifi不能用5G的，且不能用中文、空格
#define WIFItime_PSWD 						"123456789"				//	WIFI密码

#define ESP8266time_WIFI_INFO			"AT+CWJAP=\"" WIFItime_SSID "\",\"" WIFItime_PSWD "\"\r\n"
#define ESP8266time_ONENET_INFO		"AT+CIPSTART=\"TCP\",\"" TIME_SERVERIP "\"," TIME_PORTNUM "\r\n"

extern unsigned char esp8266_buf[128];
extern unsigned short esp8266_cnt , esp8266_cntPre ;
	
char oledB[100];
 nt_calendar_obj nwt;  //定义结构体变量
 present_time time_now;//当前时间
extern u8 ESP8266_INIT_OK;
void ESP8266_ExitUnvarnishSend ( void );
//获取北京时间
void esp8266_time_init()
{

	HAL_GPIO_WritePin(ESP8266_RST_GPIO_Port, ESP8266_RST_Pin, GPIO_PIN_RESET);
	HAL_Delay(250);
	HAL_GPIO_WritePin(ESP8266_RST_GPIO_Port, ESP8266_RST_Pin, GPIO_PIN_SET);
	HAL_Delay(500);
	LCD_Clear(WHITE);//清屏
	if(!ESP8266_INIT_OK){
		LCD_DisplayString_color(0,64,24,"Waiting For",RED,WHITE);
		LCD_DisplayString_color(0,90,24,"WiFi",RED,WHITE);
		LCD_DisplayString_color(0,120,24,"Connection",RED,WHITE);
	}
	ESP8266_Clear();
	
	printf("0. AT - 测试MCU-8266通讯\r\n");
	while(ESP8266_SendCmd("AT\r\n","OK"))
		HAL_Delay(500);
	
	printf("1. AT+RST - 软复位8266\r\n");
	ESP8266_SendCmd("AT+RST\r\n", "");
	HAL_Delay(500);

	printf("2. AT+CWMODE=1,1 - 设置8266工作模式为STA\r\n");
	while(ESP8266_SendCmd("AT+CWMODE=1\r\n", "OK"))
		HAL_Delay(500);

	
	printf("4. AT+CWJAP - 连接WIFI\r\n");
	while(ESP8266_SendCmd(ESP8266time_WIFI_INFO, "GOT IP"))
	HAL_Delay(500);
	
	
 printf("4. AT+CWJAP - 连接WIFI\r\n");
	while(ESP8266_SendCmd("AT+CIPMUX=0\r\n", "OK"))
	HAL_Delay(500);
	
	printf("5. AT+CIPSTART - 连接服务器\r\n");
	while(ESP8266_SendCmd(ESP8266time_ONENET_INFO, "OK"))
	HAL_Delay(500);
	
	printf("6.  ESP8266初始化成功\r\n");
}
void get_beijing_time(void)
{
  int i;
  char *cmd;
  esp8266_time_init();
	HAL_Delay(300);    
  ESP8266_Clear();
	LCD_Clear(WHITE);//清屏
	LCD_DisplayString_color(0,64,24,"Waiting For",RED,WHITE);
  LCD_DisplayString_color(0,90,24,"Network data  ",RED,WHITE);
	LCD_DisplayString_color(0,120,24,"Transfer",RED,WHITE);
	printf("7. AT+CIPMODE=1\r\n");
	while(ESP8266_SendCmd("AT+CIPMODE=1\r\n", "OK")) //传输模式为：透传	
	HAL_Delay(500);


	printf("8. T+CIPSEND\r\n");
	while(ESP8266_SendCmd("AT+CIPSEND\r\n", "OK")) //传输模式为：透传	
  HAL_Delay(500);
	
	printf("9. GET\r\n");	    
	cmd="GET http://api.k780.com:88/?app=life.time&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json\r\n";
  HAL_UART_Transmit(&huart2,(unsigned char *)cmd,strlen((const char *)cmd),100);

	HAL_Delay(20);
	ESP8266_Clear();
	HAL_Delay(2000);
	if(esp8266_cnt != 0)
    {	
	

	char *str_p;
	char str[20];//2022-06-12 17:55:39

	str_p = strstr(esp8266_buf,"datetime_1");
	str_p += 13;
	sscanf(str_p, "%[^\"]",str);
	sscanf(str, "%d-%d-%d %d:%d:%d",&nwt.year,&nwt.month,&nwt.day,&nwt.hour,&nwt.min,&nwt.sec);

		for(i=0;i<20;i++)
		{
			printf("%c",str[i]);
		}
		printf("\r\n");
		printf("%d-%d-%d %d:%d:%d\r\n",nwt.year,nwt.month,nwt.day,nwt.hour,nwt.min,nwt.sec);
	LCD_Clear(WHITE);//清屏
	sprintf(oledB,"%d-%d-%d %d:%d:%d",nwt.year,nwt.month,nwt.day,nwt.hour,nwt.min,nwt.sec);
	Gui_DrawFont_GBK24(64,60,RED,WHITE,oledB);
	LCD_DisplayString_color(0,90,24,"Network data  ",RED,WHITE);
	LCD_DisplayString_color(0,120,24,"success",RED,WHITE);
	}
	
  ESP8266_ExitUnvarnishSend(); //关闭透传
	printf("9. AT+CIPCLOSE\r\n");
	while(ESP8266_SendCmd("AT+CIPCLOSE\r\n", "OK"))   //关闭连接
  printf("get——time success\r\n"); 

}

/*
 * 函数名：ESP8266_ExitUnvarnishSend
 * 描述  ：配置WF-ESP8266模块退出透传模式
 * 输入  ：无
 * 返回  : 无
 * 调用  ：被外部调用
 */
void ESP8266_ExitUnvarnishSend ( void )
{
	HAL_Delay ( 1000 );
	aRxBuffer2[0]='+';
	HAL_UART_Transmit(&huart2,aRxBuffer2,1,100);
	HAL_UART_Transmit(&huart2,aRxBuffer2,1,100);
	HAL_UART_Transmit(&huart2,aRxBuffer2,1,100);
	HAL_Delay ( 500 ); 
	
}