ESP8266 Tutorial

De Wiki do Bernardino
Ir para: navegação, pesquisa


Connection using a FTDI232

ESP8266-FTDI232.jpg Usb-ttl-ft232rl-pinout.png

AT Commands



First you need a firmware file. You can build one yourself with the Toolchain or download one:



ESPlorer — Integrated Development Environment (IDE) for ESP8266 developers.

  • Package Description

The essential multiplatforms tools for any ESP8266 developer from luatool author’s, including a LUA for NodeMCU and MicroPython. Also, all AT commands are supported. Required JAVA (SE version 7 and above) installed.

  • Supported platforms

Windows(x86, x86-64) Linux(x86, x86-64, ARM soft & hard float) Solaris(x86, x86-64) Mac OS X(x86, x86-64, PPC, PPC64)

Program: Author Site:

Firmware upload with

A cute Python utility to communicate with the ROM bootloader in Espressif ESP8266. It is intended to be a simple, platform independent, open source replacement for XTCOM.

Commands examples (it's necessary connect GPIO0 to GND):

Dump a region from the ESP8266 memory space. For example, to dump the ROM (64 KiB) from the chip:

$ --port /dev/ttyUSB0 dump_mem 0x40000000 65536 iram0.bin
65536 bytes read... (100 %) Done!

Get Flash ID

$ --port /dev/ttyUSB0 flash_id
Manufacturer: e0
Device: 4014

Get Mac address

$ --port /dev/ttyUSB0 read_mac
MAC: 18:fe:34:82:a7:4b

Flash NodeMCU

$ --port /dev/ttyUSB0 write_flash 0x00000 nodemcu_latest.bin
Erasing flash...
Took 2.28s to erase flash block
Wrote 450560 bytes at 0x00000000 in 44.0 seconds (82.0 kbit/s)...


Interesting links


LUA 5.1 the complete reference manual

Operating commands

Restart de module

> node.restart()  

Execute a script

> dofile("script.lua")

Manage Files

Create a file

>"hello/world.txt", "w")
> file.writeline("Hello World!")                                                
> file.close()

File list

> for k,v in pairs(file.list()) do print(k.." ("..v.." bytes)") end
hello/world.txt (13 bytes)

Read file content

> print(file.readline())
Hello World!
> file.close()

The default mode for is “r”, which is read-only. It’s usually a good idea to check the return value from to ensure that the file exists (return value = true) before you try to do something:

> ="foobar.noexist")
> file.readline()
stdin:1: open a file first

The file can be renamed:

> file.rename("hello/world.txt", "helloworld.txt")
> for k,v in pairs(file.list()) do print(k) end

And finally, the file can be deleted:

> file.remove("helloworld.txt")
> for k,v in pairs(file.list()) do print(k) end


A GPIO (or general-purpose input/output) is a pin that can be used by our code to interface to the outside world.

We can use a GPIO pin to listen for things, like a button press (input) or to control things, like light an LED (output).

Set pin mode

When using GPIO pins we first need to specify in which mode we’d like to use it. There are three modes into which a pin can be set:

Mode Reference Description
Input gpio.INPUT Poll a pin to get its value.
Output gpio.OUTPUT Assign a pin its value.
Interrupt gpio.INT Same as input + set a callback to be executed every time a pin’s value changes.

To set a pin to one of these modes, we use the gpio.mode() function:

gpio.mode(pin1, gpio.INPUT)        -- set pin1 to input mode
gpio.mode(pin2, gpio.OUTPUT)    -- set pin2 to output mode
gpio.mode(pin3, gpio.INT)        -- set pin3 to interrupt mode

All pretty straightforward stuff, but don’t relax just yet! This is where it gets a little confusing.

You might think that the value of pin1, pin2 and pin3 are the numbers on the GPIO pins. Am I right? Yeah, I thought that too, but it’s not that easy. The first argument to gpio.mode() (and other gpio functions) is actually an IO index.

IO indices map to the same GPIO pins on all modules, but they don’t map in any particular order. Instead you’ll have to memorise them all, or keep referring back to this table:

GPIO pin IO index
GPIO1 10
GPIO9 11
GPIO10 12
GPIO12 6
GPIO13 7
GPIO14 5
GPIO15 8
GPIO16 0

Therefore, if you’d like to set GPIO2 to output mode, you use this:

gpio.mode(4, gpio.OUTPUT)

And if you’d like to set GPIO0 to interrupt mode:

gpio.mode(3, gpio.INT)

You could setup global vars:
for k,v in ipairs{3,10,4,9,2,1,nil,nil,nil,11,12,nil,6,7,5,8,0} do _G['GPIO'..k-1]=v end
and use them like this:
gpio.mode(GPIO2, gpio.OUTPUT)
gpio.write(GPIO2, gpio.WRITE)

Using output mode

Next up, let’s set some output pins either low (0v) or high (3.3v). To do that we use gpio.write().

So, to set GPIO0 pin high and GPIO2 pin low:

gpio.mode(3, gpio.OUTPUT)
gpio.write(3, gpio.HIGH)

gpio.mode(4, gpio.OUTUT)
gpio.write(4, gpio.LOW)

Using input mode

Equally we can ask if an input pin is currently set low or high. We do that using

Let’s print the current value of GPIO2:

gpio.mode(4, gpio.INPUT)
local pinValue =

if pinValue == gpio.LOW then
    print 'GPIO2 is low'
    print 'GPIO2 is high'

Using interrupt mode

If we want to know when a pin’s value changes (ie. when a button is pressed) we could set the pin to input mode and keep reading its value. A better solution, however, would be to use interrupt mode. That way we can create a function and set it to be called every time the pin’s value changes.

We define which callbacks are called when by using gpio.trig(). Let’s write to the console when GPIO2 changes value:

local pin = 4    --> GPIO2

function onChange ()
    print('The pin value has changed to '

gpio.mode(pin, gpio.INT)
gpio.trig(pin, 'both', onChange)

The second argument to gpio.trig() is the name of the event to which to listen. As you can see, in the previous example we are listening for the both event; this occurs both when the pin moves from high to low and when it moves low to high.

There are several events on a pin to which you can listen:

Event name Description
up Occurs when the pin moves high.
down Occurs when the pin moves low.
both Occurs when the pin moves high or low.
low Occurs repeatedly while the pin is low.
high Occurs repeatedly while the pin is high.


When using hardware buttons for the first time you may notice something odd: When you press a button down the pin changes value, changes back again and finally settles on the new value once more. This is called contact bounce and it is common.

The simple way to mitigate against this bounce is to ignore events for a short period of time after a change in state has occurred. This is called debouncing.

The following shows how you can debounce the previous example:

local pin = 4    --> GPIO2

function debounce (func)
    local last = 0
    local delay = 200000

    return function (...)
        local now =
        if now - last < delay then return end
        last = now
        return func(...)

function onChange ()
    print('The pin value has changed to '

gpio.mode(pin, gpio.INT)
gpio.trig(pin, 'both', debounce(onChange))