Getting Started With ESP8266

Ari Mahpour
|  Created: October 20, 2021  |  Updated: April 8, 2024
Getting Started with ESP8266

If you haven’t heard about the Internet of Things (IoT) movement by now you’ve probably been living under a rock. Almost all embedded devices these days connect to the internet in one way or another to either collect or send data to the cloud. That data can then be analyzed and/or acted upon depending on different conditions that are set by the server that processes the data. To get the data to the cloud we need smart, reliable, and cheap internet-connected devices to assist us with the process. While most IoT devices can be developed using powerful embedded computing devices such as the Raspberry Pi 4 most of our applications can be achieved through cheap, low-powered devices.

Most will agree that the Arduino was the first of its kind to be the simplest and cheapest microcontroller platform out there. Its proliferation throughout the Maker community is a testament to exactly that. As the company and user, the community grew so did the Arduino Shields and the devices themselves. The shields (and some newer Arduino devices) added the sorely needed internet connectivity support to enable the Arduinos to become true IoT devices.

Enter the ESP8266 MCU. This puppy has a microcontroller + Wi-Fi onboard. Couple that with the Arduino IDE (through the use of an external library package) and you now have a super cheap, Arduino-like device with Wi-Fi built-in. A board like the NodeMCU board from HiLetgo is completely self-contained and can act just like a smaller version on an Arduino. In this article, we’re going to learn how to get started with the NodeMCU board from HiLetgo and run through a few examples that will work on any ESP8266 based evaluation board (supported by the Arduino IDE libraries).

Getting Set Up

This tutorial assumes that you have the Arduino IDE Software installed and running. The following steps will walk you through getting your ESP8266 board up configured in the Arduino IDE environment.

Go to File > Preferences and paste the following URL in the “Additional Boards Manager URLs” field:
http://arduino.esp8266.com/stable/package_esp8266com_index.json

Figure 1: Arduino IDE Preferences
Figure 1: Arduino IDE Preferences

Go to Tools > Board > Boards Manager…
Search for “esp8266” and hit Enter. Once the “esp8266” library shows up click on Install.

Figure 2: Arduino IDE Board Manager
Figure 2: Arduino IDE Board Manager

Now plug in the Micro-USB cable into your board and configure it according to your board type. In this example we are using a NodeMCU board from HiLetgo so we will need to configure a few more board specific settings:

  1. Navigate to Tools > Board and click on “NodeMCU 1.0 (ESP-12E Module)”
  2. Navigate to Tools > Flash Size and click on “4MB (FS:3MB OTA:~512KB)”
  3. Navigate to Tools > CPU Frequency and click on “80 MHz”
  4. Navigate to Tools > Upload Speed and click on “921600”
  5. Navigate to Tools > Port and select the COM port associate with your device

Hello World: Blinking LED

In software, the most basic test people usually perform with a new language or setup is a “Hello World” test. The concept is very simple: print “Hello World” to the screen. For embedded systems a blinking LED is much like the same idea. A blinking LED is always a great way to test if your microcontroller is working because it lets us know that the circuit is good such as the power supplies, routing, and other electronics. It also lets us know that the compiled code has been accepted and loaded onto the device without any issues.

To get started with a canned blinking LED sketch navigate to File > Examples > ESP8266 and click on “Blink.” This example sketch will blink the LED on and off. On the top left corner click on the “Upload” button:

Figure 3: Arduino IDE Upload Button
Figure 3: Arduino IDE Upload Button

Once the device has been programmed you should see the NodeMCU board’s LED flashing on and off every second or so.

Testing the Wi-Fi

As mentioned in the introduction the whole idea of using the ESP8266 within the Arduino environment is to get an all-in-one, cheap, Arduino-like device with Wi-Fi. To take advantage of that we’ll want to explore some of the Wi-Fi sketch examples that came with the ESP8266 library which we installed earlier.

Let’s navigate to File > Examples > ESP8266HTTPClient and click on “BasicHttpClient.” This will demonstrate the device’s ability to connect to a Wi-Fi network and grab the content from a webpage. At around line 35 you will see the following code:

WiFiMulti.addAP("SSID", "PASSWORD");

Replace “SSID” with your router’s Wi-Fi SSID and “PASSWORD” with your Wi-Fi password. One thing to keep in mind if you run into issues is dealing with non-alphanumeric characters. For example, if your router SSID is “I’m a Router” then there is a chance that the apostrophe will not be caught correctly. For the best results make sure to use an SSID that contains only numbers and letters.

Once you replace the SSID and PASSWORD go ahead and upload the sketch. After the sketch has been uploaded to the device, navigate to Tools and click on “Serial Monitor.” The serial monitor output should look something like this:

[SETUP] WAIT 3...
[SETUP] WAIT 2...
[SETUP] WAIT 1...
[HTTP] begin...
[HTTP] GET...
[HTTP] GET... code: 200
<HTML>
<HEAD>
  <!-- Created with AOLpress/2.0 -->
  <TITLE>Connection Header</TITLE>
</HEAD>
<BODY>
<P>
<IMG ALT="Jigsaw" BORDER="0" WIDTH="212" HEIGHT="49" SRC="/icons/jigsaw">
<H1>
  The <I>Connection</I> header
</H1>
<P>
This page will be served to you with the following headers:
<P>
<CODE>ExtensionHeader: ExtensionValue<BR>
Connection: ExtensionHeader</CODE>
<P>
If you're getting this page through a proxy, you should <I>not</I> see the
<I>ExtensionHeader</I> !
<P>
  <HR>
<BR>
<A HREF="mailto:jigsaw@w3.org">jigsaw@w3.org</A>
</BODY></HTML>

Congratulations! You have now successfully connected your ESP8266 device to the internet.

ESP8266 and MQTT

In Getting Started with MQTT we reviewed the concept of MQTT and how to set up an Arduino Uno with Adafruit IO using the Ethernet Shield. This is where a device like the NodeMCU really shines. For less than the cost of the Arduino Uno itself we are able to throw together a complete MQTT enabled device without any external shields (since we have Wi-Fi built in). To get started we’ll use the same example demonstrated in Getting Started with MQTT except we’ll grab the sketch that’s specific to the ESP8266 module. Navigate to File > Examples > Adafruit MQTT Library and click on “mqtt_esp8266.” Just like in the HttpClient example above we’ll need to provide the Wi-Fi SSID and Password around line 24. We’ll also need to put in our Adafruit IO username and key as well (a few lines down further). For instructions on setting up an Adafruit IO account or how to get your key see Getting Started with MQTT. After you’ve filled out all the connection details and uploaded the sketch you should see a similar output in your serial monitor (Tools > Serial Monitor):
.......
WiFi connected
IP address: 
192.168.68.136
Connecting to MQTT... MQTT Connected!

Sending photocell val 0...OK!

Sending photocell val 1...OK!

Sending photocell val 2...OK!

Sending photocell val 3...OK!

You can now navigate to photocell feed on Adafruit IO to observe the data being sent from your ESP8266 module to your Adafruit IO MQTT broker. Here’s what my own feed looks like after a little while:

Figure 4: MQTT Data Observed on Adafruit IO

Figure 4: MQTT Data Observed on Adafruit IO

Conclusion

In this article, we reviewed the benefits of using a device such as the NodeMCU board from HiLetgo and how to get up and running with a few examples. Within these examples, we covered blinking LEDs, basic HTTP requests, and utilizing the Adafruit IO service to publish data from the device to the MQTT broker. Now that you’re familiar with the device try using your home Wi-Fi and the Adafruit IO broker service to set up a sensor monitoring project of your own. Happy prototyping!

About Author

About Author

Ari is an engineer with broad experience in designing, manufacturing, testing, and integrating electrical, mechanical, and software systems. He is passionate about bringing design, verification, and test engineers together to work as a cohesive unit.

Related Resources

Related Technical Documentation

Back to Home
Thank you, you are now subscribed to updates.