Getting started with ESP8266 Wi-Fi Module

In the last few months I have done several IoT based projects very efficiently and cost effectively using ESP8266 Wi-Fi module. It can be used as an independent microcontroller with built-in Wi-Fi and two GPIO pins or could be used with another microcontroller through serial communication to give a Wi-Fi connectivity to the microcontroller. It could be used to make IoT sensors network to report sensor data to the internet or the internet connected dashboards, it could be used to make a home automation device that is connected to the internet or local network. ESP8266 could be used to develop an IoT based security system, smart plugs and lights, mesh networks or wearable devices. Because of its low cost, low power consumption and small size it could be used to develop any kind of IoT device.

Shortly About Architecture and Features

ESP8266 Wi-Fi module has 32-bit RISC microprocessor clocked at 80Mhz and can be overclocked to 160Mhz. It has 32 KiB Instruction RAM, 32 KiB instruction cache RAM, 80 KiB user data RAM and all over that it has GPIO, 12C, ADC, SPI, and PWM

Power Consumption

The maximum voltage and current required to operate ESP8266 Wi-Fi module is 3.6V and 120.5mA, the Arduino has 3.3V output pin but its output current is just 40mA that is not enough to run the esp8266, so voltage regulator LM317 is used to regulate Arduino’s 5V to 3.3V to make it run properly as LM317 maximum output current is 1.5A. ESP8266 I/O pins also run at 3.3V, so logic level shifter 3.3V zener diode is used to convert 5V logic coming from Arduino TX pin to 3.3V, but according to my experience there is not much need for it. It is all fine to simply make the circuit given in figure below

ESP8266 Pinouts

                                       

Connecting With Arduino

          Components

Schematic

As ESP8266 Wi-Fi module communicate with Arduino or any other microcontroller using serial communication and it has required 3.3V minimum to run. Arduino’s 5V output will be connected to LM317 input as shown in figure below

                      

ESP8266 Connections

ESP8266 Connections
RXD Arduino’s I/O Pin 3
VCC LM317 Output
CH_PD LM317 Output
GND Arduino’s GND
TXD Arduino’s I/O Pin 2

 

         

I have made an ESP8266 Wi-Fi module flasher or burner based on same circuit above, as shown below. It will be very useful to have such flasher to test and debug circuit and code. Using it will surely be useful in making any standalone ESP8266 Wi-Fi module based project, as through this circuit any firmware or code can be flashed or uploaded in to ESP8266 ROM using Arduino or USB to serial converter and any Arduino + ESP8266 Wi-Fi module based project could be made easily.

 

Transfer Control Protocol

                 The Transfer Control Protocol is one of the main protocol of the internet protocol suit. The entire protocol is commonly referred to as TCP/IP because it originated the initial network implementation in which it correlated the Internet Protocol

TCP Server

               The TCP server is implemented on computer or any network connected device, the router assign it an IP address and port number is assigned to it manually, to set it as a TCP server, it wait until a TCP client device request has been initiated once the TCP connection has been initiated, the server can send and receive data to and from TCP client through the corresponding port assigned. This mode is useful when users want to send or receive data through the same network, port forwarding could be an option.

TCP Client

               The TCP client mode is implemented on a device on a network to exchange data with the TCP server, the TCP client has required the IP address of TCP server and its port number to communicate with it, once a TCP session is established between the two, the device server can send or receive data to and from the TCP client. This mode is useful when users want to establish a connection with TCP server to exchange data through the port on same network.

HTTP Protocol

The HTTP is referred as Hypertext Transfer Protocol, based on client-server computing model, in this function a client (could be a web browser and an application running on the computer) submits an HTTP request message to the server, the server in return the web pages or HTML resources hosted on the server. For example when user enter a URL in the browser, it actually sends the request message to the web server to fetch the requested webpage in response, this function is also called request-response function

MQTT

            The MQTT (Message Queuing Telemetry Transmission) is a light weight messaging protocol that provides data to the client on network with simple way to distribute telemetry information. It is the protocol that uses a publish/subscribe communication pattern and broker is involved among the clients to communicate with each other, is used for machine-to-machine (M2M) communication and plays important role in the internet of things                                                             

AT Commands

 Change Baudrate

AT+UART_DEF=9600,8,1,0,0

Wi-Fi Modes

Station Mode

In Station mode ESP8266 could be connected to any other Wi-Fi router

AT+CWMODE=1 //Station Mode

Response: OK

 

T+CWLAP // To check the Wi-Fi networks that are available

 

 Access Point Mode                                                                                                                                                 In Access Point mode ESP8266 could be set as a Wi-Fi hotspot.

AT+CWMODE=2 //Access Point Mode

Response: OK

Access Point+ Station

 AT+CWMODE=3 //Access Point+Station

AT+CWLAP // To check the Wi-Fi networks that are available

Connect To Router

AT+CWMODE=1 //Station Mode

Response: OK

 

AT+CWLAP // To check the Wi-Fi networks that are available

 

AT+CWJAP=”SSID”,”PASSWORD” //SSID and Password of router

Response: OK

 

Disconnect Router

AT+CWQAP

Response: WIFI DISCONNECT

Query Device’s IP

AT+CWMODE=1 //Station Mode

Response: OK

 

AT+CWJAP=”SSID”,”PASSWORD” //SSID and Password of router

Response: OK

AT+CIFSR

Response: 192.168.0.104 // Device got an IP from router

ESP8266 as TCP Server                                                                                                                                      Set Access Point Mode

AT+CWMODE=1 //Station Mode

Response: OK

To connect ESP8266 with Wi-Fi router

AT+CWJAP=”SSID”,”PASSWORD” //SSID and Password of router

Response: OK

To get IP from router’s DHCP

AT+CIFSR

Response: 192.168.0.104 // Device got an IP from router

To allow multiple connections

AT+CIPMUX=1 //Enable Multiple connection

Response: OK

To setup TCP server with default Port number

AT+CIPSERVER=1// default port=333 

Response: OK

To setup TCP server with custom port number

AT+CIPSERVER=1,8080

Response: OK

Send data to the TCP client

AT+CIPSEND=0, 4 // Set data length which will be sent, such as 4 bytes to connection NO.0

>ASAD                //Enter the data

Response: SEND OK

Receive Data

+IPD, n: xxxxxx //Received n bytes, data=xxxxxx

 

ESP8266 as a single TCP client connecting to the server

Set Access Point Mode

AT+CWMODE=1 //Station Mode

Response: OK

To connect ESP8266 with Wi-Fi router

AT+CWJAP=”SSID”,”PASSWORD” //SSID and Password of router

Response: OK

To get IP from router’s DHCP

AT+CIFSR

Response: 192.168.0.104 // Device got an IP from router

To set ESP8266 as TCP client

AT+CIPSTART=”TCP”, “192.168.0.106”, 8080  // Protocol, Server Ip and Port

Response: OK

Send data to the server

AT+CIPSEND=4 // Set data length which will be sent, such as 4 bytes

>ASAD                //Enter the data

Response: SEND OK

Receive Data

+IPD, n: xxxxxx //Received n bytes, data=xxxxxx

Creality 3D® Ender-3 V-slot Prusa I3 DIY 3D Printer Kit 220x220x250mm Printing Size With Power Resume Function/MK10 Extruder 1.75mm 0.4mm Nozzle

Programming Arduino to Send At commands serially


#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); //RX,TX
// Arduino pin 2 (RX) to ESP8266 TX
// Arduino pin 3 to voltage divider then to ESP8266 RX
// Connect GND from the Arduiono to GND on the ESP8266
// Pull ESP8266 CH_PD HIGH
// When a command is entered in to the serial monitor on the computer
// the Arduino will relay it to the ESP8266
// AT+UART_DEF=9600,8,1,0,0

int LEDPIN = 13;

void setup(){
pinMode(LEDPIN, OUTPUT);
Serial.begin(9600); // communication with the host computer
//while (!Serial) { ; }
// Start the software serial for communication with the ESP8266
mySerial.begin(9600);//Serial.println("");
//Serial.println("Remember to to set Both NL & CR in the serial monitor.");
//Serial.println("Ready");
// Serial.println("");
}

void loop()
{
// listen for communication from the ESP8266 and then write it to the serial monitor
if ( mySerial.available() ) { Serial.write(mySerial.read() ); }

// listen for user input and send it to the ESP8266
if ( Serial.available() ) { mySerial.write( Serial.read() ); }
}

Application Links

ESP8266 Datasheet

https://www.espressif.com/sites/default/files/documentation/0a-esp8266ex_datasheet_en.pdf

ESP8266 AT Command Reference

https://www.espressif.com/sites/default/files/documentation/4aesp8266_at_instruction_set_en.pdf

 

Want your project done? follow me on Fiverr

null

2 thoughts on “Getting started with ESP8266 Wi-Fi Module

  1. It is a great article. Thanks for sharing it.
    A single small esp8266-01 Is very useful for projects with only one or two sensors but instead of using a combination of Arduino and esp8226 a single board like D1 , D1 mini, NodeMCU may be more useful as it has all the required pins for sensors and esp on board. Most of these have their own USB ports for connectivity with system and thus with IDE. These boards do not need level converters to 3.3. The only thing I find a bit strange is that most of these boards have only one Analog pin. A combination of Arduino and ESP therefore would suit most if there are more Analog sensors to be used.
    Adding a short demo code will make it further useful for enthusiasts. Please keep it up.

Leave a Reply

Your email address will not be published. Required fields are marked *

Copied!