element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Problem reading the RTC time from SIM900
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Not Answered
  • Replies 6 replies
  • Subscribers 391 subscribers
  • Views 908 views
  • Users 0 members are here
Related

Problem reading the RTC time from SIM900

Former Member
Former Member over 10 years ago

I've got a bit of a problem trying to read the time from the RTC from a SIM900 shield and was hoping someone on here might have an answer.  Its probably very obvious but as a fairly inexperienced hobbyist it is driving me mad.

 

I'll give you some background even though it might be boring it could be helpful.

 

I've had the SIM900 based GPRS shield from Maplin (Linksprite) for some time but only now started to include it into a project idea I have.

 

I want to build a monitoring system for my narrow boat and want it to send text messages if there is a problem or to receive text messages to turn things on / off.

 

Part of the project was using a cheap RTC module and I found that it kept time about as good as my 5 euro Rolex I brought from some dodgy bloke on Tenerife beach.

 

As the Sim900 has a RTC built and also get the time from the network its seemed a good idea to ditch the cheap RTC.

 

I managed to change the setting so the SIM900 gets the network time and during test this has proved to be very accurate so a big plus on that one.

 

The problem I have is reading the time back into my Arduino and after hours spent trawling forums came to the conclusion that everyone had given up.

 

So I spent some time trying to work it out and have had some success but its not perfect and I can't seem to work out the problem.

 

My set up for testing is very basic.  Arduino Mega, GPRS shield (but not placed on top of the mega) connected to serial 1.  Once I work out the problems I will build it into my project.

 

The time is accessed by the command AT+CCLK? this returns a long string with the date and time (plus all the other rubbish that they send back)

 

The code below basically sends the command to get the time then using parseInt to put the date and time into number of strings.

 

The strings are Year, Months, Days, Hours, Mins, Sec and Nu.  Nu is not used and is only there to put the time zone code in.  If I don't have this the next read is out.

 

The date and time is then sent to the Serial Monitor on the PC, this is repeated every 10 seconds. I've tried longer delays but doesn't make any difference.

The code I'm using is:-

 

void setup() {
  Serial.begin(19200);
  Serial1.begin(19200);
}
void loop() {
   Serial1.print("AT+CCLK?\r");
  while (Serial1.available() > 0) {
    int Years = Serial1.parseInt();
    int Months = Serial1.parseInt();
    int Days = Serial1.parseInt();
    int Hours = Serial1.parseInt();
    int Mins = Serial1.parseInt();
    int Secs = Serial1.parseInt();
    int Nu = Serial1.parseInt();

    Serial.print(Years);
    Serial.print("/");
    Serial.print(Months);
    Serial.print("/");
    Serial.print(Days);
    Serial.print("  ");
    Serial.print(Hours);
    Serial.print(":");
    Serial.print(Mins);
    Serial.print(":");
    Serial.print(Secs);
    Serial.println();
  }
delay(10000);
}

 

What I get is two good reads, a delay then all zeros and this repeats.  There does seem to be a delay and puts the timing out of sequence by about 7 seconds.

 

15/3/25  20:41:30
15/3/25  20:41:40
0/0/0  0:0:0
15/3/25  20:41:57
15/3/25  20:42:7
0/0/0  0:0:0
15/3/25  20:42:24
15/3/25  20:42:34
0/0/0  0:0:0
15/3/25  20:42:51
15/3/25  20:43:1
0/0/0  0:0:0
15/3/25  20:43:18
15/3/25  20:43:28
0/0/0  0:0:0
15/3/25  20:43:45
15/3/25  20:43:55
0/0/0  0:0:0

 

Going to need a wig at this rate so any help will be greatly appreciated.

  • Sign in to reply
  • Cancel
  • clem57
    0 clem57 over 10 years ago

    void loop() {

       Serial1.print("AT+CCLK?\r");

      while (Serial1.available() !> 0) { } <== Add so loops until available

        int Years = Serial1.parseInt();

        int Months = Serial1.parseInt();

        int Days = Serial1.parseInt();

        int Hours = Serial1.parseInt();

        int Mins = Serial1.parseInt();

        int Secs = Serial1.parseInt();

        int Nu = Serial1.parseInt();

     

        Serial.print(Years);

        Serial.print("/");

        Serial.print(Months);

        Serial.print("/");

        Serial.print(Days);

        Serial.print("  ");

        Serial.print(Hours);

        Serial.print(":");

        Serial.print(Mins);

        Serial.print(":");

        Serial.print(Secs);

        Serial.println();

    } <=== Delete

    delay(10000);

    }

     

    This logic should work! Test and let me know,

    Clem

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 10 years ago in reply to clem57

    Thanks Cleb but putting the !>0 in wont compile, give the error "expected primary-expression before '>' token"

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • clem57
    0 clem57 over 10 years ago in reply to Former Member

    <= instead of !=.

    Thanks,

    Clem

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 10 years ago in reply to clem57

    After the site went down last night I had a look at other examples of reading serial data and changed the while to an if and all works well.

     

    no idea why if works and while doesn't but I'm a happy man now and can move on to the next stage.

     

    thanks for your help Clem.

     

    Chris

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • clem57
    0 clem57 over 10 years ago in reply to clem57

    That is why I was taking your code out of the loop, The loop should do nothing until serial1 is available, then do the code. The if works too. What was happening you were going into serial1 at wrong times!

    Clem

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 10 years ago

    Chris,

        I have a project of my own that I would like to send a text every day around 8:00 am.  How exactly did you get your SIM900 to sync with Greenwich time?  Is there a time server that you can text, or did you sync over a data connection?  I've got a SIM from T-Mobile, but I haven't been able to figure out how to sync the internal RTC with anything.  I have been able to use "AT+CCLK?" and "AT+CCLK=<time>".  Any help would be greatly appreciated. 

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube