WS_RTC.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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; // 秒
  27. Time[1] = Time[1]&0x7F; // 分钟
  28. Time[2] = Time[2]&0x3F; // 小时
  29. Time[3] = Time[3]&0x07; // 星期
  30. Time[4] = Time[4]&0x3F; // 日期
  31. Time[5] = Time[5]&0x1F; // 月份
  32. Time[6] = Time[6]; // 年份
  33. Time[7] = 0x20; // 年份
  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. 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. // Set the network time to DS3231
  64. 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);
  65. // Turn off WiFi connection
  66. // WiFi.disconnect(true);
  67. // WiFi.mode(WIFI_OFF);
  68. }
  69. void RTC_Loop()
  70. {
  71. DS3231_ReadTime();
  72. if(Time[2] == RTC_OPEN_Hour && Time[1] == RTC_OPEN_Min && RTC_Flag == 1 && RTC_Open_OK == 1){ // Open all relays
  73. RTC_Open_OK =0;
  74. digitalWrite(GPIO_PIN_CH1, HIGH); // Open CH1 relay
  75. digitalWrite(GPIO_PIN_CH2, HIGH); // Open CH2 relay
  76. digitalWrite(GPIO_PIN_CH3, HIGH); // Open CH3 relay
  77. digitalWrite(GPIO_PIN_CH4, HIGH); // Open CH4 relay
  78. digitalWrite(GPIO_PIN_CH5, HIGH); // Open CH5 relay
  79. digitalWrite(GPIO_PIN_CH6, HIGH); // Open CH6 relay
  80. memset(Relay_Flag,1, sizeof(Relay_Flag));
  81. Buzzer_PWM(300);
  82. printf("|*** Relay ALL on ***|\r\n");
  83. }
  84. else if(Time[2] == RTC_Closs_Hour && Time[1] == RTC_Closs_Min && RTC_Flag == 1 && RTC_Closs_OK == 1){ // Turn off all relays
  85. RTC_Closs_OK = 0;
  86. digitalWrite(GPIO_PIN_CH1, LOW); // Turn off CH1 relay
  87. digitalWrite(GPIO_PIN_CH2, LOW); // Turn off CH2 relay
  88. digitalWrite(GPIO_PIN_CH3, LOW); // Turn off CH3 relay
  89. digitalWrite(GPIO_PIN_CH4, LOW); // Turn off CH4 relay
  90. digitalWrite(GPIO_PIN_CH5, LOW); // Turn off CH5 relay
  91. digitalWrite(GPIO_PIN_CH6, LOW); // Turn off CH6 relay
  92. memset(Relay_Flag,0, sizeof(Relay_Flag));
  93. Buzzer_PWM(100);
  94. delay(100);
  95. Buzzer_PWM(100);
  96. printf("|*** Relay ALL off ***|\r\n");
  97. }
  98. if(RTC_Flag == 1 && Time[1] != RTC_OPEN_Min){
  99. RTC_Open_OK = 1;
  100. }if(RTC_Flag == 1 && Time[1] != RTC_Closs_Min){
  101. RTC_Closs_OK = 1;
  102. }
  103. }