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

Just scary to me... by VirgilReevy
[Today at 05:04:43 AM]


Vous ne savez pas comment m'aider? by VirgilReevy
[Today at 05:03:28 AM]


Just a short intro... by MEgg
[March 18, 2019, 05:49:28 PM]


MKS GEN L Servo Issue by cmptrguy1
[March 14, 2019, 01:18:28 AM]


Yes I am alive! by jinx
[March 10, 2019, 04:38:53 AM]


CZ-1 2.0: problems, calibration and printing, part2 by MEgg
[February 26, 2019, 03:35:15 PM]


Hi, Mushroom glue here. by nschreiber0813
[February 25, 2019, 02:09:13 PM]


Arduino IK by BaldwinK
[February 25, 2019, 05:54:44 AM]


Raspberry Pi / Python eBook - free for the next 11 hours! by MEgg
[February 24, 2019, 09:49:43 AM]


MKS Gen L 1.0 by MasterDomino
[February 24, 2019, 06:34:24 AM]


Fun Fact by Impala
[February 13, 2019, 11:05:01 PM]


Animabot Rev3 - Advanced Hexapod Robot by Smashley
[February 05, 2019, 03:41:50 PM]


Finally tried out a Chinese PCB service by Bajdi
[February 02, 2019, 08:40:49 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]

* Recent Posts

Re: Just scary to me... by VirgilReevy
[Today at 05:04:43 AM]


Vous ne savez pas comment m'aider? by VirgilReevy
[Today at 05:03:28 AM]


Re: Just a short intro... by MEgg
[March 18, 2019, 05:49:28 PM]


Re: Just a short intro... by Killer Angel
[March 18, 2019, 06:55:35 AM]


Re: Just a short intro... by jinx
[March 16, 2019, 04:36:13 AM]


Re: Just a short intro... by Bajdi
[March 15, 2019, 04:22:09 PM]


Just a short intro... by cybersamurai
[March 15, 2019, 04:19:22 PM]


Re: MKS GEN L Servo Issue by cmptrguy1
[March 14, 2019, 01:18:28 AM]


Re: Yes I am alive! by jinx
[March 10, 2019, 04:38:53 AM]


Re: Yes I am alive! by nschreiber0813
[March 09, 2019, 03:59:04 PM]


Re: CZ-1 2.0: problems, calibration and printing, part2 by MEgg
[February 26, 2019, 03:35:15 PM]


Re: Hi, Mushroom glue here. by nschreiber0813
[February 25, 2019, 02:09:13 PM]


Yes I am alive! by nschreiber0813
[February 25, 2019, 02:08:12 PM]


Arduino IK by BaldwinK
[February 25, 2019, 05:54:44 AM]


Re: Raspberry Pi / Python eBook - free for the next 11 hours! by MEgg
[February 24, 2019, 09:49:43 AM]