Esp8266 Advanced Peripheral Road 36 [articles] Lexin esp8266 chip SDK programming time driving chip ds1302, network time synchronization to a local, no longer afraid of breaking down the network can be synchronized time! (Incidental Demo)

Disclaimer: This article is a blogger and a half stars heart bent on a blood knock out original works, shall not be reproduced without the bloggers allowed, Thanks for your support. https://blog.csdn.net/xh870189248/article/details/88976216

  • This series of blog study by the non-official and a half stars heart painstaking written by force, just do personal technology exchange and sharing, without any commercial purposes. If wrong, please leave a message, I promptly changed.

1, Esp8266 the development environment to build, start a "hellow world" serial printer. 2, started using GPIO Esp8266 the button to light your "first lamp." 3, the use Esp8266 "software timers," 0.5 seconds flashing timing of the lighting an LED. 4, Esp8266 understanding of the PWM, the PWM control in more depth with a change in luminance of the LED. Native 5, Esp8266 the advanced use of Lexin SDK package Post and Get requests clouds, got the "weather forecast information." 6, Esp8266 of understanding SmartConfig and Airkiss a key distribution network, to 8266 with the Internet cloud. Wifi without having to write code in the firmware in the name. 7, Esp8266 of understanding softAP hot spot distribution network mode principle, imitation "witty cloud" to define your own distribution network hotspot mode protocol. 8, Esp8266 you're looking for 8266 as of UDP, TCP client or server roles communications, in which the. 9, Esp8266 advanced Road Part 1: [Part small combat] the Windows system set Mqtt local server 8266, an LED lamp lighting LAN. 10, Esp8266 advanced Road Section 2: [next small combat] the Windows system set Mqtt local server 8266, an LED lamp lighting LAN. 11, Esp8266 advanced The Way 3: 8266 intelligent access Ali lit a LED light, look Lynx wizard voice control does not come to the party! 12, Esp8266 advanced Way to 4: illustrations learning to build 8266MQTT Ali cloud host server, for mobile network remote control an LED. 13, Esp8266 advanced Way to 5:











8266 set to be a complete hands-on small cases, smartConfig + MQTT agreement easy remote control of an LED.
14, Esp8266 advanced Way to 6: FreeRTOS system esp8266 learning the correct posture ------ environment to build, burn.
15, Esp8266 advanced road of 7: Things esp8266 yet clear stream, 8266 aliyun internet access non Ali SDS intelligent service, lighting an LED.
16, Esp8266 advanced Road Chapter 8: esp8266 based graft Nonos infrared H1838, to achieve infrared remote distribution network, a remote control light.
17, Esp8266 advanced Way to 9: Fast power switching five esp8266 self-development (as a switch), no buttons to trigger the 8266 into a key distribution network mode. 18, Esp8266 advanced Way to 10: esp8266 based NONOS achieve remote OTA upgrade, wireless "hot fix" to upgrade the firmware. 19, Esp8266 advanced on the road 11 : esp8266 drive ds18b20, dht11 temperature and humidity sensors, temperature and humidity sensors to collect server. 20, Esp8266 advanced Way to 12 : in-depth study of esp now esp8266 mode, do a little imitation wit cloud gateway, for easy connection to each other without the need for interactive data communications network. 21, Esp8266 advanced Way to 13 : On esp8266 how to achieve maximum efficiency and front-end data exchange in the local LAN network conditions.




22, Esp8266 advanced Way to 14 : esp8266 project How to add third-party static library files, and how to customize a folder, makeFile talk about those things. .
23, Esp8266 advanced Way to 15 : coming wave of esp8266 based freeRtos own private system connects remote server implementation OTA upgrade, contact with the basic knowledge under lwip. .
24, Esp8266 advanced Way to 16 : penetration study under review esp8266 external spi chip 25q series, the familiar 8266 block of code in its distribution, and handy place images or other information.
25, Esp8266 advanced Way to 17 : deep chat under esp8266 Uart serial communications interrupt programming for the serial port driver you are ready to file NONOS version and RTOS systems.
26, Esp8266 advanced Way to 18 : RTOS analysis MQTT implementation process to achieve transplantation MQTT agreement esp8266 rtos real-time system, may reconnection.
27, Esp8266 advanced Way to 19 : stick with the pace of development esp8266 rtos SDK v3.0 version with VisualStudio Code, idf new framework, saving memory modular development.
28, Esp8266 advanced Way to 20 : teach you how to parse the ease of use cJson some json data and how to generate some json data Lexin esp8266.
29, Esp8266 advanced Way to 21: Millions of messages free of Lexin esp8266 direct mode using the TCP protocol access MQTT Ali cloud Things platform to support private servers more support aliyun rules engine.
30, Esp8266 advanced Way to 22 : Lexin esp8266 SDK programming using IIC bus driver 0.96-inch OLED screen, display the weather forecast information.
31, Esp8266 advanced Way to 23 : When esp8266 encountered Html, how the built-in web control equipment, built-in web pages to sort out the implementation process, and to achieve do not need a router mobile phones can also be controlled esp8266.
32, Esp8266 advanced Way to 24 : small talk brings security encryption methods HmacMD5 and practice on esp8266, security of information transmission process of maximum protection.
33, Esp8266 advanced The Way 25 : How to gracefully like Lexin original package logical thinking esp8266 bottom register, make your own static library library files, let people use a third party?
34, Esp8266 advanced on the road 26 : Lexin esp8266 NONOS SDK 3.0-based programming using the SPI driver chip eight digital Max7219, date information is displayed.
35, Esp8266 advanced Way to 27 : Lexin esp8266 chip With wit cloud platform to do a commercial RGB bulb colorful adjustable overall program projects a bright eye-catching tall yet.
36, Esp8266 advanced Way to 27 :Lexin esp8266 chip SDK programming time driving chip ds1302, network time synchronization to a local, no longer afraid of power failure can also synchronize time off the net!
37, Esp8266 learning rtos3.0 notes first one : recognize esp8266 Rtos 3.0 sdk engineering structures, how esp8266 close to esp-idf project, how to customize the header files compiled?
38, Esp8266 learning rtos3.0 notes of the two : you're looking at here are the basic peripheral functions, including Gpio, Pwm and Uart interface.
39, Esp8266 learning rtos3.0 first three notes : An article with you fixed up understanding and use of storage technology NVS, how to use NVS save integers, strings, arrays and structures.


  • A lot of people learn how to contact me with progress, the following notice and make a small dry goods free Share:

Fun esp8266with you fly, plus group paid QQgroup, to raise the threshold, do not like do not spray friends do not add: 434 878 850
esp8266 Free Source Code Learning Summary (continuously updated, welcomed the Star): https://github.com/xuhongv/StudyInEsp8266
esp32 free source Code learning summary (continuously updated, welcomed the Star): https://github.com/xuhongv/StudyInEsp32


I. Introduction.


  • Sedentary knock Code has been our programmers bad habits! His recent good acid sore back, have to pay attention to the lower body! If there is a time to be able to remind sedentary enough!
  • Recently I hear esp8266how synchronized network time to a local, even after power down or off the network, the local time is still in the running! The idea, at first I thought was RTC, but a Baidu esp8266 Rtc's algorithm is too complicated! Later, I thought about it before we use the C52drive ds1302, run time is very accurate, and add a button batteries, can run up a few years!
  • So, in search of information on the Internet! Transplant is very simple, with the sntpnetwork timing, soon somehow! Or to refer the blogger 's information!

Here Insert Picture Description


Second, prepare materials, and how about wiring.


2.1 selection;

      ds1302There are many models, I started using ds1302nthat hard unsuccessful! Later, we see a lot of bloggers two that ds1302nhave a problem? After I bought a new ds1302module, the ds1302ninsert try next! Actually it! This time suspected the circuit module itself is the problem, suspected to be crystal problem! I do not tested! In a word, good deep water some cheap products on Taobao!

  • A esp8266 module + ds1302 module!

      Here I provide, the link I recently bought the module ds1302! Point me in!

      Or you want to know the information under this module: Download


2.2 connection;


  • io port is analog! You can modify! Wiring of my code is shown below!
    Here Insert Picture Description

Third, local synchronized network time to think logically.


      Overall, we added a clock chip peripherals ds1302do time synchronization, in order to:

  • 1, saving esp8266internal resources, the use of iomouth on it!
  • 2, the clock is more accurate! Because it is a professional clock chip, it has the characteristics of discrimination leap year!
  • 3, even when disconnected from the network, or power down, still in the running time of the recording is not stopped!

      So, my programming ideas are as follows:

Here Insert Picture Description


Fourth, programming.


4.1 The main program

       实现这样的功能的难点在于如何巧妙使用 sntp 时间授权的时间戳,换为真实的年月日 时分秒,之后写入 ds1302! 这还不算什么。主要是 bcd算法,毕竟也很久没搞算法了!也是从网上拿到了 bcd算法文件,也放在我的这个工程里面去 了!

       具体的 sntp授权中心的网址,我很感谢群里面的一个人分享给我们的帖子:点我查看,这帖子提供了很多国内的 sntp网址,很好用!那么本博文就用了阿里巴巴提供的"time1.aliyun.com"

       具体的驱动ds1302的寄存器说明,各位可以去百度下就有了!

       下面是程序入口以及处理网络授时的代码!


LOCAL os_timer_t second_timer, getNetSyncTimer;
LOCAL unsigned char time_buf1[8] = { 20, 19, 3, 29, 16, 20, 00, 6 }; //20空年3月13日18时50分00秒6周
LOCAL unsigned char time_buf[8]; //空年月日时分秒周
LOCAL void ICACHE_FLASH_ATTR second_timer_Callback(void) {

    unsigned char i, tmp;
	time_buf[1] = DS1302_master_readByte(ds1302_year_add);  //年
	time_buf[2] = DS1302_master_readByte(ds1302_month_add);  //月
	time_buf[3] = DS1302_master_readByte(ds1302_date_add);  //日
	time_buf[4] = DS1302_master_readByte(ds1302_hr_add);  //时
	time_buf[5] = DS1302_master_readByte(ds1302_min_add);  //分
	time_buf[6] = (DS1302_master_readByte(ds1302_sec_add)) & 0x7F;  //秒
	time_buf[7] = DS1302_master_readByte(ds1302_day_add);  //周

	for (i = 0; i < 8; i++) {           //BCD处理
		tmp = time_buf[i] / 16;
		time_buf1[i] = time_buf[i] % 16;
		time_buf1[i] = time_buf1[i] + tmp * 10;
	}

	os_printf("20%x-%d-%d  %d:%d:%d \n", time_buf[1], time_buf[2], time_buf[3],
			time_buf1[4], time_buf1[5], time_buf1[6]);
}

LOCAL void ICACHE_FLASH_ATTR second_timer_init(void) {
	os_timer_disarm(&second_timer); //关闭second_timer
	os_timer_setfn(&second_timer, (os_timer_func_t *) second_timer_Callback,
	NULL); //设置定时器回调函数
	os_delay_us(60000); //延时等待稳定
	os_timer_arm(&second_timer, 1000, 1); //使能毫秒定时器
}

LOCAL void ICACHE_FLASH_ATTR SyncNetTimeCallBack(void) {

    //获取时间戳
	uint32 ts = sntp_get_current_timestamp();

	if (ts != 0) {
		os_timer_disarm(&getNetSyncTimer);
		os_delay_us(60000); //延时等待稳定
		os_delay_us(60000); //延时等待稳定
        //处理时间戳,返回具体的时间
		char *pDate = (void *) sntp_get_real_time(ts);
		data = sntp_get_time_change(pDate);
		os_printf("20%x_%x_%x_%x:%x:%x_%x\n",data.year,data.month,data.day,data.hour,data.minute,data.second,data.week);

		time_buf1[1] = data.year;
		time_buf1[2] = data.month;
		time_buf1[3] = data.day;
		time_buf1[4] = data.hour;
		time_buf1[5] = data.minute;
		time_buf1[6] = data.second;
        //写入ds1302
		DS1302_Clock_init(time_buf1);

		os_timer_arm(&second_timer, 1000, 1); //使能毫秒定时器
	}
}

void ICACHE_FLASH_ATTR wifi_event_handler_cb(System_Event_t *event) {

	if (event == NULL) {
		return;
	}

	switch (event->event) {

	case EVENT_STAMODE_GOT_IP:
		//开始网络授时
		sntp_setservername(0, "time1.aliyun.com");
		sntp_setservername(1, "time2.aliyun.com");
		sntp_setservername(2, "time3.aliyun.com"); // set sntp server after got ip address
		sntp_init();
		os_timer_disarm(&getNetSyncTimer);
		os_timer_setfn(&getNetSyncTimer, (os_timer_func_t *) SyncNetTimeCallBack,NULL);
		os_timer_arm(&getNetSyncTimer, 1000, 1);
		break;

	case EVENT_STAMODE_DISCONNECTED:
		wifi_station_connect();
		break;
	}
}



void ICACHE_FLASH_ATTR user_init(void) {


    //连接指定的路由器
	struct station_config stationConf;
	wifi_set_opmode_current(STATION_MODE);
	os_memset(&stationConf, 0, sizeof(struct station_config));
	os_sprintf(stationConf.ssid,  "HUAWEI-APPT");
	os_sprintf(stationConf.password,  "xlinyun#123456");
	wifi_station_set_config_current(&stationConf);
	wifi_set_event_handler_cb(wifi_event_handler_cb);
	wifi_station_connect();

    //初始化ds1302
	DS1302_master_gpio_init();

	//读取时间的定时器开始
	second_timer_init();

}


4.3 sntp授权得到的时间戳转为具体时间的封装分享;



  • 头文件:xSntp.h

/*
 * xSntp.h
 *
 *  Created on: 2019年4月2日
 *      Author: XuHongYss
 */

#ifndef APP_INCLUDE_XSNTP_H_
#define APP_INCLUDE_XSNTP_H_

//使用结构体变量保存转换后的数据
typedef  struct sntp_data{
	unsigned char  week;
	unsigned char  month;
	unsigned char  day;
	unsigned char  hour;
	unsigned char  minute;
	unsigned char  second;
	unsigned char  year;
} sntp_data;
sntp_data sntp_get_time_change(char *s);

#endif /* APP_INCLUDE_XSNTP_H_ */


  • 源文件:xSntp.c

/*
 * xSntp.c
 *
 *  Created on: 2019年4月2日
 *      Author: XuHongYss
 */

#include "xSntp.h"
#include "c_types.h"
#include "osapi.h"
//使用结构体变量作为函数的返回值
sntp_data ICACHE_FLASH_ATTR sntp_get_time_change(char *s) {
	s[24]='\0';//不要年份后面的数据
	sntp_data data;
	unsigned char status[7] = { 3, 3, 2, 2, 2, 2, 4 };
	unsigned char c[5], i = 0, j = 0;
	while (*s != '\0') {
		if (*s == ' ' || *s == ':') {
			s++;
			i++;
		} else {
			os_memset(c, '\0', 5); //赋初值
			for (j = 0; j < status[i]; j++) {
				c[j] = *s;
				s++;
			}
			switch (i) {
			case 0:
				if (strcmp(c, "Mon") == 0)
					data.week = 1; //星期一
				else if (strcmp(c, "Tue") == 0)
					data.week = 2; //星期二
				else if (strcmp(c, "Wed") == 0)
					data.week = 3; //星期三
				else if (strcmp(c, "Thu") == 0)
					data.week = 4; //星期四
				else if (strcmp(c, "Fri") == 0)
					data.week = 5; //星期五
				else if (strcmp(c, "Sat") == 0)
					data.week = 6; //星期六
				else if (strcmp(c, "Sun") == 0)
					data.week = 7; //星期天
				break;
			case 1:
				if (strcmp(c, "Jan") == 0)
					data.month = 1; //一月
				else if (strcmp(c, "Feb") == 0)
					data.month = 2; //二月
				else if (strcmp(c, "Mar") == 0)
					data.month = 3; //三月
				else if (strcmp(c, "Apr") == 0)
					data.month = 4; //四月
				else if (strcmp(c, "May") == 0)
					data.month = 5; //五月
				else if (strcmp(c, "Jun") == 0)
					data.month = 6; //六月
				else if (strcmp(c, "Jul") == 0)
					data.month = 7; //七月
				else if (strcmp(c, "Aug") == 0)
					data.month = 8; //八月
				else if (strcmp(c, "Sep") == 0)
					data.month = 9; //九月
				else if (strcmp(c, "Oct") == 0)
					data.month = 10; //十月
				else if (strcmp(c, "Nov") == 0)
					data.month = 11; //十一月
				else if (strcmp(c, "Dec") == 0)
					data.month = 12; //十二月

				break;
			case 2: //日
				data.day = (c[0] & 0x0f) << 4;
				data.day = data.day | (c[1] & 0x0f);
				break;
			case 3: //时
				data.hour = (c[0] & 0x0f) << 4;
				data.hour = data.hour | (c[1] & 0x0f);
				break;
			case 4: //分
				data.minute = (c[0] & 0x0f) << 4;
				data.minute = data.minute | (c[1] & 0x0f);
				break;
			case 5: //秒
				data.second = (c[0] & 0x0f) << 4;
				data.second = data.second | (c[1] & 0x0f);
				break;
			case 6: //年
				data.year = (c[2] & 0x0f) << 4;
				data.year = data.year | (c[3] & 0x0f);
				break;
			default:
				break;
			}
		}

	}
	return data;
}


五、其他;


  • 从下面的截图可以看到,相差时间一秒内,是非常准确的!如果配合驱动 OLED ,可以做一个小型的手表了!
  • 本工程链接下载可以在群里免费下载,或者点击下面链接!感谢大家查阅我的文章:https://download.csdn.net/download/xh870189248/11082744

  • 很多人怎么联系我一起学习进步,下面打个小小公告和干货无偿分享:

Fun esp8266with you fly, plus group paid QQgroup, to raise the threshold, do not like do not spray friends do not add: 434 878 850
esp8266 Free Source Code Learning Summary (continuously updated, welcomed the Star): https://github.com/xuhongv/StudyInEsp8266
esp32 free source Code learning summary (continuously updated, welcomed the Star): https://github.com/xuhongv/StudyInEsp32

Here Insert Picture Description

Guess you like

Origin blog.csdn.net/xh870189248/article/details/88976216