ESP8266 – Arduino wireless communication

ESP8266

ESP is a wonderful module – it can connect to wifi networks, create a webserver (accepting connections) and is very very cheap ! Its a lot more than this – just google it and you’ll find out.

The communication is made through Serial communication using AT commands.  You can connect it to a FTDI board and communicate with it !

There’s a lot of documentation in the Web, but room-15 has a very nice page with the command reference for the ESP8266.

But, the ESP8266 module can provide wifi (like a wifi shield) for the Arduino to connect to and start communication with other devices – Just like the example we’re going to see here.

Hardware

1x Arduino UNO (or any other)

1x ESP8266 module

1x breadboard

1x PC with a web server with PHP support

Wiring

esp8266_pinout_h-569x236
ESP-01 Pinout – Image from http://randomnerdtutorials.com

NOTE: The ESP8266 connects to 3.3v AND NOT 5v

ESP

Software

Now, setup the apache webserver (or any other you may prefer). Just copy and paste the below code in any directory of your server. Mine is in /tmp and the page is called val.php

Apache with PHP

The code just expects a variable named string, opens a file (always overwriting) and puts the content of the string inside.

Arduino

A little explaining of the code

Because Arduino boards can get a bit cranky when connecting something to the PINs 0 and 1, I’ve changed them to 2 and 3 – PINs 0 and 1 are used for serial communication and if used we might lose the ability to load sketches onto the board.  You can read more about that here and here.

Running just one time (void setup()), we do a reset of the board and connect to a wireless network

Reset the ESP8266 board

Put the ESP in client mode (station mode)

Connect to a wireless network

<ssid> and <psk> to be replaced by the correct values (ie: wireless SSID and network password)

In the void loop() section, the code to send data to a web server

Establish a TCP connection to a web server and port

Prepare data to send – HTTP call, GET method

Send the data

First, send the length of the data

After the execution of the above command, ESP will enter “unvarnished mode – returning of  ‘>’ character”  – that we search for in the return of the command

And, if successful, send the actual data

If not, we close the connection

Now, connect your arduino and monitor the logs of your web server

In the apache logs – access.log – if it works, will get something like the following:

192.168.50.146 is the IP of the ESP8266

This is just an example of how to use the ESP8266 module as a wireless card for the Arduino. You can also use the ESP8266 independently – that’s the power of the ESP8266

ES8266-full

7 Replies to “ESP8266 – Arduino wireless communication

  1. Hello,

    thanks for the explanation. I have some questions:
    – the functions like : ESPSerial.print(), are written by you in special library? As these functions are not part of standard SoftSerial library from Ardunino.
    – But i tried to copy your code with with mySerial.write(), like in standard library. But some how it doesnt work. Any hint?
    Thanks a lot for any suggestion.

    1. Hi Prashant !

      No, the ESPSerial.print is like the Serial.print function, but with the ESPSerial variable name.
      On line 15, I’m initialing a variable called ESPSerial using the SoftwareSerial library. Is the same as Serial.begin or Serial.println – but ESPSerial variable uses the PINs 2 and 3 that are connected to the ESP8266 and takes advantage of the SoftwareSerial library whereas Serial uses the Arduino serial port on PINs 0 and 1.

      Do you have the SoftwareSerial library installed ?

  2. I have interfaced with the above circuit set up but after executing ,In my serial monitor, it displays AT+CIPCLOSE continously.

    1. ah ah ah ah ah 🙂 You must be one of those trolls…

      Have you tried the code ? Of course not.. If you did, you new it works.. It is to show how to communicate using the ESP8266. Is not rocket science on how to go to mars! Next time, just leave a constructive comment. If it is wrong, just offer a solution !

Leave a Reply to DURGESH JOSHI Cancel reply

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