MP-ESP8266

From ElectroDragon Wiki
Revision as of 01:30, 22 October 2019 by Chao (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

ESP8266

Demo code

  • GPIO Access
>>> import pyb
>>> pin = pyb.Pin(14, pyb.Pin.OUT_PP)  # Set pin 14 as an output.
>>> for i in range(10):
...    pin.value(0)     # Set pin low (or use pin.low())
...    pyb.delay(1000)  # Delay for 1000ms (1 second)
...    pin.value(1)     # Set pin high (or use pin.high())
...    pyb.delay(1000)
  • Reading GPIO Input
>>> import pyb
>>> pin = pyb.Pin(14, pyb.Pin.IN)
>>> pin.value()  # Read pin value, will show 0 when low (connected to ground).
0
>>> pin.value()  # Read pin value again, will show 1 when high (connected to 3.3V).
1
  • Wifi Setup
>>> import esp
>>> 
>>> # Connect to a WiFi network.
>>> esp.connect('YOUR WIFI SSID NAME', 'YOUR WIFI SSID PASSWORD')
>>> 
>>> # Define function to print data received from socket.
>>> def socket_printer(socket, data):
>>>     print(data)
>>> 
>>> # Create a socket and setup the print function.
>>> soc = esp.socket()
>>> soc.onrecv(socket_printer)
>>> 
>>> # Connect to adafruit.com at port 80.
>>> soc.connect(('207.58.139.247', 80))
>>> 
>>> # Send a request for the wifi test page.
>>> soc.send('GET /testwifi/index.html HTTP/1.0\r\n\r\n')
37
>>> b'HTTP/1.1 200 OK\r\nDate: Tue, 12 May 2015 18:44:49 GMT\r\nServer: Apache\r\nAccess-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Accept-Encoding, Authorization, Referer, User-Agent\r\nAccess-Control-Allow-Methods: GET, POST, OPTIONS\r\nAccess-Control-Allow-Credentials: true\r\nAccess-Control-Max-Age: 1728000\r\nAccept-Ranges: bytes\r\nX-Mod-Pagespeed: 1.9.32.3-4448\r\nVary: Accept-Encoding\r\nCache-Control: max-age=0, no-cache\r\nContent-Length: 74\r\nConnection: close\r\nContent-Type: text/html\r\n\r\nThis is a test of the CC3000 module!\nIf you can read this, its working :)\n'
  • If you're curious what other functions exist on the esp.socket class (or any other object in MicroPython) you can see them by using the dir function:
>>> dir(esp.socket)
['__del__', 'close', 'bind', 'listen', 'accept', 'connect', 'send', 'recv', 'sendto', 'recvfrom', 'onconnect', 'onrecv', 'onsent', 'ondisconnect']
  • Once connected you can start entering MicroPython code in a read-eval-print loop (REPL). For example here's the classic Hello World and counting to 10:
>>> print("Hello world!")
Hello world!
>>> for i in range(1, 11):
...     print("Number {0}".format(i))
... 
Number 1
Number 2
Number 3
Number 4
Number 5
Number 6
Number 7
Number 8
Number 9
Number 10
>>>

Usage of esp class

import esp

Imports the ESP8266 module.

esp.scan(print)

Scans for visible SSIDs. The argument "print" is the callback method receiving tuples which do describe the found SSIDs.

esp.connect('<ssid>', 'pre-shared-key')

Connects to the given network.

esp.disconnect()

Build firmware

  • Follow the ESP8266 Open SDK page to completely setup the environment and compile the SDK first

Compile the firmware

cd ~/micropython/esp8266
make

And the final result should look like below:

LINK build/firmware.elf
   text	   data	    bss	    dec	    hex	filename
 304096	   1332	  53776	 359204	  57b24	build/firmware.elf
Create build/firmware-combined.bin
('flash    ', 52800)
('padding  ', 12736)
('irom0text', 252672)
('total    ', 318208)

And finally copy the firmware out to vagrant folder

cp ./build/firmware-combined.bin /vagrant/

write the blink LED on main.py

turn on the vagrant machine and edit the main.py file:

cd ~/micropython/esp8266/scripts/
nano main.py
# This script is run on boot
import pyb
pin = pyb.Pin(14, pyb.Pin.OUT_PP)
while True:
    pin.value(1)
    pyb.delay(1000)
    pin.value(0)
    pyb.delay(1000)

and finally make the firmware:

cd ~/micropython/esp8266
make

Documents

ESP8266