Saturday 3 September 2016

Get Started with ESP8266 Using AT Commands, NodeMCU, or Arduino (ESP-12E)

Original 

Get Started with ESP8266 Using AT Commands, NodeMCU, or Arduino (ESP-12E)

by
Featured
Picture of Get Started with ESP8266 Using AT Commands, NodeMCU, or Arduino (ESP-12E)
This Instructable will show you the steps needed to get started with the ESP8266 using a fantastic little development board for the ESP-12E module. The board includes everything needed including a USB to Serial adapter, surface-mount LED, and voltage regulator. In addition, it is easily mountable on a breadboard for developing your own IoT project(s)!
Edit: we now have the steps in this tutorial in video form!

The main goal of the Instructable is to cover all the different options you can use in terms of firmware for developing applications that use the ESP8266 to connect to the web. The content, although available, is scattered throughout various sites around the web thus we decided to condense it in one place.
Excited? Let's get started!

Step 1: What is the ESP8266?

Picture of What is the ESP8266?
esp-01.jpg
Since the summer of 2014, the ESP8266 has seen a wide adoption as a cost-effective solution for IoT and WiFi-capable devices. The ESP8266 was developed by Shangai-based Espressif Systems, as a Serial (UART) to Wi-Fi SoC (System On a Chip) based around a Tensilica Xtensa LX3 DPU. This tiny IC includes an RF front end, RAM, and (usually) an onboard TCP/IP stack that allows it ready to connect to a nearby Access Point, to act as an Access Point itself, or both.

Family of Breakout Boards (ESP-NN)

Quickly after launch, a variety of breakout boards for the ESP8266 started becoming available. The most popular ones have been the ESP-NN series, which typically integrate the SoC along with Flash RAM, a crystal, and even an onboard antenna. The most salient distinction between different ESP-NN models are the pins that are broken out from the ESP8266 (for a full list see http://www.esp8266.com/wiki/doku.php?id=esp8266-module-family).
As the ESP8266 was developed as a Serial to Wi-Fi adapter, its firmware implemented an intertrepeter for AT commands. Thus initial usage of the IC was limited to using a either a USB to Serial adapter, or a separate microcontroller (e.g., ATmega328) to issue AT commands over the ESP8266's Serial UART interface. For this reason, the ESP-01 board quickly became popular amongst the ESP8266 community becaue of its 2×4, 0.1in-pitch connector that can be easily wired to a USB to Serial adapter. The connector gave access to the pins used for serial communication, namely RX and TX, as well as 4 control pins, GPIO0, GPIO2, CH_PD and RST (reset), along with VCC and GND.
However, other ESP-NN boards offer access to a wider variety of pins, although their packaging is of a custom Surface-Mount Device, with castellated pins as seen in the documentation page linked above. From the different ESP-NN boards, we began to experiment with the ESP-12E. The main reason is that this module was the one chosen by the developers of the NodeMCU project for their hardware DevKit 1.0 (see Firmware Options for details about NodeMCU).
Because the ESP8266 provides a cost-effective solution to the rapidly growing market of internet-connected projects and devices (i.e., the so-called Internet Of Things), it has become one of the most popular development platforms over the past year and a half. In consequence, a dedicated community has formed around the platform (http://esp8266.com), which has been focused on improving its functionality. For starters, different firmware options have been ported to run on the ESP8266, effectively taking it from a simple Serial to Wi-Fi adapter into a fully functional microcontroller with access to its GPIO and hardware-based functions like PWM, I2C, 1-Wire communication, and ADC; all this, of course, in addition to maintaining its Wi-Fi capabilities.

Step 2: List of Materials

Picture of List of Materials
For this Instructable we'll be using the following materials:
1 x ESP8266 (12-E) Development Board with onboard USB-to-Serial
1 x USB-A to Micro-B Cable Male/Male (3ft)

Step 3: Firmware Options

Picture of Firmware Options
esp8266_arduino_ide.jpg
nodemcu_devkit_port.jpg
A few different firmware options are available for the ESP8266. These allow us to access the module in different ways, as you can see below.

AT Command Processor (Default)

The quickest way to get started with the ESP8266 is to use its original firmware, which allows it to process any AT commands that it receives over its Serial UART interface. The biggest advantage of this option is that we need not be familiar with any specific language or framework to use the module. We can simply send it a series of commands to achieve our goal. The downside to this is that we need either an additional microcontroller involved or a USB to Serial adapter to send the necessary commands.
For the full list, please see the documentation (https://acrobotic.com/downloads/ESP8266_at_commands.pdf).
Whereas the AT commandsare the standard way of communicating with wireless-capable ICs (e.g., Bluetooth, Wi-Fi, GSM), they pose the limitation of needing another module to run the application that specifies these commands accordingly. However, if we could run the application within the ESP8266 itself then we'd have everything self-contained by a single IC. Fortunately, Espressif made a Software Development Kit (SDK) available that allowed users to flash different firmware options.

NodeMCU

NodeMCU is, at the moment, the most popular alternative for firmware running on the ESP8266. Based on the eLua project, it runs a Lua interpreter onboard the ESP8266, which is able to execute commands written in the Lua scripting language. The commands are sent to the ESP8266 via the Serial UART interface.
NodeMCU is a great starting point for Makers as it provides an interactive environment that allows running commands not only for controlling the ESP8266's wireless interface, but also its GPIO and hardware functionality such as i2c and PWM. In addition, we have access to the full scope of the Lua programming language for writing our applications. In the case of the default firmware (AT Commands Interpreter), the application code would have to be developed using a programming language suited to the microcontroller or SoC we use to develop the interface for sending the commands over Serial (e.g., C/C++ for microcontrollers in the Arduino boards).
Finally, not only does the NodeMCU firmware allows us to execute commands interactively, but we can save our applications as a script in the ESP8266's flash memory, and instruct it to run the application code every time it restarts! Even though this is a convenient option, our preferred method of working with the ESP8266 is to write our own firmware using the Arduino framework (details below).

Custom Firmware (using the Arduino IDE)

Whereas the NodeMCU Development Board is shipped with the latest version of the NodeMCU firmware, we can very easily replace it using the Arduino IDE. This allows us to run our own firmware on the ESP8266, which executes every time the module is powered. Even though this method erases the NodeMCU firmware from the ESP8266's flash memory, it is a straight-forward process to go back to using it!

Other

For advanced users, there is the esp-open-sdk toolchain which allows us to progam the ESP8266 directly (more info at the esp8266.com wiki) without having to use the Arduino IDE for uploading the code to it.
This neat little SoC has also received the attention from the Espruino and MicroPython communitites. Both MicroPython and Espruino are fantastic interpreters (Python for the former and JavaScript for the latter) that can run on embedded platforms. However, both of these options are still in very early stages of development for the ESP8266 and not all functionality is availab

Step 4: Installing the USB drivers

Picture of Installing the USB drivers
The USB to Serial UART module included on the NodeMCU Development Board is Silicon Labs' CP2012, for which we usually need to install the readily available Virtual COM Port (VCP) drivers.
Once installed, we can verify that the CP2102 is recognized by the computer. After connecting the USB cable to both the USB port on the computer and the board, we can open a terminal window and list the available devices by:
ls /dev/tty.*
The device file created to communicate with the CP2102 has the name tty.SLAB_USBtoUART. Now that we're ready to communicate with our ESP8266 via the CP2102, we can explore a few different firmware options.

Step 5: The AT Commands Interpreter

Picture of The AT Commands Interpreter
esp8266_at_commands_test.jpg
The developers of the fantastic ESP8266 SoC have made available the necessary tools to update the firmware on the module. In our case, we need to use these tools to be able to use the AT Commands Interpreter. As mentioned above, our NodeMCU Development Board comes loaded with the NodeMCU firmware. Similarly, if you're using this guide with other development boards and modules, you might also need to replace their firmware so this will be good practice.
For loading any firmware to the ESP8266 we need two things: a flashing tool, and an image to flash (bear in mind that in this context, the term 'flashing' means writing data to a flash memory storage device).

Flashing tool

There are a few different options for tools that are able to flash firmware onto the ESP8266. Our typical preference leans towards solutions that work on any Operating System (i.e., cross-platform). In this case we'll use esptool, a Python-based ESP8266 flashing tool developed by Fredrik Ahlberg.
From our terminal, we clone the entire repository by:
git clone https://github.com/themadinventor/esptool

Firmware image

With the flashing tool downloaded, the next step is to download the image we want to flash. Same as before, we have a few different options for which image (containing the AT Commands Interpreter) to use. In our case, we'll use the one made available by Electrodragon (as recommended in their wiki). Navigate to their shared folder ESP8266 (Google Drive) and then download the .bin file:
Firmware → AT_Bin files → 0.952 support SmartLink → v0.9.5.2 AT Firmware.bin

Flashing the firmware to the ESP8266

Before being able to use esptool, we need to install the Python module pySerial on our system. This can be done in a few ways depending on your Operating System. One way is to download the module's source code:
git clone https://github.com/pyserial/pyserial
And install it by running the following command in the Terminal (or PowerShell for Windows users):
sudo python setup.py install
With the module installed, we can now flash the image we downloaded using the esptool program. We'll need to know the location of the file in our system as well as the name of the device file or COM port, which in our case are "~/Downloads/v0.9.5.2\ AT\ Firmware.bin" and "/dev/tty.SLAB_USBtoUART" respectively. Then, we can run the esptool program by:
python esptool.py --port /dev/tty.SLAB_USBtoUART write_flash 0x00000 ~/Downloads/v0.9.5.2\ AT\ Firmware.bin
Once this is done, we can start AT commands to communicate with the ESP8266 .

Usage

From the different ways of sending messages over the computer's USB, we're going to use the Arduino IDE's Serial Monitor for this purpose. We only need to choose the appropriate option from Tools → Serial Port → yourserialport (in our case /dev/cu.SLAB_USBtoUART) and opening up the Serial Monitor window. Because we're not going to upload firmware to the board with the IDE, we need not worry about the selected Board, the default Arduino Uno selection is okay.
In the Serial Monitor window we need to first adjust two settings. We set the line ending drop down menu to "Both NL & CR" and also the baud rate to 115200. Once that's done, we can enter the test AT command:
AT
which simply returns an OK message. Then, for instance, we can try to scan for nearby networks by entering:
AT+CWLAP
See the entire list of available commands on the "Firmware Options" step of this tutorial.

Step 6: NodeMCU Firmware and Lua

Picture of NodeMCU Firmware and Lua
esplorer_wifi_scan.jpg

Firmware image

Similar to what we did before, we want to download an image for the NodeMCU firmware, so that we can use the esptool program to flash it. The latest version of NodeMCU can be downloaded from the project's Github repository.

Flashing the firmware to the ESP8266

Once downloaded, we can use esptool as we did before, remembering to change the name of the image file and device:
python esptool.py --port /dev/tty.SLAB_USBtoUART write_flash 0x00000 ~/Downloads/nodemcu_float_0.9.6-dev_20150704.bin

Usage

Because the NodeMCU firmware is a Lua interpreter we're need to make use of the fantastic Java-based tool written by Victor Brutskiy, the ESPlorer. For a comprehensive getting started guide covering the ESPlorer tool, refer to Rui Santos' documentation.
After making sure that the latest Java Runtime Environment (JRE)and the Java Development Kit (JDK) are installed (and their versions match), we can simply double-click the ESPlorer.jar file inside the .zip archive we downloaded. The first thing we want to do in ESPlorer is configure the baud rate to 9600 and select the correct device from the drop-down menu (in our case /dev/u.SLAB_USBtoUART). We can then click on Open to start communicating with the ESP8266.
After we've established communication with the device we can start running code written using Lua. In the ESPlorer's editor we can write the simple script:
-- print ap list
function listap(t)      
    for k,v in pairs(t) do        
        print(k.." : "..v)      
    end
end
wifi.sta.getap(listap)
Once we're done, we can click on the "Send to ESP" buttons in order to send the code to the NodeMCU Lua Interpreter running on the ESP8266. After the code runs, the ESP8266 sends back a response that is displayed in the output window of ESPlorer containing a list of the nearby Wi-Fi networks.
In a follow-up tutorial featuring the ESP8266, we'll show you how to make the code run automatically, every time the ESP8266 starts.

Step 7: Custom Firmware with the Arduino IDE

Picture of Custom Firmware with the Arduino IDE
arduino_ide_options_esp8266.jpg
arduino_nodemcu_devkit_port.jpg
arduino_ide_preferences_esp8266.png
arduino_ide_wifiscan_esp8266.jpg
For those of us who want to use the ESP8266 as a regular microcontroller such as the ATmega328 in the Arduino UNO, then it's possible to write custom firmware and load it on the chip. As is typical in programming microcontrollers, the custom firmware will replace anything previously stored in the chip's flash memory.
Although we can use the manufacturer's SDK to develop our custom firmware, it is much easier to use the good ol' Arduino IDE. If you don't have it installed, please look at our detailed tutorial on how to get it on your system.
In the Arduino IDE open the Preferences window and enter the URL below into the Additional Boards Manager URLs field, and select OK.
http://arduino.esp8266.com/stable/package_esp8266c...
Select the menu option Tools → Board → Boards Manager... and scroll down and to locate the option esp8266 by ESP8266 Community which should be the last item on the list, and click Install.
After restarting the Arduino IDE we can now select the board we're using from the menu option Tools → Board → NodeMCU 1.0 (ESP-12E Module). Then, we specify the correct CPU Frequency (Tools → CPU Frequency: "" → 80MHz) and Upload Speed (Tools → Upload Speed: "" → 115200). Finally, the last step is to select the correct option for the Port (Tools → Port → /dev/cu.SLAB_USBtoUART).
At this point we can write our own firmware and upload it. To get started we can try one of the examples File → Examples → ESP8266WiFi → WiFiScan. After uploading it, we can open the Serial Monitor window and observe the results. Note that we need to match the baud rate, so check that 115200 is selected from the drop down menu
Post a comment
Be nice! We have a be nice comment policy.
Please be positive and constructive.
2 months ago
@eduardov1:
The LoLin board (which has the same form factor as those Amica v0.9 NodeMCUs... which means it's not very breadboard friendly (I use a pair of mini breadboards and space the MCU between the two)) but I digress) has the CH340G USB-to-UART Bridge chip which requires its own driver with WinXP, Win7, and early versions of Win8 (as well as Mac OS-X Yosemite and El Capitan , and perhaps some early distributions of Linux.) Here is a link:
http://kig.re/2014/12/31/how-to-use-arduino-nano-m...
to a website that discusses this problem fow the inexpensive Chinese versions of the Arduino Nano which uses the same CH340G chip and the drivers you download from there should work for the LoLin NodeMCU V3.
Here is an image of how I connect up a LoLin NodeMCU V3 using two mini breadboards. Seems there is like a 1.5cm gap between the two mini breadboards.
-WRM
2 months ago
Also, there seems to be three major providers of NodeMCU boards: Amica, DoIt, and LoLin. Each has boards which use the CH340G and both Amica and DoIt has switched to the Silicon Labs CP2101 (not sure why, the CH340G always works well for me. I have them on my several Arduino Nanos, WeMos D1 WiFi boards and LoLin NodeMCU boards.
[Note: There appear to be a few other USB-to-UART bridge chip manufacturers, but on Node MCUs I have only seen the CP2102s and the CH340Gs. However, if you work with those AI-Thinker (or similar) ESP-xx WiFi modules, then you may decide to buy a general purpose TTL UART to USB adapter and I have seen versions of this kind of module board which use two other vendor's chips as well. These other two USB-to-UART bridge chips are the FTDI F232R and the Taiwanese Prolific Technologies PL2303, so I suppose you may need to download drivers for either of these should you happen to buy one for use with say an ESP-01 or ESP-12E module.]
4 months ago
This was amaizing!
I have a problem, my 8266 (Lolin it says nodemcu v.3...) is not detected in windows anymore!! but windows detects arduino (seem that the problem is not the usb port nor the driver) Any ideas?? I was using an OLED and I pressed reset and stopped working, the blue light work, though.
Thanks so much!
3 months ago
I have the same problem. Have you solved it per chance?
acrobotic (author) CrtSuznik
3 months ago
Thanks for checking out our tutorial!

Usually unplugging the board and re-plugging it back into the USB port works. It gives the driver a chance to re-establish the connection.

Navigate to your Device Manager, and expand the Ports (COM & LPT) category. As you plug/unplug the device from the computer, an entry should appear in there.

Also, try a different USB port on your computer as a sanity check. Let us know if neither of these solutions work for you!
9 months ago
Hmmm it is a fantastic idea to put all of this in one place - thank you. Shame your links and descriptions couldn't be in plain English ... a sure gap in the market for someone.
However, my terminal prog in windows xp says it is connected at port 21 but I get no response from any entered AT commands.
Can the ESP8266 MCU USB module have many 'sketches' installed at the same time? I can not get a directory of the files nor a heap even though lualoader tells me it is connected and I've flashed / installed / loaded several files as the various webs seem to suggest. Perhaps it is broken.
I get a pile of junk characters when it connects and then nothing Certainly nothing on my wireless lan to say it is even existing. Right now I don't see the point of the thing and am pining for my reliable and intuitive Arduinos.
I shall cook the family Sunday roast and get back to it this evening ...
5 months ago
is iit now working?
i too have same problem what to do? please respond
10 months ago
Just wanted to say thank you for your hard work putting this together.
Even with this guide it was a mission, so for you to put this together must have taken quite a few days of work. But at the end of the day it all worked.
So it is possible for a total beginner like myself to make it happen. Learnt about github, python, using terminal in mac,
Cheers,
denn...
7 months ago
maybe its your ssid and password of your internet at home.. becous your on the router but no connection/
8 months ago
Hello all
I have a problem about nodemcu esp 8266
I wrote a script for nodemcu by lua loader but when I reset cheap the script has been deleted is there any way to save program whiteout any external memory on it's memory?
thanks
9 months ago
HI
Plugged in my first esp8250 board and all worked fine Wi-Fi and all. Tried the blink app an now AT commands do not reply.
On reset I get
ets Jan 8 2013,rst cause:2, boot mode:(3,7)
load 0x4010f000, len 1264, room 16
tail 0
chksum 0x42
csum 0x42
~ld
Any recommendations - I have tried AT-RESTORE but no luck.
If I get this thing working I will join the club.
Thanks
Bob
9 months ago
Hi,
I have started working on ESP8266(8-Pin). I have flashed it using firmware version nodemcu_integer_0.9.6-dev_20150704.bin.Just after flashing it shows a Wi-Fi signal in my cell phone and computer. but when i start with AT commands wifi signal gets off.
I am using AT commands to get the IP of that particular ESP module so that i can connect with android apps. Its not responding the AT commands. its shows its getting the AT commands but not giving response of that command.
AT
>
AT+RST
>>
AT+CIFSR
>>I m getting response Like this.
I have also tried with lualoader.
Baud rate changed to 115200= node.heap()
35184
> = wifi.sta.getip()
nil
> = wifi.sta.getip()
nil
>> = wifi.sta.status()
3 AP NOT FOUND
>
I have also tried to fix the IP by using this code by making init.lua
wifi.setmode(wifi.STATIONAP)wifi.setmode(wifi.STATION)
wifi.sta.config("SSID","PASSWORD")
wifi.sta.setip({ip="192.168.0.136",netmask="255.255.255.0",gateway="198.168.0.1"})
After that IP is fixed but the problem is, i m not getting the WiFi signal in my Computer and Cellphones. so i m getting problem to communicate with android apps. So, Please tell me how to solve these issues. If you are suggesting any firmware please attach it. Because i m getting so many firmware version on Github.
Send me a video or no. of steps how to solve this issues.
chandankr502@gmail.com
11 months ago
very interesting and condensed !
Thanks a lot.
acrobotic (author) Ambro1950
11 months ago
Thanks for checking it out. Glad you liked it :)
11 months ago
Wow! Fantastic primer! Thank you
acrobotic (author) PhilTee
11 months ago
:) Thanks! We're very glad you liked it.

About This Instructable


99,888views
404favorites
122
Bio: Designing fun, unique, and Open-Source electronic kits for education, DIY, hobby, art, science, and more. For the tinkerer in all of us!
More by acrobotic:Get Started with ESP8266 Using AT Commands, NodeMCU, or Arduino (ESP-12E) Raspberry Pi  Bluetooth LE Controller for WS2812B (NeoPixels) Control Bluetooth LE Devices From A Raspberry Pi 
Tags:

1 comment: