• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

HTTP Requests (WorldTime API and ThingSpeak)

Q&A Forum › Category: ESP32 › HTTP Requests (WorldTime API and ThingSpeak)
0 Vote Up Vote Down
NAZAR PP asked 6 months ago

Hi,
I’m trying to get work with the example program ” HTTP Requests (WorldTime API and ThingSpeak)
 
Though ArduinoJson was installed successfully, I’m getting following errors while compling.
 
Compilation error: control reaches end of non-void function [-Werror=return-type]
 
I think the below program line is either not supporting or have some syntax error 
 
StaticJsonDocument<1024> doc; 
 
Please help and guide me how to solve this

7 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 6 months ago

Hi.
 
I think there was an update on the library.
 
I think that now you should declare the json doc as follows:

JsonDocument doc; 

instead of

StaticJsonDocument<1024> doc; 

 
Let me know if that fixes the issue.
 
If so, I’ll update the codes.
 
Regards,
Sara

0 Vote Up Vote Down
NAZAR PP answered 6 months ago

Hi,
I tried changing the instruction to JsonDocument doc; as you said
The syntax errors that were shown earlier related to StaticJsonDocument <1024> doc;  are now fixed, but the return type error [Werror=return-type] still exists. 

0 Vote Up Vote Down
Sara Santos Staff answered 6 months ago

Hi.
What line of code is giving you that error?
Regards,
SAra

0 Vote Up Vote Down
NAZAR PP answered 6 months ago

Hi,
I solved the problem, but in a different way. I think the problem is because the function return String variable was not supported, I am not sure. 
I changed the function type for String getDateTime as Void getDateTime (that don’t have return value) and assigned the received JsonDocument datetime value to the variable requestData from the function loop itself. 

#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
 
// Replace with your network credentials
const char* ssid = “xxxxxx”;
const char* password = “xxxxxxxx”;
 
// Specify the timezone you want to get the time for: https://worldtimeapi.org/api/timezone/
const char* timezone =  “Asia/Calcutta”;//”Europe/Lisbon”;
// API endpoint
String url = String(“http://worldtimeapi.org/api/timezone/&#8221;) + timezone;
 
String currentDate;
String currentTime;
 
// Store hour, minute, second
int hour;
int minute;
int second;
 
// Store the result from the API request
String requestData = “failed”;
 
String formatTime(int time) {
  return (time < 10) ? “0” + String(time) : String(time);
}
  void getDateTime() {
   if (WiFi.status() == WL_CONNECTED) {
    HTTPClient http;
    // Construct the request
    http.begin(url);
    // Make the GET request
    int httpCode = http.GET();
    if (httpCode > 0) {
      // Check for the response
      if (httpCode == HTTP_CODE_OK) {
        String payload = http.getString();
        Serial.println(“Time information:”);
        Serial.println(payload);
        // Parse the JSON to extract the time
        JsonDocument doc;
        DeserializationError error = deserializeJson(doc, payload);
        if (!error) {
          const char* datetime = doc[“datetime”];          
          // Split the datetime into date and time
          requestData =datetime;
        }
        else {
          requestData =“failed”;
        }
       }
    }
}
 
void setup() {
  Serial.begin(115200);
  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  Serial.print(“Connecting”);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(“.”);
  }
  Serial.print(“\nConnected to Wi-Fi network with IP Address: “);
  Serial.println(WiFi.localIP());
 
  while(requestData == “failed”) {
 //requestData = getDateTime();
 getDateTime();
  }
 
  int splitIndex = requestData.indexOf(‘T’);
  // Extract date
  currentDate = requestData.substring(0, splitIndex);
  Serial.println(“Current Date: “ + currentDate);
 
  // Extract time
  currentTime = requestData.substring(splitIndex + 1, splitIndex + 9);
  Serial.println(“Current Time: “ + currentTime);
 
  // Extract hour, minute, second
  hour = currentTime.substring(0, 2).toInt();
  minute = currentTime.substring(3, 5).toInt();
  second = currentTime.substring(6, 8).toInt();
  Serial.println(“Hour: “ + String(formatTime(hour)));
  Serial.println(“Minute: “ + String(formatTime(minute)));
  Serial.println(“Second: “ + String(formatTime(second)));
}
 
void loop() {
 
}
 

0 Vote Up Vote Down
Sara Santos Staff answered 6 months ago

Hi.
This issue was already resolved with Rui via email.
We’ll update the eBook with the new code.
You can check the new code here: https://github.com/RuiSantosdotme/Learn-ESP32-eBook/blob/main/Module_5/5_3_1_HTTP_GET_WorldTimeAPI/5_3_1_HTTP_GET_WorldTimeAPI.ino 
Regards,
Sara
 

0 Vote Up Vote Down
NAZAR PP answered 6 months ago

Thanks, my pleasure. 

0 Vote Up Vote Down
Sara Santos Staff answered 6 months ago

Great.
I’ll mark this issue as resolved. If you need further help, you just need to open a new question in our forum.
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025
  • [eBook Updated] Learn ESP32 with Arduino IDE eBook – Version 3.2 April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.