Connect to WiFI with Arduino
and ESP8266 Module
The ESP8266 WiFI module is a cheap and powerful circuit for enabling micro-controller boards like the Arduino to work with WiFI.
The ESP8266 WiFi module has 8 pins, and there are multiple ways to connect them, depending on the mode you want it to work in. However, in this article, we intend for our Arduino to communicate with the ESP8266 board, so we’ll use the following connection:
Esp8266 | Arduino
— — — — — — — — -
RX | 7
TX | 6
GND | GND
VCC | 3.3v
EN | 3.3v
GPIO 0 | None
GPIO 2 | None
Note that the EN pin is also known as CH_PD or CH_EN.
Normally,
- the RX pin on the ESP8266 should be connected to the TX (1) pin on the Arduino, and
- the TX pin on the ESP8266 should be connected to the RX (0) pin on the Arduino
But the default serial interface of the Arduino uses those pins, so we emulate the pins 6 and 7 to act as our RX and TX, using a library called SoftwareSerial.
Add Library
A really good library I found for communicating between Arduino and ESP8266 module is WiFiESP.
I recommend it because it provided an abstraction over the standard way of communicating via the AT command set.
To use in your Arduino IDE,
- Go to Tools → Manage Libraries
- Filter by “WiFiEsp”
- Select the latest version
- Install
Our Arduino Program
At the top of your Arduino program, include WiFiESP,
#include <WiFiEsp.h>
Add the SoftwareSerial library to emulate the pins 6 and 7,
#ifndef HAVE_HWSERIAL1
#include <SoftwareSerial.h>
SoftwareSerial wSerial(6, 7);
#endif
Note that our emulated Serial stream is called wSerial
.
To start the WiFiEsp module, we’ll use this setup() function,
void setup() {
Serial.begin(9600);
wSerial.begin(115200);
WiFi.init(&wSerial);while (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
delay(1000);
}
}
This code
- starts our default Serial connection with baud rate 9600.
- starts our emulated Serial connected
wSerial
, for communicating with the ESP8266 board at baud rate 115200. - Initialises our WiFi with WiFi.init(…), and passes the address of
wSerial
. - If something goes wrong, it gets stuck printing out
“WiFi shield not present”
.
List Networks
A full program to list available WiFi networks would be,
See other examples of what you can do with the WiFiEsp library.