Advertisement
pleasedontcode

"RTC Synchronization" rev_24

Jun 13th, 2024
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: "RTC Synchronization"
  13.     - Source Code NOT compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2024-06-13 15:52:27
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* initialize gsm */
  21. /****** SYSTEM REQUIREMENT 2 *****/
  22.     /* align one time per day the clock from gsm to RTC */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <SoftwareSerial.h> //https://github.com/plerup/espsoftwareserial
  27. #include <Wire.h>
  28. #include <Sim800L.h>    //https://github.com/vittorioexp/Sim800L-Arduino-Library-revised
  29. #include <DS3231.h>    //https://github.com/NorthernWidget/DS3231
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34. void updateOutputs(void);
  35. void inputDateFromSerial(void);
  36. void syncClockFromGSM(void);
  37.  
  38. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  39. const uint8_t gsm_SIM800L_RING_PIN_D3        = 3;
  40.  
  41. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  42. const uint8_t gsm_SIM800L_RST_PIN_D2        = 2;
  43. const uint8_t gsm_SIM800L_DTR_PIN_D4        = 4;
  44.  
  45. /***** DEFINITION OF Software Serial *****/
  46. const uint8_t gsm_SIM800L_Serial_PIN_SERIAL_TX_A0        = A0;
  47. const uint8_t gsm_SIM800L_Serial_PIN_SERIAL_RX_A1        = A1;
  48. EspSoftwareSerial::UART gsm_SIM800L_Serial;
  49.  
  50. /***** DEFINITION OF I2C PINS *****/
  51. const uint8_t clock_DS3231_I2C_PIN_SDA_A4        = A4;
  52. const uint8_t clock_DS3231_I2C_PIN_SCL_A5        = A5;
  53. const uint8_t clock_DS3231_I2C_SLAVE_ADDRESS        = 104;
  54.  
  55. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  56. /***** used to store raw data *****/
  57. bool    gsm_SIM800L_RST_PIN_D2_rawData        = 0;
  58. bool    gsm_SIM800L_DTR_PIN_D4_rawData        = 0;
  59.  
  60. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  61. /***** used to store data after characteristic curve transformation *****/
  62. float    gsm_SIM800L_RST_PIN_D2_phyData        = 0.0;
  63. float    gsm_SIM800L_DTR_PIN_D4_phyData        = 0.0;
  64.  
  65. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  66. Sim800L GSM(gsm_SIM800L_Serial_PIN_SERIAL_RX_A1, gsm_SIM800L_Serial_PIN_SERIAL_TX_A0, gsm_SIM800L_RST_PIN_D2, gsm_SIM800L_DTR_PIN_D4, gsm_SIM800L_RING_PIN_D3); // Initialize Sim800L object
  67. DS3231 myRTC; // Initialize DS3231 object
  68.  
  69. // Variables for DS3231
  70. byte year, month, date, dow, hour, minute, second;
  71. bool century = false, h12Flag, pmFlag;
  72. unsigned long lastSyncTime = 0; // Variable to store the last sync time
  73. const unsigned long syncInterval = 86400000; // 24 hours in milliseconds
  74.  
  75. void setup(void)
  76. {
  77.     // put your setup code here, to run once:
  78.  
  79.     pinMode(gsm_SIM800L_RING_PIN_D3,    INPUT_PULLUP);
  80.  
  81.     pinMode(gsm_SIM800L_RST_PIN_D2,     OUTPUT);
  82.     pinMode(gsm_SIM800L_DTR_PIN_D4,     OUTPUT);
  83.  
  84.     gsm_SIM800L_Serial.begin(9600, SWSERIAL_8N1, gsm_SIM800L_Serial_PIN_SERIAL_RX_A1, gsm_SIM800L_Serial_PIN_SERIAL_TX_A0, false);
  85.  
  86.     /* initialize gsm */
  87.     GSM.begin(4800); // Initialize GSM module
  88.     GSM.updateRtc(+1); // Update RTC with UTC offset
  89.  
  90.     // Initialize I2C communication for DS3231
  91.     Wire.begin(clock_DS3231_I2C_PIN_SDA_A4, clock_DS3231_I2C_PIN_SCL_A5);
  92.  
  93.     // Start the serial port for debugging
  94.     Serial.begin(57600);
  95.  
  96.     // Request the time correction on the Serial
  97.     delay(4000);
  98.     Serial.println("Format YYMMDDwhhmmssx");
  99.     Serial.println("Where YY = Year (ex. 20 for 2020)");
  100.     Serial.println("      MM = Month (ex. 04 for April)");
  101.     Serial.println("      DD = Day of month (ex. 09 for 9th)");
  102.     Serial.println("      w  = Day of week from 1 to 7, 1 = Sunday (ex. 5 for Thursday)");
  103.     Serial.println("      hh = hours in 24h format (ex. 09 for 9AM or 21 for 9PM)");
  104.     Serial.println("      mm = minutes (ex. 02)");
  105.     Serial.println("      ss = seconds (ex. 42)");
  106.     Serial.println("Example for input : 2004095090242x");
  107.     Serial.println("-----------------------------------------------------------------------------");
  108.     Serial.println("Please enter the current time to set on DS3231 ended by 'x':");
  109. }
  110.  
  111. void loop(void)
  112. {
  113.     // put your main code here, to run repeatedly:
  114.  
  115.     updateOutputs(); // Refresh output data
  116.  
  117.     // If something is coming in on the serial line, it's
  118.     // a time correction so set the clock accordingly.
  119.     if (Serial.available()) {
  120.         inputDateFromSerial();
  121.  
  122.         myRTC.setClockMode(false);  // set to 24h
  123.  
  124.         myRTC.setYear(year);
  125.         myRTC.setMonth(month);
  126.         myRTC.setDate(date);
  127.         myRTC.setDoW(dow);
  128.         myRTC.setHour(hour);
  129.         myRTC.setMinute(minute);
  130.         myRTC.setSecond(second);
  131.  
  132.         // Give time at next five seconds
  133.         for (uint8_t i = 0; i < 5; i++){
  134.             delay(1000);
  135.             Serial.print(myRTC.getYear(), DEC);
  136.             Serial.print("-");
  137.             Serial.print(myRTC.getMonth(century), DEC);
  138.             Serial.print("-");
  139.             Serial.print(myRTC.getDate(), DEC);
  140.             Serial.print(" ");
  141.             Serial.print(myRTC.getHour(h12Flag, pmFlag), DEC); //24-hr
  142.             Serial.print(":");
  143.             Serial.print(myRTC.getMinute(), DEC);
  144.             Serial.print(":");
  145.             Serial.println(myRTC.getSecond(), DEC);
  146.         }
  147.  
  148.         // Notify that we are ready for the next input
  149.         Serial.println("Please enter the current time to set on DS3231 ended by 'x':");
  150.     }
  151.  
  152.     /* align one time per day the clock from gsm to RTC */
  153.     if (millis() - lastSyncTime >= syncInterval) {
  154.         syncClockFromGSM();
  155.         lastSyncTime = millis();
  156.     }
  157.  
  158.     delay(1000);
  159. }
  160.  
  161. void updateOutputs()
  162. {
  163.     digitalWrite(gsm_SIM800L_RST_PIN_D2, gsm_SIM800L_RST_PIN_D2_rawData);
  164.     digitalWrite(gsm_SIM800L_DTR_PIN_D4, gsm_SIM800L_DTR_PIN_D4_rawData);
  165. }
  166.  
  167. void inputDateFromSerial() {
  168.     // Call this if you notice something coming in on
  169.     // the serial port. The stuff coming in should be in
  170.     // the order YYMMDDwHHMMSS, with an 'x' at the end.
  171.     boolean isStrComplete = false;
  172.     char inputChar;
  173.     byte temp1, temp2;
  174.     char inputStr[20];
  175.  
  176.     uint8_t currentPos = 0;
  177.     while (!isStrComplete) {
  178.         if (Serial.available()) {
  179.             inputChar = Serial.read();
  180.             inputStr[currentPos] = inputChar;
  181.             currentPos += 1;
  182.  
  183.             // Check if string complete (end with "x")
  184.             if (inputChar == 'x') {
  185.                 isStrComplete = true;
  186.             }
  187.         }
  188.     }
  189.     Serial.println(inputStr);
  190.  
  191.     // Find the end of char "x"
  192.     int posX = -1;
  193.     for(uint8_t i = 0; i < 20; i++) {
  194.         if(inputStr[i] == 'x') {
  195.             posX = i;
  196.             break;
  197.         }
  198.     }
  199.  
  200.     // Consider 0 character in ASCII
  201.     uint8_t zeroAscii = '0';
  202.  
  203.     // Read Year first
  204.     temp1 = (byte)inputStr[posX - 13] - zeroAscii;
  205.     temp2 = (byte)inputStr[posX - 12] - zeroAscii;
  206.     year = temp1 * 10 + temp2;
  207.  
  208.     // now month
  209.     temp1 = (byte)inputStr[posX - 11] - zeroAscii;
  210.     temp2 = (byte)inputStr[posX - 10] - zeroAscii;
  211.     month = temp1 * 10 + temp2;
  212.  
  213.     // now date
  214.     temp1 = (byte)inputStr[posX - 9] - zeroAscii;
  215.     temp2 = (byte)inputStr[posX - 8] - zeroAscii;
  216.     date = temp1 * 10 + temp2;
  217.  
  218.     // now Day of Week
  219.     dow = (byte)inputStr[posX - 7] - zeroAscii;
  220.  
  221.     // now Hour
  222.     temp1 = (byte)inputStr[posX - 6] - zeroAscii;
  223.     temp2 = (byte)inputStr[posX - 5] - zeroAscii;
  224.     hour = temp1 * 10 + temp2;
  225.  
  226.     // now Minute
  227.     temp1 = (byte)inputStr[posX - 4] - zeroAscii;
  228.     temp2 = (byte)inputStr[posX - 3] - zeroAscii;
  229.     minute = temp1 * 10 + temp2;
  230.  
  231.     // now Second
  232.     temp1 = (byte)inputStr[posX - 2] - zeroAscii;
  233.     temp2 = (byte)inputStr[posX - 1] - zeroAscii;
  234.     second = temp1 * 10 + temp2;
  235. }
  236.  
  237. void syncClockFromGSM() {
  238.     // Get the current time from the GSM module
  239.     GSM.RTCtime(&date, &month, &year, &hour, &minute, &second);
  240.  
  241.     // Set the DS3231 RTC with the GSM time
  242.     myRTC.setClockMode(false);  // set to 24h
  243.     myRTC.setYear(year);
  244.     myRTC.setMonth(month);
  245.     myRTC.setDate(date);
  246.     myRTC.setDoW(dow);
  247.     myRTC.setHour(hour);
  248.     myRTC.setMinute(minute);
  249.     myRTC.setSecond(second);
  250.  
  251.     Serial.println("RTC synchronized with GSM time.");
  252. }
  253.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement