Difference between revisions of "Micropython"

From ElectroDragon
Jump to navigation Jump to search
(Micropython Bootloader)
Line 11: Line 11:
* press CTRL+C to stop or CTRL+D to run program again.
* press CTRL+C to stop or CTRL+D to run program again.
* get >>> to start.
* get >>> to start.
== SAMD21 ==
* circuitpython - https://github.com/adafruit/circuitpython
* Load tool BOSSA - https://github.com/shumatech/BOSSA
== ESP8266 ==
== ESP8266 ==
* Official github page: https://github.com/micropython/micropython/tree/master/esp8266
* Official github page: https://github.com/micropython/micropython/tree/master/esp8266

Revision as of 12:19, 10 July 2019



  • Must use putty for first time, less easy to get error
  • press CTRL+C to stop or CTRL+D to run program again.
  • get >>> to start.



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).
>>> pin.value()  # Read pin value again, will show 1 when high (connected to 3.3V).
  • Wifi Setup
>>> import esp
>>> # Connect to a WiFi network.
>>> # 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(('', 80))
>>> # Send a request for the wifi test page.
>>> soc.send('GET /testwifi/index.html HTTP/1.0\r\n\r\n')
>>> 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:\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.


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.


Build firmware

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

Compile the firmware

cd ~/micropython/esp8266

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:

and finally make the firmware:

cd ~/micropython/esp8266

Micropython Bootloader

For STM32F405RGT

  • Upload firmware hex by ST-link, no need reset pin
  • Reboot board and connect by USB, it will format itself, two LEDs ON, this takes 3-5 seconds
  • after this, U-disk will show up and sequential LED blink.
  • Now can upload new sketch into it.





  • Only restart or re-power after the files is fully copied, the on board status led indicating file transfer should turn off after it done