Real time clock not always working
Hi guys
I'm having trouble with the real time clock in my Industrino D21 G.
When starting up the device I'm getting three variations of the time:
1) The real time that I have already initialized
2) 2000.1.30-20.14.00
3) 2165.45.45-45:85:85
What could be the reason for this behavior?
Are there available some functions that update the time using the cellular system?
Thank you!
How I'm using the library:
#include <MCP7940.h>
Setup:
RTCind.get(rtc,true);
RTCind.start(true);
Updating the time:
RTCind.get(rtc,true);
Using the current time:
String time = String(rtc[6])+"."+String(rtc[5])+"."+String(rtc[4])+"-"+String(rtc[2])+":"+String(rtc[1])+":"+String(rtc[0])+......
The RTC problem is caused by RF interference from the GSM module when it's transmitting.
The code below resets the date and then reads the RTC every 100ms for 15 seconds. If the year is correct it counts it as a success, if it's wrong it's a fail. With the supplied antenna pointing away from the Industruino, I get an average of 25 fails and 125 successes. If the antenna is angled towards the Industruino, I get 86 fails and 64 successes. The fails result in the RTC returning a year of 2065 or 2000.
With an external antenna on the end of a cable I don't get any errors. I used a 2G antenna with SMA connector from RS Components - RS Stock No.124-8694
#include <Wire.h>
#include <MCP7940.h>
#include <UC1701.h>
static UC1701 lcd;
#define TINY_GSM_MODEM_SIM800
#include <TinyGsmClient.h>
#define SerialAT Serial1
TinyGsm modem(SerialAT);
TinyGsmClient client(modem);
int rtc[7];
unsigned long rtcSuccess = 0;
unsigned long rtcFail = 0;
const int gsmPowerPin = 6;
void setup()
{
pinMode(26, OUTPUT);
digitalWrite(26, HIGH);
Wire.begin();
RTCind.get(rtc, true);
lcd.begin();
lcd.clear();
RTCind.set(MCP7940_SEC, 0);
RTCind.set(MCP7940_MIN, 30);
RTCind.set(MCP7940_HR, 12);
RTCind.set(MCP7940_DOW, 6);
RTCind.set(MCP7940_DATE, 10);
RTCind.set(MCP7940_MTH, 11);
RTCind.set(MCP7940_YR, 18);
RTCind.start(true);
pinMode(gsmPowerPin, OUTPUT);
digitalWrite(gsmPowerPin, HIGH);
delay(1000);
digitalWrite(gsmPowerPin, LOW);
SerialAT.begin(115200);
delay(3000);
modem.restart();
for (int i = 0; i < 150; i++)
{
RTCind.get(rtc, true);
if (rtc[6] == 2018) rtcSuccess++;
else rtcFail++;
lcd.setCursor(12,1);
lcd.print("Year");
lcd.setCursor(60,1);
lcd.print(rtc[6]);
lcd.setCursor(12,2);
lcd.print("Success");
lcd.setCursor(60,2);
lcd.print(rtcSuccess);
lcd.setCursor(12,3);
lcd.print("Fail");
lcd.setCursor(60,3);
lcd.print(rtcFail);
delay(100);
}
}
void loop()
{}
These two functions work well for me
// ---------------------------------------------------------------------------
void rtcInit()
// ---------------------------------------------------------------------------
{
// get TOD from RTC and set system time - requires time library
int rtc[7];
// get time data
RTCind.get(rtc, true);
SerialUSB.print(F("\nTime "));
SerialUSB.print(dayStr(rtc[3]));
SerialUSB.print(F(" "));
SerialUSB.print(rtc[4], DEC);
SerialUSB.print(F(" "));
SerialUSB.print(monthStr(rtc[5]));
SerialUSB.print(F(" "));
SerialUSB.print(rtc[6], DEC);
SerialUSB.print(F(" "));
if (rtc[2] < 10) SerialUSB.print(F("0"));
SerialUSB.print(rtc[2], DEC);
SerialUSB.print(F(":"));
if (rtc[1] < 10) SerialUSB.print(F("0"));
SerialUSB.print(rtc[1], DEC);
SerialUSB.print(F(":"));
if (rtc[0] < 10) SerialUSB.print(F("0"));
SerialUSB.print(rtc[0], DEC);
// set local time function void setTime(int hr,int min,int sec,int day, int month, int yr);
setTime(rtc[2], rtc[1], rtc[0], rtc[4], rtc[5], rtc[6]);
}
// ---------------------------------------------------------------------------
void rtcSet()
// ---------------------------------------------------------------------------
{
// requires time library - set time using thime library and this will then set RTC
int rtc[7];
RTCind.get(rtc, true);
RTCind.set(MCP7940_SEC, second());
RTCind.set(MCP7940_MIN, minute());
RTCind.set(MCP7940_HR, hour());
RTCind.set(MCP7940_DOW, weekday()); //RTC day 1 = MON, timelib day 1 = SUN
RTCind.set(MCP7940_DATE, day());
RTCind.set(MCP7940_MTH, month());
RTCind.set(MCP7940_YR, year());
RTCind.start(true);
}
Your answer
Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!
Keep Informed
About This Forum
This community is for professionals and enthusiasts of our products and services.
Read GuidelinesQuestion tools
Stats
Asked: 1/11/18, 7:53 PM |
Seen: 5527 times |
Last updated: 11/16/18, 5:18 PM |
Er í leyfi frá störfum. Er væntanlegur aftur til vinnu í seinnihluta Febrúar.
Ef erindið varðar hússtjórnarkerfi má hafa samband við Erlend Ólason, erlendur.olason@efla.is
Önnur mál, Símon Elvar simon.elvar.vilhjalmsson@efla.is
Björgvin