Skip to content

AT Command firmware based Client Server

Bose Ji edited this page Oct 1, 2015 · 12 revisions

1.0 Init

First use the method Bootloader section to load the AT-Firmware as detailed. This would enable the ESP8266 module to be able to interact using the AT-Firmware.

In this section we would be using 3 different types of methods to interact with the ESP8266 module having the AT-Firmware.


1.1 Notes for HW-group Hercules SETUP utility

After copying the HW-group Hercules SETUP utility to direction on first run it would request for Permissions to connect to Network make sure to give permission to access both Public Networks and Private Networks (closed group). This would help it to be used for diagnostics.


1.2 Notes for Python

While installation of Python on Windows one needs to turn on the check-box that specifies python directory needs to be added to PATH this would help in making pip to work right off the command line.

After the Installation is done its time to install the libraries type the following command:

pip install pyserial

This would install the package pyserial from the internet.


1.3 Notes for NetCat on Windows

Make sure the correct version of the netcat is installed as per the Windows version (64-bit or 32-bit).

Also make sure that the nc.exe file is accessible by PATH so that the command line can be used to call this utility.


2.0 Lets Begin - Serial Config

Connect you module to the PC using the serial connection as detailed in the Bootloader sections.

Open the Serial port terminal of your choice with the following configuration

  • BAUD: 115200

  • Data Size: 8 bit

  • Parity: None

  • Handshake: Off

  • Line End: CR+LF (For AT-Commands)

We prefer to use the HW-group Hercules SETUP utility on the Windows 7 target where we created this tutorial.

Here is the configuration picture For serial settings: and right-clicked config):

Hercules Configuration1

Here is the configuration picture For right-clicked End of line config:

Hercules Configuration2

Now Open the Serial Port with the above configuration.

2.1 ESP8266 module as Server (PC as Client)

In normal cases using a SoftAP on the ESP8266 module is better but we are going to Join the ESP8266 module into our wireless network and then perform this experiment.

2.1.1 ESP8266 module configuration as Server

First we need to make sure that the module is in the initialized state.

AT+RST

This would reset the module and wait till the ready prompt appears.

Next we need to join the Wireless network so give the command:

AT+CWMODE=1

OK
AT+CWJAP="SSID","password"
WIFI CONNECTED
WIFI GOT IP

The first one would configure the ESP8266 module as an Station which means it can connect to the Wireless network but will not host a SoftAP of its own.

The second command helps to connect the wireless network "SSID" with the password as "password" (of course without quotes). The last two status lines WIFI CONNECTED and WIFI GOT IP are only shown if connection attempt to the wireless network was successful.

Next we need to configure the number of connections supported:

AT+CIPMUX=1

This would help us to host multiple clients on the same ESP8266 module the maximum number being 4 as of now.

Finally lets know our IP address of the ESP8266 module that it obtained from the wireless network:

AT+CIFSR
+CIFSR:STAIP,"192.168.1.4"
+CIFSR:STAMAC,"18:fe:34:f5:12:23"

OK

The first response STATIP is the IP address of the ESP8266 module and second response STAMAC is the Station MAC Address. The IP address would be handy for connecting to the ESP8266 module from PC.

Next step is to host the Server we would use the port 1001 for ESP8266 module

AT+CIPSERVER=1,1001

OK

The command contains two parts : first being the Flag 1 to Start Server and second 1001 is the port number on which the server would listen. Once this is done we are ready to begin our next steps on the PC.

2.1.2 PC Side configuration using HW-group Hercules SETUP utility

Hercules Client

Configure the IP address and Port properly as stated in the earlier section 2.1.1.

On pressing the Connect button it would make the Connection show the message

Connecting to 192.168.1.4 ...
Connected to 192.168.1.4

And one the ESP8266 module Serial port side it would show

0,CONNECT

This means that one node is connected to the ESP8266 module

We will keep this session open for later use. Next lets do the same using NetCat.

2.1.3 PC Side configuration using NetCat on Linux

Give this command to start the connection with terminal attached:

nc 192.168.1.4 1001

In this command there are two parts IP Address for the ESP8266 module and the Port opened on hosted Server

And one the ESP8266 module Serial port side it would show

1,CONNECT

This means that two nodes are connected to the ESP8266 module keeping in mind we also have a Windows machine connected to the ESP8266 module from the previous section 2.1.2.

2.1.4 Using Python as Client

For python we can directly do this via the python console to do the most:

    >>> import socket

Instantiate the socket package.

Next lets create a socket instance:

    >>> sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

This would create the object for the socket type for communication

Finally to start the link:

    >>> sock.connect(('192.168.1.4',1001))

In order to prepare the connection a tuple containing (<Ip address>,<port>) is prepared to initiate the client side connection.

And on the ESP8266 module Serial port we get:

2,CONNECT

This means that three nodes are connected to the ESP8266 module keeping in mind we also have a Windows machine & linux machine with NetCat connected to the ESP8266 module from the previous section 2.1.2 and section 2.1.3 .

Its time now to send some data to the Clients one by one of course - as broadcast is not possible in ESP8266 module.

2.1.4 Send data from ESP8266 module to its Clients

In order to send data we will have an invisible command this time:

AT+CIPSEND=0,5

OK
> 
Recv 5 bytes

SEND OK

In this command there are 3 parts.

First would be the command spec AT+CIPSEND=<Connection ID>,<Bytes>, in this case the Connection ID:0 which is our HW-group Hercules SETUP utility.

Next part comes after > prompt where we type the words Hello containing 5 bytes and the same is received on the other side on the Hercules Terminal as

Hello

This was our first transmission.

Next lets try the same to the Client ID:2 which is the NetCat tool on Linux

AT+CIPSEND=1,5

OK
> 
Recv 5 bytes

busy s...

SEND OK

Some times you might see the busy s... message also which means that the ESP8266 module was busy in sending the data and you pressed enter key or tried to command the ESP8266 module or pressed additional keys.

And we Receive on NetCat prompt:

Hello

Finally its time to check for Python.

In Python we need to specify the number of bytes to be received and we know that already 5 bytes

    >>> data = sock.recv(5)

Next on the ESP8266 module side we do:

AT+CIPSEND=2,5

OK
> 
Recv 5 bytes

SEND OK

Finally we would need to see what data received on Python side:

    >>> data
    'Hello'
    >>> sock.close()

The last line in the python console closes the Socket as there is no more data to be received.

This concludes the demonstration of the different clients connecting to ESP8266 module.


2.1.5 Note ESP8266 module Server Disconnect Problem

One might notice that the Client side connections might get closed. You get to see the message on ESP8266 module serial port as 0,CLOSED. This means the client has either stopped the connection or timed out. The default time out would be 180 seconds. This can be changed using the command AT+CIPSTO=<Time> where <Time> is TCP server timeout in range 0~7200 seconds.


2.2 ESP8266 module as Client (PC as Server)

Interestingly switching from client to Server is very easy. In fact this can be done at tandem given that all the TCP connections are closed. One easy way around is to reset the ESP8266 module using the AT+RST command.


2.2.1 Notes on PC finding IP address

Before we begin to make the PC as the server we would need to determine the IP address assigned to the PC. To do this there are several ways, but differ based on OS.

In a Linux OS the easy way would be to enter the following command in shell:

ifconfig -a | more

This command would list all the available network adapters. Typically the Ethernet port is denoted by device eth0 and the WiFi adapter as wlan0. Search for these in the output with up and down keys. Under the correct device look for inet addr:192.168.1.10 this indicates the address assigned to the particular network adapter.

In Windows OS open a command prompt and type the command:

ipconfig /all

The network adapters are generally indicated as Wireless LAN adapter Wireless Network Connection: or Ethernet adapter Local Area Connection:. These are the common types but depending on your configuration the name might differ, however in most cases its the same.

Look for IPv4 Address. . . . . . . . . . . : 192.168.1.11 this gives the IP address for the given network adapter in the PC.


2.2.2 ESP8266 Module Configuration as Client

There are two ways to proceed on this.

  • First would be if the ESP8266 module is already configured as Server as in the previous sections then it would easy. Just shut down the Server function of ESP8266 module:

AT+CIPSERVER=0


* Second would be restart from the beginning:
```shell
AT+RST
...
....
ready
WIFI CONNECTED
WIFI GOT IP

One might wonder why we got the WIFI CONNECTED and WIFI GOT IP message after the reset. Actually if you have previously configured the ESP8266 module using the AT+CWJAP command then that configuration would remain. And, hence after reset the module automatically connects back to the wireless network. Its always better to configure the WiFi network again just in case some problem occurs.

  AT+CWMODE=1
  
  OK
  AT+CWJAP="SSID","password"
  WIFI CONNECTED
  WIFI GOT IP

Now we are ready to go to the next step which is important for ESP8266 module to be client.

AT+CIPMUX=0

This makes sure that we do not have Multiple connection feature disabled. Lets now look at the PC Side configuration.

2.2.3 PC side Server Configuration using HW-group Hercules SETUP utility

We will setup port 123 for fun to be our server port number on the PC side.

Hercules Server Config

Click on the Listen button to begin the server on the PC. Also make sure as per the Note 1.1 the ports are opened in Firewall under Windows.

2.2.4 PC side Server Configuration using NetCat on Linux

We would like to open a separate port 222 for the Linux PC:

nc -l -p 222

This would open a command line port listing for connections.

2.2.5 PC side Server Configuration using Python

We would open another port 333 using the Python package socket:

    >>> import socket
    >>> sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    >>> sock.bind(('',333))
    >>> sock.listen(10)

These commands would first create the Socket sock. Then bind ('',333) the port 333 on the PC local host with the Socket object. Next command begins to listen with capacity to have connection request from 10 clients. In order to begin the connection we would need the accept command to actually connect to the ESP8266 module.

2.2.6 Data Transaction from ESP8266 module to HW-group Hercules SETUP utility

We will use the earlier obtained IP address of PC to establish connection:

AT+CIPSTART="TCP","192.168.1.10",123
CONNECT

OK

This command would establish a TCP connection to the PC with IP address 192.168.1.10 and port 123 which we know is the HW-group Hercules SETUP utility we configured in section 2.2.3.

On the HW-group Hercules SETUP utility window the first update in the Client Info Window showing:

10:15:03 192.168.1.4 Client Connected

This means that the ESP8266 module has connected to the Hercules Terminal. ✨

Now lets try to send some data from the Hercules Terminal. To do so we need to write in Transmit Prompt and Press Send button. We are sending some short message ESP8266 is awesome, lets see on the ESP8266 module serial port it should show:

+IPD,18:ESP8266 is awesome

This means we are able to receive the data from the Server Hercules Terminal. In this message +IPD,18 has the length 18 for the string received ESP8266 is awesome. This can be useful for parsing data. Also an important thing the length is not null-terminated so its the exact length on the string after : character. Make sure to take care of this while implementing the parser for data.

Lets now send some data from ESP8266 module to the Hercules Terminal.

Here are the commands:

AT+CIPSEND=5

OK
> 
Recv 5 bytes

SEND OK

On the Hercules Terminal it shows Great in Receive Window which is 5 bytes we typed after the > prompt in ESP8266 module.

Notice the change in the command AT+CIPSEND=<Bytes> this is due to the fact that we will have only one connection from ESP8266 module connecting outwards.

In order to close the Connection use the command

AT+CIPCLOSE
CLOSED

OK

This would close the TCP connection with the Hercules Terminal, as we can initiate only one connection from ESP8266 module client.

2.2.7 Data Transaction from ESP8266 module to NetCat on Linux

We know that NetCat terminal interface is only one way so we would send out the data from ESP8266 Module

AT+CIPSTART="TCP","192.168.1.11",222
CONNECT

OK
AT+CIPSEND=5

OK
> 
Recv 5 bytes

SEND OK

First command Initiates the connection with Linux machine IP Address 192.168.1.11 at port 222, where NetCat program was configured in Section 2.2.4.

Next we send out 5 bytes to the NetCat Server from ESP8266 module. We receive Great on the NetCat Linux shell.

Finally we also need to disconnect NetCat server and use the command

AT+CIPCLOSE
CLOSED

OK

This would cause the NetCat server terminal to exit.

2.2.8 Data Transaction from ESP8266 module to Python

In python there is blocking call to make the connection we would need to call this and wait for the system to return back. This call is best made using threads but this can be done in a Python terminal also.

    >>> consock,ipaddr = sock.accept()

This will make the connection active. Make sure to do this before initiating connection from the ESP8266 Module as the server needs to accept the connection before communication can work out.

Now on the ESP8266 Module serial port we give the commands to start the connection:

AT+CIPSTART="TCP","192.168.1.10",333
CONNECT

OK

Here the port is 333 as configured earlier in Section 2.2.5.

Now on the Python Terminal the accept command would return. Lets now look at the IP address that we have received. If we remember our last IP address for ESP8266 Module was 192.168.1.4 this should be in place:

    >>> ipaddr
    ('192.168.1.4',56258)

As expected the connection is from the ESP8266 Module, though I am not sure why the socket number is so different.

Another important point here is that the Socket object returned consock only can be used to communicate with the ESP8266 Module.

Lets now try to send data from the ESP8266 Module (We would send Great again):

AT+CIPSEND=5

OK
> 
Recv 5 bytes

SEND OK

Well on the Python we do have any thing yet, ya we need to read the output:

    >>> consock.recv(100)
    'Great'

Here in this command we are reading out all data from the Socket consock with the maximum number of bytes being 100. Now this is fixed limit hence one needs to execute this again and again to be able to receive a big message. Since we sent only 5 bytes from the ESP8266 Module side its easy to receive.

Next Lets send out data from Python terminal side.

    >>> consock.send('Hi ESP8266')
    10

This command sends out 10 bytes of data to ESP8266 Module, here is what we receive:

+IPD,10:Hi ESP8266

There are 10 bytes received on the ESP8266 Module also.

Now to close the Connection in Python we issue the close command. And important point here might be if one has multiple connections in context then checking the fileno of the socket helps to determine if its closed.

    >>> consock.fileno()
    385L
    >>> consock.close()

On the ESP8266 Module side we get:

CLOSED

This means the connection from the server side is closed. In case we need to close the connection from the ESP8266 Module then we need to use the command AT+CIPCLOSE without the Connection ID. This would give freedom to the implementation on the ESP8266 module side to disconnect if needed.



The procedures provided above have been tested with the AT Command Firmware version 0.50.0 using a PC connected with USB-TTL-serial (FTDI cable).


Clone this wiki locally