collapse

Welcome!



Welcome to Robot Rebels, an online robot maker community.

Register today to post your projects, ask questions, share knowledge and meet like-minded people from around the world.


The RobotRebel.org Community

Author Topic: Web TIme API -  (Read 613 times)

Gareth

  • Member
  • ****
  • Posts: 54
Web TIme API -
« on: December 07, 2018, 04:34:37 PM »
My latest ESP32/Nextion touch screen Weather/bus/train times project required a realtime clock.... however it has taken me quite a few weeks to find a good solid solution.
Firstly :-
....if you want to stay sane then avoid epoch time (best thing that happened in 1970's was "punk Rock" not unix count my seconds birth)....rant over.

If your ESP(range of MCU's) requires a neat way to sync things to a clock then here is the easiest way I have found (tnx KA for the shoutbox Ping>>>).

Many if not all API web calls have a handshake header message before the real data is exchanged.
I am extracting weather data from the openweathermap.com project.
Its relatively easy to get the weather_data however it a bit more tricky to obtain some coherent date and time from it.

Code below is for an ESP32 :-

#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "Gareth";         //Router name
const char* password =  "Slatybartfast";  //Router Password
const String PollTimeAPI = "http://api.openweathermap.org/data/2.5/weather?lat=-77.66&lon=168.22&units=metric&APPID="; // simple web call to extract weatherdata , however can double up to extract header data as well
const char * headerKeys[] = {"date"};//The data key you require from the HTTP Header
const size_t numberOfHeaders = 1;    //The number of keys in your array above
int pollApiTime;
void setup() {
 Serial.begin(115200);
 WiFi.begin(ssid, password);
 while (WiFi.status() != WL_CONNECTED) {  delay(200); Serial.print(">"); }
 Serial.print("Connected to "); Serial.println(ssid);
 }
void loop() {
  if (millis()-pollApiTime >=10000) {PrintDateTime(); pollApiTime=millis(); }
}
void PrintDateTime()
{
 if ((WiFi.status() == WL_CONNECTED)) {              //Check the current connection status
   HTTPClient http;
   http.begin(PollTimeAPI);                          //Specify the URL
   http.collectHeaders(headerKeys, numberOfHeaders); // here is how to access certain data fields in the HTTP responce header
   int httpCode = http.GET();                        //Make the request
   if (httpCode > 0) {                               //Check for the returning status code  :-)
   String headerDate = http.header("date");          //extract the date and time field
   Serial.println(headerDate);
   http.end();       }                               //free up the request
                                      }
}


Resulting output :-

I am polling the data every 10 seconds here for demo, though in reality I would only need to sync the time maybe once/day.
As far as I can gather the HTTP specifications for the Date & GMT Time layout is quite strict, which makes it easy to split the data up.
« Last Edit: December 08, 2018, 07:36:15 AM by Gareth »

 

* Search


* Recent Topics

MKS Gen L 1.0 by jimmyeao
[Today at 12:15:32 PM]


Animabot Rev3 - Advanced Hexapod Robot by Nemesis
[January 17, 2019, 09:34:32 AM]


PLX-DAQ by BaldwinK
[January 16, 2019, 06:44:18 AM]


Four Motor Leg Module by BaldwinK
[January 09, 2019, 04:55:14 PM]


Android Adware by Diegotorry
[January 08, 2019, 05:34:48 AM]


Hi, Mushroom glue here. by Diegotorry
[January 08, 2019, 05:31:52 AM]


Solid Works maker license 1 year Free!! by maelh
[January 04, 2019, 01:50:08 PM]


Merry Christmas by Protowrxs
[December 28, 2018, 11:44:25 PM]


Ultralite or flying car of sorts by Impala
[December 27, 2018, 09:30:02 PM]


Web TIme API - by Gareth
[December 07, 2018, 04:34:37 PM]


API Directory by Gareth
[December 06, 2018, 03:34:16 PM]


The Original Prusa I3 Mk3 by tinhead
[November 30, 2018, 02:49:52 PM]


Email notifications by CraigCox
[November 28, 2018, 10:26:06 PM]


And another one. by CraigCox
[November 28, 2018, 10:17:54 PM]


This kids a freaking genius by ossipee
[November 21, 2018, 04:55:40 PM]

* Recent Posts

Re: MKS Gen L 1.0 by jimmyeao
[Today at 12:15:32 PM]


Animabot Rev3 - Advanced Hexapod Robot by Nemesis
[January 17, 2019, 09:34:32 AM]


PLX-DAQ by BaldwinK
[January 16, 2019, 06:44:18 AM]


Re: Four Motor Leg Module by BaldwinK
[January 09, 2019, 04:55:14 PM]


Re: Android Adware by Diegotorry
[January 08, 2019, 05:34:48 AM]


Re: Hi, Mushroom glue here. by Diegotorry
[January 08, 2019, 05:31:52 AM]


Re: Solid Works maker license 1 year Free!! by maelh
[January 04, 2019, 01:50:08 PM]


Solid Works maker license 1 year Free!! by tinhead
[January 02, 2019, 06:15:07 AM]


Re: Merry Christmas by Protowrxs
[December 28, 2018, 11:44:25 PM]


Ultralite or flying car of sorts by Impala
[December 27, 2018, 09:30:02 PM]


Re: Merry Christmas by tinhead
[December 27, 2018, 05:15:54 AM]


Re: Merry Christmas by Impala
[December 24, 2018, 04:34:27 PM]


Re: Merry Christmas by MEgg
[December 24, 2018, 06:56:46 AM]


Merry Christmas by jinx
[December 24, 2018, 03:18:32 AM]


Web TIme API - by Gareth
[December 07, 2018, 04:34:37 PM]