0

I'm trying to merge 2 Arduino samples, in order to be able to use DS3231 RTC module with ESP8266 WiFi simple web server.

My RTC library is this: https://github.com/Makuna/Rtc/wiki

Generally, to code runs ok (I have the Web server responses to my browser calls, and I think the DS3231 RTC code also works). But when I'm trying to use a variable from within the RTC object into the Web server function - I get nothing.

I thought declaring the 'now' object outside any function (Setup() nor Loop()) will make it global, and when I'll update it in the Loop() function using

now = Rtc.GetDateTime();

It will be available to other functions as well, but all I get is "0" when I try to print it.

My code is here: Definitions part:

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>

/* This block is for I2C protocol for DS3231 module (for normal hardware wire use below) */
#include <Wire.h> // must be included here so that Arduino library object file references work
#include <RtcDS3231.h>
RtcDS3231<TwoWire> Rtc(Wire);
/* The above block is for I2C protocol and DS3231 module  (for normal hardware wire use above) */

ESP8266WebServer server(80);
RtcDateTime now;

My Loop():

void loop(void) {
  now = Rtc.GetDateTime();
  Serial.println(now.Minute());
  server.handleClient();
  delay(500);

}

And all I get is "0" printed.

Any help would be appreciated!

3
  • And your code outputs the correct minutes, if you move the declaration of now into the loop() function?
    – chrisl
    Dec 21, 2019 at 23:42
  • 1
    What do you get if you remove the WiFi Web server code? Dec 22, 2019 at 11:12
  • Update: Thank you so much for everyone looked into it. I've just now tried to debug it, and found that for a strange reason - my DS3231 module stopped working (forgot the time, and also didn't advance, even after program resets). What worked was to disconnect it from power and take out the battery. Now it works ok, and the scope of the variable is good also. Should I delete my question now? or just leave it like this? Many thanks again! I appreciate it so much!
    – Offer
    Dec 22, 2019 at 21:31

1 Answer 1

2

As was suggested by more 'native' friends here - I'm 'Answering my question' to make things clear.

After a check and debug - I've found a mysterious behavior that happened to my DS3231 module. Probably a mistaken initializing set of calls - made it stop working - not getting time updates and not counting the time at all. (Freeze at 1/1/2000, 00:00:00).

Disconnecting it from power source, and taking out the battery - returned it to normal behavior, able to get time updates, and answering the calls from the library.

When that part solved - everything else went as easy as it should.

The code sample I posted in my question (declaring the 'now' object outside of setup() and loop() functions) made that object global, so another function (WebServer) can read its current value and use it internally.

Thanks everyone who looked at my problem and helped, It was a great lesson about how not to take anything as 'known' without double-checking.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.