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
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
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.
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/”) + 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() {
}
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