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!
1 Answer 1
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.
now
into theloop()
function?