macca-nz

Manually_Set_RTC_ESP32

Jul 10th, 2021 (edited)
149
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.    MIT License
  3.  
  4.   Copyright (c) 2021 Felix Biego
  5.  
  6.   Permission is hereby granted, free of charge, to any person obtaining a copy
  7.   of this software and associated documentation files (the "Software"), to deal
  8.   in the Software without restriction, including without limitation the rights
  9.   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10.   copies of the Software, and to permit persons to whom the Software is
  11.   furnished to do so, subject to the following conditions:
  12.  
  13.   The above copyright notice and this permission notice shall be included in all
  14.   copies or substantial portions of the Software.
  15.  
  16.   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17.   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18.   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19.   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20.   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21.   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22.   SOFTWARE.
  23. */
  24.  
  25. /*  This sketch will let you manually update the ESP32 Real Time Clock via the IDE Serial Monitor
  26.  *  
  27.  *  You enter Update Mode by pressing the BOOT/Flash button on an ESP Dev board
  28.  *  I used the onboard LED to blink while the system is waiting for an update
  29.  *  Only a "true" input will be accepted, no leading zeros
  30.  *  The int format to enter is: day/month/year hour:minute:second IE: 10/7/2021 16:30:0
  31.  *  
  32.  *  Author: R McCleery macca448@gmail.com
  33.  *  Date:   July 10th 2021
  34.  */
  35.  
  36. #include <ESP32Time.h>
  37. #define UDT 0                                 // DEV board BOOT/FLASH button
  38. #define LED 2                                 // Onboard LED on ESP32
  39. #define DUTY_CYCLE 50                         // Single millis() duty cycle for the loop()
  40.  
  41. ESP32Time rtc;                                // Real Time Clock Object
  42.  
  43. long      currentTime = 0;
  44.  
  45. uint8_t   count = 0, blinkCount = 0, udtState,
  46.           lastUDTstate = HIGH, ledState = LOW;
  47.  
  48. int       Sc, Mn, Hr, Dy, Mh, Yr;
  49.          
  50. bool      updateDateTime = false, ledUpdate = false,
  51.           UDTpressed = false, blink = false, sPtime = true;
  52.  
  53. bool inRange(int val, int minimum, int maximum){
  54.     return ((minimum <= val) && (val <= maximum));
  55. }
  56.  
  57. void setup() {
  58.     Serial.begin(115200);
  59.     pinMode(UDT, INPUT);
  60.     pinMode(LED, OUTPUT);
  61.     digitalWrite(LED, ledState);
  62. }
  63.  
  64. void loop() {
  65.     udtState = digitalRead(UDT);
  66.         if(udtState != lastUDTstate){
  67.             UDTpressed = true;
  68.     }
  69.  
  70.     if(millis() - currentTime > DUTY_CYCLE){
  71.         currentTime = millis();
  72.         count++;
  73.         blinkCount++;
  74.             if(UDTpressed){
  75.                 if(udtState == LOW){
  76.                     Serial.println("**********   Update Time and Date   **********");
  77.                     Serial.println("********** Fast Blinking LED = True **********");
  78.                     Serial.println("**********     No Leading ZERO's    **********");
  79.                     Serial.println("* Type in: dd/mm/yyyy hh:mm:ss to update RTC *");                
  80.                     updateDateTime = true;
  81.                     blink = true;
  82.                     UDTpressed = false;
  83.                 }
  84.             }
  85.            
  86.             if(sPtime){
  87.                 if(count >= 100){            
  88.                     Serial.println(rtc.getTime("%A, %B %d %Y %H:%M:%S"));
  89.                     //formating options  http://www.cplusplus.com/reference/ctime/strftime/
  90.                     count = 0;
  91.                 }              
  92.             }
  93.  
  94.             if(blink){
  95.                 if(blinkCount > 5){
  96.                     blinkCount = 0;
  97.                     ledState = !ledState;
  98.                     ledUpdate = true;
  99.                 }
  100.             }
  101.            
  102.             if(ledUpdate){
  103.                 digitalWrite(LED, ledState);
  104.                 ledUpdate = false;
  105.             }
  106.     }  // END millis() statements
  107.  
  108.     if(updateDateTime){
  109.         sPtime = false;  
  110.             if(Serial.available() > 0) {
  111.                 Dy = Serial.parseInt();
  112.                 Mh = Serial.parseInt();
  113.                 Yr = Serial.parseInt();
  114.                 Hr = Serial.parseInt();
  115.                 Mn = Serial.parseInt();
  116.                 Sc = Serial.parseInt();
  117.                 Serial.printf("I have understood %u/%u/%u %u:%u:%u\n", Dy, Mh, Yr, Hr, Mn, Sc);
  118.                 // Values being set ( Day(%d), Month(%m), Year(%Y), Hr(%H), Min(%M), Sec(%S) )  
  119.                 boolean validDate = (inRange(Dy, 1, 31) && inRange(Mh, 1, 12) && inRange(Yr, 2021, 2031));
  120.                 boolean validTime = (inRange(Hr, 0, 23) && inRange(Mn, 0, 59) && inRange(Sc, 0, 59));
  121.                     if(validTime && validDate){    
  122.                         rtc.setTime(Sc, Mn, Hr, Dy, Mh, Yr);
  123.                         Serial.flush();
  124.                         sPtime = true;        
  125.                         blink = false;
  126.                         ledState = LOW;
  127.                         ledUpdate = true;
  128.                         updateDateTime = false;
  129.                     }
  130.             }
  131.     }
  132.     lastUDTstate = udtState;        // Update Last button State
  133. }
RAW Paste Data Copied