• 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

ESP32 API

Q&A Forum › Category: ESP32 › ESP32 API
0 Vote Up Vote Down
krunal patel asked 6 months ago

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

Question Tags: Esp32 API Problems
9 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 6 months ago

Hi.
What course and what project are you following?

0 Vote Up Vote Down
krunal patel answered 6 months ago

Learn ESP32 on Arduino IDE

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

What is the project?

0 Vote Up Vote Down
krunal patel answered 6 months ago

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.
 

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

Hi.
To share your code use github gist or pastebin.
Regards,
Sara

0 Vote Up Vote Down
krunal patel answered 6 months ago
  1. #include <WiFi.h>
  2. #include <HTTPClient.h>
  3. #include <Arduino_JSON.h>
  4.  
  5. const char* ssid = “Thunderbird”;
  6. const char* password = “7327628990”;
  7.  
  8. // Your Domain name with URL path or IP address with path
  9.  
  10. // Example:
  11.  
  12.  
  13. // Replace with your country code and city
  14.  
  15.  
  16. // THE DEFAULT TIMER IS SET TO 10 SECONDS FOR TESTING PURPOSES
  17. // For a final application, check the API call limits per hour/minute to avoid getting blocked/banned
  18. unsigned long lastTime = 0;
  19. // Timer set to 10 minutes (600000)
  20. //unsigned long timerDelay = 600000;
  21. // Set timer to 10 seconds (10000)
  22. unsigned long timerDelay = 10000;
  23.  
  24. String jsonBuffer;
  25.  
  26. void setup() {
  27. Serial.begin(115200);
  28.  
  29. WiFi.begin(ssid, password);
  30. Serial.println(“Connecting”);
  31. while(WiFi.status() != WL_CONNECTED) {
  32. delay(500);
  33. Serial.print(“.”);
  34. }
  35. Serial.println(“”);
  36. Serial.print(“Connected to WiFi network with IP Address: “);
  37. Serial.println(WiFi.localIP());
  38.  
  39. Serial.println(“Timer set to 10 seconds (timerDelay variable), it will take 10 seconds before publishing the first reading.”);
  40. }
  41.  
  42. void loop() {
  43. // Send an HTTP GET request
  44. if ((millis() – lastTime) > timerDelay) {
  45. // Check WiFi connection status
  46. if(WiFi.status()== WL_CONNECTED){
  47. 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”;
  48.  
  49. jsonBuffer = httpGETRequest(serverPath.c_str());
  50. Serial.println(jsonBuffer);
  51. JSONVar myObject = JSON.parse(jsonBuffer);
  52.  
  53. // JSON.typeof(jsonVar) can be used to get the type of the var
  54. if (JSON.typeof(myObject) == “undefined”) {
  55. Serial.println(“Parsing input failed!”);
  56. return;
  57. }
  58.  
  59. Serial.print(“JSON object = “);
  60. Serial.println(myObject);
  61.  
  62. }
  63. else {
  64. Serial.println(“WiFi Disconnected”);
  65. }
  66. lastTime = millis();
  67. }
  68. }
  69.  
  70. String httpGETRequest(const char* serverName) {
  71. WiFiClient client;
  72. HTTPClient http;
  73. Serial.println(serverName);
  74. // Your Domain name with URL path or IP address with path
  75. http.begin(client, serverName);
  76.  
  77. // Send HTTP POST request
  78. int httpResponseCode = http.GET();
  79.  
  80. String payload = “{}”;
  81.  
  82. if (httpResponseCode>0) {
  83. Serial.print(“HTTP Response code: “);
  84. Serial.println(httpResponseCode);
  85. payload = http.getString();
  86. }
  87. else {
  88. Serial.print(“Error code: “);
  89. Serial.println(httpResponseCode);
  90. }
  91. // Free resources
  92. http.end();
  93.  
  94. return payload;
  95. }
0 Vote Up Vote Down
krunal patel answered 6 months ago
  1. Is this right?
0 Vote Up Vote Down
Sara Santos Staff answered 6 months ago

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

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

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

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

  • [eBook Updated] SMART HOME with RPi, ESP32, and ESP8266 (version 1.2) March 8, 2023
  • [eBook Updated] ESP32-CAM Projects (version 1.3) January 7, 2023

You must be logged in to view this content.

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

Copyright © 2013-2023 · 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.