Introduction: ESP8266 and Python Communication For Noobs
This guide allows you to get any data from ESP8266 and control it over python without AT commands.
For the beginners, most of the guides about using ESP8266 is hard, because they want you to flash “AT COMMANDS” into chip, which is:
- Unneccessary
- Wasting memory of ESP
- Gives you limited control
- Hard and Challenging
- And not suitable for all ESP8266 modules
That's why I created a very simple mDNS communication system which is being controlled only with 3 simple functions. It also gives you full control.
Step 1: Theory
Our esp connects to our wifi and creates a localhost server and starts to waiting a request. Everytime our python sends a request to that localhost, esp runs the desired code and then returns the result as an http request. Finally python reads that returned data as http request and grab that variables from it. With this, esp can return strings, datas and arrays. Python code will understand their datatype.
Step 2: Preparing the Required Libraries
First of all, you must be downloaded the ESP8266 card library to arduino ide. If you don't know how, here is the guide.
After that, you have to download my micro library from here.
After you downloaded, in library folder there is a file called "ESP_MICRO.h", copy it to your coding folder of current arduino project. Yes, don't copy it to arduino's libraries, it's a micro library so you will copy it to folder of the your current arduino project.
So now, our requirements are satisfied. We can start to coding it.
Step 3: Writing a Simple Request Code
When you open your project.ino, you will see two tabs on arduino ide. One is your project, the other is "ESP_MICRO.h" our micro library.
Now you have that 5 function in ESP_MICRO.h in your main code, (the functions are explained in first lines of ESP_MICRO.h)
Here is a simple variable increasing code.
Arduino code:
/* F5 TEST FOR ESP2PY * Written by Junicchi * https://github.com/KebabLord/esp_to_python * It simply increases and returns a variable everytime a python req came */ #include "ESP_MICRO.h" //Include the micro library int testvariable = 0; void setup(){ Serial.begin(9600); // Starting serial port for seeing details start("USERNAME","PASSWORD"); // EnAIt will connect to your wifi with given details } void loop(){ waitUntilNewReq(); //Waits until a new request from python come /* increases index when a new request came*/ testvariable += 1; returnThisInt(testvariable); //Returns the data to python }
Step 4: Uploading
Programming Nodemcu ESP8266s are simply plugging usb and uploading the sketch from arduino.
But programming ESP8266-1 are harder, there is two methods to program them
Programming ESP through arduino
If you're fine with jumpers, you can program it through arduino with this circuit. But for long term, it's pain. So I suggest other method.
Programming it with ESP programmer
It's much easier and faster. It's only 1 dollar, buy one and use a programmer usb.
Learning the IP adress of ESP
While code is being uploaded, open the serial port, you'll see details are printed when uploading is done. Learn the IP of esp and note that. Remember, ESP's IP on local; changes by wifi to wifi, not session to session, so when you close and open it later, it will not be changed.
Step 5: Reading and Python
In the esp_to_python/library there is a "EXAMPLE_PYTHON_READER.py"
edit it, change the 5th line with the IP adress of the esp module that printed on serial port
and run the python script. In this project, I used python to send and read request. But you can also view the raw data with a browser while pasting the ip of ESP on a browser. Or you can make an application to read it, or you can even use another language. Controlling the module over python is also explained in "ledControl" project in examples folder.
Step 6: Finalising
All functions and codes are explained in the ESP_MICRO.h and in the README.md file.
If this project helped you, you can star the original project on github.

Participated in the
Arduino Contest 2019
5 Comments
Question 1 year ago
i have an error urllib.error.URLError:
Question 1 year ago
Hi
I am new to python and I have written an API using FLASK framework for getting user input from HTML forms, for that I am using HTTP GET and POST method. I need to send that received form value to ESP8266 I am using the same mode of transmission that you are using (i.e.) Through localhost. The connection between Server(API) and Client(ESP8266) was established but whenever i enter the integer value in form and hit submit button entire HTML code of that form is getting displayed in Arduino serial monitor. The input values is not getting displayed . I don't know how to solve it. I might be doing some minor mistakes in proving root url. Can you help me in solving it???. But the value i am giving inside the form is getting displayed in Python's Output terminal perfectly. The problem is with sending that value to ESP8266.
4 years ago
Does this also work with an ESP32 Node MCU?
How would you modify the code to have 1 PC/Laptop with Python talking to multiple ESPs?
What about 1 ESP with multiple Python PCs?
Can this be used with (almost) any number of Python PCs and ESPs?
Reply 3 years ago
It works with all ESP modules since the method is universal. Different ESPs would use different and independent machine ips so there would be no conflict. And python can listen more than one mDNS server. And since esp releases the data to localhost mDNS server, any device that connected to the same network could reach it. Which means there can be unlimited PCs or ESPs used.
4 years ago
Thanks for sharing your tips!