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

<?php
	$val = $_GET['string'];
	$bf = fopen("test.txt","w+");
	fwrite ($bf,$val);
	fclose($bf);
?>

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

Arduino

/**
* Testar o ESP8266
* Ligando apenas a uma rede sem fios
* E tentar aceder a uma pagina web
* e enviar uma string
*
* Feiticeir0 - http://blog.whatgeek.com.pt
*/

#include <SoftwareSerial.h>
#include <stdlib.h>

// RX ESP -> TX Arduino
// TX ESP -> RX Arduino
SoftwareSerial ESPSerial(2, 3); // RX, TX

// this runs once
void setup() {                

  // enable debug serial
  Serial.begin(9600); 
  // enable software serial
  ESPSerial.begin(9600);
 
  // reset ESP8266
  ESPSerial.println("AT+RST");
  
  //conect to network
  ESPSerial.println("AT+CWMODE=1");
  ESPSerial.println("AT+CWJAP=\"<SSID>\",\"<PSK>\"");
  delay(4000);
}


// the loop 
void loop() {
  
  // TCP connection
  String cmd = "AT+CIPSTART=\"TCP\",\"";
  cmd += "<WEBSERVER_IP>";
  cmd += "\",<PORT>";
  ESPSerial.println(cmd);
   
  if(ESPSerial.find("Error")){
    Serial.println("AT+CIPSTART error");
    return;
  }
  
  // prepare GET string to send - page to get - val.php
  String getStr = "GET /tmp/val.php?string=";
  // String to display
  getStr += "fromESPv4";
  getStr += "\r\n\r\n";

  // send data length
  cmd = "AT+CIPSEND=";
  cmd += String(getStr.length());
  ESPSerial.println(cmd);

  if(ESPSerial.find(">")){
    ESPSerial.print(getStr);
  }
  else{
    ESPSerial.println("AT+CIPCLOSE");
    // alert user
    Serial.println("AT+CIPCLOSE");
  }
    
  delay(1000);  
}

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

AT+RST

Put the ESP in client mode (station mode)

AT+CWMODE=1

Connect to a wireless network

AT+CWJAP=\"<SSID>\",\"<PSK>\"

<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

AT+CIPSTART=\"TCP\",\"";
  cmd += "<WEBSERVER_IP>";
  cmd += "\",<PORT>";

Prepare data to send – HTTP call, GET method

String getStr = "GET /tmp/val.php?string=";
  // String to display
  getStr += "fromESPv4";
  getStr += "\r\n\r\n";

Send the data

First, send the length of the data

cmd = "AT+CIPSEND=";
  cmd += String(getStr.length());
  ESPSerial.println(cmd);

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

if(ESPSerial.find(">")){

And, if successful, send the actual data

ESPSerial.print(getStr);

If not, we close the connection

 ESPSerial.println("AT+CIPCLOSE");

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 - - [19/Oct/2015:22:31:01 +0100] "GET /tmp/val.php?string=fromESPv4" 200 1
192.168.50.146 - - [19/Oct/2015:22:32:07 +0100] "GET /tmp/val.php?string=fromESPv4" 200 1
192.168.50.146 - - [19/Oct/2015:22:32:28 +0100] "GET /tmp/val.php?string=fromESPv4" 200 1
192.168.50.146 - - [19/Oct/2015:22:33:18 +0100] "GET /tmp/val.php?string=fromESPv4" 200 1

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