How to call an api with nodemcu from this wesite:http: (//davidayala.eu/current-time/)?

http://davidayala.eu/current-time/

The website above offers free Timezone APIs. I have been trying to request the API with my esp8266 NodeMCU, but still, i get no response. Can anyone please demonstrate to me with a code sample how it's supposed to be done? I will be posting my code soon.

The following is my code (In arduino IDE) for my NodeMCU

#include <ESP8266WiFi.h>

String result;
char host[]="script.google.com";
WiFiClient client;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
WiFi.begin("Faiz", "hautepackard");
while(WiFi.status() != WL_CONNECTED){
Serial.print(".");
delay(500);
}
Serial.println("Connected!");
Serial.println(WiFi.localIP());
}
void loop() {
Time();
delay(3000);
}
String url = "/macros/s/AKfycbyd5AcbAnWi2Yn0xhFRbyzS4qMq1VucMVgVvhul5XqS9HkAyJY/exec";
void Time(){
if (client.connect(host, 443)) { //starts client connection, checks for connection
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
Serial.println("Server is accessible");
} else {
Serial.println("connection failed"); //error message if no client connect
Serial.println();
}
result = "";
while (client.available()) { //connected or data available
char c = client.read(); //gets byte from ethernet buffer
result = result+c;
}
Serial.println(result);
client.stop(); //stop client
Serial.println("end of function");
}

And this is what i get in the Serial monitor:

........Connected!
Server is accessible

end of function

Am i going wrong anywhere in the code? Please help this burning question.

sort by: active | newest | oldest
steveastrouk3 months ago

You need to make the actual API call, according to the site you quote its.

[api base url]?tz=Europe/Madrid or whereever. You then need to read back the returned string, I guess with something like client.readln.

Where did you find the code you're quoting that you are using on your arduino ?

Techovator0819 (author)  steveastrouk3 months ago

I have included in the code a way to read the available string. If you look deeply into the code you will find this:

while (client.available()) { //connected or data available
char c = client.read(); //gets byte from ethernet buffer
result = result+c;
}

So, as long as data is available in the client buffer, the while loop will store it in a string. Also, this code works fine with other APIs, except this one. So, i am absolutely perplexed about where i am going wrong.

If i use Google ARC, and call the URL it gives me the required data. So that clearly indicates that I am going wrong somewhere. Any suggestions?

I saw that, but what happens if there is NO result at the instant it starts to execute the while ? It drops through, loops round, and sends another call to the API.

Techovator0819 (author)  steveastrouk3 months ago

I tried altering my code so that there is a small delay to wait until the data is read from the Client buffer. But still, the problem seems to persist. What do I do?

How much delay ? Can you wait until there is a reply, and let it lock the processor up if one doesn't arrive ?

steveastrouk3 months ago

You probably need to wait UNTIL there is data available too.

while (!client.available) {} //Dangerous, since if client is never available, this won't end.

Better

int incer=0;

while (!client.available) | (incer<1000) {incer++}

steveastrouk3 months ago

You have server access. Now call the time function and print the result.

Techovator0819 (author)  steveastrouk3 months ago

I didn't quite get you, what is the time function? Can you type that bit of code and point it out to me?

Techovator0819 (author)  Techovator08193 months ago

I typed this code to get the available data after making sure the server is available, please check whether it's right or wrong.

client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");

+1

2+