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
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
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
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