{"id":182443,"date":"2025-12-11T15:04:33","date_gmt":"2025-12-11T15:04:33","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=182443"},"modified":"2025-12-11T15:18:57","modified_gmt":"2025-12-11T15:18:57","slug":"micropython-http-get-requests-esp32-esp8266","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/micropython-http-get-requests-esp32-esp8266\/","title":{"rendered":"MicroPython: HTTP GET Requests with ESP32\/ESP8266"},"content":{"rendered":"\n<p>Learn how to make HTTP GET Requests with the ESP32 or ESP8266 boards programmed with MicroPython. We&#8217;ll first cover the basics of HTTP GET requests, and then, we&#8217;ll create and explain two different examples using the <span class=\"rnthl rntliteral\">requests<\/span> library. <\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/ESP32-ESP8266-HTTP-GET-Requests.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"MicroPython ESP32 ESP8266 NodeMCU Getitng Started with HTTP GET Requests\" class=\"wp-image-182476\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/ESP32-ESP8266-HTTP-GET-Requests.jpg?w=1920&amp;quality=100&amp;strip=all&amp;ssl=1 1920w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/ESP32-ESP8266-HTTP-GET-Requests.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/ESP32-ESP8266-HTTP-GET-Requests.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/ESP32-ESP8266-HTTP-GET-Requests.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/ESP32-ESP8266-HTTP-GET-Requests.jpg?resize=1536%2C864&amp;quality=100&amp;strip=all&amp;ssl=1 1536w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n<p>HTTP requests are essential to interact with web services to retrieve data from external sources, integrate with web APIs to access third-party services, and much more, as you&#8217;ll see in this tutorial.<\/p>\n\n\n\n<p>This tutorial is compatible with ESP32 and ESP8266 boards.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Table of Contents<\/h2>\n\n\n\n<p>In this tutorial, we&#8217;ll cover the following subjects.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"#http-requests-intro\" title=\"\">What are HTTP Requests?<\/a><\/li>\n\n\n\n<li><a href=\"#http-requests-technical-overview\" title=\"\">Technical Overview of HTTP Requests<\/a>\n<ul class=\"wp-block-list\">\n<li><a href=\"#http-requests\" title=\"\">HTTP GET Requests<\/a><\/li>\n\n\n\n<li><a href=\"#http-request-methods\" title=\"\">HTTP Request Methods<\/a><\/li>\n\n\n\n<li><a href=\"#http-status-codes\" title=\"\">HTTP Status Codes<\/a><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><a href=\"#esp32-esp8266-http-requests-micropython\" title=\"\">HTTP Requests with the ESP32 and ESP8266 (MicroPython)<\/a><\/li>\n\n\n\n<li><a href=\"#get-data-from-web\" title=\"\">Getting Data from the Web Using HTTP GET Requests<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites &#8211; MicroPython Firmware<\/h2>\n\n\n\n<p>To follow this tutorial, you need MicroPython firmware installed on your ESP32 or ESP8266 boards. You also need an IDE to write and upload the code to your board. We suggest using Thonny IDE:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-thonny-micropython-python-ide-esp32-esp8266\/\">Installing and getting started with Thonny IDE<\/a><\/li>\n<\/ul>\n\n\n\n<p class=\"rntbox rntclblue\"><strong>New to MicroPython?<\/strong> Check out our eBook: <a href=\"https:\/\/randomnerdtutorials.com\/micropython-programming-with-esp32-and-esp8266\/\" title=\"\">MicroPython Programming with ESP32 and ESP8266 eBook (2nd Edition)<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"http-requests-intro\">What are HTTP Requests?<\/h2>\n\n\n\n<p>The most common way to exchange data with another computer (server) on the internet is using the HTTP protocol.<\/p>\n\n\n\n<p>HTTP (Hypertext Transfer Protocol) works through a client-server model. In this model, the client communicates with a server using HTTP requests. An HTTP request is a message sent by the client to a server, typically to request a specific action or retrieve information. The server sends a response back to the client also through HTTP.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"383\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/http-request-esp32.jpeg?resize=750%2C383&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"HTTP Request ESP32\" class=\"wp-image-182444\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/http-request-esp32.jpeg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/http-request-esp32.jpeg?resize=300%2C153&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Client<\/strong>: initiates communication through a request.<\/li>\n\n\n\n<li><strong>Server<\/strong>: receives and processes the request and sends a response in return. <\/li>\n<\/ul>\n\n\n\n<p>The ESP32 can either be a client or a server. When it is a client, it sends requests to servers. When it is a <a href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-micropython-web-server\/\" title=\"\">server<\/a>, it handles the client\u2019s requests. In this tutorial, we\u2019ll take a look at the ESP32 and ESP8266 as an HTTP client that makes requests to a server.<\/p>\n\n\n\n<p>Here are some examples of what you can do using HTTP requests with the ESP32\/ESP8266:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Get data from the internet: for example, time, current weather, stock prices, traffic updates, and much more\u2026<\/li>\n\n\n\n<li>Datalogging: send data to the cloud to save your data online;<\/li>\n\n\n\n<li>Remote control of your ESP32: by interacting with IoT platforms like Adafruit IO, Node-RED, and others\u2014you can interact with your board remotely by exchanging data via HTTP requests;<\/li>\n\n\n\n<li>Interact with third-party services to send notifications: SMS, emails, notifications, and much more\u2026<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"http-requests-technical-overview\">Technical Overview of HTTP Requests<\/h2>\n\n\n\n<p>Let\u2019s take a quick look at the technical aspects of HTTP requests. If this is your first time dealing with HTTP requests in a technical manner and it seems confusing, that\u2019s perfectly normal. <\/p>\n\n\n\n<p>But, don\u2019t worry, there are MicroPython modules that abstract all the technical stuff, making it very straightforward to make and handle HTTP requests. Additionally, for more information about the technical aspects of HTTP requests, we recommend the following resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.realisable.co.uk\/support\/documentation\/iman-user-guide\/DataConcepts\/WebRequestAnatomy.htm\" target=\"_blank\" rel=\"noopener\" title=\"\">Anatomy of an http request &amp; response<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.ibm.com\/docs\/en\/cics-ts\/5.3?topic=concepts-http-protocol\" target=\"_blank\" rel=\"noopener\" title=\"\">The HTTP Protocol (documentation by IBM)<\/a><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"http-requests\">HTTP GET Requests&nbsp;&nbsp;&nbsp;<\/h3>\n\n\n\n<p>HTTP request consists of several parts. The main elements are: request line, headers, and body.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Request Line<\/h4>\n\n\n\n<p>The request line specifies the HTTP method, the resource being requested (the URL), and the version of the HTTP protocol being used. Common HTTP methods include GET, POST, PUT, and DELETE. For example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">GET \/path\/to\/resource HTTP\/1.1<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Headers<\/h4>\n\n\n\n<p>Headers provide information about the request. It can include the host to which the client is sending the request, the type of content being sent or accepted, the user\u2011agent (which identifies the client making the request), and more. For example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Host: example.com<br>User-Agent: ESP32<br>Accept: application\/json<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Body<\/h4>\n\n\n\n<p>The body is an optional content, and it contains data or information that we want to send to the server. This is used in POST and PUT requests. For example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">POST \/submit-form HTTP\/1.1<br>Content-Type: application\/x-www-form-urlencoded<br>username=johndoe&amp;password=secretpassword<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"http-request-methods\">HTTP Request Methods<\/h3>\n\n\n\n<p>The choice of HTTP method in a request indicates the intended action to be performed on the server. Some commonly used methods include:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>GET:<\/strong> retrieve data from the server.<\/li>\n\n\n\n<li><strong>POST:<\/strong> submit data to be processed to a specified resource.<\/li>\n\n\n\n<li><strong>PUT:<\/strong> update a resource on the server.<\/li>\n\n\n\n<li><strong>DELETE<\/strong>: remove a resource from the server.<\/li>\n<\/ul>\n\n\n\n<p>In this tutorial, we&#8217;ll focus on using <strong>GET requests with the ESP32\/ESP8266<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"http-status-codes\">HTTP Status Codes<\/h3>\n\n\n\n<p>After receiving an HTTP request, the server responds with an HTTP status code indicating the outcome of the request. Status codes fall into categories such as 2xx (successful), 3xx (redirection), 4xx (client error), and 5xx (server error). Some of the most common:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>200 OK:<\/strong> the request was successful.<\/li>\n\n\n\n<li><strong>404 Not Found:<\/strong> the requested resource could not be found.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"esp32-esp8266-http-requests-micropython\">HTTP Requests with the ESP32 and ESP8266 NodeMCU (MicroPython)<\/h2>\n\n\n\n<p>The easiest way to make HTTP requests with the ESP32\/ESP8266 programmed with MicroPython is using the <span class=\"rnthl rntliteral\">requests<\/span> library, which uses a high-level approach. This is very similar to the <em>Python<\/em> <span class=\"rnthl rntliteral\">requests<\/span> library, but is more limited.<\/p>\n\n\n\n<p>You can also use the <span class=\"rnthl rntliteral\">socket<\/span> module, but it\u2019s a more low-level approach and is not so beginner-friendly.<\/p>\n\n\n\n<p>The following code is a basic example of how you can make a simple HTTP request to a URL using the <span class=\"rnthl rntliteral\">requests<\/span> library. This example makes a request to a Github page we created that simply contains the text &#8220;<span class=\"rnthl rntliteral\">RandomNerdTutorials.com<\/span>&#8220;<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\"># Rui Santos &amp; Sara Santos - Random Nerd Tutorials\r\n# Complete project details: https:\/\/RandomNerdTutorials.com\/micropython-http-get-requests-esp32-esp8266\/\r\n \r\nimport network\r\nimport requests\r\nimport time\r\n\r\n# Wi-Fi credentials\r\nssid = 'REPLACE_WITH_YOUR_SSID'\r\npassword = 'REPLACE_WITH_YOUR_PASSWORD'\r\n\r\n# Connect to network\r\ndef connect_wifi(ssid, password):\r\n    # Connect to your network\r\n    station = network.WLAN(network.STA_IF)\r\n    station.active(True)\r\n    station.connect(ssid, password)\r\n    \r\n    # Wait for connection\r\n    timeout = 10\r\n    while not station.isconnected() and timeout &gt; 0:\r\n        time.sleep(1)\r\n        timeout -= 1\r\n    \r\n    if station.isconnected():\r\n        print('Connection successful')\r\n        print(station.ifconfig())\r\n        return True\r\n    else:\r\n        print('Connection failed. Timeout reached')\r\n        return False\r\n\r\nif connect_wifi(ssid, password):    \r\n    # Make GET request\r\n    try:\r\n        response = requests.get(&quot;https:\/\/gist.githubusercontent.com\/RuiSantosdotme\/9e834367f02c198bc6474938e4e3bb0d\/raw\/de66b26a95fc2d0b7620a43b49e57607bd4348e3\/url.txt&quot;)\r\n        # Get response code\r\n        response_code = response.status_code\r\n        # Get response content\r\n        response_content = response.content\r\n        # Print results\r\n        print('Response code: ', response_code)\r\n        print('Response content:', response_content)\r\n    except Exception as e:\r\n        print('An error occurred during the request:', str(e))\r\nelse:\r\n    print('Failed to connect to WiFi')<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP-MicroPython\/esp_http_get_requests.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How the Code Works<\/h3>\n\n\n\n<p>Start by including the <span class=\"rnthl rntliteral\">network<\/span> and the <span class=\"rnthl rntliteral\">requests<\/span> library.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>import network\nimport requests<\/code><\/pre>\n\n\n\n<p>Replace &#8216;<span class=\"rnthl rntliteral\">REPLACE_WITH_YOUR_SSID<\/span>&#8216; and &#8216;<span class=\"rnthl rntliteral\">REPLACE_WITH_YOUR_PASSWORD<\/span>&#8216; with your Wi-Fi network name (SSID) and password.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>ssid = 'REPLACE_WITH_YOUR_SSID'\npassword = 'REPLACE_WITH_YOUR_PASSWORD'<\/code><\/pre>\n\n\n\n<p>The following function <span class=\"rnthl rntliteral\">connect_wifi()<\/span> connects to a network with the SSID and password you pass when you call the function.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code># Connect to network\ndef connect_wifi(ssid, password):\n    # Connect to your network\n    station = network.WLAN(network.STA_IF)\n    station.active(True)\n    station.connect(ssid, password)\n    \n    # Wait for connection\n    timeout = 10\n    while not station.isconnected() and timeout &gt; 0:\n        time.sleep(1)\n        timeout -= 1\n    \n    if station.isconnected():\n        print('Connection successful')\n        print(station.ifconfig())\n        return True\n    else:\n        print('Connection failed. Timeout reached')\n        return False<\/code><\/pre>\n\n\n\n<p>Check if we are connected to the internet before attempting to make the request:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>if connect_wifi(ssid, password):<\/code><\/pre>\n\n\n\n<p>If we have a successful internet connection, make the request. Using the <span class=\"rnthl rntliteral\">requests<\/span> module and the <span class=\"rnthl rntliteral\">get()<\/span> method, the code sends a GET request to the desired URL. The response from the server is stored in the <span class=\"rnthl rntliteral\">response<\/span> variable.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>response = requests.get(\"https:\/\/gist.githubusercontent.com\/RuiSantosdotme\/9e834367f02c198bc6474938e4e3bb0d\/raw\/de66b26a95fc2d0b7620a43b49e57607bd4348e3\/url.txt\")<\/code><\/pre>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">response<\/span> is an object of type <span class=\"rnthl rntliteral\">Response<\/span>. This object has several attributes that you can access that might be useful. For example:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">response.status_code<\/span>: returns an integer value representing the status of the response;<\/li>\n\n\n\n<li><span class=\"rnthl rntliteral\">response.content<\/span>: returns the actual content in bytes;<\/li>\n\n\n\n<li><span class=\"rnthl rntliteral\">response.text<\/span>: returns the content converted to a string, using a character encoding such as UTF-8;<\/li>\n\n\n\n<li><span class=\"rnthl rntliteral\">response.json()<\/span>: returns the response as a JSON object (dictionary);<\/li>\n\n\n\n<li><span class=\"rnthl rntliteral\">response.headers()<\/span>: access information about the response\u2019s headers.<\/li>\n<\/ul>\n\n\n\n<p>After getting the response, we get the status of the response using the <span class=\"rnthl rntliteral\">status_code<\/span> attribute.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>response_code = response.status_code<\/code><\/pre>\n\n\n\n<p>We save the content of the response in the <span class=\"rnthl rntliteral\">response_content<\/span> variable using the <span class=\"rnthl rntliteral\">content<\/span> attribute.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>response_content = response.content<\/code><\/pre>\n\n\n\n<p>Finally, we print the status code and request content to the Shell.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>print('Response code: ', response_code)\nprint('Response content:', response_content)<\/code><\/pre>\n\n\n\n<p>We use <span class=\"rnthl rntliteral\">try<\/span> and <span class=\"rnthl rntliteral\">except<\/span> statements in case there is an error during the request:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>except Exception as e:\n    print('An error occurred during the request:', str(e))<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Testing the Code<\/h3>\n\n\n\n<p>After inserting your network credentials, you can run the previous code on your ESP32 or ESP8266. Make sure Thonny IDE has a connection established with the board. Then, press the green RUN button.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"470\" height=\"114\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/12\/thonny-ide-run-button.png?resize=470%2C114&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Thonny IDE Green Run Button\" class=\"wp-image-144594\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/12\/thonny-ide-run-button.png?w=470&amp;quality=100&amp;strip=all&amp;ssl=1 470w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/12\/thonny-ide-run-button.png?resize=300%2C73&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 470px) 100vw, 470px\" \/><\/figure><\/div>\n\n\n<p>If everything goes as expected, the response code should be 200, and it will print the content of the GitHub page, which should be &#8220;<span class=\"rnthl rntliteral\">RandomNerdTutorials.com<\/span>&#8220;.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"634\" height=\"331\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/HTTTP-Request-ESP32-Result-Micropython.png?resize=634%2C331&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Result of HTTP GET Request with the ESP32 on Thonny IDE console\" class=\"wp-image-182449\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/HTTTP-Request-ESP32-Result-Micropython.png?w=634&amp;quality=100&amp;strip=all&amp;ssl=1 634w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/HTTTP-Request-ESP32-Result-Micropython.png?resize=300%2C157&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 634px) 100vw, 634px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"get-data-from-web\">Getting Data from the Web Using HTTP GET Requests<\/h2>\n\n\n\n<p>Now that you know how to make a basic HTTP GET request, we\u2019ll show you a different example to get useful data from the internet. We&#8217;ll request information about the current weather using the <a href=\"https:\/\/www.weatherapi.com\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Weather API<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">ESP32\/ESP8266 Weather Forecaster (HTTP GET Request)<\/h3>\n\n\n\n<p>In this example, we\u2019ll use the <a href=\"https:\/\/www.weatherapi.com\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Weather API <\/a>to create a weather forecaster. This API is free and provides useful information about the weather in almost any location in the world.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"350\" height=\"108\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/07\/weather-forecaster.jpg?resize=350%2C108&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Weather forecaster example\" class=\"wp-image-159901\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/07\/weather-forecaster.jpg?w=350&amp;quality=100&amp;strip=all&amp;ssl=1 350w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/07\/weather-forecaster.jpg?resize=300%2C93&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 350px) 100vw, 350px\" \/><\/figure><\/div>\n\n\n<p>We\u2019ll get the weather data for your location and display it on the shell.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Getting Your API Key<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Go to the Weather API website: <a href=\"https:\/\/www.weatherapi.com\/\" target=\"_blank\" rel=\"noopener\" title=\"\">weatherapi.com<\/a><\/li>\n\n\n\n<li>Signup to create an account or login if you already have one.<\/li>\n\n\n\n<li>After verifying your account, login into your account.<\/li>\n\n\n\n<li>On your dashboard at <a href=\"https:\/\/www.weatherapi.com\/my\/\" target=\"_blank\" rel=\"noopener\" title=\"\">weatherapi.com\/my\/<\/a> , you\u2019ll find your API key.<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"242\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/Weather-API-Key.png?resize=750%2C242&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Weather APi Key\" class=\"wp-image-182455\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/Weather-API-Key.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/Weather-API-Key.png?resize=300%2C97&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>Copy the API key to a safe place because you\u2019ll need it later.<\/p>\n\n\n\n<p>To pull information on the weather in your chosen location, enter the following URL in your web browser, but insert your location and API key in the right places:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">https:\/\/api.weatherapi.com\/v1\/current.json?q=<strong>YOUR_LOCATION<\/strong>+&amp;key=<strong>YOUR_API_KEY<\/strong><\/pre>\n\n\n\n<p>For example, in my case:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>https:&#47;&#47;api.weatherapi.com\/v1\/current.json?q=Oporto+&amp;key=d1578a064b07xxxc917164350240106<\/code><\/pre>\n\n\n\n<p>Copy your URL and paste it into your browser, and the API will return information corresponding to your local weather. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>{\n  \"location\": {\n    \"name\": \"Oporto\",\n    \"region\": \"Porto\",\n    \"country\": \"Portugal\",\n    \"lat\": 41.15,\n    \"lon\": -8.6167,\n    \"tz_id\": \"Europe\/Lisbon\",\n    \"localtime_epoch\": 1762168698,\n    \"localtime\": \"2025-11-03 11:18\"\n  },\n  \"current\": {\n    \"last_updated_epoch\": 1762168500,\n    \"last_updated\": \"2025-11-03 11:15\",\n    \"temp_c\": 16.1,\n    \"temp_f\": 61,\n    \"is_day\": 1,\n    \"condition\": {\n      \"text\": \"Partly Cloudy\",\n      \"icon\": \"\/\/cdn.weatherapi.com\/weather\/64x64\/day\/116.png\",\n      \"code\": 1003\n    },\n    \"wind_mph\": 4,\n    \"wind_kph\": 6.5,\n    \"wind_degree\": 142,\n    \"wind_dir\": \"SE\",\n    \"pressure_mb\": 1023,\n    \"pressure_in\": 30.21,\n    \"precip_mm\": 0,\n    \"precip_in\": 0,\n    \"humidity\": 68,\n    \"cloud\": 0,\n    \"feelslike_c\": 16.1,\n    \"feelslike_f\": 61,\n    \"windchill_c\": 14.8,\n    \"windchill_f\": 58.6,\n    \"heatindex_c\": 14.7,\n    \"heatindex_f\": 58.4,\n    \"dewpoint_c\": 7.5,\n    \"dewpoint_f\": 45.5,\n    \"vis_km\": 10,\n    \"vis_miles\": 6,\n    \"uv\": 2.3,\n    \"gust_mph\": 5.1,\n    \"gust_kph\": 8.2\n  }\n}<\/code><\/pre>\n\n\n\n<p class=\"rntbox rntclgray\">For more information about the API, check the docs: <a href=\"https:\/\/www.weatherapi.com\/docs\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Weather API Documentation<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Code \u2013 HTTP GET Request: Weather API (MicroPython)<\/h3>\n\n\n\n<p>The following code makes a request to the Weather API to get weather data on your location. Then, we display the data on the shell. We&#8217;ll display information about the current weather, temperature, humidity, and rain.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\"># Rui Santos &amp; Sara Santos - Random Nerd Tutorials\r\n# Complete project details: https:\/\/RandomNerdTutorials.com\/micropython-http-get-requests-esp32-esp8266\/\r\n\r\nimport network\r\nimport time\r\nimport requests\r\n\r\n# Wi-Fi credentials\r\nssid = 'REPLACE_WITH_YOUR_SSID'\r\npassword = 'REPLACE_WITH_YOUR_PASSWORD'\r\n\r\napi_key = 'REPLACE_WITH_YOUR_API_KEY'\r\nlocation ='REPLACE_WITH_YOUR_LOCATION'\r\n\r\n# Request URL\r\nurl = f'https:\/\/api.weatherapi.com\/v1\/current.json?q={location}+&amp;key={api_key}'\r\n\r\n# Connect to network\r\ndef connect_wifi(ssid, password):\r\n    # Connect to your network\r\n    station = network.WLAN(network.STA_IF)\r\n    station.active(True)\r\n    station.connect(ssid, password)\r\n    \r\n    # Wait for connection\r\n    timeout = 10\r\n    while not station.isconnected() and timeout &gt; 0:\r\n        time.sleep(1)\r\n        timeout -= 1\r\n    \r\n    if station.isconnected():\r\n        print('Connection successful')\r\n        print(station.ifconfig())\r\n        return True\r\n    else:\r\n        print('Connection failed - timeout reached')\r\n        return False\r\n\r\nif connect_wifi(ssid, password):\r\n    try:\r\n        # Make the request\r\n        response = requests.get(url)\r\n        #Print the response code\r\n        print('Response code: ', response.status_code)\r\n        \r\n        # Get response content\r\n        weather = response.json()\r\n        # Close the request\r\n        response.close()\r\n        \r\n        # Print bulk weather data\r\n        print('Weather JSON: ', weather)\r\n        \r\n        # Get specific weather data\r\n        weather_description = weather['current']['condition']['text']\r\n        print('Current weather: ', weather_description)\r\n        \r\n        # Temperature and humidity\r\n        temperature_c = weather['current']['temp_c']\r\n        temperature_f = weather['current']['temp_f']\r\n        humidity = weather['current']['humidity']\r\n        print(f'Temperature in Celsius: {temperature_c:.2f}')\r\n        print(f'Temperature in Fahrenheit: {temperature_f:.2f}')\r\n        print(f'Humidity (%): {humidity:.2f}')              \r\n        \r\n        # Precipitation\r\n        precipitation = weather['current']['precip_mm']\r\n        print(f'Precipitation in mm: {precipitation}')              \r\n        \r\n        # Wind\r\n        wind_speed = weather['current']['wind_kph']\r\n        print('Wind speed in kph:', wind_speed)\r\n\r\n    except Exception as e:\r\n        # Handle any exceptions during the request\r\n        print('Error during request:', e)<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP-MicroPython\/esp_http_get_weatherapi.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">How the Code Works<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">First, you need to insert your network credentials to connect your board to the internet.<\/h5>\n\n\n\n<pre class=\"wp-block-code language-python\"><code># Wi-Fi credentials\nssid = 'REPLACE_WITH_YOUR_SSID'\npassword = 'REPLACE_WITH_YOUR_PASSWORD'<\/code><\/pre>\n\n\n\n<p>Then, insert your Weather API key, and the city from where you want to get the data.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>api_key = 'REPLACE_WITH_YOUR_API_KEY'\nlocation = 'REPLACE_WITH_YOUR_CITY'<\/code><\/pre>\n\n\n\n<p>For example, in my case:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>api_key = 'd1578a064b07xxxc917164350240106'\nlocation ='Oporto'<\/code><\/pre>\n\n\n\n<p>We save the request URL in the <span class=\"rnthl rntliteral\">url<\/span> variable. This is an f string with placeholders so that the URL is updated with your details at run time.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>url = f'https:\/\/api.weatherapi.com\/v1\/current.json?q={location}+&amp;key={api_key}'<\/code><\/pre>\n\n\n\n<p>Then, we connect to the internet by calling the <span class=\"rnthl rntliteral\">connect_wifi()<\/span> function (created before) and passing the SSID and password as arguments:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>if connect_wifi(ssid, password):<\/code><\/pre>\n\n\n\n<p>If we are successfully connected to the internet, we&#8217;ll make the request.<\/p>\n\n\n\n<p>As we&#8217;ve seen in the previous example, making the request is as simple as using the <span class=\"rnthl rntliteral\">get()<\/span> method of the <span class=\"rnthl rntliteral\">requests<\/span> module and passing the URL as an argument.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>response = requests.get(url)<\/code><\/pre>\n\n\n\n<p>This will return a JSON object. So, we can convert the result into a Python dictionary using the <span class=\"rnthl rntliteral\">json()<\/span> method.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>weather = response.json()<\/code><\/pre>\n\n\n\n<p>Then, we close the connection to release any resources.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code># Close the request\nresponse.close()<\/code><\/pre>\n\n\n\n<p>Now, we have all the data you\u2019ve seen on your web browser in the previous steps saved in the <span class=\"rnthl rntliteral\">weather<\/span> variable.<\/p>\n\n\n\n<p>You access specific information by using the keys of the data you want to get. We get weather description, temperature, humidity, precipitation, and wind speed. You can also get other properties by accessing the corresponding keys on the JSON object.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code># Get specific weather data\nweather_description = weather&#091;'current']&#091;'condition']&#091;'text']\nprint('Current weather: ', weather_description)\n        \n# Temperature and humidity\ntemperature_c = weather&#091;'current']&#091;'temp_c']\ntemperature_f = weather&#091;'current']&#091;'temp_f']\nhumidity = weather&#091;'current']&#091;'humidity']\nprint(f'Temperature in Celsius: {temperature_c:.2f}')\nprint(f'Temperature in Fahrenheit: {temperature_f:.2f}')\nprint(f'Humidity (%): {humidity:.2f}')              \n        \n# Precipitation\nprecipitation = weather&#091;'current']&#091;'precip_mm']\nprint(f'Precipitation in mm: {precipitation}')              \n        \n# Wind\nwind_speed = weather&#091;'current']&#091;'wind_kph']\nprint('Wind speed in kph:', wind_speed)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Testing the Code<\/h3>\n\n\n\n<p>After inserting your network credentials, API key, and city, you can run the code on your ESP32 or ESP8266 board.<\/p>\n\n\n\n<p>You should get something as shown in the screenshot below, but with the information for your chosen location.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"615\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/ESP32-Get-Weather-Data-HTTP-Get-Request.png?resize=750%2C615&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 Get Weather Data from Weather API - MicroPython Thonny IDE\" class=\"wp-image-182457\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/ESP32-Get-Weather-Data-HTTP-Get-Request.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/ESP32-Get-Weather-Data-HTTP-Get-Request.png?resize=300%2C246&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>And that&#8217;s it. You successfully requested data from the Weather API.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>In this tutorial, you learned about HTTP GET requests with the ESP32 and ESP8266 programmed with MicroPython. You learned why they are useful for IoT projects and learned how to get data from the internet using HTTP GET requests.<\/p>\n\n\n\n<p>As an example, we&#8217;ve shown you how to get weather data. You can take this project further by displaying the data on a display, like an LCD, OLED, or TFT display. You can create a while loop or a <a href=\"https:\/\/randomnerdtutorials.com\/micropython-timer-interrupts-ep32-esp8266\/\" title=\"\">timer interrupt<\/a> to request data periodically, or you can even request data for multiple locations just by changing the value of the <span class=\"rnthl rntliteral\">location<\/span> variable.<\/p>\n\n\n\n<p>We have some tutorials that you can find useful:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/micropython-oled-display-esp32-esp8266\/\">MicroPython: OLED Display with ESP32 and ESP8266<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/micropython-i2c-lcd-esp32-esp8266\/\">MicroPython: I2C LCD Display with ESP32\/ESP8266<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/micropython-cheap-yellow-display-board-cyd-esp32-2432s028r\/\">MicroPython: ESP32 Cheap Yellow Display Board \u2013 CYD (ESP32-2432S028R)<\/a><\/li>\n<\/ul>\n\n\n\n<p>If you want to learn more about programming the ESP32 and ESP8266 boards using MicroPython, check out our resources: <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/micropython-programming-with-esp32-and-esp8266\/\">MicroPython Programming with ESP32 and ESP8266 eBook<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-esp32-esp8266-micropython\/\" title=\"\">All our MicroPython Projects and Tutorials<\/a><\/li>\n<\/ul>\n\n\n\n<p>Thanks for reading.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to make HTTP GET Requests with the ESP32 or ESP8266 boards programmed with MicroPython. We&#8217;ll first cover the basics of HTTP GET requests, and then, we&#8217;ll create and &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"MicroPython: HTTP GET Requests with ESP32\/ESP8266\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/micropython-http-get-requests-esp32-esp8266\/#more-182443\" aria-label=\"Read more about MicroPython: HTTP GET Requests with ESP32\/ESP8266\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":182476,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[310,309,264],"tags":[],"class_list":["post-182443","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-micropython","category-0-esp32-micropython","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/ESP32-ESP8266-HTTP-GET-Requests.jpg?fit=1920%2C1080&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/182443","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=182443"}],"version-history":[{"count":16,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/182443\/revisions"}],"predecessor-version":[{"id":185784,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/182443\/revisions\/185784"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/182476"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=182443"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=182443"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=182443"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}