ESP-IDF in CPP 09: Simple Network Time Protocol (SNTP)

Поделиться
HTML-код
  • Опубликовано: 1 янв 2025

Комментарии • 21

  • @OckertM
    @OckertM 3 года назад +1

    0:28 On today's video
    2:15 Create Sntptime.h and populate
    9:20 Explanation of have a static declare class in the constructor (10:00 clarification)
    12:20 fix file name SntpTime
    15:00 auto and other special keywords
    21:15 How valid is our time / when was our last update?
    34:35 Create theSntpTime.cpp file and populate
    1:15:10 Time to compile!
    1:15:45 Make an instance of Sntp
    1:16:25 Put our Sntp in a namespace SNTP
    1:18:15 add sntp.init() to main.cpp
    1:18:30 Fix some errors
    1:30:00 flash
    1:30:40 it worked
    1:32:20 How to display serial messages in time
    1:33:18 Next up
    This one was a bit tricky for me, I had to compare the files on Github with my own to find out I rushed the additions to Wifi.cpp and had wifi events between the IP stuff.
    All working now and super excited for the next part!
    Thanks a million for these videos Simon!

  • @masretta
    @masretta Год назад +2

    Extremely instructive and interresting. I particularly appreciate it that on top of explaining what you are doing, you also explain WHY you choose the particular solution. A lot of AHA!-moments for me :). Another question, why do you choose to create an "init"-function and not just use the constructors for initialisation and save some code?

    • @DrGreenGiant
      @DrGreenGiant  Год назад +1

      Thank you for the kind words :)
      So the init Vs constructor is very debatable. Either are valid. Personally though, I don't like a lot of work to be done in a constructor (i.e. acquiring a resource). I certainly don't like them to block because they're waiting on IO, for example. Calling a function, to me, implies time will be taken, whereas a constructor is just setting up some memory enough is fast.
      Being an embedded guy with exceptions disabled, with a function you have the opportunity to fail and return an error. With a constructor, if something goes wrong you can only abort, failure isn't a handleable option. (Or set some "valid" flag which is an idiom I detest)

  • @Magic-Smoke
    @Magic-Smoke 3 года назад +1

    Hi Simon, still following along! This has been very helpful as I work on my projects. I chose the harder path of ESP-IDF since I wanted to use the facilities that ESP Rainmaker delivers as a bit of a shortcut but you've definitely helped me get my head around a few things - especially the 'dark' world of make files :)

    • @DrGreenGiant
      @DrGreenGiant  3 года назад +1

      It took me ages to figure out cmake. I can still only scratch the surface with what I know but it's a start! Glad you're enjoying, thank you for the lovely comments

  • @peut
    @peut 3 года назад +1

    It is surprisingly difficult to find a good (=easily usable) source for TZ strings online. Best method I found is to log in to an up-to-date Linux/FreeBSD/Raspberry system and view for instance /usr/share/zoneinfo/Europe/Amsterdam, ignore the binary information, the TZ string will be in there.
    I found it intriguing to see you init WIfi in the SNTP class and I thought you must be much cleverer than I am.. I still think so, but I was relieved to see you remove it at the end,
    at least I understood again.

    • @DrGreenGiant
      @DrGreenGiant  3 года назад +1

      Thanks! The idea is that the code is as portable as possible. So if you only wanted sntp, then you just include that component and all the wi-fi happens "automatically." No doubt things will evolve when we go back and refactor the original code, when the time comes. Still got lots to do. Haven't even started on mqtt and sensors yet!

  • @abdelrahmanyasser5720
    @abdelrahmanyasser5720 11 месяцев назад +1

    I am using Nodemcu. Facing that error.
    error: 'time_point_now' function uses 'auto' type specifier without trailing return type
    [[nodiscard]] static auto time_point_now(void) noexcept
    note: deduced return type only available with -std=c++14 or -std=gnu++14

    • @DrGreenGiant
      @DrGreenGiant  11 месяцев назад +1

      Yeah I was using C++17. I presume you are on C++11 which is very old now. You will have to trail the declaration with the return type (or change auto to the actual type expected which is probably easier, or a template.)
      More info here: en.cppreference.com/w/cpp/language/auto

    • @abdelrahmanyasser5720
      @abdelrahmanyasser5720 11 месяцев назад +1

      Thx for specifying the problem.
      To solve it I've added
      target_compile_options(${COMPONENT_LIB} PUBLIC -std=gnu++17)
      line in the CMakeLists file in Wifi folder.

  • @masretta
    @masretta Год назад

    Would it be possible to circumvent the idiosyncrasies of the chronos library by a clever use of typedefs?

    • @DrGreenGiant
      @DrGreenGiant  Год назад +1

      Yes, or probably better than a typedef is the "using" keyword so it works with templates and a few other benefits too :)

  • @eraldosantos6480
    @eraldosantos6480 3 года назад +1

    Hi Simon, thanks for your videos, they are so greate to learn and understand how to program ESPs using C++. I'm trying to program them using Plataformio on ESPs enviroment, but with some issue when declare SNTP::Sntp& sntp, got 'sntp' declared as reference but not initialized, I don't use main.h and I'd like to know if is it possible initialize SNTP in a different way or I need to use sntp{SNTP::Sntp::get_instance()}{} line? I did it at this way SNTP::Sntp& sntp{SNTP::Sntp::get_instance()}; and appears its working

    • @DrGreenGiant
      @DrGreenGiant  3 года назад

      You don't need to use the get_instance method, this is just a way of ensuring only one instance is ever created. As long as you can ensure only one is created then you can just create an instance in any other way you normally would on the stack {SNTP::Sntp my_instance;}, on the heap {SNTP::Sntp* my_instance = new SNTP::Sntp;}, or statically {static SNTP::Sntp my_instance;} You will need to make the constructor in SntpTime.h public to do this though.

  • @vincentdeman5225
    @vincentdeman5225 3 года назад +1

    Wow so cool to see it working. good job!
    Maybe its a bit nikpicky but shouldn't you delete the copy constructors in youre singleton?
    MySingleton(const MySingleton&)= delete;
    MySingleton& operator=(const MySingleton&)= delete;
    cool to see that you can also change it in the log messages i would have never thought about it,

    • @DrGreenGiant
      @DrGreenGiant  3 года назад

      Yes you're right, a copy constructor would make no sense here for a singleton

  • @boyceschrack2537
    @boyceschrack2537 2 года назад

    I'm a real noob with PlatformIO. I'm getting errors with the auto return type specifier and it looks like c++14 is required. Is c++14 the version used now for ESP32 programming? Is changing the C++ version done by changing the PlatformIO.ini file?

    • @DrGreenGiant
      @DrGreenGiant  2 года назад

      I have no idea about PlatformIO. I used it once or twice a few years ago and didn't like it.
      Iirc the IDF uses gcc8.4 so can go up to c++17, which is my normal. I think the default is c++11 but not certain. Whether this is applicable to PIO, not sure

    • @boyceschrack2537
      @boyceschrack2537 2 года назад +1

      @@DrGreenGiant Oh ok, I'm awake now. Hello. Now I see the ESP_IDF in the titles of all of these videos. I'm not giving up on PIO yet but I want to be like you. Thanks for the help and for doing these great videos.

    • @DrGreenGiant
      @DrGreenGiant  2 года назад

      @@boyceschrack2537 I assume PIO is just using the IDF under the hood, so it may just bea PIO config issue? I guess they might have a support forum that might be worth asking on? Sorry I can't help more

  • @abdelrahmanyasser5720
    @abdelrahmanyasser5720 11 месяцев назад

    16:47 N.I.C.E