Programando o ESP8266 com Python

42
Globalcode – Open4education Trilha – Arduino e Makers Relsi Maron Programando o ESP8266 com Python

Transcript of Programando o ESP8266 com Python

Page 1: Programando o ESP8266 com Python

Globalcode – Open4education

Trilha – Arduino e MakersRelsi Maron

Programando o ESP8266 com Python

Page 2: 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

Page 3: Programando o ESP8266 com Python

Globalcode – Open4education

Para quem?

Page 4: Programando o ESP8266 com Python

Globalcode – Open4education

Por que Python?

Page 5: Programando o ESP8266 com Python

Globalcode – Open4education

Por que Python?

Page 6: Programando o ESP8266 com Python

Globalcode – Open4education

Por que Python?

Page 7: Programando o ESP8266 com 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!

Page 8: Programando o ESP8266 com Python

Globalcode – Open4education

Por que Python?

Page 9: Programando o ESP8266 com Python

Globalcode – Open4education

Por que Python?

Page 10: Programando o ESP8266 com 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

Page 11: Programando o ESP8266 com Python

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

Page 12: Programando o ESP8266 com Python

Globalcode – Open4education

ESP8266

Page 13: Programando o ESP8266 com Python

Globalcode – Open4education

Por que ESP8266?

Page 14: Programando o ESP8266 com Python

Globalcode – Open4education

Por que ESP8266?

Page 15: Programando o ESP8266 com Python

Globalcode – Open4education

Por que ESP8266?

Page 16: Programando o ESP8266 com Python

Globalcode – Open4education

Porque roda Python! =D

Page 17: Programando o ESP8266 com Python

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/

Page 18: Programando o ESP8266 com Python

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

Page 19: Programando o ESP8266 com Python

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

MicroPythonStandard libraries

https://goo.gl/w1Q3Yy

Page 20: Programando o ESP8266 com Python

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

MicroPythonSpecific libraries

https://goo.gl/w1Q3Yy

Page 21: Programando o ESP8266 com Python

Globalcode – Open4education

import machine

machine.freq() # get the current frequency of the CPUmachine.freq(160000000) # set the CPU frequency to 160 MHz

MicroPythonMódulo machine

https://goo.gl/8hCppg

Page 22: Programando o ESP8266 com Python

Globalcode – Open4education

from machine import Pin

p0 = Pin(0, Pin.OUT) # create output pin on GPIO0p0.high() # set pin to highp0.low() # set pin to lowp0.value(1) # set pin to high

p2 = Pin(2, Pin.IN) # create input pin on GPIO2print(p2.value()) # get value, 0 or 1

p4 = Pin(4, Pin.IN, Pin.PULL_UP) # enable internal pull-up resistorp5 = Pin(5, Pin.OUT, value = 1) # set pin high on creation

MicroPythonMódulo machine

https://goo.gl/8hCppg

Page 23: Programando o ESP8266 com Python

Globalcode – Open4education

import network

wlan = network.WLAN(network.STA_IF) # create station interfacewlan.active(True) # activate the interfacewlan.scan() # scan for access pointswlan.isconnected() # check if the station is connected to an APwlan.connect('essid', 'password') # connect to an APwlan.config('mac') # get MAC adddresswlan.ifconfig() # get the interface's #IP/netmask/gw/DNS

MicroPythonMódulo network

https://goo.gl/8hCppg

Page 24: Programando o ESP8266 com Python

Globalcode – Open4education

import network

ap = network.WLAN(network.AP_IF) # create access-point interfaceap.active(True) # activate the interfaceap.config(essid='ESP-AP') # set the ESSID of the access point

MicroPythonMódulo network

https://goo.gl/8hCppg

Page 25: Programando o ESP8266 com Python

Globalcode – Open4education

ESP8266 + MicroPythonPreparando o Terreno

Page 26: Programando o ESP8266 com Python

Globalcode – Open4education

ESP8266 + MicroPython

Page 27: Programando o ESP8266 com Python

Globalcode – Open4education

ESP8266 + MicroPython

Page 28: Programando o ESP8266 com Python

Globalcode – Open4education

ESP8266 + MicroPython

http://pedrominatel.com.br/arduino/utilizando-o-arduino-para-programar-o-esp/

Page 29: Programando o ESP8266 com Python

Globalcode – Open4education

ESP8266 + MicroPython

https://goo.gl/FtgaJ7

Page 30: Programando o ESP8266 com Python

Globalcode – Open4education

ESP8266 + MicroPythonGravando o firmware

Page 31: Programando o ESP8266 com Python

Globalcode – Open4education

ESP8266 + MicroPythonGravando o firmwareVerificar a porta do dispositivo

$ lsusbBus 001 Device 006: ID 10c4:ea60 Cygnal Integrated Products, Inc. CP210x UART...

$ dmesg | grep USBusb 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

Page 32: Programando o ESP8266 com Python

Globalcode – Open4education

ESP8266 + MicroPythonGravando o firmwareApagar o firmware atual

$ esptool.py --port /dev/ttyUSB0 erase_flash

esptool.py v1.1Connecting...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 0x0220Writing 565248 @ 0x0... 565248 (100 %)Wrote 565248 bytes at 0x0 in 12.7 seconds (357.1 kbit/s)...Leaving...

Page 33: Programando o ESP8266 com Python

Globalcode – Open4education

ESP8266 + MicroPythonAcessando

Page 34: Programando o ESP8266 com Python

Globalcode – Open4education

https://github.com/micropython/webrepl

ESP8266 + MicroPythonAcessando

Page 35: Programando o ESP8266 com Python

Globalcode – Open4education

ESP8266 + MicroPythonAcessando

Page 36: Programando o ESP8266 com Python

Globalcode – Open4education

ESP8266 + MicroPythonAcessando

Page 37: Programando o ESP8266 com Python

Globalcode – Open4education

ESP8266 + MicroPythonAcessando

http://esp8266.ru/esplorer/

Page 38: Programando o ESP8266 com Python

Globalcode – Open4education

ESP8266 + MicroPythonHello 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)

Page 39: Programando o ESP8266 com Python

Globalcode – Open4education

ESP8266 + MicroPythonControlando

def lampada(estado): import machine pin = machine.Pin(5, machine.Pin.OUT) if estado == 1: pin.high() elif estado == 0: pin.low()

Page 40: Programando o ESP8266 com Python

Globalcode – Open4education

ESP8266 + MicroPythonMonitorando

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')

Page 41: Programando o ESP8266 com Python

Globalcode – Open4education

Ajuda

Referência

Tutorialhttps://goo.gl/LVKXn9

https://goo.gl/Fw9wPD

Bibliotecahttps://goo.gl/9s6DS8

Fórumhttp://forum.micropython.org/

Page 42: Programando o ESP8266 com Python

Globalcode – Open4education

Perguntas?

Obrigado pela atenção! :)

- http://github.com/relsi- http://pt.slideshare.net/relsi- http://linkedin.com/in/relsi- http://ikebanacw.com