ESP8266 Webserver: Receiving GET requests from Python

The objective of this post is to explain how to make a Python application talk with a ESP8266 by sending HTTP GET Requests to a HTTP web server running on the microcontroller.


Introduction

The objective of this post is to explain how to make a Python application talk with a ESP8266 by sending HTTP GET Requests to a HTTP web server running on the microcontroller.

In this case, we will set a simple HTTP webserver on the ESP8266 that will receive HTTP requests on a certain URL. You can check in detail how to do it in this previous post.

In this case, instead of using a web browser to access the URL defined in the ESP8266, we will create a very simple Python application that will send a HTTP GET request to the web server. You can check here a more detailed walk-through on how to perform GET requests in a Python program.

As mention in that post, we will need to have the Request library installed in Python. Check the installation page here.

 

The ESP8266 code

For the ESP8266, the code will be pretty much the same we’ve been covering in the previous posts, so I will not do a very detailed explanation.

First, we need to include 2 libraries, one to access the functions needed to connect to a WiFi network, and the other to access the functions to setup the web server.

We then declare a global object variable of the class ESP8266WebServer, which has the methods we need to setup the HTTP server. Note that the argument of the constructor corresponds to the port where the server will be listening to incoming HTTP requests (in this case, we have chosen port 80, the default HTTP port).

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>

ESP8266WebServer server(80);

Then we do most of the coding in the setup function. Here, we will connect to the WiFi network and initialize our web server.

It’s in the setup function that we will bind a specific handler function to a certain URL path where the ESP8266 will be listening to. If a HTTP request is received on that path, then the handler function (which we will call “handlePath”) will be executed. In this case, we will be listening on the “/Python” path, as seen below.

server.on(“/Python”, handlePath);  

Both the complete setup and the main loop functions are shown bellow. The previous line of code is included in the setup function.

void setup() {

Serial.begin(115200);
WiFi.begin(“NetworkName”, “Password”);  //Connect to the WiFi network

while (WiFi.status() != WL_CONNECTED) {  //Wait for connection

delay(500);
Serial.println(“Waiting to connect…”);

}

Serial.print(“IP address: “);
Serial.println(WiFi.localIP());                         //Print the local IP of the webserver

server.on(“/Python”, handlePath);              //Associate the handler function to the path
server.begin();                                                   //Start the server
Serial.println(“Server listening”);

}

void loop() {

server.handleClient(); //Handling of incoming requests

}

We finally declare the handler function that will execute when the HTTP request is received on the “/Python” path. In this case, we will just reply to the HTTP request with the HTTP response code 200 (“OK”), in the “text/plain” format and with a simple “Hello Python” message.

void handlePath() { //Handler for the path

server.send(200, “text/plain”, “Hello Python”);

}

As seen, the code is pretty simple and the libraries we used hide the low level details from us.

Important: When directly copying the code from the blog, a stray error may occur when trying to compile it on Arduino. In my case, this occurs because the editor assumes the wrong type of quotes. The easiest way to fix this, given the number of existing quotes, is to do a find and replace and put the correct quotes.


The Python code

The Python code needed to perform an HTTP request to the ESP8266 web server is even simpler and only needs 3 lines.

First of all, we import the Requests library.

import requests

Now, we call the get method defined in this requests module and pass the URL where we want to make the request as argument. Remember that our URL will be in the format http://IP/path. In our case, the IP will be the value printed on the serial console when we run the code on the ESP8266 and the path will be “/Python”.

We will store the result of the invocation of this method in a variable called response. Check the code bellow. Take in consideration that the IP that I’m using is the local IP of the ESP8266 on my WiFi network. Yours will most likely be different.

response = requests.get(‘http://192.168.1.103/Python’)

Finally, we use the text method on the response object to get the answer from the web server

print response.text

Important: In order for WordPress to not escape the single quote in the end of URLs, I used a special character single quote. When copying and running the code, we need to change the single quote at the end of the URL to a regular one (just delete this and put a regular one). Otherwise, an error will occur and the code will not execute.


Final result

After uploading the ESP8266 code and running the Python script, we should get the “Hello Python” message defined. Figure 1 shows the output of the Python script in IDLE’s console.

esp8266-python-http-get-request

Figure 1 – Output of the Python script in IDLE.

In order to detect problems in an early stage, I recommend a first test on a web browser. So, after loading the code to the ESP8266, try to access the URL in Chrome or Firefox. If you don’t receive the “Hello Python” message, then re-check your code for errors.

If you get the defined message, them move forward to the execution of the Python script.

Important: The computer that is running the Python script to perform the HTTP request must be in the same WiFi network where the ESP8266 is. Otherwise, it won’t be able to send the HTTP request. In order for a client (in this case, the Python program) to be able to contact the ESP8266 from outside its network, it would need to use the external IP of the network and the router would need to be configured to forward the requests on its public IP for that port to the private IP of the ESP8266. Since port forwarding depends on the router used, explaining how to do it is outside of the scope of this post.


Related posts

 

Technical details

  • ESP8266 libraries: v2.3.0
  • Python version:2.7.8
  • Requests library: 2.11.1
Advertisements
This entry was posted in ESP8266 and tagged , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s