The ESP8266 WiFi Module–how to get it to work with an Arduino

WP_20160106_15_27_46_Pro_LIFinally got around to playing with the ESP8266 WiFi Module with an Arduino UNO.  I am using the UNO simply because it has a steady, 3.3v output while the HalfByte Console isn’t as steady and the module is, from everything I have read, is not tolerant of much more than the 3.3 volts.  Until I fix the console’s 3.3volt output, I will use the UNO.

Before I go on, I have to say, this little board isn’t very reliable. It is only connecting about a third of the time.

The documentation is spotty, even though it has been out for quite sometime now.  So, I hope to help anyone who just wants to use it as a WiFi module and not reprogram it to play Tic Tac Toe.

First, it is important to know which module you have as there are quite a few variations.  You go here to figure that out. The board I have is an ESP-01 Rev 2.  The Rev is important as you have a couple of extra steps from the rev 1 board.

From the wiki, here’s the pin out for the rev 2 board:

image

In order for this thing to work, you MUST jumper the CHIP_EN (or CHIP_PD) pin to the VCC pin, pulling the pin high.  This enables the whole thing to, you know, work. 

One other important detail…the baud rate is 115,200.  Most documentation I read said it was 9600, but, at least on mine, it is 115,200 baud.

Those two things were the key for me to get my module to connect and work.

Wiring it up is easy:image

ESP8266 ARDUINO
UTX RX (pin 0)
URX TX (pin 1)
GND GND
VCC 3.3V
CHIP_EN 3.3V
RST Reset

The code is straightforward enough. I copied an example from the wiki and modified it to work with my module and took out some extraneous junk.WP_20160106_15_59_14_Pro_LI (2)

Code:

#include <SoftwareSerial.h>
#define SSID        “xxx”
#define PASS        “xxx”
#define DST_IP      “173.194.116.116”    //google.com
SoftwareSerial dbgSerial(10, 11); // RX, TX
void setup() 
{
    // Open serial communications and wait for port to open:
    Serial.begin(115200);
    Serial.setTimeout(5000);
    dbgSerial.begin(9600);  //can’t be faster than 19200 for softserial
    dbgSerial.println(“ESP8266 Demo”);
    //test if the module is ready
    Serial.println(“AT+RST”);
    delay(1000);
    if(Serial.find(“ready”))
    {
        dbgSerial.println(“Module is ready”);
     }
    else
    {
        dbgSerial.println(“Module have no response.”);
        while(1);
     }
    delay(1000);
    //connect to the wifi
    boolean connected=false;
    for(int i=0;i<5;i++)
    {
    if(connectWiFi())
      {
      connected = true;
      break;
      }
    }
    if (!connected){while(1);}
      delay(5000);
      //set the single connection mode
      Serial.println(“AT+CIPMUX=0”);
}
void loop()
{
  String cmd = “AT+CIPSTART=\”TCP\”,\””;
  cmd += DST_IP;
  cmd += “\”,80″;
  Serial.println(cmd);
  dbgSerial.println(cmd);
  if(Serial.find(“Error”)) return;
  cmd = “GET / HTTP/1.0\r\n\r\n”;
  Serial.print(“AT+CIPSEND=”);
  Serial.println(cmd.length());
  if(Serial.find(“>”))
  {
    dbgSerial.print(“>”);
  }else
  {
    Serial.println(“AT+CIPCLOSE”);
    dbgSerial.println(“connect timeout”);
    delay(1000);
    return;
  }
  Serial.print(cmd);
  delay(2000);
  //Serial.find(“+IPD”);
  while (Serial.available())
  {
  char c = Serial.read();
  dbgSerial.write(c);
  if(c==’\r’) dbgSerial.print(‘\n’);
  }
  dbgSerial.println(“====”);
  delay(1000);
}
boolean connectWiFi()
{
  Serial.println(“AT+CWMODE=1”);
  String cmd=”AT+CWJAP=\””;
  cmd+=SSID;
  cmd+=”\”,\””;
  cmd+=PASS;
  cmd+=”\””;
  dbgSerial.println(cmd);
  Serial.println(cmd);
  delay(2000);
  if(Serial.find(“OK”))
  {
    dbgSerial.println(“OK, Connected to WiFi.”);
    return true;
  }else
  {
    dbgSerial.println(“Can not connect to the WiFi.”);
    return false;
  }
}

Because I am using the serial I/O on the UNO for the module, I used softwareserial to talk to a HalfByte Console running the HalfByte graphical terminal sketch so I could see the output of the module.  Normally, you would, likely, not have any kind of output on the Arduino as you’d be using the WiFi module for your I/O. I’m guessing.

I may order a few more of these things to play with reprogramming them and running code directly on them.  There is a Basic Language interpreter, LUA and Javascript for them, so I may play with that.  For now, IF I can get it working reliably, I may pair one with an ATTINY85, like a Trinket or DigiSpark, and a DHT11 temp sensor and set up a network of wireless thermometers in the house. 

I can see the potential, but the reliability is an issue.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s