node mcu Ds1307

I am trying to interface the DS1307 with node MCU unit. Is there any library function available

Below code is working fine . But it wont allow user to add and change set_date&time . Can i use Wire library of arduino Uno Directly?? Is there any web based timing control

/* for normal hardware wire use below */
#include <Wire.h> // must be included here so that Arduino library object file references work
#include <RtcDS1307.h>
RtcDS1307<TwoWire> Rtc(Wire);
/* for normal hardware wire use above */

void setup ()
{
    Serial.begin(115200);

    Serial.print("compiled: ");
    Serial.print(__DATE__);
    Serial.println(__TIME__);

    //--------RTC SETUP ------------
    // if you are using ESP-01 then uncomment the line below to reset the pins to
    // the available pins for SDA, SCL
    // Wire.begin(0, 2); // due to limited pins, use pin 0 and 2 for SDA, SCL
   
    Rtc.Begin();

    RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
    printDateTime(compiled);
    Serial.println();

    if (!Rtc.IsDateTimeValid())
    {
        // Common Cuases:
        //    1) first time you ran and the device wasn't running yet
        //    2) the battery on the device is low or even missing

        Serial.println("RTC lost confidence in the DateTime!");

        // following line sets the RTC to the date & time this sketch was compiled
        // it will also reset the valid flag internally unless the Rtc device is
        // having an issue

        Rtc.SetDateTime(compiled);
    }

    if (!Rtc.GetIsRunning())
    {
        Serial.println("RTC was not actively running, starting now");
        Rtc.SetIsRunning(true);
    }

    RtcDateTime now = Rtc.GetDateTime();
    if (now < compiled)
    {
        Serial.println("RTC is older than compile time!  (Updating DateTime)");
        Rtc.SetDateTime(compiled);
    }
    else if (now > compiled)
    {
        Serial.println("RTC is newer than compile time. (this is expected)");
    }
    else if (now == compiled)
    {
        Serial.println("RTC is the same as compile time! (not expected but all is fine)");
    }

    // never assume the Rtc was last configured by you, so
    // just clear them to your needed state
    Rtc.SetSquareWavePin(DS1307SquareWaveOut_Low);
}

void loop ()
{
    if (!Rtc.IsDateTimeValid())
    {
        // Common Cuases:
        //    1) the battery on the device is low or even missing and the power line was disconnected
        Serial.println("RTC lost confidence in the DateTime!");
    }

    RtcDateTime now = Rtc.GetDateTime();

    printDateTime(now);
    Serial.println();

    delay(1000); // ten seconds
}

#define countof(a) (sizeof(a) / sizeof(a[0]))

void printDateTime(const RtcDateTime& dt)
{
    char datestring[20];

    snprintf_P(datestring,
            countof(datestring),
            PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
            dt.Month(),
            dt.Day(),
            dt.Year(),
            dt.Hour(),
            dt.Minute(),
            dt.Second() );
    Serial.print(datestring);
}

I expect the Arduino library to work fine for this chip. Wire works well on NodeMCU.

Do note that it's a pretty old one, so it's rated for >4.5V, and your NodeMCU operates at 3.3V. You may consider getting a more up to date RTC such as the DS3231 or an MCP7940, both of which are designed to operate at lower voltages.

By the way, a more accurate and even simpler way to get your time is NTP. Make use of that WiFi connection!

OP already knows that you don't need a RTC with a NodeMCU.

Leo..

I will try with Ds3211 .
Yes Using NTP protocol we can get date & time . But i am thinking how about achieving Timezone.

Which is correct arduino compatible library for ds1307

Here is my code i used normal one. I would like to refresh page. In my code it wont do autorefresh so led remain last refresh stage. so how can make autorefresh page.other wise code working fine.

#include <ESP8266WiFi.h>
#include "DHT.h"
#include <Wire.h> // must be included here so that Arduino library object file references work
#include <RtcDS1307.h>
RtcDS1307<TwoWire> Rtc(Wire);

static int Act_date,Act_month,Act_Year,Act_Hour,Act_Min,Act_Sec;
static int Set_date=17;
static int Set_month=07;
static int Set_Year=2018;
static int Set_Hour=12;
static int Set_Min=35;
static int Set_Sec=50;
static int Set_Min2=15;
int value = LOW;


 RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
const RtcDateTime Real_date;


static float Set_Temp=30.0;
DHT dht;
 
const char* ssid = "esp8266";
const char* password = "Test123456";
 
int ledPin = 13; // GPIO13
WiFiServer server(80);
 
void setup() {
  Serial.begin(115200);
  delay(10);

   Rtc.Begin();
     
 if (!Rtc.IsDateTimeValid())
    {
       Rtc.SetDateTime(compiled);
    }

    if (!Rtc.GetIsRunning())
    {
        Serial.println("RTC was not actively running, starting now");
        Rtc.SetIsRunning(true);
    }

RtcDateTime now = Rtc.GetDateTime();
    if (now < compiled)
    {
        //Serial.println("RTC is older than compile time!  (Updating DateTime)");
        Rtc.SetDateTime(compiled);
    }
    else if (now > compiled)
    {
       // Serial.println("RTC is newer than compile time. (this is expected)");
    }
    else if (now == compiled)
    {
       // Serial.println("RTC is the same as compile time! (not expected but all is fine)");
    }

    // never assume the Rtc was last configured by you, so
    // just clear them to your needed state
    Rtc.SetSquareWavePin(DS1307SquareWaveOut_Low);


      
 
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  dht.setup(D3);   /* D1 is used for data communication */
  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
 
  // Start the server
  server.begin();
  Serial.println("Server started");
 
  // Print the IP address
  Serial.print("Use this URL to connect: ");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");
 
}
 
void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
 
  // Wait until the client sends some data
  Serial.println("new client");
  while(!client.available()){
    delay(1);
  }

 if (!Rtc.IsDateTimeValid())
    {
       Rtc.SetDateTime(compiled);
    }

    if (!Rtc.GetIsRunning())
    {
        Serial.println("RTC was not actively running, starting now");
        Rtc.SetIsRunning(true);
    }

RtcDateTime now = Rtc.GetDateTime();
    printDateTime(now);
    
   delay(dht.getMinimumSamplingPeriod());  /* Delay of amount equal to sampling period */

  float humidity = dht.getHumidity(); /* Get humidity value */
  float temperature = dht.getTemperature(); /* Get temperature value */

 // Serial.print(dht.getStatusString());  /* Print status of communication */

  
 
  // Read the first line of the request
  String request = client.readStringUntil('\r');
  Serial.println(request);
  client.flush();
 
  // Match the request
 
 // int value = LOW;
  if (request.indexOf("/LED=ON") != -1)  {
    digitalWrite(ledPin, HIGH);
    value = HIGH;
  }
  if (request.indexOf("/LED=OFF") != -1)  {
    digitalWrite(ledPin, LOW);
    value = LOW;
  }
 
// Set ledPin according to the request
//digitalWrite(ledPin, value);
 
  // Return the response
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println(""); //  do not forget this one
  client.println("<!DOCTYPE HTML>");
  client.println("<html>");
   
  
   
   client.println("

");
  client.println("EAPL TIME SWITCH APPLICATION");   
  client.println("

");
    
  client.println("Led pin is now: ");


     
if((Act_Hour==Set_Hour)&&(Act_Min<Set_Min))
{

   digitalWrite(ledPin, HIGH);
    value = HIGH;
}else
{
   digitalWrite(ledPin, LOW);
    value = LOW;
}

 
  if(value == HIGH) {
    client.print("On");
  } else {
    client.print("Off");
  }
 /* client.println("

");
  client.println("<a href=\"/LED=ON\"\"><button>Turn On </button></a>");
  client.println("<a href=\"/LED=OFF\"\"><button>Turn Off </button></a>
");  
  client.println("</html>");
 */
   client.println("

");
  client.println("DHT11_HumidityReading: ");
  client.println(humidity,1);
   client.println("

");
  client.println("DHT11_Temprature Reading: ");
  client.println(temperature,1);
   client.println("

");

  client.println("Set_Temp: ");
  client.println(Set_Temp);
  client.println("

");
  
client.println("Real Time Clock: ");
client.print(Act_date);client.print("/");client.print(Act_month);client.print("/");client.print(Act_Year);client.print("    ");
client.print(Act_Hour);client.print(":");client.print(Act_Min);client.print(":");client.print(Act_Sec);
client.println("

");

client.println("Time Switch Clock: ");
client.print(Set_date);client.print("/");client.print(Set_month);client.print("/");client.print(Set_Year);client.print("    ");
client.print(Set_Hour);client.print(":");client.print(Set_Min);client.print(":");client.print(Set_Sec);
client.println("

");


  
 
  delay(1);
  Serial.println("Client disonnected");
  Serial.println("");
 
}


#define countof(a) (sizeof(a) / sizeof(a[0]))
void printDateTime(const RtcDateTime& dt)
{
    char datestring[20];

    snprintf_P(datestring,
            countof(datestring),
            PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
            dt.Month(),
            dt.Day(),
            dt.Year(),
            dt.Hour(),
            dt.Minute(),
            dt.Second() );
    Serial.print(datestring);
   Act_month=dt.Month();
   Act_date=dt.Day();
   Act_Year= dt.Year();
   Act_Hour=dt.Hour();
  Act_Min= dt.Minute();
  Act_Sec= dt.Second();
    
}

To auto-refresh a page you have to set the HTML REFRESH header.

Timezone you have to set by hand and is simply a correction to the time received over NTP. Just add/subtract the time as needed.

Is any one has example to get client data from texbox and save it in value.

I have gone through many web forum arduino. I could able to generate the text box and submit button. But i couldn't able to save data into parameter

Like wise I am looking for change value in above topic. If it fixed value it can be easily displayed & works well . But How to update the parametric value.

Like HTTP uses get and post . if i tried those example it work out in HTML/HTTP. Is there any way to change or set client data.

Witsie74:
Not quite, you need to adjust for local time going over 2400.

Just use UNIX timestamps (seconds since epoch). No problem. Just add/subtract the time as per timezone (in seconds) and done.

It's anyway a good idea to use UNIX timestamps for anything but where you need it human readable, as it's compact (just four bytes - unsigned long type) and easy to do calculations with (you can simply do additions and subtractions without bothering with leap years, different lengths of the months, etc).