I was reading through the extras portion of your course, which is very good by the way, and came across an API section. When I executed my code, I got an Error Code: -1! Is this even possible, and is it possible to fix it? If you need more information then I am happy to give it.
Thanks in advance
ESP32 HTTP GET (OpenWeatherMap and ThingSpeak)
It says that I can customize it to my need to any API so I did some digging and I found out there is no API key. So I just customized my code with the endpoint (https://en.wikipedia.org/w/api.php?action=query&exlimit=1&explaintext=1&exsentences=10&formatversion=2&prop=extracts&titles=Pet_door&format=json). I would like to share my code but I have no idea how to.
- #include <WiFi.h>
- #include <HTTPClient.h>
- #include <Arduino_JSON.h>
- const char* ssid = “Thunderbird”;
- const char* password = “7327628990”;
- // Your Domain name with URL path or IP address with path
- // Example:
- // Replace with your country code and city
- // THE DEFAULT TIMER IS SET TO 10 SECONDS FOR TESTING PURPOSES
- // For a final application, check the API call limits per hour/minute to avoid getting blocked/banned
- unsigned long lastTime = 0;
- // Timer set to 10 minutes (600000)
- //unsigned long timerDelay = 600000;
- // Set timer to 10 seconds (10000)
- unsigned long timerDelay = 10000;
- String jsonBuffer;
- void setup() {
- Serial.begin(115200);
- WiFi.begin(ssid, password);
- Serial.println(“Connecting”);
- while(WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(“.”);
- }
- Serial.println(“”);
- Serial.print(“Connected to WiFi network with IP Address: “);
- Serial.println(WiFi.localIP());
- Serial.println(“Timer set to 10 seconds (timerDelay variable), it will take 10 seconds before publishing the first reading.”);
- }
- void loop() {
- // Send an HTTP GET request
- if ((millis() – lastTime) > timerDelay) {
- // Check WiFi connection status
- if(WiFi.status()== WL_CONNECTED){
- String serverPath = “en.wikipedia.org/w/api.php?action=query&exlimit=1&explaintext=1&exsentences=10&formatversion=2&prop=extracts&titles=Pet_door&format=json”;
- jsonBuffer = httpGETRequest(serverPath.c_str());
- Serial.println(jsonBuffer);
- JSONVar myObject = JSON.parse(jsonBuffer);
- // JSON.typeof(jsonVar) can be used to get the type of the var
- if (JSON.typeof(myObject) == “undefined”) {
- Serial.println(“Parsing input failed!”);
- return;
- }
- Serial.print(“JSON object = “);
- Serial.println(myObject);
- }
- else {
- Serial.println(“WiFi Disconnected”);
- }
- lastTime = millis();
- }
- }
- String httpGETRequest(const char* serverName) {
- WiFiClient client;
- HTTPClient http;
- Serial.println(serverName);
- // Your Domain name with URL path or IP address with path
- http.begin(client, serverName);
- // Send HTTP POST request
- int httpResponseCode = http.GET();
- String payload = “{}”;
- if (httpResponseCode>0) {
- Serial.print(“HTTP Response code: “);
- Serial.println(httpResponseCode);
- payload = http.getString();
- }
- else {
- Serial.print(“Error code: “);
- Serial.println(httpResponseCode);
- }
- // Free resources
- http.end();
- return payload;
- }
Hi.
Please share your code using pastebin or Github Gist.
The code gets messed up with the formatting and I can’t test it.
Thanks for your patience.
Regards,
Sara
Hi.
Try this code.
https://gist.github.com/sarasantos/e4673cefc088ae664422dabe19f83258
It’s simpler. Don’t forget to add your SSID and password.
The response is saved on the payload variable. Then, modify the variable or convert it to a JSON object if that’s more convenient for your project.
Regards,
Sara