logo elektroda
logo elektroda
X
logo elektroda

Clock synchronizer with I2C bus with time downloaded from the Internet

Tytus Kosiarski 3690 23
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • Electronic circuit with ESP8266 module and ATtiny4313 microcontroller.

    Hello everyone.

    This time I would like to present the I2C clock synchronizer with time downloaded from the Internet. Using the previously presented clock on the Z8, I thought it would be worth extending it with this possibility, hence the presented circuit was created.

    The system itself is based on the ESP8266-01 module, which obtains the time from the NTP server after logging into the WiFi network. The module cooperates with the ATtiny4313 microcontroller, whose task is generally to be seen as an I2C slave with address 0x0A by the clock cooperating with this system, which is an I2C master. In addition to serving as an I2C slave device, the microcontroller also operates the ESP8266 module via UART and supports an optional LCD display as a monitor of data exchanged via I2C with the master. You may ask why I didn't implement the I2C slave function in the ESP itself? Well, I didn't succeed. Multiple attempts (also using external interrupts to which ESP can respond) did not bring the expected effect. Here: https://github.com/esp8266/Arduino/issues/5762
    there is even a discussion about it. So, in order to continue with this project, I decided to use an additional microcontroller. The microcontroller periodically sends data received from the I2C master to the WiFi module to log in to the WiFi network. Cyclic, because after turning on the power, the I2C master sends the SSID and password (both ending with CR and LF) needed to log in to the described synchronizer system once, and at that moment the WiFi network may not be available. After the ESP8266 module successfully establishes a connection to WiFi, the module periodically sends the received RSSI, time and date data to the microcontroller in the form of an 8-element int16 array. The ATtiny4313 microcontroller receives this data from the module, makes sure that the data is in the right order, and then transfers the ordered data to a buffer intended for reading by the I2C master. The optional display presents the SSID on the first line and the password received from the I2C master on the second line, and after establishing an Internet connection, displays the contents of the buffer for reading by the I2C master.
    The representation of data placed in the correct order in the buffer and ready to be read by the I2C master is as follows:

    I2C clock synchronizer circuit with LCD display and other components.

    Where:
    first element of this buffer: RSSI; a value less than 0 (0xFFC7 from LCD) means establishing an Internet connection; the lack of this connection is signaled by the value 0x001F
    second element of this buffer: seconds in BCD (0057 from LCD)
    third element of this buffer: minutes in BCD (0049 from LCD)
    fourth element of this buffer: hours in BCD (0018 from LCD)
    the fifth element of this buffer: day of the week; 1 - Monday, ..., 7 - Sunday (0001 from LCD)
    sixth element of this buffer: day of the month in BCD (0023 from LCD)
    seventh element of this buffer: month in BCD (0010 from LCD)
    eighth element of this buffer: year in BCD (2023 from LCD).

    The video on YT shows the synchronization of the clock, deliberately set to January 1, 2001, with the time downloaded from the Internet by the described module.

    Now a little about the preparations for this project. When I was thinking about this project, I first came across the website: https://randomnerdtutorials.com/esp8266-nodemcu-date-time-ntp-client-server-arduino/
    This is almost the finished product on which I based my sketch in the Arduino environment. Even this worked, but the inconvenience was the lack of automatic change of summer/winter time (despite the presence of the tm_isdst component in the tm structure, however, the value of this component did not change when switching from winter to summer time in spring). Moreover, the author asked us to add/subtract the number of hours expressed in seconds to the UTC time received by the module programmed with this example sketch, depending on the expected time zone. While looking for how to solve the above problems, I finally found the website: https://werner.rothschopf.net/202011_arduino_esp8266_ntp_en.htm
    and after matching this sketch to our time zone using #define MY_TZ, the program began to correctly display the time in Poland on the terminal. After that, it remained only to adapt the sketch to the needs of cooperation with the described system.

    A few more words about preparing the ESP8266 module itself to work with this system. Apart from adjusting the voltage levels on UART (module powered by 3.3V, ATTiny powered by 5V), which I implemented as described at http://mirekk36.blogspot.com/2015/05/konwerter-levelow-33v-ttl.html, the module itself must be programmed with the NTP_time_date.ino sketch. To do this you need:
    1. Disconnect power from the module.
    2. Short-circuit pins 2-1 with jumper JP1
    3. Connect e.g. the ZL4USB adapter to the J2 socket. The pin arrangement in the J2 socket is adapted to the adapter I have.
    4. Short pin 5 (GPIO0/Flash) of the ESP8266-01 module to ground using a microswitch.
    5. With pin 5 shorted to ground, supply power to the module
    6. You can now disconnect the connection from point 4. The module is now in firmware update mode.
    7. In the Arduino 1.8.5 environment (this is what I use), compile and upload the NTP_time_date.ino sketch to the module
    8. After uploading the sketch, disconnect and connect the power supply to the module again
    9. Open e.g. RealTerm and connect to the module, connection parameters 19200.8N1.
    10. Send the module the SSID name and password (both strings ending with CR LF, max. 14 characters in the string) for the WiFi network to which we want to connect. The characters of the sent SSID and password should be displayed in the appropriate lines on the connected LCD.
    11. After successful connection, the module should periodically send data to RealTerm, as in the screenshot. This data will also be presented on the connected LCD.

    Screenshot of Arduino IDE with NTP sketch code and RealTerm window displaying data.

    If you are unable to complete the above points 7...11 (the firmware in the module may be inappropriate), you should update the firmware (which works for it). For this purpose:
    1. Download the firmware with flasher from https://www.elektroda.pl/rtvforum/topic3844441.html.
    2. Follow steps 1...6 above to enter firmware update mode.
    3. Run the ESP8266Flasher.exe file
    4. In this program, in the Config tab, indicate the files eagle.app.v6.flash.bin and eagle.app.v6.irom0text.bin as in the screenshot:

    NodeMCU Firmware Programmer interface with two binary files to load.

    5. Then, in the Operation tab of this program, select the appropriate COM Port and press the Flash button.
    6. After successfully uploading the firmware, try again to upload the NTP_time_date.ino sketch according to the above points 1...11

    I wrote the program for ATtiny4313 in the Rowley Crossworks for AVR ver.1.4 IDE.

    To sum up: the clock with this synchronizer works well, at least I have a reference time according to which I can set other clocks in the house. I also had to equip the described system with:a 220uF capacitor connected to the 5V side (visible in one of the photos), because during startup it turned out that when the WiFi network was lost (e.g. when the router was turned off), the Z8681 clock microcontroller could hang. The reason was a drop in the 5V voltage supplying the clock. An example of the cooperation of the described system with the Z8681 clock is here:
    https://www.elektroda.pl/rtvforum/topic3795375.html

    Regards, KT

    Printed circuit board with a 220uF capacitor and mounting holes.
    I2C time synchronizer module with ESP8266-01 and LCD




    Cool? Ranking DIY
    Do you have a problem with Arduino? Ask question. Visit our forum Arduino.
    About Author
    Tytus Kosiarski
    Level 16  
    Offline 
    Tytus Kosiarski wrote 272 posts with rating 462, helped 10 times. Been with us since 2007 year.
  • ADVERTISEMENT
  • #2 20821788
    tos18
    Level 42  
    I think it would be possible to get rid of the attina. ESP01 can support I2C.
  • ADVERTISEMENT
  • #3 20821794
    szymon122
    Level 38  
    The title is a bit awkwardly written. This is not an I2C clock synchronizer because you do not synchronize the bus, but it is a clock synchronizer WITH an I2C INTERFACE :D

    At first I thought your clock based its time on the CLK line frequency..
  • #4 20821809
    Tytus Kosiarski
    Level 16  
    @tos18, I didn't manage to do it, as I already mentioned. If you could present a working sketch or at least a concept of how to do it, that would be great :) And it would definitely be used.

    @szymon122, I corrected it. I hope that now the thread title will be more realistic.
  • ADVERTISEMENT
  • #5 20821976
    acctr
    Level 38  
    Have you considered DCF-77? Then the Internet and ESP would be redundant.
    Tytus Kosiarski wrote:
    The reason was a drop in the 5V voltage supplying the clock.

    What is a dip? It reminds me of some military exercises abroad.
    Helpful post? Buy me a coffee.
  • #6 20822055
    tmf
    VIP Meritorious for electroda.pl
    acctr wrote:
    Have you considered DCF-77? Then the Internet and ESP would be redundant.

    DCF77 requires a large antenna (the ferrite one is a few cm long) and range. WiFi is now practically everywhere, and certainly at home.
    Tytus Kosiarski wrote:
    I had to equip the described system with a 220uF capacitor connected to the 5V side (visible in one of the photos), because during startup it turned out that when the WiFi network was lost (e.g. when the router was turned off), the clock microcontroller could hang. The reason was a drop in the 5V voltage supplying the clock.

    That's why you also have BOD and WD in the MCU. It's worth using.
  • #7 20822077
    khoam
    Level 42  
    Tytus Kosiarski wrote:
    I didn't manage to do it, as I already mentioned.

    Failed to run I2C in Slave mode on ESP8266? You should have asked on the forum. Examples from Arduino SDK ( Link ) are insufficient?
    This design looks as if a carriage was pulling a car.
  • ADVERTISEMENT
  • #8 20822558
    Tytus Kosiarski
    Level 16  
    acctr wrote:
    What is a dip? It reminds me of some military exercises abroad.


    There is something like this:
    https://pl.wikipedia.org/wiki/Zapad_napi%C4%99cia

    tmf wrote:
    That's why you also have BOD and WD in the MCU. It's worth using


    I know it's worth using, but Z8 doesn't have such inventions ;) These are not the times yet ;)

    khoam wrote:
    This design looks as if a carriage was pulling a car.


    Perhaps the important thing is that it works. However, the I2C slave examples you provided may be OK, but for the TWI peripheral in AVR. They do not work in ESP, which I personally checked. This was also confirmed on the website I provided in the post opening this thread, where there was a discussion on this topic. A quote from a discussion fragment from this page:

    " Hi, Is the problem solved?I have the same issue here. This driver works well between two ESP8266,But it does not work using one ESP8266 and arduino uno

    I have the same issue. I can get the ESP8266 to work as a master with my Mega2560, but when I flip the roles where the ESP8266 is in slave mode it doesn't appear to respond to either request or receive event. Tried setClock at 50000, 100000, 200000, and 400000. Also, on the same bus I have a PCA9685 that responds to the mega when it's the master with no problem. Using a 5v/3.3v level shifter between the two sides (also skipped that and ran the mega at 3.3v too with no luck).”
  • #9 20822598
    efi222
    Level 19  
    I confirm problems with the operation of the I2C bus on the ESP 8266.
    I once tried to connect two ESP8266 via I2C.
    Only when setting Wire.setClock(20000); somehow it worked...
  • #10 20822701
    tmf
    VIP Meritorious for electroda.pl
    Tytus Kosiarski wrote:
    I know it's worth using, but the Z8 doesn't have such inventions. These are not the times yet

    You wrote that you use ATtiny4313, which has these inventions.... BOD is especially important, because when the voltage increases slowly, the MCU may enter some undefined state from which it cannot recover on its own.
  • #11 20822709
    khoam
    Level 42  
    Tytus Kosiarski wrote:
    I also found confirmation on the website I provided in the opening post of this thread, where there was a discussion on this topic.

    There is also mentioned the alternative Brzo I2C library for ESP8266, which I used. It is more difficult to use, but sometimes it is worth using such solutions if the "popular" Arduin ones fail.

    Generally, I believe that such gateway solutions are easier to implement using ESP32-C3, which has two I2C hardware controllers. The cost will be around a dozen or so zlotys. This solution will be more reliable, it will be free of additional and unnecessary communication channels (in this case UART), which are not 100% reliable, and certainly not in the case of ESP8266. But if you're happy with your solution, that's the most important thing.

    efi222 wrote:
    I once tried to connect two ESP8266 via I2C.

    The ESP8266 does not have an I2C hardware controller, so I2C support is implemented in software. The quality of this communication depends on how it is implemented.
  • #12 20822874
    Tytus Kosiarski
    Level 16  
    khoam wrote:
    Generally, I believe that such gateway solutions are easier to implement using ESP32-C3, which has two I2C hardware controllers


    ABOUT! And that's great news. It is worth knowing about the existence of such a module. Then indeed, ATtiny would no longer be needed.

    There is something like this in Botland:

    https://botland.com.pl/plytki-zgodne-z-arduin...essif-esp32-c3-risc-v-wifi-bluetooth-dfrobot- dfr0868-6959420922178.html

    Added after 9 [minutes]:

    tmf wrote:
    You wrote that you use ATtiny4313, which has these inventions


    Yes, in the presented synchronizer system. And it works perfectly.

    The synchronizer system works with a clock based on the Z8 microcontroller. And this Z8 had a problem with freezing.
  • #13 20822989
    khoam
    Level 42  
    Tytus Kosiarski wrote:
    There is something like this in Botland:

    On Ali, similar ESP32-C3 MINI boards are priced at approximately PLN 15.
  • #14 20823026
    krzbor
    Level 27  
    khoam wrote:
    This solution will be more reliable, it will be devoid of additional and unnecessary communication channels (in this case UART), which are not 100% reliable, and certainly not in the case of ESP8266
    Have you had problems with UART on ESP8266? Unlike I2C, UART is hardware-based and has quite a large I/O buffer.
  • #15 20823167
    jacek24642
    Level 9  
    They are available on Ali, but delivery is expensive.
  • #16 20823196
    efi222
    Level 19  
    My friend @krzbor made me realize that in fact, I don't remember ever having a problem with communication. Very often, ESP8266 in my projects is used to generate a wireless GUI with data to other MCUs.
    ESP communicates with them via UART.
    This means that AVR tends to generate "bushes" with non-UART crystals.

    Added after 5 [minutes]:

    jacek24642 wrote:
    They are available on Ali, but delivery is expensive.

    PLN 4 for shipping, isn't that expensive?
  • #17 20823240
    jacek24642
    Level 9  
    Somehow I couldn't find one for PLN 4.
  • #18 20823289
    efi222
    Level 19  
    ESP32 development board with WiFi and Bluetooth, priced at 14.98 PLN.
    PLN 43 applies to purchases that will be shipped within 14 days. And this is the minimum purchase amount to deliver them within 14 days.
  • #19 20823356
    tmf
    VIP Meritorious for electroda.pl
    efi222 wrote:
    This means that AVR tends to generate "bushes" with non-UART crystals.

    It's not a question of AVR, but of mathematics. Calculate the error for a given quartz and baudrate and the puzzle is solved....
  • #20 20823469
    efi222
    Level 19  
    Of course. What I meant was that with e.g. AVR you need to match the quartz to the baudrate or vice versa. With ESP, we actually have no influence on the timing and this UART works as it should.
  • #21 20823558
    tmf
    VIP Meritorious for electroda.pl
    efi222 wrote:
    Of course. What I meant was that, for example, with AVR, you need to match the quartz to the baudrate or vice versa. With ESP, we actually have no influence on the timing and this UART works as it should.

    Or it doesn't work because it doesn't generate a specific baudrate, but only a close one... Therefore, it's better to agree on a baudrate that gives common divisors on both sides. In newer AVRs you also have fractional generators to improve the situation. This is for information purposes only, because it is best to remove the AVR from this system :) You just need to remember about the voltage converter if you have pull-ups to 5V on I2C.
  • #22 20823571
    gulson
    System Administrator
    @TYTUS... Kosiarski, thanks for presenting the solution, I would like to thank you for this presentation, if you write me a PM, I will send you a small gift :)
  • #23 20840938
    jarekgol
    Level 39  
    I will also add that the time can be taken from GPS and SIM800L modems, but I don't remember whether you need a SIM card for this.
  • #24 20840980
    tos18
    Level 42  
    jarekgol wrote:
    sim800L modems, but I don't remember if you need a sim card for it.

    Is needed.

Topic summary

The discussion revolves around the design of an I2C clock synchronizer utilizing the ESP8266-01 module to fetch time from an NTP server over WiFi. The system incorporates an ATtiny4313 microcontroller functioning as an I2C slave, while the ESP8266 operates via UART. Participants debate the feasibility of eliminating the ATtiny by enabling I2C support directly on the ESP8266, although challenges with I2C slave mode on the ESP8266 are noted. Alternatives such as DCF-77 for time synchronization are mentioned, but the consensus leans towards the convenience of WiFi. The conversation also highlights the potential of using the ESP32-C3, which has better I2C support, as a more reliable solution. Issues with UART communication and the importance of voltage compatibility in mixed-voltage systems are also discussed.
Summary generated by the language model.
ADVERTISEMENT