experiments with Pylontech/GroWatt PV tech
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
1.8 KiB

  1. /*
  2. * TimeRTC.pde
  3. * example code illustrating Time library with Real Time Clock.
  4. *
  5. */
  6. #include <TimeLib.h>
  7. void setup() {
  8. // set the Time library to use Teensy 3.0's RTC to keep time
  9. setSyncProvider(getTeensy3Time);
  10. Serial.begin(115200);
  11. while (!Serial); // Wait for Arduino Serial Monitor to open
  12. delay(100);
  13. if (timeStatus()!= timeSet) {
  14. Serial.println("Unable to sync with the RTC");
  15. } else {
  16. Serial.println("RTC has set the system time");
  17. }
  18. }
  19. void loop() {
  20. if (Serial.available()) {
  21. time_t t = processSyncMessage();
  22. if (t != 0) {
  23. Teensy3Clock.set(t); // set the RTC
  24. setTime(t);
  25. }
  26. }
  27. digitalClockDisplay();
  28. delay(1000);
  29. }
  30. void digitalClockDisplay() {
  31. // digital clock display of the time
  32. Serial.print(hour());
  33. printDigits(minute());
  34. printDigits(second());
  35. Serial.print(" ");
  36. Serial.print(day());
  37. Serial.print(" ");
  38. Serial.print(month());
  39. Serial.print(" ");
  40. Serial.print(year());
  41. Serial.println();
  42. }
  43. time_t getTeensy3Time()
  44. {
  45. return Teensy3Clock.get();
  46. }
  47. /* code to process time sync messages from the serial port */
  48. #define TIME_HEADER "T" // Header tag for serial time sync message
  49. unsigned long processSyncMessage() {
  50. unsigned long pctime = 0L;
  51. const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013
  52. if(Serial.find(TIME_HEADER)) {
  53. pctime = Serial.parseInt();
  54. return pctime;
  55. if( pctime < DEFAULT_TIME) { // check the value is a valid time (greater than Jan 1 2013)
  56. pctime = 0L; // return 0 to indicate that the time is not valid
  57. }
  58. }
  59. return pctime;
  60. }
  61. void printDigits(int digits){
  62. // utility function for digital clock display: prints preceding colon and leading 0
  63. Serial.print(":");
  64. if(digits < 10)
  65. Serial.print('0');
  66. Serial.print(digits);
  67. }