|
10 | 10 | #include <MCP79412RTC.h> // https://github.com/JChristensen/MCP79412RTC
|
11 | 11 | #include <TimeLib.h> // https://github.com/PaulStoffregen/Time
|
12 | 12 |
|
13 |
| -const uint32_t PRINT_INTERVAL(1000); // ms between printing the time |
| 13 | +const uint32_t PRINT_INTERVAL(10000); // ms between printing the time |
14 | 14 | uint32_t ms, msLast;
|
15 | 15 |
|
16 | 16 | void setup()
|
@@ -40,36 +40,38 @@ void loop()
|
40 | 40 | void readCommand()
|
41 | 41 | {
|
42 | 42 | char cmd[24] = "Set yyyy-mm-dd hh:mm:ss";
|
43 |
| - static int i; |
44 |
| - tmElements_t tmSet; |
45 |
| - time_t tSet; |
46 | 43 |
|
47 |
| - if (Serial.available() >= 23) { // enough characters for the whole command? |
48 |
| - i = 0; // yes, read the available characters |
| 44 | + // serial terminal will send 23 char command plus line terminator (0x0A) |
| 45 | + if (Serial.available() >= 24) { // enough characters for the whole command? |
| 46 | + unsigned int i = 0; // yes, read the available characters |
49 | 47 | while (Serial.available() > 0) {
|
50 |
| - if (i >= sizeof(cmd) - 1) { // more than we can enjoy |
51 |
| - flushInput(); // clear out the input buffer |
| 48 | + if (i >= sizeof(cmd)) { // more than we can enjoy |
| 49 | + flushInput(); // clear out the input buffer |
| 50 | + cmd[sizeof(cmd) - 1] = 0; // string terminator |
52 | 51 | Serial.print("Too long: ");
|
53 | 52 | Serial.println(cmd);
|
54 | 53 | return;
|
55 | 54 | }
|
56 |
| - delay(2); //let the next character trickle in |
57 |
| - cmd[i++] = char(Serial.read()); |
| 55 | + delay(2); // let the next character trickle in |
| 56 | + char c = Serial.read(); |
| 57 | + if (c >= ' ') cmd[i++] = c; // printable characters and spaces only |
58 | 58 | }
|
59 |
| - cmd[i] = 0; //put in string terminator |
| 59 | + cmd[i] = 0; // put in string terminator |
60 | 60 |
|
| 61 | + tmElements_t tmSet; |
61 | 62 | if (strncmp(cmd, "Set ", 4) == 0) {
|
62 | 63 | tmSet.Year = 1000 * (cmd[4] - '0') + 100 * (cmd[5] - '0') + 10 * (cmd[6] - '0') + cmd[7] - '0' - 1970;
|
63 | 64 | tmSet.Month = 10 * (cmd[9] - '0') + cmd[10] - '0';
|
64 | 65 | tmSet.Day = 10 * (cmd[12] - '0') + cmd[13] - '0';
|
65 | 66 | tmSet.Hour = 10 * (cmd[15] - '0') + cmd[16] - '0';
|
66 | 67 | tmSet.Minute = 10 * (cmd[18] - '0') + cmd[19] - '0';
|
67 | 68 | tmSet.Second = 10 * (cmd[21] - '0') + cmd[22] - '0';
|
68 |
| - tSet = makeTime(tmSet); // convert to time_t |
69 |
| - setTime(tSet); // set the system time |
70 |
| - RTC.set(now()); // set the rtc |
| 69 | + time_t tSet = makeTime(tmSet); // convert to time_t |
| 70 | + setTime(tSet); // set the system time |
| 71 | + RTC.set(now()); // set the rtc |
71 | 72 | Serial.println("RTC set!");
|
72 |
| - flushInput(); // discard any extraneous trailing characters |
| 73 | + printTime(RTC.get()); |
| 74 | + flushInput(); // discard any extraneous trailing characters |
73 | 75 | }
|
74 | 76 | else {
|
75 | 77 | Serial.print("Unknown: ");
|
|
0 commit comments