KEMBAR78
Programando o ESP8266 com Python | PDF
Globalcode – Open4education
Trilha – Arduino e Makers
Relsi Maron
Programando o ESP8266 com Python
Globalcode – Open4education
Quem?
- Programador
- 7 Anos no teclado
- 3 Anos num relacionamento sério com Python
- http://github.com/relsi
- http://pt.slideshare.net/relsi
- http://linkedin.com/in/relsi
- http://ikebanacw.com
Globalcode – Open4education
Para quem?
Globalcode – Open4education
Por que Python?
Globalcode – Open4education
Por que Python?
Globalcode – Open4education
Por que Python?
Globalcode – Open4education
Por que Python?
Bonito é melhor que feio.
Explícito é melhor que implícito.
Simples é melhor que complexo.
Complexo é melhor que complicado.
Linear é melhor do que aninhado.
Esparso é melhor que denso.
Legibilidade conta.
Casos especiais não são especiais o bastante para quebrar as regras.
Ainda que praticidade vença a pureza.
Erros nunca devem passar silenciosamente.
A menos que sejam explicitamente silenciados.
Diante da ambigüidade, recuse a tentação de adivinhar.
Deveria haver um — e preferencialmente só um — modo óbvio para fazer algo.
Embora esse modo possa não ser óbvio a princípio a menos que você seja holandês.
Agora é melhor que nunca.
Embora nunca freqüentemente seja melhor que *já*.
Se a implementação é difícil de explicar, é uma má idéia.
Se a implementação é fácil de explicar, pode ser uma boa idéia.
Namespaces são uma grande idéia — vamos ter mais dessas!
Globalcode – Open4education
Por que Python?
Globalcode – Open4education
Por que Python?
Globalcode – Open4education
Por que Python?
- Linguagem de altíssimo nível (VHLL)
- Criada por Guido van Rossum em 1991
- Interpretada e interativa
- Multiplataforma
- Multipropósito
- Muito Foda
Globalcode – Open4education
ESP8266
- 32-bit RISC CPU 80 MHz
- 64 KiB RAM, 96 KiB of data RAM
- External QSPI flash - 512 KiB to 4 MiB
- IEEE 802.11 b/g/n Wi-Fi
- WEP/WPA/WPA2
- 16 GPIO pins
- SPI, I²C,
- UART
Globalcode – Open4education
ESP8266
Globalcode – Open4education
Por que ESP8266?
Globalcode – Open4education
Por que ESP8266?
Globalcode – Open4education
Por que ESP8266?
Globalcode – Open4education
Porque roda Python! =D
Globalcode – Open4education
Micropython
MicroPython is a lean
and efficient implementation
of the Python 3
programming language
that includes a small subset
of the Python standard library
and is optimised to run on microcontrollers
and in constrained environments.
http://www.micropython.org/
Globalcode – Open4education
MicroPython
- STM32F405RG microcontroller
- 168 MHz Cortex M4 CPU
- 1024KiB flash ROM and 192KiB RAM
- Micro USB connector
- Micro SD card slot
- 3-axis accelerometer (MMA7660)
- Real time clock with optional battery backup
- 24 GPIO on left and right edges
- 5 GPIO on bottom row
- 3x 12-bit analog to digital converters
- 2x 12-bit digital to analog (DAC) converters
- 4 LEDs (red, green, yellow and blue)
- 1 reset and 1 user switch
- On-board 3.3V LDO voltage regulator,
capable of supplying up to 250mA,
input voltage range 3.6V to 16V
- DFU bootloader in ROM
Globalcode – Open4education
- array – arrays of numeric data
- Builtin Functions
- gc – control the garbage collector
- math – mathematical functions
- sys – system specific functions
- ubinascii – binary/ASCII conversions
- ucollections – collection and container types
- uhashlib – hashing algorithm
- uheapq – heap queue algorithm
- uio – input/output streams
- ujson – JSON encoding and decoding
- uos – basic “operating system” services
- ure – regular expressions
- usocket – socket module
- ussl – ssl module
- ustruct – pack and unpack primitive data types
- utime – time related functions
- uzlib – zlib decompression
MicroPython
Standard libraries
https://goo.gl/w1Q3Yy
Globalcode – Open4education
- machine — functions related to the board
- micropython – access and control MicroPython internals
- network — network configuration
- uctypes – access binary data in a structured way
- esp — functions related to the ESP8266
MicroPython
Specific libraries
https://goo.gl/w1Q3Yy
Globalcode – Open4education
import machine
machine.freq() # get the current frequency of the CPU
machine.freq(160000000) # set the CPU frequency to 160 MHz
MicroPython
Módulo machine
https://goo.gl/8hCppg
Globalcode – Open4education
from machine import Pin
p0 = Pin(0, Pin.OUT) # create output pin on GPIO0
p0.high() # set pin to high
p0.low() # set pin to low
p0.value(1) # set pin to high
p2 = Pin(2, Pin.IN) # create input pin on GPIO2
print(p2.value()) # get value, 0 or 1
p4 = Pin(4, Pin.IN, Pin.PULL_UP) # enable internal pull-up resistor
p5 = Pin(5, Pin.OUT, value = 1) # set pin high on creation
MicroPython
Módulo machine
https://goo.gl/8hCppg
Globalcode – Open4education
import network
wlan = network.WLAN(network.STA_IF) # create station interface
wlan.active(True) # activate the interface
wlan.scan() # scan for access points
wlan.isconnected() # check if the station is connected to an AP
wlan.connect('essid', 'password') # connect to an AP
wlan.config('mac') # get MAC adddress
wlan.ifconfig() # get the interface's
#IP/netmask/gw/DNS
MicroPython
Módulo network
https://goo.gl/8hCppg
Globalcode – Open4education
import network
ap = network.WLAN(network.AP_IF) # create access-point interface
ap.active(True) # activate the interface
ap.config(essid='ESP-AP') # set the ESSID of the access point
MicroPython
Módulo network
https://goo.gl/8hCppg
Globalcode – Open4education
ESP8266 + MicroPython
Preparando o Terreno
Globalcode – Open4education
ESP8266 + MicroPython
Globalcode – Open4education
ESP8266 + MicroPython
Globalcode – Open4education
ESP8266 + MicroPython
http://pedrominatel.com.br/arduino/utilizando-o-arduino-para-programar-o-esp/
Globalcode – Open4education
ESP8266 + MicroPython
https://goo.gl/FtgaJ7
Globalcode – Open4education
ESP8266 + MicroPython
Gravando o firmware
Globalcode – Open4education
ESP8266 + MicroPython
Gravando o firmware
Verificar a porta do dispositivo
$ lsusb
Bus 001 Device 006: ID 10c4:ea60 Cygnal Integrated Products, Inc. CP210x UART...
$ dmesg | grep USB
usb 1-1: cp210x converter now attached to ttyUSB0
Instalar o esptool
$ pip install esptool
Ou
$ git clone https://github.com/themadinventor/esptool.git
Python 2.7
Globalcode – Open4education
ESP8266 + MicroPython
Gravando o firmware
Apagar o firmware atual
$ esptool.py --port /dev/ttyUSB0 erase_flash
esptool.py v1.1
Connecting...
Erasing flash (this may take a while)...
$ esptool.py --port /dev/ttyUSB0 --baud 115200 write_flash --flash_size=8m -fm
dio 0 esp8266-20160909-v1.8.4.bin
Connecting...
Running Cesanta flasher stub...
Flash params set to 0x0220
Writing 565248 @ 0x0... 565248 (100 %)
Wrote 565248 bytes at 0x0 in 12.7 seconds (357.1 kbit/s)...
Leaving...
Globalcode – Open4education
ESP8266 + MicroPython
Acessando
Globalcode – Open4education
https://github.com/micropython/webrepl
ESP8266 + MicroPython
Acessando
Globalcode – Open4education
ESP8266 + MicroPython
Acessando
Globalcode – Open4education
ESP8266 + MicroPython
Acessando
Globalcode – Open4education
ESP8266 + MicroPython
Acessando
http://esp8266.ru/esplorer/
Globalcode – Open4education
ESP8266 + MicroPython
Hello World
def blink():
import time
import machine
pin = machine.Pin(5, machine.Pin.OUT)
while True:
pin.high()
time.sleep(1)
pin.low()
time.sleep(1)
Globalcode – Open4education
ESP8266 + MicroPython
Controlando
def lampada(estado):
import machine
pin = machine.Pin(5, machine.Pin.OUT)
if estado == 1:
pin.high()
elif estado == 0:
pin.low()
Globalcode – Open4education
ESP8266 + MicroPython
Monitorando
def medida(tipo):
import dht
import machine
d = dht.DHT11(machine.Pin(4))
d.measure()
if tipo == 't':
r = d.temperature()
print(str(r) + ' °C')
elif tipo == 'h':
r = d.humidity()
print(str(r) + ' %RH')
Globalcode – Open4education
Ajuda
Referência
Tutorial
https://goo.gl/LVKXn9
https://goo.gl/Fw9wPD
Biblioteca
https://goo.gl/9s6DS8
Fórum
http://forum.micropython.org/
Globalcode – Open4education
Perguntas?
Obrigado pela atenção! :)
- http://github.com/relsi
- http://pt.slideshare.net/relsi
- http://linkedin.com/in/relsi
- http://ikebanacw.com

Programando o ESP8266 com Python