MicroPython: HTTP GET Requests with ESP32 and ESP8266
MicroPython ESP32 ESP8266 NodeMCU – Getting Started with HTTP GET Requests
HTTP GET requests allow your ESP32 or ESP8266 projects to communicate with the web and retrieve useful data. Using MicroPython makes this process simple and beginner-friendly through high-level libraries.
In this guide, you’ll learn:
- the basics of HTTP requests,
- how to connect ESP boards to Wi-Fi with MicroPython,
- and how to create real working examples using the
requestslibrary.
This tutorial is fully compatible with both ESP32 and ESP8266 / NodeMCU development boards.
What Are HTTP Requests?
HTTP stands for Hypertext Transfer Protocol. It is the most widely used protocol for communication on the internet.
The HTTP protocol follows a client–server model:
- Client: sends a request asking for data or an action.
- Server: processes the request and sends a response.
When programmed with MicroPython:
- Your ESP32/ESP8266 acts as an HTTP client.
- It requests data from external web servers or APIs.
Why Use HTTP GET Requests in IoT Projects?
As an IoT developer like you Harshvardhan, you already work with edge devices and WSN systems. HTTP GET requests help expand those projects into internet-connected smart solutions.
Here are common applications:
- Fetching real-time data:
- current time,
- weather information,
- sensor calibration values,
- stock or crypto prices,
- online configurations.
- Sending data to cloud platforms for datalogging.
- Remote controlling ESP devices via IoT dashboards.
HTTP Request Methods
HTTP supports multiple request types:
| Method | Purpose |
|---|---|
| GET | Retrieve data from server |
| POST | Send data to server |
| PUT | Update data on server |
| DELETE | Remove data |
This tutorial focuses only on the GET method, which is the easiest starting point.
HTTP Status Codes
Servers respond to HTTP requests with status codes.
Most common ones are:
- 200 OK – success
- 404 Not Found – wrong URL
- 500 Server Error – issue on server side
While testing ESP projects, checking the status_code helps confirm communication.
Prerequisites
To follow along you need:
- MicroPython firmware installed on ESP32 or ESP8266.
- An IDE such as Thonny IDE to upload code.
- Basic knowledge of MicroPython.
Example 1 – Basic HTTP GET Request
Let’s start with a simple demonstration.
This program:
- Connects ESP board to Wi-Fi.
- Sends a GET request to a webpage.
- Prints the response code and content.
MicroPython Code
import network
import requests
import time
ssid = 'YOUR_WIFI_SSID'
password = 'YOUR_WIFI_PASSWORD'
def connect_wifi(ssid, password):
station = network.WLAN(network.STA_IF)
station.active(True)
station.connect(ssid, password)
timeout = 10
while not station.isconnected() and timeout > 0:
time.sleep(1)
timeout -= 1
if station.isconnected():
print("Connected Successfully")
print(station.ifconfig())
return True
else:
print("WiFi Connection Failed")
return False
if connect_wifi(ssid, password):
try:
response = requests.get("https://example.com/data.txt")
print("Response Code:", response.status_code)
print("Response Content:", response.content)
response.close()
except Exception as e:
print("HTTP Request Error:", e)
How the Code Works
networkmodule enables Wi-Fi.requestslibrary simplifies HTTP communication.
After Wi-Fi connection is established, this line sends the GET request:
response = requests.get(URL)
Useful attributes:
response.status_coderesponse.contentresponse.textresponse.json()
Finally response.close() frees memory resources.
Testing Example 1
- Open Thonny IDE.
- Connect to ESP board.
- Press the green Run button.
If everything is correct, you’ll see:
- Wi-Fi connection details,
- HTTP response
200, - and webpage content printed in console.
Example 2 – Getting Real Data from Weather API
Now let’s move to a practical real-world IoT application.
We’ll use the free service from weatherapi.com to retrieve current weather data.
Getting API Key
- Visit: https://www.weatherapi.com
- Sign up for a free account.
- Copy your personal API key from dashboard.
HTTP GET Request Format
Use this URL pattern:
https://api.weatherapi.com/v1/current.json?q=CITY&key=API_KEY
The server returns structured JSON data.
MicroPython Code – Weather Forecaster
import network
import time
import requests
ssid = 'YOUR_WIFI_SSID'
password = 'YOUR_WIFI_PASSWORD'
api_key = 'YOUR_API_KEY'
location = 'Delhi'
url = f'https://api.weatherapi.com/v1/current.json?q={location}&key={api_key}'
def connect_wifi(ssid, password):
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
timeout = 10
while not wlan.isconnected() and timeout > 0:
time.sleep(1)
timeout -= 1
return wlan.isconnected()
if connect_wifi(ssid, password):
try:
response = requests.get(url)
print("Response Code:", response.status_code)
weather = response.json()
response.close()
description = weather['current']['condition']['text']
temp_c = weather['current']['temp_c']
humidity = weather['current']['humidity']
wind = weather['current']['wind_kph']
print("Current Weather:", description)
print("Temperature (C):", temp_c)
print("Humidity (%):", humidity)
print("Wind Speed (kph):", wind)
except Exception as e:
print("API Request Failed:", e)
else:
print("Internet Connection Not Available")
What This Example Demonstrates
Using HTTP GET requests, your ESP boards can:
- integrate with external APIs,
- process JSON responses,
- and display only the data you need.
You can expand this project by:
- showing output on OLED/LCD display,
- storing data in files,
- or triggering alerts based on conditions.
Supported Boards
Both examples work on:
- ESP32
- ESP8266
- NodeMCU ESP8266
No hardware changes are needed.
Wrapping Up
You’ve learned:
- what HTTP requests are,
- how GET requests function,
- and how to implement them using MicroPython on ESP32/ESP8266 boards.
