Difference: RealTimeClockAndDataLogging (1 vs. 10)

Revision 102020-08-09 - UliRaich

Line: 1 to 1
 
META TOPICPARENT name="Exercises"

Exercise 8: RTC and data logging

Introduction

Line: 78 to 78
  MPY epoch is 2000-01-01 instead of unix epoch, which is 1970-01-01.
Added:
>
>
The exercise sheet in odt format:

https://iotworkshop.africa/pub/IoT_Course_English/RealTimeClockAndDataLogging/exercise_8.odt

 -- Uli Raich - 2020-05-06

Comments

Line: 89 to 93
 
META FILEATTACHMENT attachment="dayOfWeek.py.txt" attr="" comment="" date="1590400839" name="dayOfWeek.py.txt" path="dayOfWeek.py.txt" size="1738" user="UliRaich" version="1"
META FILEATTACHMENT attachment="esp32RTC.py.txt" attr="" comment="" date="1590400839" name="esp32RTC.py.txt" path="esp32RTC.py.txt" size="4602" user="UliRaich" version="1"
META FILEATTACHMENT attachment="rtc-1.png" attr="" comment="" date="1590428864" name="rtc-1.png" path="rtc-1.png" size="115573" user="UliRaich" version="1"
Added:
>
>
META FILEATTACHMENT attachment="exercise_8.odt" attr="" comment="" date="1596963676" name="exercise_8.odt" path="exercise_8.odt" size="184324" user="UliRaich" version="1"

Revision 92020-07-26 - UliRaich

Line: 1 to 1
 
META TOPICPARENT name="Exercises"
Changed:
<
<

Exercise 9: RTC and data logging

>
>

Exercise 8: 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 an 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.

Revision 82020-05-27 - UliRaich

Line: 1 to 1
 
META TOPICPARENT name="Exercises"

Exercise 9: RTC and data logging

Introduction

Line: 74 to 74
  Concerning the time stamp there is a difference between MicroPython and CPython: MicroPython starts counting at 2000-01-01 while CPython uses Unix epoch, which starts at 1970-01-01.
Changed:
<
<
timeStampCPython = timeStampMicroPython + time.UNIX_TIME which is the constant 946681200
>
>
timeStampCPython = timeStampMicroPython + time.UNIX_TIME which is the constant 946677600
  MPY epoch is 2000-01-01 instead of unix epoch, which is 1970-01-01.

Revision 72020-05-26 - UliRaich

Line: 1 to 1
 
META TOPICPARENT name="Exercises"

Exercise 9: RTC and data logging

Introduction

Line: 56 to 56
 Write a module with 2 functions synchronizing the clocks:
  • synchronize the ds1307 clock with the ESP32RTC time
  • synchronize the ESP32RTC with the ds3017 time
Added:
>
>

Exercise 5: Mount the SD card

Write a script to mount the SD card on the RTC and data logger module on "/sd". Create a directory /sd/data and check that is has been created as expected.

Exercise 6: Data logging

Write a script that

  • sets up the ESP32 RTC using ntptime
  • reads temperature and humidity values from the SHT30 every 5 s
  • write the time stamp and temperature and humidity values for the file /sd/data/sht30Data.txt
  • transfer the data to the PC and analyze them
The binary of MicroPython contains an ftp server named uftpd. You can start it by simply importing the module:

import uftpd

After that you can transfer the file with ftp to your PC

Concerning the time stamp there is a difference between MicroPython and CPython: MicroPython starts counting at 2000-01-01 while CPython uses Unix epoch, which starts at 1970-01-01.

timeStampCPython = timeStampMicroPython + time.UNIX_TIME which is the constant 946681200

MPY epoch is 2000-01-01 instead of unix epoch, which is 1970-01-01.

  -- Uli Raich - 2020-05-06

Revision 62020-05-25 - UliRaich

Line: 1 to 1
 
META TOPICPARENT name="Exercises"

Exercise 9: RTC and data logging

Introduction

Changed:
<
<
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.
>
>
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 an 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.
 
Changed:
<
<
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.
>
>
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 to the current time read from the Internet

The module ntptime allows you to get the current UTC time from the NTP server. It's function settime() sets the time of the ESP32 real time clock. Write a module that connects to the Internet through Wifi and that sets up the ESP32 with the current UTC time

Line: 13 to 13
 Check with the ESP32 RTC module that the time was correctly set.

Exercise 2: Set the ESP32 RTC manually

Changed:
<
<
If you to not have Internet access you can still set up the ESP32 RTC manually.
>
>
If you do not have Internet access you can still set up the ESP32 RTC manually.
 
Changed:
<
<
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.
>
>
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 user input.
  Be careful about the parameters passed into rtc.datetime((currentTime)). currentTime is a Python tuple with the following members: yyyy mm dd ww hh mm ss us where
  • yyyy: 4 digits for the year
Line: 28 to 28
 
  • 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
https://artofmemory.com/blog/how-to-calculate-the-day-of-the-week-4203.html
Changed:
<
<
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
    It the calls parse, another function of the esp32RTC module checking user input and parsing it.
    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"
>
>
In my solution 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
    It then calls parse, another function of the esp32RTC module checking user input and parsing it.
    rtcSetUserTime finally creates a machine.RTC instance and sets the RTC with rtc.datetime()
  • rtcSetTime(year,month,day,hours,minutes,secs): takes the parameters year,month,day ... and tries to set the RTC time without any checks
  • rtcGetTime(): returns the current time as a tuple (yyyy,mm,dd,hh,mm,ss)
  • dayOfWeek(year,month,day): returns the code for the weekday (0=sun,6=sat)
  • dayOfWeekString(dayCode); returns a humanly readable string to the day of the week: "sun"..."sat"
  Here is a screen dumps showing a session controlling the ESP32 RTC:
Line: 42 to 42
  The disadvantage of getting the current time from the ESP32 RTC is that time is lost when the ESP32 is powered off. The DS1307 RTC however is batttery backed and time is kept even when the rest of the system is shut down.
Added:
>
>
Here is a photo of the DS1307 RTC shield:

rtc-1.png

As you can see, it features the DS1307 RTC chip itself but also an SD card reader. This board can therefore be used to generate a time stamp of the measurement, which can be saved, together with the measurement data, on an SD card file. Since SD cards have very high capacity, data can be taken over many hours without filling the file system.

 Do the same thing as exercise 1 and 2 but use the DS1307 RTC instead of the ESP32 internal RTC.

You will need the DS1307 data sheet and the ds1307 driver found at github.

Line: 61 to 67
 
META FILEATTACHMENT attachment="cetTime.py.txt" attr="" comment="" date="1590400839" name="cetTime.py.txt" path="cetTime.py.txt" size="860" user="UliRaich" version="1"
META FILEATTACHMENT attachment="dayOfWeek.py.txt" attr="" comment="" date="1590400839" name="dayOfWeek.py.txt" path="dayOfWeek.py.txt" size="1738" user="UliRaich" version="1"
META FILEATTACHMENT attachment="esp32RTC.py.txt" attr="" comment="" date="1590400839" name="esp32RTC.py.txt" path="esp32RTC.py.txt" size="4602" user="UliRaich" version="1"
Added:
>
>
META FILEATTACHMENT attachment="rtc-1.png" attr="" comment="" date="1590428864" name="rtc-1.png" path="rtc-1.png" size="115573" user="UliRaich" version="1"

Revision 52020-05-25 - UliRaich

Line: 1 to 1
 
META TOPICPARENT name="Exercises"

Exercise 9: RTC and data logging

Introduction

Line: 38 to 38
 Here is a screen dumps showing a session controlling the ESP32 RTC:

esp32RTC.png

Changed:
<
<

Exercise 3: Set the DS1307 RTC

>
>

Exercise 3: Set and read the time on the DS1307 I2C Real Time Clock

  The disadvantage of getting the current time from the ESP32 RTC is that time is lost when the ESP32 is powered off. The DS1307 RTC however is batttery backed and time is kept even when the rest of the system is shut down.
Changed:
<
<
Do the same thing with the DS1307 RTC
>
>
Do the same thing as exercise 1 and 2 but use the DS1307 RTC instead of the ESP32 internal RTC.

You will need the DS1307 data sheet and the ds1307 driver found at github.

Exercise 4: Synchronize the clocks

Write a module with 2 functions synchronizing the clocks:

  • synchronize the ds1307 clock with the ESP32RTC time
  • synchronize the ESP32RTC with the ds3017 time
  -- Uli Raich - 2020-05-06

Revision 42020-05-25 - UliRaich

Line: 1 to 1
 
META TOPICPARENT name="Exercises"

Exercise 9: RTC and data logging

Introduction

Line: 51 to 51
 
<--/commentPlugin-->

META FILEATTACHMENT attachment="esp32RTC.png" attr="" comment="" date="1588795317" name="esp32RTC.png" path="esp32RTC.png" size="44535" user="UliRaich" version="1"
Added:
>
>
META FILEATTACHMENT attachment="cetTime.py.txt" attr="" comment="" date="1590400839" name="cetTime.py.txt" path="cetTime.py.txt" size="860" user="UliRaich" version="1"
META FILEATTACHMENT attachment="dayOfWeek.py.txt" attr="" comment="" date="1590400839" name="dayOfWeek.py.txt" path="dayOfWeek.py.txt" size="1738" user="UliRaich" version="1"
META FILEATTACHMENT attachment="esp32RTC.py.txt" attr="" comment="" date="1590400839" name="esp32RTC.py.txt" path="esp32RTC.py.txt" size="4602" user="UliRaich" version="1"

Revision 32020-05-07 - UliRaich

Line: 1 to 1
 
META TOPICPARENT name="Exercises"

Exercise 9: RTC and data logging

Introduction

Line: 6 to 6
 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.

Changed:
<
<

Exercise 1: Set the ESP32 RTC

>
>

Exercise 1: Set the ESP32 to the current time read from the Internet

 
Changed:
<
<
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 module ntptime allows you to get the current UTC time from the NTP server. It's function settime() sets the time of the ESP32 real time clock. Write a module that connects to the Internet through Wifi and that sets up the ESP32 with the current UTC time

Check with the ESP32 RTC module that the time was correctly set.

Exercise 2: Set the ESP32 RTC manually

If you to not have Internet access you can still set up the ESP32 RTC manually.

 
Changed:
<
<
The driver for the ESP32 RTC is described in https://docs.micropython.org/en/latest/esp32/quickref.html#real-time-clock-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.
 
Changed:
<
<
Be careful about the parameters passed into rtc.datetime. These are yyyy mm dd ww hh mm ss us where
>
>
Be careful about the parameters passed into rtc.datetime((currentTime)). currentTime is a Python tuple with the following members: yyyy mm dd ww hh mm ss us where
 
  • yyyy: 4 digits for the year
  • mm: 2 digits for the month
  • dd: 2 digits for the day
Line: 33 to 38
 Here is a screen dumps showing a session controlling the ESP32 RTC:

esp32RTC.png

Changed:
<
<

Exercise 2: Set the DS1307 RTC

>
>

Exercise 3: Set the DS1307 RTC

 
Changed:
<
<
Do the same thing with the DS1307 RTC

Exercise 3: Get the time from the Internet and setup the RTC

>
>
The disadvantage of getting the current time from the ESP32 RTC is that time is lost when the ESP32 is powered off. The DS1307 RTC however is batttery backed and time is kept even when the rest of the system is shut down.
 
Changed:
<
<
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.
>
>
Do the same thing with the DS1307 RTC
  -- Uli Raich - 2020-05-06

Revision 22020-05-06 - UliRaich

Line: 1 to 1
 
META TOPICPARENT name="Exercises"

Exercise 9: RTC and data logging

Introduction

Line: 8 to 8
 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

Changed:
<
<
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 late and print it out.
>
>
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.

Be careful about the parameters passed into rtc.datetime. These are yyyy mm dd ww hh mm ss us 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
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
    It the calls parse, another function of the esp32RTC module checking user input and parsing it.
    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:

esp32RTC.png

 

Exercise 2: Set the DS1307 RTC

Do the same thing with the DS1307 RTC

Line: 21 to 45
 

Comments

<--/commentPlugin-->
Added:
>
>
META FILEATTACHMENT attachment="esp32RTC.png" attr="" comment="" date="1588795317" name="esp32RTC.png" path="esp32RTC.png" size="44535" user="UliRaich" version="1"

Revision 12020-05-06 - UliRaich

Line: 1 to 1
Added:
>
>
META TOPICPARENT name="Exercises"

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 late and print it out.

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.

-- Uli Raich - 2020-05-06

Comments

<--/commentPlugin-->
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 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