macca-nz

Time without a network ESP and AVR

May 2nd, 2023 (edited)
1,041
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 8.92 KB | Source Code | 0 0
  1. /*
  2.    MIT License
  3.  
  4.   Copyright (c) 2023
  5.   Author: R McCleery [email protected]
  6.   Date:   May 3rd 2023
  7.  
  8.   Permission is hereby granted, free of charge, to any person obtaining a copy
  9.   of this software and associated documentation files (the "Software"), to deal
  10.   in the Software without restriction, including without limitation the rights
  11.   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12.   copies of the Software, and to permit persons to whom the Software is
  13.   furnished to do so, subject to the following conditions:
  14.  
  15.   The above copyright notice and this permission notice shall be included in all
  16.   copies or substantial portions of the Software.
  17.  
  18.   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19.   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20.   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21.   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22.   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23.   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24.   SOFTWARE.
  25. */
  26.  
  27. /*  
  28.  *  PLEASE READ ME Before you begin
  29.  *  
  30.  *  #1  This sketch will work with any controller that supports hardware "Serial.begin(BAUD)"
  31.  *  
  32.  *  #2  When you set the time it is created by syncing your controller millis() to UTC EPOCH
  33.  *      including any timezone offsets defined by #8 below
  34.  *      
  35.  *  #3  The beauty of this sketch is you don't need a network connection or RTC module to make time
  36.  *  
  37.  *  #4  On ESP Dev boards the FLASH/BOOT button is used to enter "Update Time Mode" UDT button
  38.  *  
  39.  *  #5  On any other controllers you need to add a button wired between your UDT pin and GND
  40.  *  
  41.  *  #6  The "LED" has been defined as "LED_BUILTIN" which is supported on Uno, Nano and ESP8266
  42.  *      If you either don't have an on-board LED or it's a different pin your can change it and/or
  43.  *      wire up a LED to the PIN you define
  44.  *      The LED is used as in indicator when in "Update Time Mode"
  45.  *      
  46.  *  #7  You enter your local time as current "UTC" time which means
  47.  *      Lets say you are in a timezone that is UTC -5:00 and the local time is 12:00:00 [PM] ie:noon
  48.  *      Be mindful of daylight offset so if in summer time it would be UTC -4:00 if your timezone supports it.
  49.  *      You would enter 12 + 4 = 16 for the hour component if in daylight savings time
  50.  *      
  51.  *  #8  For correct local time you must enter your "POSIX" string as noted
  52.  *      Link to a database for "POSIX" strings
  53.  *      https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv
  54.  *      This string will take care of your zone offset to UTC
  55.  *      
  56.  *  #9  You set your clock by pressing the button with the "Serial Monitor" Opened
  57.  *      Once you are in "Update Time Mode" you must enter the time in the correct format to continue
  58.  *      The correct format is dd/mm/yyyy hh:mm:ss with NO leading zeros
  59.  *      EG Local Time is 12:00:00 [PM] and it's the 3rd of May 2023
  60.  *      You'll enter 3/5/2023 16:0:0 the hit the return key and the time will start
  61.  *      
  62.  *  #10 You can download the "ezTime" library via the Arduino IDE library manager
  63.  *      see the Github Manual for more options   https://github.com/ropg/ezTime
  64.  *      
  65.  *  #11 *******   VERY IMPORTANT TASK TO DISABLE NETWORK IN "exTime"  ********
  66.  *      ******* Because we won't be using and external time sources   ********
  67.  *      ***** We have to comment out one line in the "exTime.h" file  ********
  68.  *      ***** After you install the "ezTime" library navigate to your ********
  69.  *      ***** "libraries" folder and then open "\ezTime\src\ezTime.h" ********
  70.  *      ***** just a few lines down on line 11 add "//" to   *****************
  71.  *      *** "//#define EZTIME_NETWORK_ENABLE" to disable it  *****************
  72.  *      ******* Save and close the file then you can compile *****************
  73.  *      ******* and upload your new clock sketch             *****************
  74.  *      
  75.  *  #12 Look for "//************* XXXXX ************" for user configuration options
  76.  *
  77.  */
  78.  
  79. //************ If you're using an ESP8266 uncomment this define *************
  80. //#define MY_ESP
  81.  
  82. //************ Enter the POSIX string for your Timezone location *************
  83. //************             See READ ME #8 above                  *************
  84. #define TZ_INFO "NZST-12NZDT,M9.5.0,M4.1.0/3"
  85.  
  86. //Library to use our controllers clock for "Human Readable Local Time"
  87. #include <ezTime.h>                                
  88. Timezone myTZ;
  89.  
  90. //************ LED and Button PIN here ****************************************
  91. //* If you areusing an ESP you can set "UDT 0" to use the FLASH/BOOT button ***
  92. #define LED LED_BUILTIN
  93. #define UDT 3
  94.  
  95. //Non-blocking timer for button de-bounce and LED Blink functions
  96. #define DUTY_CYCLE 50                              
  97.  
  98. //Variables
  99. uint32_t  previousTime;
  100. uint8_t   count = 0, blinkCount = 0, udtState,
  101.           lastUDTstate = HIGH, ledState, lastSec;
  102. int       Sc = 0, Mn = 0, Hr = 0, Dy = 1, Mh = 1, Yr = 2023;          
  103. bool      updateDateTime = false, ledUpdate = false,
  104.           UDTpressed = false, blink = false, sPtime = true;
  105.  
  106. //To test that you entered the time correctly
  107. bool inRange(int val, int minimum, int maximum){
  108.     return ((minimum <= val) && (val <= maximum));
  109. }
  110.  
  111. void myClock(void){
  112.     Serial.println(myTZ.dateTime("l F jS Y g~:i~:s [A] [~U~T~C P]"));
  113.     //formating options found at https://github.com/ropg/ezTime
  114.     lastSec = myTZ.second();
  115.     return;
  116. }
  117.  
  118. void setup() {
  119.     Serial.begin(115200);
  120.     pinMode(LED, OUTPUT);
  121.   #if defined MY_ESP
  122.     digitalWrite(LED, HIGH);
  123.   #else
  124.     digitalWrite(LED, LOW);
  125.   #endif
  126.     pinMode(UDT, INPUT_PULLUP);
  127.     myTZ.setPosix(TZ_INFO);
  128.     UTC.setTime(Sc, Mn, Hr, Dy, Mh, Yr);
  129.     lastSec = myTZ.second();
  130.    
  131. }
  132.  
  133. void loop() {
  134.  
  135.     //Watching for a button press
  136.     udtState = digitalRead(UDT);
  137.         if(udtState != lastUDTstate){
  138.             UDTpressed = true;
  139.             previousTime = millis();
  140.     }
  141.  
  142.     //Once your clock has been set this runs
  143.     if(sPtime){
  144.         if(myTZ.second() != lastSec){
  145.             myClock();
  146.         }
  147.     }
  148.  
  149.     //Non-Blocking button de-bounce and led blink timer function
  150.     if(millis() - previousTime > DUTY_CYCLE){
  151.         previousTime = millis();
  152.         count++;
  153.         blinkCount++;
  154.             if(UDTpressed){
  155.                 if(udtState == LOW){
  156.                     Serial.println("**********   Update Time and Date   **********");
  157.                     Serial.println("********** Fast Blinking LED = True **********");
  158.                     Serial.println("**********     No Leading ZERO's    **********\n");
  159.                     Serial.println("\n*Enter Current myTZ Time in following Format*");
  160.                     Serial.println("\n** System will Calculate Your Local Time ***\n");
  161.                     Serial.println("\n* Type in: dd/mm/yyyy hh:mm:ss to update RTC *\n");                
  162.                     updateDateTime = true;
  163.                     blink = true;
  164.                 }
  165.                 UDTpressed = false;
  166.             }
  167.             if(blink){
  168.                 if(blinkCount > 5){
  169.                     blinkCount = 0;
  170.                     ledState = !ledState;
  171.                     ledUpdate = true;
  172.                 }
  173.             }
  174.             if(ledUpdate){
  175.                 digitalWrite(LED, ledState);
  176.                 ledUpdate = false;
  177.             }
  178.     }  // END millis() statements
  179.  
  180.     if(updateDateTime){
  181.         sPtime = false;  
  182.             if(Serial.available() > 0) {
  183.                 Dy = Serial.parseInt();
  184.                 Mh = Serial.parseInt();
  185.                 Yr = Serial.parseInt();
  186.                 Hr = Serial.parseInt();
  187.                 Mn = Serial.parseInt();
  188.                 Sc = Serial.parseInt();
  189.  
  190.                 char buf[40];
  191.                 sprintf(buf,"I have understood %u/%u/%u %u:%u:%u\n\n", Dy, Mh, Yr, Hr, Mn, Sc);
  192.                 Serial.println(buf);
  193.                
  194.                 // Values being set ( Day(%d), Month(%m), Year(%Y), Hr(%H), Min(%M), Sec(%S) )  
  195.                 boolean validDate = (inRange(Dy, 1, 31) && inRange(Mh, 1, 12) && inRange(Yr, 2021, 2031));
  196.                 boolean validTime = (inRange(Hr, 0, 23) && inRange(Mn, 0, 59) && inRange(Sc, 0, 59));
  197.                     if(validTime && validDate){    
  198.                         UTC.setTime(Hr, Mn, Sc, Dy, Mh, Yr);
  199.                         Serial.flush();
  200.                         sPtime = true;        
  201.                         blink = false;
  202.                       #if defined MY_ESP
  203.                         ledState = HIGH;
  204.                       #else
  205.                         ledState = LOW;
  206.                       #endif
  207.                         ledUpdate = true;
  208.                         updateDateTime = false;
  209.                     }
  210.             }
  211.     }
  212.     lastUDTstate = udtState;        // Update Last button State
  213. }
  214.  
Tags: Clock ezTime
Advertisement
Add Comment
Please, Sign In to add comment