0


ESP8266获取网络时间 实时时钟

程序现象

一、用串口调试助手调试

1.发送指令AT+RST重启模块使应用模式更改生效;

2.发送指令ATE0取消回显

3.使用串口发送指令AT+CWMODE=1设置模块Wi-Fi应用模式为Station模式;

4.发送指令AT+CWJAP ="ssid","pwd"连接AP;

5.发送指令AT+CIPMUX=0设置模块为单路连接模式,模块默认为单路连接模式;

6.发送指令AT+CIPSTART="TCP","api.k780.com",80 与服务器建立TCP连接 ;

7.发送指令AT+CIPMODE=1设置模块传输模式为透传模式;

8.发送指令AT+CIPSEND开启透传模式向服务器发送数据,模块收到此指令后先换行后返回“>”

9.剩下只需要发送 GET http://api.k780.com:88/?app=life.time&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json&HTTP/1.1\r\n,就能获取到年月时间。 (调用一个网络API接口,这个接口会返回标准网络时间)

可以看到发送完第九条指令后会收到如上图,所以整体思路:用串口进行通信,发送AT指令,获取到的数据存在Buf中,然后对Buf中的数据解析。获取timestamp后为时间戳,获取datetime_1后为日期时间。

USART串口模块

这一部分的初始化和之前一样,串口接收部分要进行判断如果接收到 '>' ,用Flag标记表示后面调用API后会收到时间戳和时间等数据。

  1. #include "stm32f10x.h" // Device header
  2. #include "MyUSART.h"
  3. #include "esp.h"
  4. #include <string.h>
  5. char Buf[512];
  6. unsigned char i;
  7. uint8_t Flag;
  8. void MyUSART_Init(void)
  9. {
  10. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 ,ENABLE);
  11. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA ,ENABLE);
  12. GPIO_InitTypeDef GPIO_InitStructure;
  13. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  14. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  15. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  16. GPIO_Init(GPIOA,&GPIO_InitStructure);
  17. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  18. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  19. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  20. GPIO_Init(GPIOA,&GPIO_InitStructure);
  21. USART_InitTypeDef USART_InitStructure;
  22. USART_InitStructure.USART_BaudRate = 115200;
  23. USART_InitStructure.USART_HardwareFlowControl =USART_HardwareFlowControl_None;
  24. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  25. USART_InitStructure.USART_Parity =USART_Parity_No ;
  26. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  27. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  28. USART_Init (USART1,&USART_InitStructure);
  29. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  30. NVIC_InitTypeDef NVIC_InitStructure;
  31. NVIC_InitStructure.NVIC_IRQChannel =USART1_IRQn;
  32. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  33. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  34. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  35. NVIC_Init(&NVIC_InitStructure);
  36. USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
  37. USART_Cmd(USART1,ENABLE);
  38. }
  39. void MyUSART_SendString(char* str)
  40. {
  41. uint8_t stri=0;
  42. while(str[stri] != '\0')
  43. USART_SendData (USART1,str[stri++]);
  44. }
  45. void USART1_IRQHandler()
  46. {
  47. if(USART_GetITStatus(USART1,USART_IT_RXNE))
  48. {
  49. Buf[i++]=USART_ReceiveData(USART1);
  50. if(Buf[i-1] == '>')
  51. {
  52. Flag=1;
  53. }
  54. else if((Buf[i-2]=='\r')&&(Buf[i-1]=='\n'))
  55. {
  56. Buf[i-2]='\0';
  57. i = 0;
  58. }
  59. USART_ClearITPendingBit(USART1,USART_IT_RXNE);
  60. }
  61. }

ESP8266WIFI模块

这一部分初始化操作发送AT指令,在主函数中根据返回的数字可以判断哪里有问题

Comman_Rec()这个函数用来处理Buf中接收到的数据。

  1. #include "stm32f10x.h" // Device header
  2. #include "MyUSART.h"
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include "Delay.h"
  6. #include "OLED.H"
  7. #include <stdlib.h>
  8. extern char Buf[512];
  9. const char* WIFI ="GT";
  10. const char* PASS="123456789";
  11. extern uint8_t Flag;
  12. uint32_t timestamp_cnt;
  13. char subdatetime_1[11];
  14. char subdatetime_2[8];
  15. uint32_t temp = 2;
  16. int fputc(int ch,FILE *f ) //printf重定向
  17. {
  18. USART_SendData(USART1,(uint8_t)ch);
  19. while(USART_GetFlagStatus (USART1,USART_FLAG_TC) == RESET);
  20. return ch;
  21. }
  22. char esp_Init(void)
  23. {
  24. memset(Buf,0,sizeof(Buf));
  25. printf("AT+RST\r\n");
  26. Delay_ms(2000);
  27. memset(Buf,0,sizeof(Buf));
  28. printf("ATE0\r\n"); //关闭回显memset(Buf,0,sizeof(Buf));
  29. Delay_ms(1000);
  30. if(strcmp(Buf,"OK")!=0 )
  31. return 1;
  32. memset(Buf,0,sizeof(Buf));
  33. printf("AT+CWMODE=1\r\n");
  34. Delay_ms(50);
  35. if(strcmp(Buf,"OK")!=0)
  36. return 2;
  37. memset(Buf,0,sizeof(Buf));
  38. printf("AT+CWJAP=\"%s\",\"%s\"\r\n",WIFI,PASS); //连接热点
  39. Delay_ms(1000);
  40. if(strcmp(Buf,"OK")!=0)
  41. return 3;
  42. memset(Buf,0,sizeof(Buf));
  43. printf("AT+CIPMUX=0\r\n");
  44. Delay_ms(500);
  45. if(strcmp(Buf,"OK")!=0)
  46. return 4;
  47. memset(Buf,0,sizeof(Buf));
  48. printf("AT+CIPSTART=\"TCP\",\"api.k780.com\",80\r\n");
  49. Delay_ms(500);
  50. if(strcmp(Buf,"OK")!=0)
  51. return 5;
  52. memset(Buf,0,sizeof(Buf));
  53. printf("AT+CIPMODE=1\r\n");
  54. Delay_ms(500);
  55. if(strcmp(Buf,"OK")!=0)
  56. return 6;
  57. memset(Buf,0,sizeof(Buf));
  58. printf("AT+CIPSEND\r\n");
  59. Delay_ms(500);
  60. if(Flag != 1)
  61. return 7;
  62. return 0;
  63. }
  64. void ESP_Pub(void) //当接收到'>'时
  65. {
  66. if(Flag == 1)
  67. {
  68. printf("GET http://api.k780.com:88/?app=life.time&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json&HTTP/1.1\r\n");
  69. Delay_ms(2000);
  70. }
  71. }
  72. void Comman_Rec(void)
  73. {
  74. char *timestamp_start = strstr(Buf,"timestamp"); // 定位到"timestamp"字段的起始位置
  75. if (timestamp_start != NULL)
  76. {
  77. timestamp_start += (strlen("timestamp")+3); // 跳过"timestamp"
  78. char timestamp[11];
  79. strncpy(timestamp, timestamp_start, 10); // 复制10个字符到timestamp数组
  80. timestamp[10] = '\0'; // 添加字符串结束符
  81. timestamp_cnt =atoi(timestamp); //转换为整数
  82. }
  83. char *datetime1_start = strstr(Buf,"datetime_1");
  84. if (datetime1_start != NULL)
  85. {
  86. datetime1_start += (strlen("datetime_1")+3);
  87. strncpy(subdatetime_1, datetime1_start, 10);
  88. subdatetime_1[10] = '\0';
  89. strncpy(subdatetime_2, datetime1_start+11, 7);
  90. subdatetime_2[7] = '\0';
  91. }
  92. }

主函数

  1. #include "stm32f10x.h" // Device header
  2. #include "OLED.H"
  3. #include <stdio.h>
  4. #include "Delay.h"
  5. #include "MyUSART.H"
  6. #include "esp.h"
  7. extern char Buf[512];
  8. extern uint8_t Flag;
  9. extern uint32_t timestamp_cnt;
  10. extern char subdatetime_1[11];
  11. extern char subdatetime_2[9];
  12. void Init(void)
  13. {
  14. uint8_t Judge=0;
  15. OLED_Init();
  16. OLED_ShowString(1,1,"Linking..");
  17. Delay_ms(100);
  18. MyUSART_Init();
  19. do
  20. {
  21. Judge = esp_Init();
  22. OLED_ShowString(1,1,"code: ");
  23. OLED_ShowNum(2,1,Judge,1);
  24. }while(Judge!=0);
  25. }
  26. int main(void)
  27. {
  28. Init();
  29. if(Flag==1)
  30. {
  31. OLED_ShowString(1,1,"SUCCESS");
  32. }
  33. else
  34. {
  35. OLED_ShowString(1,1,"FAIL");
  36. }
  37. while(1)
  38. {
  39. ESP_Pub();
  40. Comman_Rec();
  41. OLED_ShowNum(1,1,timestamp_cnt,10);
  42. OLED_ShowString(2,1,subdatetime_1);
  43. OLED_ShowString(3,1,subdatetime_2);
  44. }
  45. }

注意:这个代码存在的问题是比标准的北京时间慢两秒,因为每次调用网络API接口都会Delay2秒,所以可以只获取时间戳,加上两秒后再根据网上的代码将时间戳转换为具体的时间格式。还可以根据上一节实时时钟的代码实现掉电不丢失的功能。而且有时候显示一会会跳变一下,也有很多问题,我在网上没找到stm32库函数的这个功能实现,还没找到更好的思路。


本文转载自: https://blog.csdn.net/m0_70732442/article/details/132250642
版权归原作者 道不知。 所有, 如有侵权,请联系我们删除。

“ESP8266获取网络时间 实时时钟”的评论:

还没有评论