Wemos D1 mini continuous reboot

Hi all,

I am using the RTC_DS1307_SetTime sketch, and after uploading to the board it continuously reboots, and is not starting the sketch.

It does this with nothing connected to any pins, just the serial monitor USB cable attached.

This is the serial monitor output I see, which repeats about once per second...


 ets Jan  8 2013,rst cause:2, boot mode:(3,6)

load 0x4010f000, len 3424, room 16 
tail 0
chksum 0x2e
load 0x3fff20b8, len 40, room 8 
tail 0
chksum 0x2b
csum 0x2b
v00044320
~ld
⸮a⸮n⸮r⸮⸮n|⸮l⸮l`bbrl⸮nb⸮nl`⸮rl⸮l⸮⸮

And here is the sketch loaded to the D1 mini


#include <Wire.h>
#include <Time.h>
#include <DS1307RTC.h>

const char *monthName[12] = {
  "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};

tmElements_t tm;
#define SCL D1
#define SDA D2

void setup() {

  bool parse = false;
  bool config = false;

  Serial.begin(115200);
  Wire.begin(SDA, SCL);  //SDA, SCL

  while (!Serial) {
    ; // wait for the Serial Monitor
  delay(200);
  }
  
  // get the date and time the compiler was run
  if (getDate(__DATE__) && getTime(__TIME__)) {
    parse = true;
    // and configure the RTC with this info
    if (RTC.write(tm)) {
      config = true;
    }
  }
 
  if (parse && config) {
    Serial.print("DS1307 configured Time=");
    Serial.print(__TIME__);
    Serial.print(", Date=");
    Serial.println(__DATE__);
  } else if (parse) {
    Serial.println("DS1307 Communication Error :-{");
    Serial.println("Please check your circuitry");
  } else {
    Serial.print("Could not parse info from the compiler, Time=\"");
    Serial.print(__TIME__);
    Serial.print("\", Date=\"");
    Serial.print(__DATE__);
    Serial.println("\"");
  }
}
 
void loop() {
}
 
bool getTime(const char *str)
{
  int Hour, Min, Sec;
 
  if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false;
  tm.Hour = Hour;
  tm.Minute = Min;
  tm.Second = Sec;
  return true;
}
 
bool getDate(const char *str)
{
  char Month[12];
  int Day, Year;
  uint8_t monthIndex;
 
  if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false;
  for (monthIndex = 0; monthIndex < 12; monthIndex++) {
    if (strcmp(Month, monthName[monthIndex]) == 0) break;
  }
  if (monthIndex >= 12) return false;
  tm.Day = Day;
  tm.Month = monthIndex + 1;
  tm.Year = CalendarYrToTm(Year);
  return true;
}

Can anyone enlighten me as to why my sketch will not run.

try so

#include <Wire.h>
#include <Time.h>
#include <DS1307RTC.h>

const char *monthName[12] = {
  "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};

tmElements_t tm;
#define SCL D1
#define SDA D2

void setup() {

  bool parse = false;
  bool config = false;

  Serial.begin(115200);
  Wire.begin(SDA, SCL);  //SDA, SCL   
  // get the date and time the compiler was run
  if (getDate(__DATE__) && getTime(__TIME__)) {
    parse = true;
    // and configure the RTC with this info
    if (RTC.write(tm)) {
      config = true;
    }
  }
 
  if (parse && config) {
    Serial.print("DS1307 configured Time=");
    Serial.print(__TIME__);
    Serial.print(", Date=");
    Serial.println(__DATE__);
  } else if (parse) {
    Serial.println("DS1307 Communication Error :-{");
    Serial.println("Please check your circuitry");
  } else {
    Serial.print("Could not parse info from the compiler, Time=\"");
    Serial.print(__TIME__);
    Serial.print("\", Date=\"");
    Serial.print(__DATE__);
    Serial.println("\"");
  }
}
 
void loop() {
}
 
bool getTime(const char *str)
{
  int Hour, Min, Sec;
 
  if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false;
  tm.Hour = Hour;
  tm.Minute = Min;
  tm.Second = Sec;
  return true;
}
 
bool getDate(const char *str)
{
  char Month[12];
  int Day, Year;
  uint8_t monthIndex;
 
  if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false;
  for (monthIndex = 0; monthIndex < 12; monthIndex++) {
    if (strcmp(Month, monthName[monthIndex]) == 0) break;
  }
  if (monthIndex >= 12) return false;
  tm.Day = Day;
  tm.Month = monthIndex + 1;
  tm.Year = CalendarYrToTm(Year);
  return true;
}

OK, I can see you've removed the While(!Serial) loop, and I've done that before, with no success.

The result is the same with or without the loop.

I had inserted a Serial,print(" ") statement before that While, and it doesn't get printed, so I'm fairly certain the sketch hasn't even started.

Do you have correct baudrate (115200) selected in serial monitor?
Your sketch does only setup() anyway because your loop is empty.

Yes, I have correct Baud rates....

image

Does blink work ?
How is the RTC connected to the D1 mini ?

Search on this string. You will find that a "delay()" is inside "while()" causes a system "yield()" to crash.

This continuous reboot is occurring with NOTHING connected to the D1 mini, just the USB serial cable to the computer.

According to the code, the sketch should not find an RTC connected, and print that out on the Serial monitor. We are not getting that far.

Some suggestions to remove the While(!Serial) loop with the delay() function inside it does not cure the problem either.

That was never suggested.

The problem is in the DS1307RTC library, specifically in the constructor

DS1307RTC::DS1307RTC()
{
  Wire.begin();
}

There is no guarantee that the Wire object has been constructed before the DS1307RTC object is. Assuming that it does exist can get you into trouble. As it has here.

Remove the Wire.begin(); line from the constructor and your sketch will stop rebooting.

1 Like

Yes it was, and I tried it, to no avail. Post #3

NO, it was NOT. Your problem is probably that you do not read.

Are you saying that the library executes Wire.begin() with no parameters for which pins to use ?

How could this work when Wire can use alternative pins ?

Go back and read it yourself, I tried the suggestion in Post #2, no success, then inspected the code and realised the While(!Serial) was the only change. Then I reported back that it did not cure the problem.

Suggesting that I cannot read is lame, and somewhat offensive.

  1. Yes.
  2. It couldn't. Even though the sketch uses the default SDA and SCL pins, the Wire object may not exist when the DS1307RTC object is constructed.

The problem with the Wire.begin(); in the constructor was actually reported as an issue in the library years ago.

Relying on other global objects to already be instantiated and available for use in a constructor for a global object is a really bad idea.

1 Like

I wrote it. You failed to read it.

Suggesting is lame, and correcting your failure to read something is offensive? Hahaha.

When did I ever say you can not read? That's twice you got it wrong.

This is the first time I have tried to use I2C on the Wemos ESP8266 module.

You have posted a link to a thread that talks about a keypad implementation, and I read it, but couldn't see anything that related to my issue.

Exactly why is the CPU rebooting ?

Please explain what is happening as if you were talking to a baby, and also possibly suggest how I can get round the issue.

You obviously have superior knowledge than I, please be kind enough to help ....

Try removing the "delay()" from the while() loop. (rewording the original statement, but same content)

Unknown. Guesses were a "yield()" that passes control from task to task, choked on "delay()" (guessing: causing a reboot when the MPU stopped or got clogged or anything MPUs do)

I am, I was. You went on a tangent.

But I HAVE removed the while() loop completely, and it did not cure the problem.

I have also tried with the while() but with no delay(), still not working....

Just before I mute this topic, as no one appears to be listening, I'll point out that I actually fixed the problem on a D1 mini by removing the Wire.begin(); line from the DS1307RTC constructor. Without that line, the original sketch no longer reboots continually. But since an actual solution appears to be of no interest whatsoever, good-bye and good luck.