Use ESP8266 shield for Arduino Wi-Fi

Use ESP8266 shield for Arduino Wi-Fi

I needed a cheap way to provide Wi-Fi to an Arduino for a project that I was working on.

The ESP8266 is an amazing cheap Wi-Fi enabled micro-controller that I have started to use in some of my projects.

You can find Arduino ESP8266 shields on ebay or Amazon by searching for “esp8266 arduino shield” for €5 to €10 which is far cheaper than any of the standard Arduino Wi-Fi shields which can be anywhere from €30 to €80!

The way this shield works is the Arduino communicates with the ESP8266 over serial and the ESP8266 handles the communication and returns the results over serial.

Either the ESP8266 can be left with the stock firmware and the Arduino can control the ESP8266 using AT commands (there are several libraries to help with this) or an Arduino program can run on the ESP8266 and communicate with Arduino program on the Arduino, this is the option I choose.

In order to program the ESP8266 shield we need to connect an FTDI to the debug port of the shield and set the DIP switches to 0011 as shown in the image below. This DIP configuration sets the ESP8266 into flashing mode and disables the non-debug port serial connections. The Arduino can be used to program the shield but its easier to use the FTDI.

The code needed for the ESP8266 can be seen below:

#include <ESP8266WiFi.h>

const char* ssid     = "WIFI";
const char* password = "WIFIPASS";
const char* host = "example.com";

void setup() {
  delay(20000);
  Serial.begin(115200);
  delay(10);
  Serial.print("\r\n\r\nConnecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("\r\nWiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  Serial.print("connecting to ");
  Serial.println(host);
 
  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }
  
  // We now create a URI for the request
  String url = "/api/test.txt";
  Serial.print("Requesting URL: ");
  Serial.println(url);
  
  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" + 
               "Connection: close\r\n\r\n");
  unsigned long timeout = millis();
  while (client.available() == 0) {
    if (millis() - timeout > 5000) {
      Serial.println(">>> Client Timeout !");
      client.stop();
      return;
    }
  }
  
  // Read all the lines of the reply from server and print them to Serial
  while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
  Serial.println("\r\nclosing connection");

  delay(5000);
}

This code just retrieves an example page from the web and returns it to the serial port of the ESP8266. This basic code can be fleshed out to do whatever you need.

Set the DIP switches to 1100 as shown below and remove the FTDI.

The ESP8266 shield connects the ESP8266s serial to the first serial port of the Arduino (Serial0 or Serial) which is pins 0 and 1 on the Arduino. This is also the same serial port that the USB connection uses. In order to still use the USB Serial connection I rerouted the shields serial connection to another serial port. I bent the 0 and 1 pins so that they were not connected to the Arduino and I connected them to Serial1 on the Arduino Mega. If you are using an Arduino with only 1 hardware serial connection then you can use software serial.

I used an example serial pass-through Arduino sketch to pass data from the ESP8266 to the Computer in a normal deployment you would pass data from the Arduino to the ESP8266 and process the data sent back on the Arduino.

void setup() {
  // initialize both serial ports:
  Serial.begin(115200);
  Serial1.begin(115200);
}

void loop() {
  // read from port 1, send to port 0:
  if (Serial1.available()) {
    int inByte = Serial1.read();
    Serial.write(inByte);
  }

  // read from port 0, send to port 1:
  if (Serial.available()) {
    int inByte = Serial.read();
    Serial1.write(inByte);
  }
}

When the code was run I got the following result:

Connecting to WIFI . WiFi connected IP address: 192.168.1.180 connecting to example.com Requesting URL: /api/test.txt HTTP/1.1 200 OK Date: Wed, 02 Jan 2019 17:29:55 GMT Server: Apache X-SERVER: 3049 Last-Modified: Wed, 02 Jan 2019 17:05:20 GMT ETag: "7-57e7ca69e7400" Accept-Ranges: bytes Content-Length: 7 Connection: close Content-Type: text/plain success closing connection

The test.txt file contained the word success which is the result that was retrieved. I now have a cheap way to add Wi-Fi to my Arduino projects!

As always if you have any questions please don’t hesitate to ask,

Conor.

Leave a Reply

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

%d bloggers like this: