WS_RTC.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include "WS_RTC.h"
  2. WiFiUDP ntpUDP;
  3. NTPClient timeClient(ntpUDP, "pool.ntp.org");
  4. uint8_t Time[8] = {0};
  5. char *week[] = {"SUN","Mon","Tues","Wed","Thur","Fri","Sat"};
  6. bool RTC_Open_OK = 1; // RTC Timing Switcher flag
  7. bool RTC_Closs_OK = 1; // RTC Timing Switcher flag
  8. void RTC_Init() {
  9. Wire.begin(I2C_SDA, I2C_SCL);
  10. delay(1000);
  11. }
  12. void DS3231_ReadTime()
  13. {
  14. Wire.beginTransmission(DS3231_I2C_ADDR);
  15. Wire.write(0x00); // Start from seconds register
  16. Wire.endTransmission();
  17. Wire.requestFrom(DS3231_I2C_ADDR, 7);
  18. if (Wire.available() >= 7) {
  19. Time[0] = Wire.read(); // Seconds
  20. Time[1] = Wire.read(); // Minutes
  21. Time[2] = Wire.read(); // Hours
  22. Time[3] = Wire.read(); // Weekday
  23. Time[4] = Wire.read(); // Day
  24. Time[5] = Wire.read(); // Month
  25. Time[6] = Wire.read(); // Year
  26. Time[0] = Time[0]&0x7F; // Seconds
  27. Time[1] = Time[1]&0x7F; // Minutes
  28. Time[2] = Time[2]&0x3F; // Hours
  29. Time[3] = Time[3]&0x07; // Weekday
  30. Time[4] = Time[4]&0x3F; // Day
  31. Time[5] = Time[5]&0x1F; // Month
  32. Time[6] = Time[6]; // Year
  33. Time[7] = 0x20; // Year
  34. }
  35. }
  36. void DS3231_SetTime(uint8_t sec, uint8_t min, uint8_t hour, uint8_t dayOfWeek, uint8_t day, uint8_t month, uint8_t year) {
  37. Wire.beginTransmission(DS3231_I2C_ADDR);
  38. Wire.write(0); // 秒寄存器
  39. Wire.write(DecToBcd(sec));
  40. Wire.write(DecToBcd(min));
  41. Wire.write(DecToBcd(hour));
  42. Wire.write(DecToBcd(dayOfWeek));
  43. Wire.write(DecToBcd(day));
  44. Wire.write(DecToBcd(month));
  45. Wire.write(DecToBcd(year));
  46. Wire.endTransmission();
  47. printf("Completion time update\r\n");
  48. }
  49. uint8_t DecToBcd(uint8_t val) {
  50. return ((val / 10 * 16) + (val % 10));
  51. }
  52. void Acquisition_time() { // Get the network time and set to DS3231 to be called after the WIFI connection is successful
  53. timeClient.begin();
  54. timeClient.setTimeOffset(8 * 3600); // Set the time zone, here use East 8 (Beijing time)
  55. timeClient.update();
  56. time_t currentTime = timeClient.getEpochTime();
  57. while(currentTime < 1609459200) // Using the current timestamp to compare with a known larger value,1609459200 is a known larger timestamp value that corresponds to January 1, 2021
  58. {
  59. timeClient.update();
  60. currentTime = timeClient.getEpochTime();
  61. }
  62. struct tm *localTime = localtime(&currentTime);
  63. DS3231_SetTime(localTime->tm_sec, localTime->tm_min, localTime->tm_hour, localTime->tm_wday, localTime->tm_mday, localTime->tm_mon + 1, localTime->tm_year - 100);
  64. // Turn off the WiFi connection
  65. // WiFi.disconnect(true);
  66. // WiFi.mode(WIFI_OFF);
  67. }
  68. void RTC_Loop()
  69. {
  70. DS3231_ReadTime();
  71. if(Time[2] == RTC_OPEN_Hour && Time[1] == RTC_OPEN_Min && RTC_Flag == 1 && RTC_Open_OK == 1){ // Turn on all relays on time
  72. RTC_Open_OK =0;
  73. digitalWrite(GPIO_PIN_CH1, HIGH); // Open CH1 relay
  74. digitalWrite(GPIO_PIN_CH2, HIGH); // Open CH2 relay
  75. digitalWrite(GPIO_PIN_CH3, HIGH); // Open CH3 relay
  76. digitalWrite(GPIO_PIN_CH4, HIGH); // Open CH4 relay
  77. digitalWrite(GPIO_PIN_CH5, HIGH); // Open CH5 relay
  78. digitalWrite(GPIO_PIN_CH6, HIGH); // Open CH6 relay
  79. memset(Relay_Flag,1, sizeof(Relay_Flag));
  80. Buzzer_PWM(300);
  81. printf("|*** Relay ALL on ***|\r\n");
  82. }
  83. else if(Time[2] == RTC_Closs_Hour && Time[1] == RTC_Closs_Min && RTC_Flag == 1 && RTC_Closs_OK == 1){ // Turn off all relays regularly
  84. RTC_Closs_OK = 0;
  85. digitalWrite(GPIO_PIN_CH1, LOW); // Turn off CH1 relay
  86. digitalWrite(GPIO_PIN_CH2, LOW); // Turn off CH2 relay
  87. digitalWrite(GPIO_PIN_CH3, LOW); // Turn off CH3 relay
  88. digitalWrite(GPIO_PIN_CH4, LOW); // Turn off CH4 relay
  89. digitalWrite(GPIO_PIN_CH5, LOW); // Turn off CH5 relay
  90. digitalWrite(GPIO_PIN_CH6, LOW); // Turn off CH6 relay
  91. memset(Relay_Flag,0, sizeof(Relay_Flag));
  92. Buzzer_PWM(100);
  93. delay(100);
  94. Buzzer_PWM(100);
  95. printf("|*** Relay ALL off ***|\r\n");
  96. }
  97. if(RTC_Flag == 1 && Time[1] != RTC_OPEN_Min){
  98. RTC_Open_OK = 1;
  99. }if(RTC_Flag == 1 && Time[1] != RTC_Closs_Min){
  100. RTC_Closs_OK = 1;
  101. }
  102. }