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.

107 lines
3.1 KiB

  1. /*
  2. * TimeRTCLogger.pde
  3. * example code illustrating adding and subtracting Time.
  4. *
  5. * this sketch logs pin state change events
  6. * the time of the event and time since the previous event is calculated and sent to the serial port.
  7. */
  8. #include <TimeLib.h>
  9. #include <Wire.h>
  10. #include <DS1307RTC.h> // a basic DS1307 library that returns time as a time_t
  11. const int nbrInputPins = 6; // monitor 6 digital pins
  12. const int inputPins[nbrInputPins] = {2,3,4,5,6,7}; // pins to monitor
  13. boolean state[nbrInputPins] ; // the state of the monitored pins
  14. time_t prevEventTime[nbrInputPins] ; // the time of the previous event
  15. void setup() {
  16. Serial.begin(9600);
  17. setSyncProvider(RTC.get); // the function to sync the time from the RTC
  18. for(int i=0; i < nbrInputPins; i++){
  19. pinMode( inputPins[i], INPUT);
  20. // uncomment these lines if pull-up resistors are wanted
  21. // pinMode( inputPins[i], INPUT_PULLUP);
  22. // state[i] = HIGH;
  23. }
  24. }
  25. void loop()
  26. {
  27. for(int i=0; i < nbrInputPins; i++)
  28. {
  29. boolean val = digitalRead(inputPins[i]);
  30. if(val != state[i])
  31. {
  32. time_t duration = 0; // the time since the previous event
  33. state[i] = val;
  34. time_t timeNow = now();
  35. if(prevEventTime[i] > 0)
  36. // if this was not the first state change, calculate the time from the previous change
  37. duration = duration = timeNow - prevEventTime[i];
  38. logEvent(inputPins[i], val, timeNow, duration ); // log the event
  39. prevEventTime[i] = timeNow; // store the time for this event
  40. }
  41. }
  42. }
  43. void logEvent( int pin, boolean state, time_t timeNow, time_t duration)
  44. {
  45. Serial.print("Pin ");
  46. Serial.print(pin);
  47. if( state == HIGH)
  48. Serial.print(" went High at ");
  49. else
  50. Serial.print(" went Low at ");
  51. showTime(timeNow);
  52. if(duration > 0){
  53. // only display duration if greater than 0
  54. Serial.print(", Duration was ");
  55. showDuration(duration);
  56. }
  57. Serial.println();
  58. }
  59. void showTime(time_t t){
  60. // display the given time
  61. Serial.print(hour(t));
  62. printDigits(minute(t));
  63. printDigits(second(t));
  64. Serial.print(" ");
  65. Serial.print(day(t));
  66. Serial.print(" ");
  67. Serial.print(month(t));
  68. Serial.print(" ");
  69. Serial.print(year(t));
  70. }
  71. void printDigits(int digits){
  72. // utility function for digital clock display: prints preceding colon and leading 0
  73. Serial.print(":");
  74. if(digits < 10)
  75. Serial.print('0');
  76. Serial.print(digits);
  77. }
  78. void showDuration(time_t duration){
  79. // prints the duration in days, hours, minutes and seconds
  80. if(duration >= SECS_PER_DAY){
  81. Serial.print(duration / SECS_PER_DAY);
  82. Serial.print(" day(s) ");
  83. duration = duration % SECS_PER_DAY;
  84. }
  85. if(duration >= SECS_PER_HOUR){
  86. Serial.print(duration / SECS_PER_HOUR);
  87. Serial.print(" hour(s) ");
  88. duration = duration % SECS_PER_HOUR;
  89. }
  90. if(duration >= SECS_PER_MIN){
  91. Serial.print(duration / SECS_PER_MIN);
  92. Serial.print(" minute(s) ");
  93. duration = duration % SECS_PER_MIN;
  94. }
  95. Serial.print(duration);
  96. Serial.print(" second(s) ");
  97. }