[SOLVED] Another RTC issue

Hello all.

I am having an issue with my RTC and i can not work it out. Like everybody else that has issues i dont sync and get a time of 0:0:0 and a date of 1970.

I have tried a lot of different things and i just can not get it to work inside my full code. I can take just the RTC part of it and it works fine but when added with the full code it dosent sync. Although it looks like i get a time sync to start with but it disapears.

Any help would be great.


#define BLYNK_PRINT Serial
#include "arduino.h"

// #region    Blynk & Esp8266 Setup.
  #include <ESP8266_Lib.h>
  #include <BlynkSimpleShieldEsp8266.h>

// Blynk Project Auth Token from the Blynk App.
  char auth[] = "????????";

// WiFi AP Settings.
  char ssid[] = "ubnt-pico";
  char pass[] = "XXXXX";

// Set ESP8266 Serial object
  #define EspSerial Serial

// Software serial for communication with ESP8266
  // #include <SoftwareSerial.h>
  // SoftwareSerial EspSerial(8, 9); // RX, TX
// ESP8266 baud rate:
  #define ESP8266_BAUD 115200

ESP8266 wifi(&EspSerial);
BlynkTimer timer;


// #endregion
// #region    DHT22 Sensor Setup
  // #include <DHT.h>
  // #define DHTPIN 7
  // #define DHTTYPE DHT22
  // DHT dht(DHTPIN, DHTTYPE);
  // float h = dht.readHumidity();
  // float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
// #endregion
// #region    DS18B20 Sensor Setup
  #include <OneWire.h>
  #include <DallasTemperature.h>
// DS18S20 Temperature chip i/o on pin 10
  #define ONE_WIRE_BUS 10
// oneWire instance to communicate with any OneWire devices
  OneWire oneWire(ONE_WIRE_BUS);
// Pass oneWire reference to Dallas Temperature.
  DallasTemperature sensors(&oneWire);
// #endregion
// #region    RTC
#include <TimeLib.h>
#include <WidgetRTC.h>
WidgetRTC rtc;

// You can call hour(), minute(), ... at any time
// Please see Time library examples for details
String currentTime = String(hour()) + ":" + minute() + ":" + second();
String currentDate = String(day()) + " " + month() + " " + year();
// #endregion
// #region    LCD & Blynk LCD
  #include <LiquidCrystal.h>
// initialize interface pins for LCD
  LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
  WidgetLCD  blynkLCD(V20);
// #endregion




// #region    On Timer Function
// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V6, millis() / 1000);

}
// #endregion
// #region    DHT Sensor Function
// void sendDHT()
// {
//   if (isnan(h) || isnan(t)) {
//     Serial.println("Failed to read from DHT sensor!");
//     return;
//   }
//
//   Blynk.virtualWrite(V2, t);
//   Blynk.virtualWrite(V3, h);
//   Blynk.virtualWrite(V7, t);
//   Blynk.virtualWrite(V8, h);
//   }
// #endregion
// #region    DS18B20 Sensor Function
void sendDS18B20()
{
//to issue a global temperature;
// request to all devices on the bus
  sensors.requestTemperatures();
// Send the command to get temperature readings
   sensors.requestTemperatures();
// Output To Blynk
   Blynk.virtualWrite(V4, sensors.getTempCByIndex(0));
   Blynk.virtualWrite(V5, sensors.getTempCByIndex(1));
   Blynk.virtualWrite(V9, sensors.getTempCByIndex(0));
   Blynk.virtualWrite(V10, sensors.getTempCByIndex(1));
}
// #endregion
// #region    LCD & Serial  Functions
  // #region    LCD Start
  void startLCD()
    {
      lcd.begin(20, 4);
      lcd.clear();
      lcd.setCursor(5,1);
      lcd.print("WELCOME TO");
      lcd.setCursor(0,2);
      lcd.print("TRIPPLE B DISTILLERY");

      blynkLCD.clear();
      blynkLCD.print(0, 0, "Welcome To");
      blynkLCD.print(0, 1, "Tripple B");
    }
  // #endregion LCD Start
  // #region    LCD Regular Update Output
  void timeLCD()
    {
      lcd.clear();
      lcd.noBlink();
      lcd.setCursor(0,1);
      lcd.print("Time:");
      lcd.setCursor(7,1);
      lcd.print(currentTime);
      lcd.setCursor(0,2);
      lcd.print("Date:");
      lcd.setCursor(7,2);
      lcd.print(currentDate);
  }

  void tempsLCD()
    {
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Ambiant ");
      lcd.setCursor(13,0);
      // lcd.print(t);
      lcd.setCursor(18,0);
      lcd.print(" C");
      lcd.setCursor(0,1);
      lcd.print("Humidity ");
      lcd.setCursor(13,1);
      // lcd.print(h);
      lcd.setCursor(18,1);
      lcd.print(" %");
      lcd.setCursor(0,2);
      lcd.print("Fermenter 1 ");
      lcd.setCursor(13,2);
      lcd.print(sensors.getTempCByIndex(0));
      lcd.setCursor(18,2);
      lcd.print(" C");
      lcd.setCursor(0,3);
      lcd.print("Fermenter 2 ");
      lcd.setCursor(13,3);
      lcd.print(sensors.getTempCByIndex(1));
      lcd.setCursor(18,3);
      lcd.print(" C");
  }

  void clockDisplay()
  {
    blynkLCD.clear();
    blynkLCD.print(0, 0, "Time:");
    blynkLCD.print(6, 0, currentTime);
    blynkLCD.print(0, 1, "Date:");
    blynkLCD.print(6, 1, currentDate);


    // Send time to the App
    Blynk.virtualWrite(V1, currentTime);

  }
  // #endregion LCD Regular Update Output
  // #region    Serial Monitor Outputs
  // void serialDHT()
  // {
  //   Serial.print("DHT22");
  //   Serial.println();
  //   Serial.print("Temperature: ");
  //   // Serial.print(t);
  //   Serial.print(" *C ");
  //   Serial.println();
  //   Serial.print("Humidity: ");
  //   // Serial.print(h);
  //   Serial.print(" %\t");
  //   Serial.println();
  //   Serial.println();
  // }

  void serialDS18B20()
  {
    Serial.println("");
    Serial.println("DS18B20");
    Serial.print("Fermenter 1 is: ");
    Serial.println(sensors.getTempCByIndex(0));
    Serial.print("Fermenter 2 is: ");
    Serial.println(sensors.getTempCByIndex(1));
    Serial.println("");
  }

  void serialCLOCKOUT()
  {
    Serial.println("");
    Serial.print("Current time: ");
    Serial.print(currentTime);
    Serial.print(" ");
    Serial.print(currentDate);
    Serial.println("");
  }
  // #endregion  Serial Monitor Outputs
// #endregion


void setup()
{
// Debug console &ESP8266 baud rate
  Serial.begin(9600);
  EspSerial.begin(ESP8266_BAUD);
  delay(10);
  Blynk.begin(auth, wifi, ssid, pass, "blynk-cloud.com", 8442);

// Begin synchronizing and starting sensors
  rtc.begin();
  // dht.begin();
  sensors.begin();


// Timers
  timer.setInterval(1000, myTimerEvent);    //Send runtime every sec to blynk
  timer.setTimeout(5000, startLCD);         //LCD Start
  timer.setInterval(4000, sendDS18B20);     //Send DS18B20 data every 4 sec to blynk
  // timer.setInterval(5000, sendDHT);         //Send DHT data every 5 sec to blynk
  timer.setInterval(10000, clockDisplay);   //Update blynk lcd time every 10 sec

  timer.setInterval(4500, serialDS18B20);   //Update serial mon data every 4.5 sec
  // timer.setInterval(5500, serialDHT);       //Update serial mon data every 5.5 sec
  timer.setInterval(10000, serialCLOCKOUT);  //Update serial mon data every 6.5 sec

  timer.setInterval(5000, timeLCD);         //Update LCD time every 5 sec
  timer.setInterval(10000, tempsLCD);       //Update LCD temps every 10 sec



}

void loop()
{
  Blynk.run();
  timer.run();
}

What makes you think this will work based on the location in your sketch?

Does that not make the string global by having it there so it can be called by more than one function?

You can’t perform any actions outside of the loops

It would be global but only if hour(), minute() and second() etc had values when you created the String.
They have zero values because you haven’t done the time sync when you created the String.

So the Global declaration for time is simply:

String currentTime;

The String manipulation needs to be done elsewhere, after time sync, as per the examples.

1 Like

Thanks. Makes sense. I will see what i can do.

All sorted now. Made a new global variable, and then made it = currentTime within the function and then called the new global variable in my other functions.

Thanks a lot.