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 1213 times)

Gareth

  • Member
  • ****
  • Posts: 55
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 Bappi
[Today at 07:11:31 PM]


ESP32 "Life is Life" 64x32 flexible P4 RGB LED by jinx
[May 18, 2019, 11:19:44 AM]


Warco HV-4" Rotary Table by jinx
[May 08, 2019, 03:12:58 PM]


Warco WM14 milling machine by jinx
[April 28, 2019, 09:41:40 AM]


MKS GEN L Servo Issue by Reywas
[April 22, 2019, 09:10:08 PM]


Meowth Abandoned by Nemesis
[April 11, 2019, 11:08:00 AM]


Bench Power: Help a rebel out by jinx
[April 03, 2019, 04:20:10 AM]


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


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]


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]

* Recent Posts

Re: MKS Gen L 1.0 by Bappi
[Today at 07:11:31 PM]


Re: MKS Gen L 1.0 by jinx
[Today at 03:16:02 PM]


Re: MKS Gen L 1.0 by Bappi
[Today at 02:53:00 PM]


Re: MKS Gen L 1.0 by jinx
[Today at 03:31:49 AM]


Re: MKS Gen L 1.0 by Bappi
[May 19, 2019, 07:16:21 PM]


Re: MKS Gen L 1.0 by Bappi
[May 19, 2019, 04:04:47 PM]


Re: MKS Gen L 1.0 by jinx
[May 19, 2019, 08:50:41 AM]


Re: MKS Gen L 1.0 by Bappi
[May 18, 2019, 04:38:59 PM]


Re: MKS Gen L 1.0 by Bappi
[May 18, 2019, 04:25:37 PM]


Re: MKS Gen L 1.0 by jinx
[May 18, 2019, 04:10:22 PM]


Re: MKS Gen L 1.0 by Bappi
[May 18, 2019, 02:01:41 PM]


Re: ESP32 "Life is Life" 64x32 flexible P4 RGB LED by jinx
[May 18, 2019, 11:19:44 AM]


Re: MKS Gen L 1.0 by jinx
[May 18, 2019, 11:16:21 AM]


Re: MKS Gen L 1.0 by Bappi
[May 18, 2019, 10:04:31 AM]


Re: MKS Gen L 1.0 by Bappi
[May 18, 2019, 09:22:11 AM]