Pages Menu
TwitterRssFacebook
Categories Menu

Posted by on May 6, 2016 in Expressif ESP8266, Featured, Getting Started, IoT, Microcontrollers | 5 comments

Getting Started with ESP8266 WiFi Module

Getting Started with ESP8266 WiFi Module

This is an introductory post on getting started with ESP8266 WiFi Module/Microcontroller used in IoT applications. The purpose of this post is to get you up and running with this device using the Sparkfun Thing board and Arduino IDE. Once set up, in the next post we will learn how to connect it to the internet and stream sensor data to the cloud.

Contents

The ESP8266 WiFi Module + Microcontroller

Yes, you read it right – a low power 32-bit microcontroller with an embedded WiFi module on the same chip. That’s amazing! This tiny device can now connect almost any device to the internet. You know what’s more amazing? It costs just $2! Want even more? It is Arduino compatible! This is like a maker’s heaven. Read the datasheet to know how powerful it is!

ESP8266 Module

ESP8266 Module

Just imagine the possibilities. You can now send sensor data directly from your device to the cloud without sending it through a local host. Take the following two scenarios for instance.

Scenario 1: You have a local host (say a Raspberry Pi) connected to the internet. That’s it – no other device has internet connection. Say if your fridge wants to talk to the internet, then it needs to connect to your local host first, which then sends that data to the internet. What a pain!

Scenario 1 - Centralized Communication System

Scenario 1 – Centralized Communication System

Scenario 2: Everything remains the same as in scenario 1, except that we use the ESP8266 to connect the fridge to the internet. Now your fridge can directly talk to the internet without the need for a local host. Amazing!

Scenario 2 - Distributed Communication System

Scenario 2 – Distributed Communication System

ESP8266 – Usage Modes

The ESP8266 can be used in two ways:

ESP8266 as a WiFi Module

Here all the ESP8266 does is to provide internet connectivity to an already existing platform. Say you could connect an Arduino (or any other microcontroller/processor) to the internet using ESP8266 as a WiFi Module. So basically here it acts as a serial to WiFi converter.

ESP8266 as WiFi Module

ESP8266 as WiFi Module

ESP8266 as a Standalone Microcontroller + WiFi

Or you could get rid of the Arduino completely and replace it with the ESP8266 module and run your application code on it. Now this does has limitations – the ESP8266 doesn’t has as many peripherals as the Arduino, so it can only be used for smaller applications. But it works perfectly fine when all it needs to do is to send data from a bunch of sensors to the internet. You can also run a web server on the ESP8266 that can listen to incoming connections and serve web pages.

ESP8266 as Standalone Module

ESP8266 as Standalone Module

We will be using ESP8266 in standalone mode for this post. Once you get an idea of how to program it, you can use it the way you like, doesn’t really matter.

The Sparkfun ESP8266 Thing

But first you need the module! You can either get the cheap $2 module directly, or get a development board offering more functionality like NodeMCU, Adafruit HUZZAH, or Sparkfun Thing. We will be using SparFun ESP8266 Thing board for this post, but you should be able to get any generic ESP8266 module to work with little changes.

Sparkfun ESP8266 Thing

Sparkfun ESP8266 Thing

ESP8266 + Arduino = <3

Something that makes the ESP8266 even better is its compatibility with Arduino IDE. Which means you don’t really have to get to the nitty gritties of the ESP8266 hardware. As long as you’re little bit familiar with Arduino programming, you should be good with programming it.

There are other ways to program it as well – like using a virtual machine by Expressif, or fine-tuning Eclipse, or even the good old school method of using a text editor + gcc. To keep things simple and to encourage the maker community, we will be using Arduino IDE in this post.

arduino-splash

Setting up Arduino for use with ESP8266

Let’s set it up.

Step 1: Download and Install Arduino Software

If you haven’t done so already, go ahead and download Arduino IDE from arduino.cc. Install it as per the instructions for your OS. This should be straightforward.

Step 2: Install the ESP8266 Arduino Addon

In the latest version of Arduino (v1.6.8 today), this can be easily done using the boards manager. First we need to tell Arduino to fetch board info. We do this by updating the board manager with a custom URL. Open up Arduino and then head over to Preferences (File > Preferences or Arduino > Preferences, depends on your OS). At the bottom, add this as the Additional Boards Manager URLs:

http://arduino.esp8266.com/stable/package_esp8266com_index.json
ESP8266 Arduino Addon Custom URL

ESP8266 Arduino Addon Custom URL

If there are multiple URLs, you can separate them with comma (,) or use the expanded text box to add one URL per line. Click OK.

Step 3: Install Board Support Packages for ESP8266 based Boards

Now go to the Tools > Board > Boards Manager. Scroll to the end and you’ll find a new entry for ESP8266. Choose the latest version and install. It could take a while for Arduino to download all the new definitions, compilers and binaries.

Arduino Boards Manager

Arduino Boards Manager

Now choose the board by selecting Tools > Board > SparkFun ESP8266 Thing.

Hello World – LED Blinky

In order to quickly check if everything is set up fine, lets run the classic Hello World program – the LED blinky.

#define LED_PIN 5  // LED is pin 5 on Thing board

void setup() 
{
  // setup() runs just once before everything else
  pinMode(LED_PIN, OUTPUT);
}

void loop() 
{
  // code in loop() keeps iterating forever

  digitalWrite(LED_PIN, HIGH);  // turn LED on
  delay(500);                   // wait 500 ms
  digitalWrite(LED_PIN, LOW);   // turn LED off
  delay(500);                   // wait 500 ms
}

Connect your board and choose the appropriate serial port via Tools > Port. You can also change Tools > Upload Speed from 115200 bps to 921600 bps for faster uploads. Click Upload.

If all goes well, you should see the blue LED blinking every 500 ms. If it doesn’t upload, then try again, maybe unplug/replug the cables or turn the board on/off.

Alternate Board Setup as a Generic Module

Alternatively you could also try configuring a generic ESP8266 module:

Tools > Board > Generic ESP8266 Module
Tools > Flash Mode > DIO
Tools > Flash Frequency > 80 MHz
Tools > Upload Using > Serial
Tools > CPU Frequency > 80 MHz
Tools > Flash Size > 512K (no SPIFFS)
Tools > Debug Port > Disabled
Tools > Debug Level > None
Tools > Reset Method > nodemcu

And then try uploading again.

Summary

  • The ESP8266 is the device IoT enthusiasts have been looking for – microcontroller and WiFi connectivity on the same chip.
  • ESP8266 can be used as a WiFi module providing internet connectivity to your project, or can be used as a standalone device and be the center of your project.
  • ESP8266 support has been added to Arduino, thus allowing makers and creators to prototype their ideas much faster.
  • Programming ESP8266 is as simple as programming any other Arduino board.

In the next post, we will describe the Hello World of IoT – streaming data to the cloud. We will be building a weather station where we will stream temperature data to the cloud every minute using ESP8266. Let’s move beyond blinking lights!

Questions? Comments? Feedback? Write below! I will be glad to get a response from my awesome readers! Also subscribe to stay updated.

Written by Mayank Prasad (aka Max)

5 Comments

  1. Very good explanation Sir. I have interfaced Arduino Uno with ESP8266-01 and 16*2 LCD . The webpage has two text box and submit button. When i type text on webpage and click on submit button ,text should appear on LCD. I am stuck in web part . How to communicate web part and Arduino. I tried HTML with GET method . But not working .Can you please help.

    Thank you in advance.

    • Keshav, I’ll tell you a story. The lion summons all the animals in the jungle for a meeting. All animals show up except one. Do you know which one?
      Bottom line is, how the hell am I supposed to answer your question when you give me no background information at all about your application? How do I know which cloud platform you’re using? How are you programming your code? Are you running the web server on your ESP? Did you even write the correct APIs for HTTP requests? How did you write the webpage? Are you making the right API calls? “When i type text on webpage and click on submit button ,text should appear on LCD.” You’re talking as if all this happens magically. There are million ways to achieve this, not sure how you’re doing it. Either provide full information, or ask your question wherever you’re following the tutorial from. Thank you.

  2. Hello Max, where can we find the next post about the ESP

    • Working on it. :) I have the application ready, but writing is such a pain. :)

  3. A big thank you from the bottom of my heart for all your efforts of helping everyone to learn with such good study materials.

    Binu Joy
    Assistant Professor
    Electronics & Telecommunication

Leave a Reply

%d bloggers like this: