ESP8266 Tutorial

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

ESP8266-Pinos.png

Connection using a FTDI232

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

AT Commands

https://github.com/espressif/ESP8266_AT/wiki

Ficheiro:ESP8266ATCommandsSet.pdf


Firmware

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


TOOLS

ESPlorer

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: Ficheiro:ESPlorer.zip Author Site: http://esp8266.ru/esplorer/


Firmware upload with esptool.py

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.

https://github.com/themadinventor/esptool

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:

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

Get Flash ID

$ esptool.py --port /dev/ttyUSB0 flash_id
Connecting...
Manufacturer: e0
Device: 4014

Get Mac address

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

Flash NodeMCU

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

Leaving...

Interesting links

https://github.com/nodemcu

http://www.esp8266.com/

http://blog.filipeflop.com/wireless/esp8266-arduino-tutorial.html


LUA on ESP

LUA 5.1 the complete reference manual http://www.lua.org/manual/5.1/manual.html

Operating commands

Restart de module

> node.restart()  

Execute a script

> dofile("script.lua")

Manage Files

Create a file

> file.open("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

> file.open("hello/world.txt")
> print(file.readline())
Hello World!
 
> file.close()
>

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

> = file.open("foobar.noexist")
nil
> 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
helloworld.txt
>

And finally, the file can be deleted:

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

The GPIO API

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
GPIO0 3
GPIO1 10
GPIO2 4
GPIO3 9
GPIO4 2
GPIO5 1
GPIO6 N/A
GPIO7 N/A
GPIO8 N/A
GPIO9 11
GPIO10 12
GPIO11 N/A
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)


Example
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 gpio.read():

Let’s print the current value of GPIO2:

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

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

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.read(pin))
end

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.

Debouncing

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 = tmr.now()
        if now - last < delay then return end
        last = now
        return func(...)
    end
end 

function onChange ()
    print('The pin value has changed to '..gpio.read(pin))
end

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