In the first part of this blog I had rebuilt and described the project of our customer Hans-Ulrich Küster with its large-format display of the time. In order to obtain a large display as possible, he had placed two MAX7219 8x32 Matrix Displays on top of each other. As a clock served a Real Time Clock RTC DS3231 I2C.
In this part I would like to show how I replaced the Micro Controller with Atmega 328P by the Wi-Fi-enabled Micro Controllers of Espressif. For this purpose, I first left the sketch largely unchanged, but the two interfaces SPI and I2C are each at other pins than on the Micro Controller Board with Atmega. So I had to adapt the circuit and the sketch.
For the ESP8266, I stood for several days before a seemingly insoluble problem, because the SPI interface indicated in the Pinout chart with MOSI at SD1, CS at CMD and SCLK at CLK just did not work. More on that later.
All the faster it went for the ESP32. Here is the pin assignment for the SPI interface:
And the connections for the RTC on the I2C interface:
// SDA to D21
// SCL to D22
From previous experiments I knew that the ESP32 does not know the variable LED_BUILTIN, so - after the corresponding error message - I added the following line at the beginning of the sketch
The correct time was known by the (Battery buffered) Real Time Clock from the previous trials, So I could upload the sketch with
Only for synchronization with the time of the PC the variable must be set to true for a short time:
bool syncOnFirstStart = true
Heureka. That was really quick.
Here the sketch for the ESP32 for Download
And now? Fix the problem with the ESP8266? Or first of all, download the time from a NTP server directly from the Internet?
I decided for the quick solution with an example sketch of the library Timelib.h. In the example-sketch Timentp_esp8266wifi.ino, I only needed insert my wireless SSID and password and the current time was displayed in the serial monitor.
The other problem had still not been solved by itself. Even to upload the sketch, I had to disconnect the plug connection CS to CMD, otherwise there was an error message when connecting. So Internet search for the SPI interface on the ESP8266. To my amazement, I saw an experimental setup with the analog-to-digital converter MCP3008, which I knew well from the Raspberry Pi. However, that was connected to the pins where in the Pinout diagram in front of the known abbreviations was an H, ie HSCLK = D5 = GPIO14, HMISO = D6 = GPIO12, HMOSI = D7 = GPIO13 and HCS = D8 = GPIO15. So I attached DIN, CS and CLK accordingly, adapted to the sketch, and behold: it worked.
Here are the changes to the sketch of Mr. Küster:
And here the whole sketch to Download.
Synchronization with NTP server
The idea for using the WLAN-enabled Micro Controller came to me because one has to upload the sketch twice to synchronize the Real Time Clock with the system time of the computer, once bool syncOnFirstStart = true; once with bool syncOnFirstStart = false; For improvement and because the significantly better Real Time Clock DS3231 does not show the exact time after several weeks, I wanted to combine the sketch of Mr. Küster with the big display with the example sketch for the NTP time.
Essentially, the sketch for the internet time is reduced only for the issue in the serial monitor and then inserted into the sketch of Mr. Küster. And then of course, the source for synchronization had to be changed. This happens in Line 159:
This one Download of the sketch for the ESP8266 with time synchronization from the NTP server. Please note that you must enter your "WLAN credentials", ie SSID (name) and password in lines 35 and 36. And the switching between summer time and winter time is done by commenting in the lines 40 or 41.
Have fun with reconstruction. If you have a stable Internet connection and good Wi-Fi reception in the bedroom, then you can omit the RTC and synchronize the time more frequently with the NTP server.