-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathESP_timeHandling
More file actions
95 lines (73 loc) · 3.9 KB
/
ESP_timeHandling
File metadata and controls
95 lines (73 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
______ _____ _____ _______ _____ __ __ ______
| ____|/ ____| __ \ |__ __|_ _| \/ | ____|
| |__ | (___ | |__) | | | | | | \ / | |__
| __| \___ \| ___/ | | | | | |\/| | __|
| |____ ____) | | | | _| |_| | | | |____
|______|_____/|_| |_| |_____|_| |_|______| handling...
==============================================================
## There are basically two categories of time in the ESP system. They can be classified as :
1. Interval Time -> Time via which intervals can be measured.
This can be used to time events, or rather gap between events.
Basic timing delays fall into this category.
#Data Structure to handle interval time:
---------------------------------------
struct timeval tv; //This struct type stores the amount of time elapsed
//since system REBOOT in sec as well as usec form
gettimeofday(&tv); //gettimeofday() populates the struct timeval
#Using FREERTOS TICKS to determine interval:
--------------------------------------------
uint32_t tick_counter=xTaskGetTickCount(); //no. of ticks since REBOOT
/*TICK is a const accessed by portTICK_PERIOD_MS = 1msec*/
#Even more precise timing ~ usec interval:
------------------------------------------
>> Rare scenario
>> ESP has a 'ccount' reg --> stores machine cycles since REBOOT
>> uint32_t value = xthal_get_ccount();
2. Wall Clock -> > Real world time
> Can be used to synchonize sensor data across various IOT devices
> Can be synced to an sntp server
> SNTP server generally returns time in seconds elasped since EPOCH
i.e. Jan 1 1970.
> Various data structure are available to distribute this data into
readable format.
#Data Structure to handle:
--------------------------
time_t time1; //a typedef of a large integer type
struct tm tm1; //a structure which converts time_t ie. secs since Jan 1 1970
//to a readable format having year, month, day etc...
### CONNECT TO AN SNTP SERVER ###
---------------------------------
//we use the lpip libraries
void start_sntp()
{
ip_addr_t addr;
sntp_setoperatingmode(SNTP_OPMODE_POLL);
inet_pton(AF_INET,"/*DNS of sntp server*/",&addr);
sntp_setserver(0,&addr);
sntp_init();
}
#Retrieve data (secs) from 1970
-------------------------------
time_t time1;
time(&time1); //time() populates time1 variable with secs since Jan1 1970
#Once data is retrieved we could use various structs to display it
------------------------------------------------------------------
struct tm tm1; //struct to display time_t data in year,month,day etc...
gmtime_r(&time1,&tm1); //populates the structure with GM time
printf("%d\t%d\n",tm1.tm_year,tm1.tm_mon);//prints year and month from tm1
***IMPORTANT***
>> Year and month in tm1 struct will be since Jan 1 1970
>> Much better way is to dump the data of time1 into a formated string
using asctime() api
#To display local time we have to set the timezone via env variable:
---------------------------------------------------------------------
setenv("TZ","UTC-5:30",1); //timezone for India UTC-5:30
tzset();
#Once SNTP server has started and timezone has been set, we can use the APIs
---------------------------------------------------------------------------
/*Display GMT time in as a formated text string*/
char buffer[20]; //to store the formated time string
printf("Formated string \t%s\n",asctime_r(gmtime(&time1),buffer);
/*Display Local time (UTC-5:30)*/
char buffer[20];
printf("Formated string \t%s\n",asctime_r(localtime(&time1),buffer));