WS_RTC.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 it 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. // 获取当前时间戳
  57. time_t currentTime = timeClient.getEpochTime();
  58. 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
  59. {
  60. timeClient.update();
  61. currentTime = timeClient.getEpochTime();
  62. }
  63. // Converts the current timestamp to a tm structure
  64. struct tm *localTime = localtime(&currentTime);
  65. // Set the network time to DS3231
  66. 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);
  67. // Turn off WiFi connection
  68. // WiFi.disconnect(true);
  69. // WiFi.mode(WIFI_OFF);
  70. }
  71. void RTC_Loop()
  72. {
  73. DS3231_ReadTime();
  74. if(Time[2] == RTC_OPEN_Hour && Time[1] == RTC_OPEN_Min && RTC_Flag == 1 && RTC_Open_OK == 1){ // Open all relays
  75. RTC_Open_OK =0;
  76. digitalWrite(GPIO_PIN_CH1, HIGH); // Open CH1 relay
  77. digitalWrite(GPIO_PIN_CH2, HIGH); // Open CH2 relay
  78. digitalWrite(GPIO_PIN_CH3, HIGH); // Open CH3 relay
  79. digitalWrite(GPIO_PIN_CH4, HIGH); // Open CH4 relay
  80. digitalWrite(GPIO_PIN_CH5, HIGH); // Open CH5 relay
  81. digitalWrite(GPIO_PIN_CH6, HIGH); // Open CH6 relay
  82. memset(Relay_Flag,1, sizeof(Relay_Flag));
  83. Buzzer_PWM(300);
  84. printf("|*** Relay ALL on ***|\r\n");
  85. }
  86. else if(Time[2] == RTC_Closs_Hour && Time[1] == RTC_Closs_Min && RTC_Flag == 1 && RTC_Closs_OK == 1){ // Turn off all relays
  87. RTC_Closs_OK = 0;
  88. digitalWrite(GPIO_PIN_CH1, LOW); // Turn off CH1 relay
  89. digitalWrite(GPIO_PIN_CH2, LOW); // Turn off CH2 relay
  90. digitalWrite(GPIO_PIN_CH3, LOW); // Turn off CH3 relay
  91. digitalWrite(GPIO_PIN_CH4, LOW); // Turn off CH4 relay
  92. digitalWrite(GPIO_PIN_CH5, LOW); // Turn off CH5 relay
  93. digitalWrite(GPIO_PIN_CH6, LOW); // Turn off CH6 relay
  94. memset(Relay_Flag,0, sizeof(Relay_Flag));
  95. Buzzer_PWM(100);
  96. delay(100);
  97. Buzzer_PWM(100);
  98. printf("|*** Relay ALL off ***|\r\n");
  99. }
  100. if(RTC_Flag == 1 && Time[1] != RTC_OPEN_Min){
  101. RTC_Open_OK = 1;
  102. }if(RTC_Flag == 1 && Time[1] != RTC_Closs_Min){
  103. RTC_Closs_OK = 1;
  104. }
  105. }