Go Down

Topic: RTC1307 - String Converting Help. (Read 2049 times) previous topic - next topic


Oct 11, 2012, 12:04 pm Last Edit: Oct 11, 2012, 12:30 pm by staroflaw Reason: 1

I am using a I2C RTC DS1307 Clock Module and the RTClib in my project.
What I am trying to do is set the time 'unix time' over a serial command.

So I input S1349949137# over serial, and set as 'timeString'
S=Set / 1349949137=unix time / #=End of msg.

I then remove the S and # leaving just the unix time.
Code: [Select]
String timeString = inputString.substring(1,11);

Now this is were I am stuck.
Code: [Select]
This dose not work because I need to convert from a string to uint32_t. I think?

If I use this code it works and set the correct time and date.  
Code: [Select]
uint32_t n = 1349949137;

How can I convert 'timeString' to uint32_t.
I am new to converting and am not sure what's the best solution to my problem.



I am new to converting and am not sure what's the best solution to my problem.

First piece of advice: Ditch the String class. Use a char array, NULL terminated, instead.

Second piece of advice: Snippets-are-us is down the rad a ways. Go there for help with snippets, or post ALL of your code.

Third piece of advice: Use String::toCharArray() to get the data from the String. Then, use strtoul() to convert the array to an unsigned long. That's what a uint32_t is.
The art of getting good answers lies in asking good questions.


Fourth piece of advice: There's an example sketch that comes with the Time library (http://www.arduino.cc/playground/Code/Time) that does exactly what you're looking for.  It's called TimeRTCSet.  If you just want to set your RTC, you can use it, or you can see how they do it to work that into your own code.


Thank you both for your helpful reply.

I am just looking into them now.


Go Up