#139

แชร์
ฝัง
  • เผยแพร่เมื่อ 16 พ.ค. 2024
  • I need to know the date & time for my project - but no DS3231 RTC (Real Time Clock) module is required.
    More Asian Brands Lower Price lcsc.com
    Sponsored by LCSC Electronics - More Asian Brands, Lower Prices, 4 Hours Ready for Shipping
    All this and more in my Github:
    github.com/RalphBacon/No-Real...
    So my current Home Alone project requires the Date and Time to work properly. I immediately ordered a small RTC shield for my Wemos D1 mini - only to quickly realise I should not have bothered as I can get the date and time very accurately from a Network Time Protocol server and keep a local, accurate clock that drifts very infrequently thanks to a wonderful library, courtesy of Arduino.cc
    And the local clock can be updated every so often by another call to the NTP pool, perhaps once a day or so.
    Works like a dream. Even better, should the call to the NTP fail there is no need to panic. Eventually, it will resync and all will be well again.
    Here we dive deep into the simple C++ code that is required to do all this with the help of a couple of libraries.
    In a future video, I'll show you how you can convert the UTC (universal coordinated time) to local time. Easy to do, and works worldwide.
    Links (these may be affiliated which means that they might help my channel at no cost to you)
    Wemos D1 Mini ESP8266 One is cheap but better value if you get 3 or 5. Also check out the different warehouses, in USA, CN and UK.
    www.banggood.com/Wemos-D1-Min...
    What I'm using for parameter and data, a micro SD card shield:
    www.banggood.com/3Pcs-WeMos-M...
    A power shield so you can it from a 7-12v supply (but the 5v phone charger on the ESP8266 board works too):
    www.banggood.com/3Pcs-WeMos-D...
    All about the NTP protocol:
    tttapa.github.io/ESP8266/Chap...
    List of all ports in use:
    en.wikipedia.org/wiki/List_of...
    More on time zones (I will implement this in my project code):
    github.com/JChristensen/Timezone
    If you like this video please give it a thumbs up, share it and if you're not already subscribed please consider doing so and joining me on my Arduinite journey!
    My channel and blog are here:
    ------------------------------------------------------------------
    / ralphbacon
    ralphbacon.blog
    ------------------------------------------------------------------
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 137

  • @101appsCoZa
    @101appsCoZa 4 ปีที่แล้ว

    great video. just what i was looking for! i needed to get a regular timestamp (every 24 hrs) and was scratching my head to work out how to do it. you showed me quickly and clearly! thanks.

    • @RalphBacon
      @RalphBacon  4 ปีที่แล้ว

      Yes, this works very well. I use it in my Home Alone project which has been running successfully for many months so its reliability has been proven too. Thanks for posting, all the way from South Africa where my daughter is right now!

  • @wires4auto
    @wires4auto 5 ปีที่แล้ว

    Brilliant video/ I really appreciate the extra notes added into the code for slow coaches like me. I don't have any projects that need this but it's great to keep in mind!

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว +1

      Always good to know there are solutions for most Arduino-type situations, that's for sure. Thanks for posting, Gordon, good to hear from you.

  • @garycrockett4477
    @garycrockett4477 3 ปีที่แล้ว +2

    Ralph, thanks for the VERY informative video. I've watched through two or three times now, making sure I understand what you're saying. Earlier today, I left a comment on another video about a D1 Mini acting as an NTP server to the local network. I think this project comes VERY close. Thanks again for all your hard work!

    • @RalphBacon
      @RalphBacon  3 ปีที่แล้ว

      Glad it was helpful! I did see your other comment but I didn't fancy who knows how many Internet users coming up the wires just to grab the time from my server!

    • @garycrockett4477
      @garycrockett4477 3 ปีที่แล้ว

      @@RalphBacon If the IP address of the NTP server was 192.168.1.xxx, then only machines on one's personal network would be able to see the server, right?

    • @RalphBacon
      @RalphBacon  3 ปีที่แล้ว

      Oh, absolutely, I had visions of external PCs being able to connect to the WAN address. 😱

  • @MrBobWareham
    @MrBobWareham 5 ปีที่แล้ว

    Hi Ralph Loved the video as always I did start to yawn a bit towards the end but I will watch it again as you do have a good way of presenting the information I want to do a bedside clock with Oled so will have a go with all this information. Thanks, Bob in the UK

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว

      A few comments further down, viewer Bruce Morgan maintains that screen burn is an issue for OLED screens. Which means it may not be the best option for a bedside clock. Perhaps you need a TFT(colour)? I'm going to investigate further. Sorry about sending you to sleep; I'm still quite annoyed about the video flicker when it looks so gorgeous in real life!
      www.forbes.com/sites/johnarcher/2018/12/04/oled-screen-burn-is-there-a-ticking-time-bomb-inside-your-tv/

  • @avejst
    @avejst 5 ปีที่แล้ว

    Impressive project
    Thanks for sharing👍😀

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว

      Thanks for your post, Asger, glad you liked it, good to hear from you.

  • @mo1ppm433
    @mo1ppm433 5 ปีที่แล้ว

    Very clear, concise info. Thank you.

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว

      Thanks for your post, Gary, glad you liked the video, good to hear from you.

  • @lodesmets9815
    @lodesmets9815 5 ปีที่แล้ว

    Great informative video (as always)

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว

      Thanks for your post, Lode Smets, good to hear from you.

  • @Alamartec
    @Alamartec 5 ปีที่แล้ว

    well explained Ralph, Nice one.

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว

      Thanks for your post, Alan, good to hear from you.

  • @digihz_data
    @digihz_data 2 ปีที่แล้ว

    Hi again Ralph.
    Instead of:
    String DoW[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};// Days of week. Day 1 = Sunday.
    We should use:
    char DoW[][5] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};// Days of week. Day 1 = Söndag.
    The string array eats up 84 bytes, while the char array only eats up 35 bytes.
    And in any case it is always a good idea to avoid strings.

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว

      Yes, that is better code. My code was not optimised but I really should lead by example! Thanks, Hans.

  • @paulpkae
    @paulpkae 4 ปีที่แล้ว

    Many thanks, this has saved me a lot of time (excuse the pun). Just nicked your code and got it up & running. Was planning on building a funky LED clock for my office and this will help get it well underway :) BTW what do you think of Platform IO/ATOM as an editor (IDE)?

    • @RalphBacon
      @RalphBacon  4 ปีที่แล้ว +1

      Glad I could help, Paul! My code is there to get you up and running and then you can do with it what you will. Now, Platformio/Atom is an IDE combo used by many, but I personally use Eclipse (Sloeber) as it reasonably close the Visual Studio I've used for years in my professional capacity. Now that Arduino is releasing a new IDE based on Eclipse I'll be waiting for that, hopefully this year.

  • @sortofsmarter
    @sortofsmarter 5 ปีที่แล้ว

    Great video once again..thanks..BTW your the first one I have seen with a sponsor spot from LCSC. I have been getting my electronics from them any time I need to order a PCB from JLPCB. They will ship them all together and I dont pay for extra shipping. I can get 80% of my SMD components at pennies a piece... and great quality on most. I can get Panasonic and so on if i needed...

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว +1

      Yes, LCSC are all part of the JCLPCB/EasyEDA family. They are about to offer SMT assembly too. But as I'm sponsor agnostic I will say no more at this time! Glad you liked the video, thanks for posting.

    • @wires4auto
      @wires4auto 5 ปีที่แล้ว +1

      @@RalphBacon yeah same first video I have seen too, I really like the easy/JCB/lcsc family. Hope to see more.

  • @willofirony
    @willofirony 5 ปีที่แล้ว

    About time! Great and informative video. Time and dates (which I am still convinced are two separate features in embedded [limited resource] programming) is a fascinating subject in computer science. They are both so damned irregular. I wouldn't recommend the time library for Arduino's 8 bit MCUs (328p, ATTiny85 etc). The conversion from seconds since 00:00:00 01/01/1970 to human readable time/date is an expensive and memory hungry) calculation . The original Time library was an attempt, by the creators of the C language, to "digitize" the irregular sequences of time and dates; as such it was intended for use on main frames. Now the ESP family (and the STM family) employ 32 bit registers and much larger RAM stores. This gives makers the power to exploit intensive processing routines such as those used in the time library. 8 bit users needn't despair. You don't HAVE to generate a time or date by calculating from 00:00:00 01/01/1970, you can start from the time you created your device; it is highly unlikely that your application will be concerned with history and unlikely that the device will still be functioning in 10 years time.

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว

      "...unlikely that the device will still be functioning in 10 years time". There speaks the voice of consumerism, Michael!
      Yes, my entire working life has been plagued by the nemesis called Date & Time. It's usually OK when you are in the same country/timezone but after that, it's all bets off. Microsoft has some decent functions to sort all this out.
      And now the ESP8266 I'm using knows all about two time zones and Daylight Saving and works very well (particularly auspicious as tonight the clocks go forward in the UK). I shall be intrigued to see whether my clock will be correct tomorrow morning (very closely based on that demo code, just with added DST and Time Zone added). And on my workbench I have an ESP32 running, doing more or less the same thing but has all that NTP stuff *built in* at long last. Just one line of code does it all!

  • @tonyjohnson4572
    @tonyjohnson4572 ปีที่แล้ว

    Great stuff Ralph. Works straight out of the box. Is there an easy way to adjust time to Eastern Standard time (GMT -5) ? I tried "adjustTime(timeZone);" but no joy. Thanks

    • @RalphBacon
      @RalphBacon  ปีที่แล้ว

      There is a really good way for setting the time zone(s) you want when using RTOS and an ESP32 (I've done it several times now) but I don't know if the ESP8266 works the same way. One way for you to find out, of course.
      Here's the ESP32 version for you to compare:
      randomnerdtutorials.com/esp32-ntp-timezones-daylight-saving/

  • @isoguy.
    @isoguy. 5 ปีที่แล้ว +6

    Now (). Thats a great handy video to watch.
    Thanks for sharing.

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว

      Indeed, I find it very slick and a great way not to bombard NTP servers until they are "required". Thanks for posting.

  • @robertrobert5583
    @robertrobert5583 5 ปีที่แล้ว

    Hi Ralph - thank you very much for posting this - what perfect timing (doh) as I'm am making a clock at the moment and this video is helpful for me to start to understand how to use NTP.
    Can I ask though, if I request from UK time pool does the answer always decode as GMT or does it automatically adjust for BST?

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว

      Hi, Robert², the answer is easy. All time is ALWAYS delivered as UTC (Universal Coordinated Time, previously known as GMT). It is then up to the consumer (aka developer) to turn it into local time. But we have help there. I'm using a library called which works in collaboration with (that is, you must have both, I believe). My code (abridged, as I am calculating two timezones in reality) looks like this:
      // We'll grab the time so it doesn't change whilst we're printing it
      time_t t = now();
      // Pointer to the Time Change Rule that is used in the conversion
      TimeChangeRule *timeChangeRule;
      // Clocks go forward 1 hour on last Sunday in March at 1am
      TimeChangeRule GBSpring = { "GB", Last, Sun, Mar, 1, +60 };
      // Clocks remain at GMT on last Sunday in October at 2am
      TimeChangeRule GBAutumn = { "GB", Last, Sun, Oct, 2, 0 };
      // Two timezones
      Timezone GB(GBSpring, GBAutumn);
      // Convert to local time
      time_t myTime;
      To determine whether I'm on DST I do this:
      if (GB.locIsDST(myTime)) { ... }
      Now for the VERY bad news, I'm afraid, Robert². This works great on the Arduino with a WiFi shield (or LAN shield). It also runs great on both ESP8266 and ESP32, EXCEPT that the latter two have a built-in method to do all this, which I am experimenting with at the moment. I would implement the above for now but be prepared for changes when I do a video on it in the future!

    • @robertrobert5583
      @robertrobert5583 5 ปีที่แล้ว

      @@RalphBacon Hi Ralph, thanks for the quick reply. I was looking at Andy Doro's library and thinking that it would be possible to write a few lines of code like this:
      BST = false.
      If it is April to September then BST = true.
      If it is March + A Sunday + Date is >= 24 then BST = true for rest of March.
      If it is October + A Sunday + Date is >=25 then BST = false for the rest of October from that last Sunday.
      If BST = true then hour = hour +1
      .
      I'm going to have a go at testing this at the weekend.
      Best wishes
      Robert

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว +1

      github.com/JChristensen/Timezone FYI

    • @robertrobert5583
      @robertrobert5583 5 ปีที่แล้ว +1

      @@RalphBacon Brilliant - much easier! That will save me time this weekend. best wishes.

  • @andreweastland9634
    @andreweastland9634 5 ปีที่แล้ว

    Hi Ralph, nice video thanks. You don't want to remove that delay in your loop. The esp module needs a call to delay to give it time to do the wifi bits that you don't normally see. Always put at least a delay(1) in your loop function or risk random watchdog timer trips. And note, yeild() doesn't work on the esp, it needs to have a call to delay(). Written properly, both the 328p and the esp will run as long as it has power, you need to take another look at that fridge project if it won't stay up for more than 49 days.

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว

      You're absolutely right about the ESP8266 module requiring time to do its WiFi/TCP bits in the background. However, I use yield() in my actual project code and it all works fine. The underlying code for delay() in the ESP8266 code is:
      void delay(unsigned long ms) {
      if(ms) {
      os_timer_setfn(&delay_timer, (os_timer_func_t*) &delay_end, 0);
      os_timer_arm(&delay_timer, ms, ONCE);
      } else {
      esp_schedule();
      }
      esp_yield();
      if(ms) {
      os_timer_disarm(&delay_timer);
      }
      }
      But it's the yield() that is the key part (apparently). At least, so far nothing has crashed in any of my tight loops that have a yield() in them. Perhaps they have changed things (which they do all the time and it's quite difficult to keep abreast of those changes). I'd appreciate your comments on this.
      Regarding my fridge alarm (using a Nano) I realise I should have used the WDT so that it rebooted itself if it ever hung. I've no idea why it does (occasionally) do this; a power spike? Did it get bored? I too believe the processor should run forever, without crashing or hanging.

  • @digihz_data
    @digihz_data 2 ปีที่แล้ว

    hello Ralph.
    Great video!
    I use setSyncInterval(_resyncErrorSeconds ); insted of setSyncInterval(_resyncSeconds); in the setup, because...
    If the first attempt failed then we don't have to wait so long to retry for the nextattempt to get the time from the ntp server.
    Example:
    In your code, if we use setSyncInterval(_resyncSeconds); and _resyncSeconds is set to 60 and _resyncErrorSeconds is set to 15.
    Then if the first attempt failed, we have to wait 60 seconds for the next attempt.
    In my change, if we use setSyncInterval(_resyncErrorSeconds ); and _resyncSeconds is set to 60 and _resyncErrorSeconds is set to 15.
    Then if the first attempt failed, we only have to wait 15 seconds for the next attempt.

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว

      I wish you had told me this 3 years ago when I created my Home Alone project for my mum's apartment in Germany! As it happens that doesn't seem to have a problem getting the time but I will make a note of your improved method for future use. Thanks for the heads up.

  • @gazzacroy
    @gazzacroy 3 ปีที่แล้ว

    looks a good board.. funny enought i was trying this out a few weeks ago i had an old tiny85 so i just set it to run on its own and just send a pulse every what every i set it to and it worked quite well. i just used an an interrupt to catch the signal..cool :)

    • @RalphBacon
      @RalphBacon  3 ปีที่แล้ว

      Sounds good, but you do still have the problem that the ATTiny85 could "hang" whereas the 555 timer never will.

    • @gazzacroy
      @gazzacroy 3 ปีที่แล้ว

      @@RalphBacon oh boy i love that chip been round since i was a we lad lol i did find i circit once online somewhere, where some one used 32.......khz crystal with it and apparently it worked really well i beleve he used the cmos version theo whished i could find it again :)

  • @siamsurf
    @siamsurf 3 ปีที่แล้ว

    RobTillaart published an awesome "CountDown" library on github that even runs on a ATTiny85 :-)

    • @RalphBacon
      @RalphBacon  3 ปีที่แล้ว

      Thanks for the heads up!

  • @GoatZilla
    @GoatZilla 3 ปีที่แล้ว

    I think it would have been interesting to cover keeping time through deep sleep. Something along the lines of storing the current epoch + sleep interval in the RTC register, and then restoring it upon waking up.

    • @RalphBacon
      @RalphBacon  3 ปีที่แล้ว

      The idea is to get the date/time from the NTP server when required - like after a Deep Sleep, given that all variables are reset at that point. There doesn't seem to be much point storing the current values and trying to recalculate the time after a wake up? Or is there?

    • @GoatZilla
      @GoatZilla 3 ปีที่แล้ว

      @@RalphBacon if the device leaves home (loses WiFi) for a while yet you still want it to continue logging and deep sleep.

    • @RalphBacon
      @RalphBacon  3 ปีที่แล้ว

      Well, when you are away from WiFi (and cannot contact the NTP server) that's where the local time in the ESP8266 is being used; but once you go into Deep Sleep that's it, it has lost all variables.
      Being able to log and Deep Sleep whilst being away from a WiFi point is going to be tricky. Not impossible but certainly a whole lot more complex than what this demo was intended for.
      If you really wanted to do that, I would suggest not going into Deep Sleep but just light Sleep mode where most things are off but the ESP8266 can wake up with variables intact. Not as power efficient but that has to be balanced against the complexity of the coding.

    • @GoatZilla
      @GoatZilla 3 ปีที่แล้ว

      @@RalphBacon Yeah, that's why I was saying it would have made for more interesting content, as opposed to going through standard NTP content. If it were easy it wouldn't be worth making a 30minute video about now would it? On the other hand, if you do get it working, then you really will have fulfilled the title of your video.
      I'm already using auto light sleep, which in itself has complications related to WiFi.
      The registers in the 8266 that survive deep sleep are literally called "RTC memory". I'm not sure how much more obvious they could have made it.

  • @Ed19601
    @Ed19601 5 ปีที่แล้ว

    Interesting stuff

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว +1

      It certainly is, Ed, thanks for posting.

  • @SpinStar1956
    @SpinStar1956 2 ปีที่แล้ว

    Hi Ralph. Great Video (as always!) Hey, I was wondering if you had a more (or deeper) thoughts on why your Nano has been crashing. And yes, I too have known and used Watch-Dog timers but you piqued my interest as to why it may be happening. On the HW side, maybe some static or spike on the power/I-O lines--I've had issues with static injection on probes and DC power lines. Or, could it be SW like some form of eventual stack-overflow? Anyway, was intrigued because the world shouldn't be dependent on WD timers for normal operation (i.e. be nice to nail down the exact reason--maybe with an analyzer?)! 73...

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว

      Well, lets think about other stuff in my house that behaves outside of the expected parameters (aka "does a funny"):
      1. My TV Digibox "misbehaves" now and again.
      2. My Smart Phone Charger blanks the display once in a Blue Moon. 3. My ESP32 Web Radio sometimes decides it's not going to read a data stream.
      4. The remote control for the TV suddenly doesn't work.
      5. The fridge door alarm "hangs" every few months.
      All these things are fixed by either rebooting or removing and reinserting the (same) batteries. They then work fine again for days, weeks, even months on end then suddenly a problem (again).
      Why? I can only assume some sort of power glitch or cosmic ray muon or some other weird interference. I couldn't try and find out what is causing the issues because it's not software related as far as I can tell. That said, one wrong (memory) bit in a million could easily cause problems. I don't think it's memory/stack overflow as that would show up far sooner, I would expect.
      For critical systems (at work) we always had hardware monitors and the critical bit of the system sent out a "heartbeat" to say it was still working normally. It's the only way to check stuff is still working. Hence my hardware monitor!
      Sorry I can't be more specific but in the Real World things happen that we can't always predict (think: Challenger Space Rocket) with sometimes disastrous consequences. You would think that military spec and hardened hardware would be more resilient (and perhaps it is) but they still have redundant systems.

    • @SpinStar1956
      @SpinStar1956 2 ปีที่แล้ว +1

      @@RalphBacon well you’ve definitely given it concerted thought 💭 I think in the midst of all the little ‘Murphy-devils’ is the reason automotive and appliance manufacturers do implement WD’s. The reason I mentioned stack-overflow etc. is that a long time ago I read an article about memory-leakage and how easy it was to cause them, and how long they could take to manifest (which seemingly was proportional to available memory). At the time I was regularly programming in C and some of the examples took me by surprise! If I ever remember where I saw it, I’ll send you a link. Once again Ralph , great channel and videos! 73…

  • @SThomas1972
    @SThomas1972 5 ปีที่แล้ว

    Hi Ralph
    Just need to point out there are two types of protocol and these are as follows Transport Protocols example TCP (Transport Control Protocol) and UDP (User Datagram Protocol) and the next type of protocol is a Application Protocol example HTTP (Hyper text transfer protocol tcp port 80) and NTP ( Network Time Protocol udp port 123).
    The TCP Protocol is described as a guaranteed/reliable Transport Protocol and needs to do a 3 way handshake to setup the connection. UDP on the other hand is unreliable best way to describe it is that it is a fire and forget type of behaviour there is no handshake to setup the connection.
    TCP is a heavy weight protocol but good for applications that need a reliable communications between client and server. UDP is a lite weight protocol making it fast but unreliable so good for applications where performance over reliability in client to server communications.

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว +1

      Excellently summarised, Simon, that is spot on. I knew that if I had included this sort of detail in my video I would lose my audience but it's great that we have this in a comment (that thousands of other viewers will read going forward), so thank you, appreciated.

    • @SThomas1972
      @SThomas1972 5 ปีที่แล้ว

      Ralph S Bacon thanks Ralph. For the localPort why not use a random number generator to produce a port of over 1024 but less than 65k.

  • @chrisdavidson2275
    @chrisdavidson2275 5 ปีที่แล้ว

    Great video. Would love to use the code in my own project but not appearing in github, only the readme

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว

      Oops. Thanks for letting me know, Chris, it's in the GitHub now. Plus the Time/TimeLib library in case it changes and breaks my sketch!

  • @DanielSMatthews
    @DanielSMatthews 5 ปีที่แล้ว

    Is the RTC module more appropriate for when you need to wake up a device with an interrupt after an extended but very accurate time delay?

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว +1

      Not the ESP8266, Daniel, that can be woken up using its internal clock (by connecting Pin GPIO16 to RST), all documented in my video #116. The Arduino is a different matter, but I discussed that in video #125 using a tiny external chip-switch that could be triggered by an RTC. When you say "very accurate" would not +/- 10 seconds be fine (which all the examples I mention in the video better by a huge amount).
      The other area where an RTC works better is for LONG delays (eg 1 day or more). The internal clock on the ESP8266 wakes up after about 2 hours. The code has to count the wake-up calls until a day has passed. An RTC would be able to do that no trouble at all (with the µC chip pretty much switched off). Depends whether battery life is important (each wake up will use a small amount of power).
      Great question, thanks for posting.

  • @montectronyxz
    @montectronyxz 3 ปีที่แล้ว

    great video!. Can i use this when the internet connection lose?

    • @RalphBacon
      @RalphBacon  3 ปีที่แล้ว

      Yes you can! The clock will retain good accuracy until it can connect to the internet again (which might be days). If accuracy is important to you, or it might be weeks and months without an internet connection then you need a DS3231 RTC module (cheap and easy to use).

  • @henrikjensen3278
    @henrikjensen3278 5 ปีที่แล้ว

    That 71.6 minutes is something I have to be very aware of (I often use millis).
    But I do not like the now() function, most of the time it will answer quickly, but sometimes it will take seconds. This do not match my programs. I need multi threading to do that kind of stuff or some state machine programming, my programs can usually not be delayed for a couple of second at random intervals.
    Anyway it is fairly easy to make any processor with a crystal run within 1 second in a day, it is just a compensation factor.

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว

      It surely takes seconds _on some occasions_ Henrik, because that is when it is syncing with the NTP server (at least, that's what happens when using the TimeLib library). Just making a call to the system clock should not take that long, so I wonder what it is doing? And yes, the ESP8266 can be adjusted to make it very accurate but it's not something I've done (except on PIC chips, some years ago). I might look into how "fairly easy" that is to do to the ESP8266.

    • @henrikjensen3278
      @henrikjensen3278 5 ปีที่แล้ว

      The "fairly easy" has very little with the processor to do, as long as the clock is stable you can do it. I am wondering about if it is a good idea to write an article about this or if it is common knowledge how to code this?
      Some years ago (20-30) I made my own alarm clock with a Atmel Mega processor, generally I only have to adjust it once a year and the reason for that "frequent" adjustment is because I did not have a PC with a precise clock when I designed it, the software could be calibrated to better precision.
      Recently I made a Arduino project using the same method and with a couple of hours adjustment period I could get it within 0.2 seconds a day compared to my PC. Using a longer time period would make it more precise, at least when the temperature is stable (Crystals have some temperature sensitivity).

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว

      I had no idea ATMega chips were available so long ago. I wish I had known, I could have invented the Arduino and now be living in a mansion in the south of France (or Germany). Sigh.
      I don't think it's common knowledge _for beginners_ on how to tweak the crystal frequency. I can't even remember how to do it on the PIC now (some bit values?) so maybe I will look into this. My experience on Arduino (UNO) timekeeping is that the accuracy is pretty poor - but maybe that's the Chinese clones for you.

    • @henrikjensen3278
      @henrikjensen3278 5 ปีที่แล้ว

      I searched my harddisk and found the project, I was a bit wrong about the age, it is from 2002
      The processor is MEGA163P in a 40pin DIP.
      I am still using it as my alarm clock, there is a photo of it here: lygte-info.dk/pic/cpf5/Clock.jpg
      And one of it open: lygte-info.dk/pic/cpf5/ClockOpen.jpg
      The way to make a clock run precise has nothing to do with the processor, adjust of crystal speed or special registers, it is a software solution.

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว

      Amazing stuff! Are those small LEDs above the 7-segment displays? Did you design the PCBs or was it a kit?
      I ask because when I was about 15 or so, I bought a digital clock kit for GBP £16.00. I earned in my summer job GBP £13 per week so you can see it was an expensive kit. But I built it, eventually modified it and it lasted some 20 years. I eventually discarded it because the 7-segment displays started to fail and they were expensive to replace and getting the right model of LED was very impossible - unlike today when I could have fixed it in a heartbeat! Such is progress. But I learned all about multiplexing from that kit.

  • @JohnC-gx2ow
    @JohnC-gx2ow 3 ปีที่แล้ว

    Hi Ralph. Very useful video. I've used your code and got it working. One thing that puzzled me was that one time on startup there was no response from the 'uk.pool.ntp.org' time server and it kept repeatedly trying every _resyncErrorSeconds but always returned no response. So I restarted the program and the time server responded immediately. Any idea what might have happened? Keep up the good work with these videos,

    • @RalphBacon
      @RalphBacon  3 ปีที่แล้ว

      Odd. Were you properly connected to the WiFi? Maybe a few seconds delay after initiating the WiFi.begin might help. As you probably know I use this technique in my Home Alone project, and whilst I don't always get a reply (UDP packets are not guaranteed to arrive in either direction) I do eventually get one and it doesn't require any intervention at all. If you request the time too often (from the same pool) you will get blacklisted so don't go mad testing this! Try the universal pool (pool.ntp.org) or one of the European ones (europe.ntp.pool.org).

  • @elricho72
    @elricho72 4 ปีที่แล้ว

    Thanks for sharing Ralph, I How can I use Argentine time zone? I put const int timeZone = -3; but doesn't work.

    • @RalphBacon
      @RalphBacon  4 ปีที่แล้ว

      When you say "doesn't work" what do you get?

    • @elricho72
      @elricho72 4 ปีที่แล้ว

      @@RalphBacon I get the incorrect time because in my country there is 3 hours difference

    • @robertingersoll5289
      @robertingersoll5289 4 ปีที่แล้ว +2

      @@elricho72 Ralph's code comment says that the timeZone line is NOT used. Being in CA USA, I changed his code as follows: printDigits(hour(t) + timeZone), where timeZone=-7.
      and it prints correctly!

  • @ralphmaasgmailcom
    @ralphmaasgmailcom 5 ปีที่แล้ว +1

    Hi Ralph (nice name), useful information thanks for sharing. I have an ESP8266 project running in which I can process this feature. Thanks

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว

      Thanks for that, Ralph Maas, nice to hear from you.

  • @gartmorn
    @gartmorn 5 ปีที่แล้ว +1

    So if you're using wifi enabled microcontrollers an rtc is really unnecessary additional hardware! The code is a bit daunting but the hard bit has actually already been done by others, if you struggle with coding like myself!

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว +1

      Correct. No RTC is necessary. But WiFi access is. So a bee counter in the middle of a field may not have WiFi available, of course. Yes, the code that you describe is daunting is a bit technical, I suppose, but it's easy to tuck it away into a function (or library, or header file) and forget about it. Black box stuff. I've been running it for some time on my Wemos D1 Mini setup and it's proved its reliability.

    • @gartmorn
      @gartmorn 5 ปีที่แล้ว +1

      @@RalphBacon it's so good for the hobby that everyone shares their code and ideas! I tend to use shared code and tweak it as I'm not at a stage to write any from scratch but I really appreciate people like yourself who help others so much! I was an electrical fitter before I retired so the hardware side is OK but software is a totally different ball game!

  • @gavinsmalley1513
    @gavinsmalley1513 5 ปีที่แล้ว

    Yet another cracking video and a very detailed look at the underlying methodology for handling NTP. Thank you.
    In no way would I wish to diminish the work you've done (or that of Arduino community hero Paul Stoffregen) but there is an awful lot of the "grunt" there that would really benefit from being in the library itself (it's a lot of boilerplate to put in more than one project).
    You may wish to take a look at ezTime (github.com/ropg/ezTime) which is an alternative Time library which is backwards compatible with the Arduino Time library but also contains NTP functionality and also, crucially, implements Timezones and even more impressively implements accurate worldwide daylight saving time through a separate online query. Add that with the events scheduling support and it seems a very neat way to handle a lot of projects.
    I've only used it in one test piece thus far as I was only pointed to it myself a couple of weeks ago but already have it lined up to replace code I have in older projects using NTP.

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว

      Yes, that is a great library, Gavin, but if you have followed my videos (I know you have, thank you for your long term support) then you also know that I like to avoid libraries where I don't know what's happening and roll my own (at least, to begin with) so I can get to grips with the nitty-gritty.
      Now, the NTP stuff could easily be put into a library as you suggest (or just a header file, frankly, and included where required), but that wasn't the focus of this video which was to show how easily we can grab the time from the Internet and keep pretty accurate time on a lowly ESP8266 with an RTC module.
      In my HomeAlone project, I do indeed use the TimeZone/Daylight Saving library feature (that's one I really could not be bothered to write from scratch!) as the device will be implemented in a different time zone, so stay tuned for more on that over the forthcoming weeks. Great to hear from you and thanks for the library link.

    • @gavinsmalley1513
      @gavinsmalley1513 5 ปีที่แล้ว +1

      ​@@RalphBacon Hi Ralph, I understand your mission and I fully and wholeheartedly support it. Particularly those not familiar with a concept should always be encouraged to first research it and write a sketch to implement at least the very basics of that concept before simply using a library. Particularly for something that is the heart of a project. If every newbie on the Arduino forums were sent to watch your videos instead of told to "#include ;" then an awful lot more learning would take place! Teach a man to fish and all that...
      It's a bit like using a calculator rather than mental arithmetic. If you don't have an understanding of the region your answer should be in you can make very big mistakes very easily. The same is true with libraries - if one has no understanding of the underlying method then one can't appreciate why the libraries have limitations and why those limitations exist. Especially as the documentation for so many libraries seems to assume that the person who is using it has sufficient knowledge to have written it!
      That said once one is familiar with the underlying concept, there is no need to re-invent the wheel for the more finely grained logic. Especially not the terrible complexities of Timezones and DST! Especially in those countries where there are 15 minute timezones and even more so for the poor people who live in Morocco where there are two summer times every year (though ezTime can't help there either!)
      My point (badly and lazily phrased in the original comment) was that once the basic understanding has been achieved there are some fabulous libraries to be explored where people have used the same concepts to deliver more features. Something which I know someone with your vast experience knows more than most, but thought the link may save some viewers trying to code DST rules themselves.
      As always I look forward to the next installment! :-)

  • @25sunbeam
    @25sunbeam 5 ปีที่แล้ว +2

    millis() rollover is 49 days on an ESP8266, not 71.6 minutes.

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว +1

      Mmm, well, Pete, the unsigned long will contain millis for 49 days but the underlying call looks at the microsecond tick count (whereas the Arduino looks at the millisecond tick count) which then overflows every 71.6 minutes. BUT, to us developers it should not make any (much) difference as the ESP8266 clock (RTC) handling is more sophisticated than the Arduino's. I was worried it might affect accuracy (I'm not using sleep mode) but to date it meets my expectations.

    • @25sunbeam
      @25sunbeam 5 ปีที่แล้ว +1

      Well, just try it. Create a sketch that prints millis, or maybe millis/1000 for more readability, to the serial monitor. See if it resets to zero after 71 minutes (it doesn’t by the way). There was a bug in one of the earlier ESP cores that did cause millis to reset after 71 minutes, but this was fixed about 18 months ago - this may be what you’re thinking of?

    • @25sunbeam
      @25sunbeam 5 ปีที่แล้ว

      github.com/esp8266/Arduino/issues/3600
      “We take some effort to avoid millis rollover until it reaches UINT32_MAX, see the code here:
      github.com/esp8266/Arduino/blob/master/cores/esp8266/core_esp8266_wiring.c#L56-L68. So it should overflow after roughly 50 days, if i'm not mistaken (2^32 / 1000 / 3600 / 24 = 49.7)”

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว +1

      I had already read that first thread too - it certainly sounds like they have fixed it, but I haven't done any tests as I have ensured I never leave an interval of more than 1 hour before "doing stuff". But from that link it sounds like that this was an early bug and is no longer an issue. Thanks for the update, good to know.

  • @robertrobert5583
    @robertrobert5583 5 ปีที่แล้ว

    In case this helps anyone...I've no idea why, but I could not get the sketch to compile until I replaced the wifi #define statements (#define _ssid xxxxx
    #define _password xxxxx) with,
    const char* _ssid = "xxxxxx-xxxx";
    const char* _password = "xxxxxxxxxx";
    Also I don't have a serial monitor option for the baud rate to be 76800 but I found that 74880 works fine.

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว

      OK, last comment first, that is the Arduino using a slightly different speed to the Eclipse that I'm using. They both work at the "other" speed.
      Regarding the #define statements, did you include quotes in the definition?

    • @robertrobert5583
      @robertrobert5583 5 ปีที่แล้ว

      @@RalphBacon Ah thank you - I thought I did - but you're no doubt right! I do wish there was an Arduino function called YouKnowWhatIMean (); (knows and returns exactly what you want). Thanks for getting back to me so quickly.

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว +1

      The programmer's mantra, Robert² is:
      1. That just can't happen
      2. That shouldn't happen
      3. It works on my machine
      4. Oh.
      5. How did that ever work?
      Regarding your use of #define for a variable value, I've just read that C++ programmers don't like them and would much rather declare a const int or whatever. Apparently it's more flexible like that (for comparison and so on).

  • @RanaRao_Chandrachur
    @RanaRao_Chandrachur ปีที่แล้ว

    Professor, I have a request, sir 🙏, there are a lot of tutorials about atmega, Arduino and ESP8266 but *what about AT89s52 [8051 families]??* the AT89s52 is a very basic microcontroller that we read in our college,
    and my problem is "I'm making a digital clock [7-segment] using AT89s52 (NO RTC) but the time drifts about 5.6 seconds in every hour!! and this At89s52 microcontroller doesn't have any function like millis() inside AT89s52's IDE(Microvision Keil software), and a book suggests to play with the ppm and fix the error.But, yet the problem is At89s52 is not like Arduino , so Professor, I need your help, can u please make a dedicated video on this?? so that everyone will learn a lot, or u can also help me by writing about the solution below 🙏"
    BTW I'm using a 16MHz crystal with this microcontroller and using timer_0 to generate a 25ms delay and using interrupt_1 to count that 40times so that there will be 1 second completed, but that's not the problem I guess.. I just need your help.. .🙏🙏

    • @RalphBacon
      @RalphBacon  ปีที่แล้ว +1

      You will find that all microcontrollers will have time drift. It's happened to me with ESP8266s and ESP32s. As for the Arduino it is even worse. The ONLY way to keep accurate time is:
      1) Connect to the Internet and get the time from a NTP server every few hours and resync your processor
      2) Connect a DS3231 RTC module and read that as often as you like. Very accurate.
      I'm not familiar with the ST89S52 but if it does not have Wi-Fi capability then the second option is best for you (but will require additional programming).

    • @RanaRao_Chandrachur
      @RanaRao_Chandrachur ปีที่แล้ว

      @@RalphBacon 🤓 Thank u for your very kind reply sir. 🙏 Love u from India.

  • @johncook5872
    @johncook5872 5 ปีที่แล้ว

    Hope your secret SSID is GREYPUSS and a default IP range!

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว

      Well, I figured that my SSID is being broadcast to all and sundry so there was no need to hide it. The IP addresses are limited but I don't have a MAC address whitelist - it's too awkward with my ESP8266 work.

    • @johncook5872
      @johncook5872 5 ปีที่แล้ว

      @@RalphBacon Ralph, It was a facetious comment as I knew you had thought this through. Anyway, you use eclipse IDE and I wonder if you consider a short tutorial. I for one can compile and download "sketches", but I cannot get the inbuilt serial monitor to work. " OPEN COMMAND SHELL", If not as they say No Worries - Cheers John

  • @NickNorton
    @NickNorton 5 ปีที่แล้ว

    18:55 Missed opportunity to describe them as Time Lords.

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว

      Ha Ha! Let's not give them a superiority complex!

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว

      Now I've had time to think, I should have replied to your comment, "Who?"

    • @NickNorton
      @NickNorton 5 ปีที่แล้ว +1

      @@RalphBacon ba dun tishhhhh
      👏👏👏

  • @GT-ep7en
    @GT-ep7en 5 ปีที่แล้ว +1

    Or just use configTime( 0, 0, "pool.ntp.org", "time.nist.gov");
    setenv("TZ", "MET-1METDST,M3.5.0/01,M10.5.0/02",-1); this will be used by the timelibrary to adjust time and daylightsaving

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว

      You say this is a feature of the TimeLib library,Guido? I'm using Paul Stoffregen's TimeZone library to handle DST (and time zones) that has a dependency on TimeLib but there is no mention that I could find that TimeLib would do this natively. Did I miss something (like a library update)?

    • @peut
      @peut 5 ปีที่แล้ว

      HI, it's in the ESP IDF, i.e. built in in the LWIP TCP/IP stack and in the Arduino library. No need for Timelib or TimeZone libraries. It's a miracle :-).

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว

      Funnily enough, I'm using this right now on my ESP32 - but not in my ESP8266. Are we sure it's supported by the ESP8266?

    • @peut
      @peut 5 ปีที่แล้ว

      Yes :-). I just weeded out the TimeLib and TimeZone code out of a project to make a clock on a LED array. Works now,
      tonight we go to Summer time here in NL, so that will be the ultimate test. @@RalphBacon O, make sure you use the latest Arduino board release for ESP8266.

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว

      I'll try this out later this week, it would be good not to use libraries we don't need to. We went to Summer Time last night and all was well (that is, my code using the libraries handled Daylight Saving) but I'd prefer to use a native feature.

  • @TheEmbeddedHobbyist
    @TheEmbeddedHobbyist 5 ปีที่แล้ว

    is the millis() function a blocking function, if used in standard form on a 8266 with wifi running this would cause the wifi to fall over if delay is too long. same way as long loops should be avoided. all delays should be based on a call back functions

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว

      Luckily, delay() has a call to yield() built in, otherwise you would be correct, the Watch Dog Timer (WDT) would kick in after about 8 seconds. I've documented what delay() does in another comment in this thread. Occasionally, it's very worthwhile to add in a yield(), especially between WiFi calls.
      I use this to my advantage to force the code in my new project to fall over by doing a "do nothing" loop, knowing that the board will restart (eg if WiFi just won't connect). A sort of self-correction. Works like a dream!

    • @TheEmbeddedHobbyist
      @TheEmbeddedHobbyist 5 ปีที่แล้ว

      the problem with the ESP8266 is that is that it's a single core doing all the work. take too long in your code and the wifi times out. I'v not used the Arduino libraries with the 8266 and have learnt the hard way that with one, two many goes round the loop and all sorts of problems occur with the wifi connection or strange resetting's

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว

      That's true; keep the ESP8266 too busy and it will WDT reset itself or the Wifi falls over. Put just a few yield() statements in at critical moments and all will be well. You can prove this yourself with a do nothing loop, with and without a yield() statement in it.

  • @davidbottrill5122
    @davidbottrill5122 5 ปีที่แล้ว

    I've found that the NTPClient library: github.com/arduino-libraries/NTPClient makes NTP even easier and in conjunction with Timezone github.com/JChristensen/Timezone makes daylight savings very simple.
    By the way the ThinkSpeak guide was very interesting and I've used it as an extension of my ESP32 analogue barometer which uses a stepper motor to drive a pointer.

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว

      The wonderful thing about this hobby, David, is that there are often many ways to achieve your goal, as you have discovered. I shall have to look at the at that library.
      I'm glad you liked the Thingspeak guide, I'm using it daily to monitor stuff from my Home Alone project, it works very well. Thanks for posting, good to hear from you.

    • @tlangdon12
      @tlangdon12 ปีที่แล้ว

      Just as a general observation, I think that data logging applications should probably always log date/time at UTC, and then the application that is used to view the data should apply any timezone or daylight saving offset that that the user wants to see. This way the raw data remains consistent.