ESP32 HTTP GET with Arduino IDE (OpenWeatherMap.org and ThingSpeak)
Learn how to make HTTP GET Requests with the ESP32 board with Arduino IDE. Decode JSON data from OpenWeatherMap.org and plot values in charts using ThinkgSpeak.
- Suitable forB.Tech/Diploma IoT/ESP32 students
- Build time1-2 weeks
- LevelBeginner
- Available atAjvika Labs, Kakinada
About this project
In this guide, you’ll learn how to make HTTP GET requests using the ESP32 board with Arduino IDE. We’ll demonstrate how to decode JSON data from OpenWeatherMap.org and plot values in charts using ThingSpeak.
Recommended:
ESP32 HTTP POST with Arduino IDE (ThingSpeak and IFTTT.com)
HTTP GET Request Method
The Hypertext Transfer Protocol (HTTP) works as a request-response protocol between a client and server. Here’s an example:
The ESP32 (client) submits an HTTP request to a Server (for example: OpenWeatherMap.org or ThingSpeak);
The server returns a response to the ESP32 (client);
Finally, the response contains status information about the request and may also contain the requested content.
HTTP GET
GET
is used to request data from a specified resource. It is often used to get values from APIs.
For example, you can use a simple request to return a value or JSON object:
GET /weather?countryCode=PT
Additionally, you can also make a GET request to update a value (like with ThingSpeak). For example, you can use:
GET /update?field1=value1
Note that the query string (
name = field1
and
value = value1
) is sent in the URL of the HTTP GET request.
(With HTTP GET, data is visible to everyone in the URL request.)
Prerequisites
Before proceeding with this tutorial, make sure you complete the following prerequisites.
Arduino IDE
We’ll program the
ESP32
using Arduino IDE, so make sure you have the ESP32 add-on installed.
Installing the ESP32 Board in Arduino IDE (Windows, Mac OS X, Linux)
Arduino_JSON Library
You also need to install the
Arduino_JSON library
. You can install this library in the Arduino IDE Library Manager. Just go to
Sketch
>
Include Library
>
Manage Libraries
and search for the library name as follows:
Other Web Services or APIs
In this guide, you’ll learn how to setup your
ESP32 board
to perform HTTP requests to OpenWeatherMap.org and ThingSpeak. If you prefer to learn with a local solution you can use
HTTP with Node-RED
. All examples presented in this guide also work with other APIs.
In summary, to make this guide compatible with any service, you need to search for the service API documentation. Then, you need the server name (URL or IP address), and parameters to send in the request (URL path or request body). Finally, modify our examples to integrate with any API you want to use.
1. ESP32 HTTP GET: JSON Data (OpenWeatherMap.org)
In this example you’ll learn how to make API requests to access data. As an example, we’ll use the OpenWeatherMap API. This API has a free plan and provides lots of useful information about the weather in almost any location in the world.
Using OpenWeatherMap API
An application programming interface (API) is a set of functions written by software developers to enable anyone to use their data or services. The
OpenWeatherMap
project has an API that enables users to request weather data.
In this project, you’ll use that API to request the day’s weather forecast for your chosen location. Learning to use APIs is a great skill because it allows you access to a wide variety of constantly changing information, such as current stock prices, currency exchange rates, the latest news, traffic updates, tweets, and much more.
Note:
API keys are unique to the user and shouldn’t be shared with anyone.
OpenWeatherMap’s free plan provides everything you need to complete this project. To use the API you need an API key, known as the APIID. To get the APIID:
Open a browser and go to
https://openweathermap.org/appid/
Press the
button and create a free account.
Go to this link:
https://home.openweathermap.org/api_keys
and get your API key.
On the
API keys
tab, you’ll see a default key (highlighted in a red rectangle in figure above); this is a unique key you’ll need to pull information from the site. Copy and paste this key somewhere; you’ll need it in a moment.
To pull information on weather in your chosen location, enter the following URL:
http://api.openweathermap.org/data/2.5/weather?q=yourCityName,yourCountryCode&APPID=yourUniqueAPIkey
Replace
yourCityName
with the city you want data for,
yourCountryCode
with the country code for that city, and
yourUniqueAPIkey
with the unique API key from step 4. For example, the updated API URL for the city of Porto, Portugal, would be:
http://api.openweathermap.org/data/2.5/weather?q=Porto,
PT&APPID=801d2603e9f2e1c70e042e4f5f6e0---
Copy your URL into your browser, and the API will return a bunch of information corresponding to your local weather. We got the following information about the weather in Porto, Portugal, on the day we wrote this tutorial.
{"coord":{"lon":-8.611,"lat":41.1496},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04d"}],"base":"stations","main":{"temp":294.58,"feels_like":294.95,"temp_min":293.82,"temp_max":295.65,"pressure":1016,"humidity":83},"visibility":10000,"wind":{"speed":8.94,"deg":180,"gust":8.94},"clouds":{"all":75},"dt":1666877635,"sys":{"type":2,"id":2009460,"country":"PT","sunrise":1666853957,"sunset":1666892227},"timezone":3600,"id":2735943,"name":"Porto","cod":200}
This is how it looks with indentation for better readability.
{
"coord": {
"lon": -8.611,
"lat": 41.1496
},
"weather": [
{
"id": 803,
"main": "Clouds",
"description": "broken clouds",
"icon": "04d"
}
],
"base": "stations",
"main": {
"temp": 294.58,
"feels_like": 294.95,
"temp_min": 293.82,
"temp_max": 295.65,
"pressure": 1016,
"humidity": 83
},
"visibility": 10000,
"wind": {
"speed": 8.94,
"deg": 180,
"gust": 8.94
},
"clouds": {
"all": 75
},
"dt": 1666877635,
"sys": {
"type": 2,
"id": 2009460,
"country": "PT",
"sunrise": 1666853957,
"sunset": 1666892227
},
"timezone": 3600,
"id": 2735943,
"name": "Porto",
"cod": 200
}
Next, you’ll see how to use this information to get specific data like temperature, humidity, pressure, wind speed, etc.
Code ESP32 HTTP GET OpenWeatherMap.org
After installing the necessary board add-ons and libraries, copy the following code to your Arduino IDE, but don’t upload it yet. You need to make some changes to make it work for you.
/*
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/
#include <WiFi.h>
#include <HTTPClient.h>
#include <Arduino_JSON.h>
const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";
// Your Domain name with URL path or IP address with path
String openWeatherMapApiKey = "REPLACE_WITH_YOUR_OPEN_WEATHER_MAP_API_KEY";
// Example:
//String openWeatherMapApiKey = "bd939aa3d23ff33d3c8f5dd1dd435";
// Replace with your country code and city
String city = "Porto";
String countryCode = "PT";
// 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 readin
Recommended:
ESP32 HTTP POST with Arduino IDE (ThingSpeak and IFTTT.com)
HTTP GET Request Method
The Hypertext Transfer Protocol (HTTP) works as a request-response protocol between a client and server. Here’s an example:
The ESP32 (client) submits an HTTP request to a Server (for example: OpenWeatherMap.org or ThingSpeak);
The server returns a response to the ESP32 (client);
Finally, the response contains status information about the request and may also contain the requested content.
HTTP GET
GET
is used to request data from a specified resource. It is often used to get values from APIs.
For example, you can use a simple request to return a value or JSON object:
GET /weather?countryCode=PT
Additionally, you can also make a GET request to update a value (like with ThingSpeak). For example, you can use:
GET /update?field1=value1
Note that the query string (
name = field1
and
value = value1
) is sent in the URL of the HTTP GET request.
(With HTTP GET, data is visible to everyone in the URL request.)
Prerequisites
Before proceeding with this tutorial, make sure you complete the following prerequisites.
Arduino IDE
We’ll program the
ESP32
using Arduino IDE, so make sure you have the ESP32 add-on installed.
Installing the ESP32 Board in Arduino IDE (Windows, Mac OS X, Linux)
Arduino_JSON Library
You also need to install the
Arduino_JSON library
. You can install this library in the Arduino IDE Library Manager. Just go to
Sketch
>
Include Library
>
Manage Libraries
and search for the library name as follows:
Other Web Services or APIs
In this guide, you’ll learn how to setup your
ESP32 board
to perform HTTP requests to OpenWeatherMap.org and ThingSpeak. If you prefer to learn with a local solution you can use
HTTP with Node-RED
. All examples presented in this guide also work with other APIs.
In summary, to make this guide compatible with any service, you need to search for the service API documentation. Then, you need the server name (URL or IP address), and parameters to send in the request (URL path or request body). Finally, modify our examples to integrate with any API you want to use.
1. ESP32 HTTP GET: JSON Data (OpenWeatherMap.org)
In this example you’ll learn how to make API requests to access data. As an example, we’ll use the OpenWeatherMap API. This API has a free plan and provides lots of useful information about the weather in almost any location in the world.
Using OpenWeatherMap API
An application programming interface (API) is a set of functions written by software developers to enable anyone to use their data or services. The
OpenWeatherMap
project has an API that enables users to request weather data.
In this project, you’ll use that API to request the day’s weather forecast for your chosen location. Learning to use APIs is a great skill because it allows you access to a wide variety of constantly changing information, such as current stock prices, currency exchange rates, the latest news, traffic updates, tweets, and much more.
Note:
API keys are unique to the user and shouldn’t be shared with anyone.
OpenWeatherMap’s free plan provides everything you need to complete this project. To use the API you need an API key, known as the APIID. To get the APIID:
Open a browser and go to
https://openweathermap.org/appid/
Press the
button and create a free account.
Go to this link:
https://home.openweathermap.org/api_keys
and get your API key.
On the
API keys
tab, you’ll see a default key (highlighted in a red rectangle in figure above); this is a unique key you’ll need to pull information from the site. Copy and paste this key somewhere; you’ll need it in a moment.
To pull information on weather in your chosen location, enter the following URL:
http://api.openweathermap.org/data/2.5/weather?q=yourCityName,yourCountryCode&APPID=yourUniqueAPIkey
Replace
yourCityName
with the city you want data for,
yourCountryCode
with the country code for that city, and
yourUniqueAPIkey
with the unique API key from step 4. For example, the updated API URL for the city of Porto, Portugal, would be:
http://api.openweathermap.org/data/2.5/weather?q=Porto,
PT&APPID=801d2603e9f2e1c70e042e4f5f6e0---
Copy your URL into your browser, and the API will return a bunch of information corresponding to your local weather. We got the following information about the weather in Porto, Portugal, on the day we wrote this tutorial.
{"coord":{"lon":-8.611,"lat":41.1496},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04d"}],"base":"stations","main":{"temp":294.58,"feels_like":294.95,"temp_min":293.82,"temp_max":295.65,"pressure":1016,"humidity":83},"visibility":10000,"wind":{"speed":8.94,"deg":180,"gust":8.94},"clouds":{"all":75},"dt":1666877635,"sys":{"type":2,"id":2009460,"country":"PT","sunrise":1666853957,"sunset":1666892227},"timezone":3600,"id":2735943,"name":"Porto","cod":200}
This is how it looks with indentation for better readability.
{
"coord": {
"lon": -8.611,
"lat": 41.1496
},
"weather": [
{
"id": 803,
"main": "Clouds",
"description": "broken clouds",
"icon": "04d"
}
],
"base": "stations",
"main": {
"temp": 294.58,
"feels_like": 294.95,
"temp_min": 293.82,
"temp_max": 295.65,
"pressure": 1016,
"humidity": 83
},
"visibility": 10000,
"wind": {
"speed": 8.94,
"deg": 180,
"gust": 8.94
},
"clouds": {
"all": 75
},
"dt": 1666877635,
"sys": {
"type": 2,
"id": 2009460,
"country": "PT",
"sunrise": 1666853957,
"sunset": 1666892227
},
"timezone": 3600,
"id": 2735943,
"name": "Porto",
"cod": 200
}
Next, you’ll see how to use this information to get specific data like temperature, humidity, pressure, wind speed, etc.
Code ESP32 HTTP GET OpenWeatherMap.org
After installing the necessary board add-ons and libraries, copy the following code to your Arduino IDE, but don’t upload it yet. You need to make some changes to make it work for you.
/*
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/
#include <WiFi.h>
#include <HTTPClient.h>
#include <Arduino_JSON.h>
const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";
// Your Domain name with URL path or IP address with path
String openWeatherMapApiKey = "REPLACE_WITH_YOUR_OPEN_WEATHER_MAP_API_KEY";
// Example:
//String openWeatherMapApiKey = "bd939aa3d23ff33d3c8f5dd1dd435";
// Replace with your country code and city
String city = "Porto";
String countryCode = "PT";
// 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 readin
Technologies used
Hardware components
- ESP32
- Arduino
Software & tools
- Arduino IDE
- Node-RED
- ThingSpeak
What you get
- Working project / hardware kit, tested before delivery
- Complete source code with explanation
- Project demo and guidance for your viva
- Project report, PPT and IEEE paper support (on request)
Delivery & Payment
Delivery takes a minimum of two weeks from the date your inquiry is confirmed.
Payment is in two parts:
• 50% advance to start the project
• Remaining 50% before delivery
Note:
• Documentation and PPT are not included by default
• If you need documentation/PPT, it can be arranged for extra time and cost
We'll confirm the exact timeline and cost with you after reviewing your requirements.