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

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]


MKS Gen L 1.0 by jinx
[November 28, 2018, 02:23:50 AM]


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


Time of Flight >>> Rounded Corners !!! by Gareth
[November 15, 2018, 03:21:51 PM]


Double_ing up the "Time of Flight" sensors by Gareth
[November 13, 2018, 04:05:02 AM]


Point cloud anyone !!! maybe 3D scanner by maelh
[November 11, 2018, 05:38:33 AM]


removable magnetic print surface by Gareth
[November 09, 2018, 10:53:45 AM]


Robot Cop by Valeriex
[November 07, 2018, 05:48:20 AM]


it's lukeyes by Valeriex
[November 07, 2018, 05:47:16 AM]


TOF gets a local DEBUG screen by Gareth
[November 07, 2018, 03:47:29 AM]


BlueTooth elves choose "BlueTeeth" by Gareth
[November 07, 2018, 03:40:19 AM]

* Recent Posts

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


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


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


Re: The Original Prusa I3 Mk3 by Bajdi
[November 30, 2018, 02:26:48 PM]


Re: The Original Prusa I3 Mk3 by tinhead
[November 30, 2018, 04:38:53 AM]


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


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


Re: MKS Gen L 1.0 by jinx
[November 28, 2018, 02:23:50 AM]


Re: MKS Gen L 1.0 by Prokktor
[November 27, 2018, 08:35:38 AM]


Re: The Original Prusa I3 Mk3 by tinhead
[November 26, 2018, 02:42:26 PM]


Re: The Original Prusa I3 Mk3 by jinx
[November 26, 2018, 02:24:05 PM]


Re: The Original Prusa I3 Mk3 by Bajdi
[November 25, 2018, 07:54:21 AM]


Re: The Original Prusa I3 Mk3 by tinhead
[November 25, 2018, 06:11:22 AM]


Re: The Original Prusa I3 Mk3 by Bajdi
[November 25, 2018, 04:40:31 AM]


The Original Prusa I3 Mk3 by tinhead
[November 24, 2018, 12:02:51 PM]