• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

need help with time

Q&A Forum › Category: ESP32 › need help with time
0 Vote Up Vote Down
Shah Rafique asked 4 years ago

need help. Not sure the best way to submit.
I got API response for the Sunset time as “Sunset” = “06:39” and I have local time in ESP32 as “01:32:23”. I am not expert on software coding but can carry your instructions. Please help me how to code logical test of these two time and turn a port in ESP32. Thanks

4 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 4 years ago

Hi Shah.
Can you try to better explain your issue?
How do you get the local time with the ESP32? Do you want to compare two times to trigger something?
Regards,
Sara

0 Vote Up Vote Down
Shah Rafique answered 4 years ago

Greetings to you Sara:
At first, I built the project as stated in your article “ESP32 HTTP GET and HTTP POST with Arduino IDE (JSON, URL Encoded, Text” using Raspberry and ESP32. I used option “ESP32 HTTP GET: JSON Data Object or Plain Text”
After successful initial and basic learning, in Node-RED, I had modified function in /get-sensor with msg.payload as {“code”:200,”status”:”OK”,”data”:{“timings”:{“Sunset”:”18:19″…………………….
Then, from ESP side, I can get “Sunset” = “18:19”.
Now, I am wondering how I can turn an LED in one if the GOIP ports to “ON” at sunset time and turn of after one hour (as example).
I get local time by using “time.h” and getLocalTime(&timeinfo).
Now, I need to trigger a port either by comparing between local time and sunset times or trigger the port by sunset time.
Could you please guide me?
Thanks

0 Vote Up Vote Down
Sara Santos Staff answered 4 years ago

Hi.
 
I guess you’re getting the sunset time as a String, right?
You can split the string to get the hour and the minute in two separate strings and then convert them to a number. 
You can use the substring() method to split the string: https://www.arduino.cc/reference/en/language/variables/data-types/string/functions/substring/
Then, you can use the toInt() method to convert the string to a number: https://www.arduino.cc/reference/en/language/variables/data-types/string/functions/toint/
 
After this, you just need to compare the sunset hour with the current hour. This tutorial shows how to get time (hour and minutes separately): https://randomnerdtutorials.com/esp32-date-time-ntp-client-server-arduino/
 
You can also use the epoch time (unix time) if the API can get the sunset time in that format (that would be easier because you would only need to compare one variable).
 
I hope this helps.
Regards,
Sara
 
 

0 Vote Up Vote Down
Shah Rafique answered 4 years ago

Thanks Sara. I will work based on your suggestions in coming weekend and update you when done. Thanks again

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] Learn Raspberry Pi Pico/Pico W with MicroPython eBook – Version 1.2 May 26, 2025
  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.