How to use an ESP8266 in the Arduino IDE

Updated 09 November 2018

In August of 2014 Shanghai-based chip manufacturer, Espressif, released a ultra-cheap Serial to Wi-Fi chip called the ESP-01. At the heart of the ESP01 was an ESP8266 chip broken out into the 8 pins needed to program it via a microcontroller. You could utilize a simple two wire serial connection to relay WiFi information!

The release of the ESP opened the door to WiFi connections with microcontrollers for as little as $5.00! Hackers, makers, tinkerers and hobbyists around the world began ordering these things in bulk. Chomping at the bit to dive into the emerging reality that was IoT connected devices!

However, the only instructions that existed were only available in Chinese and very bare-bones. There was months of trial and error from the community into fully understanding and utilizing the chip.

esp8266 module from espressif

Fast forward two months and Espressif released a SDK for the chip. This isn't unusual, except that it turned the chip into an extremely powerful microcontroller. The SDK gave users all around the world access to the GPIO and other peripherals that are part of the ESP8266! People immediately began porting the SDK over to open-source versions that gave more freedom and allowed the magic of open-source to create a wave of infamy that the chip would ride to this day.

Then came the Arduino IDE port, that allowed makers to pick up an ESP8266 and use it just like an Arduino, with added wifi. The only real problem people had was that while they could access the peripherals on the software side of their chips, there was no real way to access them easily on a hardware level.

Enter the ESP-## boards. As Espressif quickly caught onto the enormous demand for their ESP8266 Module, they began producing newer models of the ESP-01 board (numbered ascending) through to ESP-14. The best version of the lot (in our opinion) is the ESP-12E, with a PCB Trace antenna that is FCC/CE approved. That's not to mention the 4MB of flash that is included. Electronics manufacturers around the world, not skipping a beat, began to produce breakout boards for this module that lowered the entry level user barrier significantly.

Now we have major brands like Sparkfun and Adafruit providing boards like the ESP8266 Thing and HUZZAH ESP8266 to the consumer market. These boards give you access to Analog inputs, GPIO, UART Pins and Power inputs without having to worry about the pat-your-head-rub-your-belly style of getting an ESP-01 to boot. There are libraries, guides and projects all over the internet that were born from this amazing little chip. We are going to take a quick look at how we can get up and running with a sketch on the ESP8266 HUZZAH Breakout from Adafruit.

Our favorite handful of ESP8266 Products from around the world include:

  • SparkFun WiFi Shield - ESP8266 - This Arduino compatible shield gives your Arduino WiFi capabilities for a much lower price point than other shields.
  • SparkFun ESP8266 Thing - This is Sparkfun's breakout board for the ESP8266. It comes loaded with a LiPo charger, power supply, and all of the other supporting circuitry it requires.
  • Wifi Bee ESP8266 - This is DFRobots Serial to WiFi board. It comes in the same unique form factor as XBee wireless boards.

Adafruit huzzah esp8266 module

The HUZZAH Breakout Board from Adafruit

We'll choose to dip into the glorious environment of Adafruit once more for this tutorial with their Huzzah board.

We haven't mentioned it yet, but these boards don't have USB connections available. If you want to program them you'll need to grab yourself an FTDI Cable.

The first thing you'll need to do is install the headers that come along with your breakout. It shouldn't take you too long to do. The tip here is place the headers into your breadboard, sit the breakout on top and solder them into place. Turn your board over and install the short-edge header facing out the opposite way.

That's all it takes, now connect your FTDI Cable to that pin header and you are ready to program your breakout. No AT commands or pin-jumping required!

Lets get our Arduino IDE ready for this foreign board. Firstly, add the the ESP boards to your additional boards manager. More guidance on that here.

http://arduino.esp8266.com/stable/package_esp8266com_index.json

Next, open your boards manager and enter ESP into the search box and install the package from the ESP community. This thing is around 150MB as it includes every ESP board and breakout available.

Restart your IDE and connect your board to your PC. When we have used boards in the past, we usually can select our Board and COM port and proceed on our merry way. The ESP is a little different in that regard. You'll need to select the processor speed you'd like to use alongside the baud rate and the usual board/COM selection. Once you've made all the required selections, you are ready to go.

Adafruit have also provided a fantastic sketch for their Huzzah as a quick way to check wireless connectivity. Grab the following code and change the SSID and Password fields at the top of the sketch as required:

Adafruits Huzzah Example Code to test HTTP connection:

/*
 *  Simple HTTP get webclient test
 */
 
#include <ESP8266WiFi.h>
 
const char* ssid     = "yourssid";
const char* password = "yourpassword";
 
const char* host = "wifitest.adafruit.com";
 
void setup() {
  Serial.begin(115200);
  delay(100);
 
  // We start by connecting to a WiFi network
 
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
 
  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}
 
int value = 0;
 
void loop() {
  delay(5000);
  ++value;
 
  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 = "/testwifi/index.html";
  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");
  delay(500);
  
  // 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();
  Serial.println("closing connection");
}

Another break in the battle rhythm with the ESP module is you'll need to place the board into boot mode by following this sequence:

  • Hold down the GPIO0 Button
  • Press the Reset Button
  • Release the GPIO0 Button
  • Ensure the LED above the GPIO0 button is dimly lit, if not you have done something wrong!

Now that you've got your board into boot mode, you can send a sketch to it.

Hit the upload button and wait for it to send the sketch over to your module.

Now you can open the serial monitor, and if everything went well, you will receive some messages from your board letting you know if it has connected to the network or not.

That's a brief history on the ESP8266 with a quick tutorial on using it with the Arduino IDE. There's so much you can do with this module, it's super-flexible and once you figure out the nuances of it, it becomes extremely simple! If you have anything to add to the conversation, please do! Go ahead and start the conversation below!

Have a question? Ask the Author of this guide today!

Please enter minimum 20 characters

Your comment will be posted (automatically) on our Support Forum which is publicly accessible. Don't enter private information, such as your phone number.

Expect a quick reply during business hours, many of us check-in over the weekend as well.

Comments


Loading...
Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.