ESP8266 — Python Connection in Arduino Based System

Ali Camdal
Analytics Vidhya
Published in
5 min readApr 7, 2020

In my last article I did explain how to connect and setup AT Commands ESP8266 with Arduino UNO.

I am giving an another Fritzing scheme for wiring ESP8266 to Arduino. We will setup our WiFi module with automatically, that's why we are going to change wiring style.

We will add 3 resistors for new scheme. We are going to do this because we want to stabilize our power system and RX receiver. These resistors can be 1kΩ(they are 220Ω in the scheme cause I couldn't find 1ks). Also, we changed our RX and TX pins from 0–1 digital pins to 2–3 digital pins. This changing allows us to connect with Python. All sketches and Python script can be found HERE.

In the beginning we will import our library, which is SoftwareSerial, to our sketch. Then we have to create an instance from this library. It is espSerial which can be seen above. Then we need our WiFi's SSID and PASSWORD that used by ESP8266 for connecting to WiFi. Also, we have 2 different string variables. Then we should start our serial communications in 9600 baud rate. Here is our point, we are seeing that there is a function called "espSetup". This function set our ESP8266 automatically when we give power to our system.

We will complete our setup in 7 steps. Very first step is resetting ESP8266. And then we will test the connection with "AT" command in step 2.

First 2 steps of setup process.

As we can see on screenshot there is a variable called "counter". With this variable we will be able to send our commands many times and we will sure about them. Also, we have another function called "getResponse".

Moreover, we will send our commands many times with delays. For "AT" command, we are going to send it until counter reach 10. But the condition of counter is finding 10 successful "AT" command response which is "OK". We can find what we seek on espSerial communication by "espSerial.find" function.

When we send a command with espSerial there should be a response for this command. With "getResponse" function we will see these responses.

Step 3 and Step 4

At the next 2 steps. We will set station mode and WiFi connection. There is just one important point in there. We should change SSID and PASSWORD definition at the beginning.

Step 5 and Step 6

In step 5 and step 6 we will find our device's IP address and enable multiple connection setting. With multiple connection setting we can reach our device by Python.

Step 7

Last but not least. In step 7 we set our device's server setting and its port. With the end of this step we completed setup. If you get an error while you are doing these steps, feel free to leave comments about it. Otherwise, congrats you have a function that can setup ESP8266 automatically.

Now we need to get messages through our system and Python. For this purpose, we have to prepare loop function of Arduino.

loop Function

As we know that this function works forever. So, we need to define a variable for our messages. We have "msg" for them. At the beginning we defined "fmsg" as a global variable and we get our raw messages with it. Then we are going to "getString" function. I created this function because if you print raw messages to serial monitor, you will see that there are some junk data on your serial monitor with ":" separator . We have to get rid of them for the sake of messages.

getString Function

"getString" function takes 1 argument which is string "flmsg" and this argument is our raw data. As I said before there is a separator in our raw data. With the for loop we will find our separator and then we will get our real messages. I know that there are some built-in functions for this process. But in these days I need to spend some times.

This is the last function of our Arduino side. Now we can prepare Python script.

Python Connection Script

I said "prepare" but IRL it is not exactly preparing. It is just 11 rows. First of all we need "socket" library. This module provides an interface for Berkeley Socket API. Now we need to find our IP and port. If you didn't change anything on setup, you can connect with shown IP and port settings. But if you changed, you can find them with "AT+CIFSR" command.

In conn variable we can give many other parameters for "socket" function. But AF_INET address family need just pair of IP and port. That is enough for us.

Then we triggered the "connect" function with our tuple which include IP and port. If everything went well, you will see "Send something :" at your Python IDLE screen. You can send any data to ESP8266 except single character of "q" and "Q". These are our quit characters.

Furthermore, I prepared an example of this application and it is at my GitHub. You can find it under Python_Led_Connection folder. In this example if you type "ON", Arduino's built-in led will be lighten up.

Everything Works Well

Ciao.

--

--