Tags:
view all tags
---+ Exercise 9: RTC and data logging ---++ Introduction The ESP32 has a real time clock implemented on chip. This will however only work as long as the ESP32 is powered. It may therefore be interesting to employ and external RTC like the DS1307, backed up with a battery, to keep time even when the ESP32 is powered off or in deep sleep mode. Getting the current (GMT) time from the internet is easy when using the ntptime module. We can make use of this to setup our RTC with the correct time. ---++ Exercise 1: Set the ESP32 RTC Ask the user for the current date and time and set the real time clock in the ESP32 correspondingly. Read back the time a little later and print it out. Use a parser similar to the one we wrote in session 1, exercise 2, for the calculator to check the user input. The driver for the ESP32 RTC is described in [[https://docs.micropython.org/en/latest/esp32/quickref.html#real-time-clock-rtc][https://docs.micropython.org/en/latest/esp32/quickref.html#real-time-clock-rtc.]] Be careful about the parameters passed into rtc.datetime. These are<i> yyyy mm dd ww hh mm ss us </i>where * yyyy: 4 digits for the year * mm: 2 digits for the month * dd: 2 digits for the day * ww: 2 digits for the day in the week. In fact these data do not seem to be used! * hh: the hour * mm: minutes * ss: seconds * us: microseconds (I always put these to 0) You can actually calculate the ww parameter: the day in the week. You find the algorithm in <br /> https://artofmemory.com/blog/how-to-calculate-the-day-of-the-week-4203.html In my solutions I wrote a module esp32RTC with the following functions: * rtcSetUserTime(): This asks the user for date and in in the form yyyy mm dd hh mm ss<br />It the calls parse, another function of the esp32RTC module checking user input and parsing it. <br />rtcSetUserTime finally create a machine.RTC instance and sets the RTC with rtc.datetime() * rtcSetTime(year,month,day,hours,minutes,secs) * rtcGetTime() returning the current time as a tuple (yyyy,mm,dd,hh,mm,ss) * dayOfWeek(year,month,day) returning the code for the weekday (0=sun,6=sat) * dayOfWeekString(dayCode) returning a humanly readable string to the day of the week: "sun"..."sat" Here is a screen dumps showing a session controlling the ESP32 RTC: <img alt="esp32RTC.png" height="350" src="%ATTACHURL%/esp32RTC.png" title="esp32RTC.png" width="923" /> ---++ Exercise 2: Set the DS1307 RTC Do the same thing with the DS1307 RTC ---++ Exercise 3: Get the time from the Internet and setup the RTC Write a function that connects your ESP32 to the Internet through !WiFi. Use the station interface to do so. Find out the IP address the ESP32 can be reached at. Setup system time and print it. -- %USERSIG{UliRaich - 2020-05-06}% ---++ Comments %COMMENT%
Attachments
Attachments
Topic attachments
I
Attachment
History
Action
Size
Date
Who
Comment
png
esp32RTC.png
r1
manage
43.5 K
2020-05-06 - 20:01
UliRaich
Edit
|
Attach
|
Watch
|
P
rint version
|
H
istory
:
r10
|
r4
<
r3
<
r2
<
r1
|
B
acklinks
|
V
iew topic
|
Raw edit
|
More topic actions...
Topic revision: r2 - 2020-05-06
-
UliRaich
Home
Site map
AFNOG web
Embedded_Systems web
IoT_Course_English web
IoT_Course_French web
Main web
Sandbox web
TWiki web
IoT_Course_English Web
Create New Topic
Index
Search
Changes
Notifications
RSS Feed
Statistics
Preferences
P
View
Raw View
Print version
Find backlinks
History
More topic actions
Edit
Raw edit
Attach file or image
Edit topic preference settings
Set new parent
More topic actions
Account
Log In
Register User
Edit
Attach
Copyright © 2008-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki?
Send feedback