ESP can't find RTC - sometimes!

I am tearing my hair out with a silly problem.

I have a NodeMCU ESP8266 connected to a 3231 RTC module. Most of the time, everything is fine, and when the system powers up the RTC is read without problem.

But, just occasionally, the system comes up but can't find the RTC - at least, it says that no RTC is connected. I can reboot, remove power, remove RTC module etc but the problem remains... and then it doesn't - everything works again. And continues to work when I reboot the system, power cycle etc.

There is no logic to this - I can recompile and reload the program and all is fine. Or I can reload the program and the problem occurs. Sometimes, if I remove the RTC, replace it and reboot, things work, but not every time.

I thought it might be something to do with timing, but the first thing the program does is to initialise the serial port with a 500mS delay, then starts Wire with another 500mS delay, so there is plenty of time for the I2C to get going.

Before posting a load of code, I'm wondering if anyone has come across similar problems and maybe has a solution?

I can suggest a solution. Don't use an RTC. With an esp, very often, no RTC is needed. As long as WiFi connection is available, the esp can get the time from a time server on the internet using Network Time Protocol (NTP) at startup. Then it can use is internal clock to maintain the time. It's internal clock is not perfectly accurate, but it can re-sync with the server, perhaps once per day.

Ah - not that easy, unfortunately! For most of the time the device will not be connected to the internet. The ESP is used a) to provide the processing for the application and b) to allow occasional access via mobile hotspot to gather stored data.

The application is one of recording 'votes' with the exact date and time, and the unit must be switched off at night, so would lose the time.

I know I seem to be putting difficulties in the way, but they are not of my making! So I do need the RTC - the question is, why does it work at switch-on most times, but occasionally fail to be acquired?

Turning off at night would not be a problem for my idea, when it turned on again, it would connect to WiFi and get the time.

But back to your RTC. Have you tried adding external pull-up resistors on the i2c lines (e.g. 3K3)? Should not be necessary in theory, the RTC probably has some built-in, but they might be quite high values, and you might have long i2c lines or something.

You should post a link to the RTC module, post your schematic, your code, error messages/serial output and some well-lit & focused pictures, so we can clearly see all wiring. Please read the forum guide in the sticky post so you know how to do that correctly. If your code too long (> 9000 chars) please make a cut-down version that illustrates the problem.