|
| 1 | +Introduction to MicroPython on the ESP8266 |
| 2 | +========================================== |
| 3 | + |
| 4 | +Using MicroPython is a great way to get the most of your ESP8266 board. And |
| 5 | +vice versa, the ESP8266 chip is a great platform for using MicroPython. This |
| 6 | +tutorial will guide you through setting up MicroPython, getting a prompt, using |
| 7 | +WebREPL, connecting to the network and communicating with the Internet, using |
| 8 | +the hardware peripherals, and controlling some external components. |
| 9 | + |
| 10 | +Let's get started! |
| 11 | + |
| 12 | +Requirements |
| 13 | +------------ |
| 14 | + |
| 15 | +The first thing you need is a board with an ESP8266 chip. The MicroPython |
| 16 | +software supports the ESP8266 chip itself and any board should work. The main |
| 17 | +characteristic of a board is how much flash it has, how the GPIO pins are |
| 18 | +connected to the outside world, and whether it includes a built-in USB-serial |
| 19 | +convertor to make the UART available to your PC. |
| 20 | + |
| 21 | +The minimum requirement for flash size is 512k. A board with this amount of |
| 22 | +flash will not have room for a filesystem, but otherwise is fully functional. |
| 23 | +If your board has 1Mbyte or more of flash then it will support a filesystem. |
| 24 | + |
| 25 | +Names of pins will be given in this tutorial using the chip names (eg GPIO0) |
| 26 | +and it should be straightforward to find which pin this corresponds to on your |
| 27 | +particular board. |
| 28 | + |
| 29 | +Powering the board |
| 30 | +------------------ |
| 31 | + |
| 32 | +If your board has a USB connector on it then most likely it is powered through |
| 33 | +this when connected to your PC. Otherwise you will need to power it directly. |
| 34 | +Please refer to the documentation for your board for further details. |
| 35 | + |
| 36 | +Deploying the firmware |
| 37 | +---------------------- |
| 38 | + |
| 39 | +The very first thing you need to do is put the MicroPython firmware (compiled |
| 40 | +code) on your ESP8266 device. There are two main steps to do this: first you |
| 41 | +need to put your device in boot-loader mode, and second you need to copy across |
| 42 | +the firmware. The exact procedure for these steps is highly dependent on the |
| 43 | +particular board and you will need to refer to its documentation for details. |
| 44 | + |
| 45 | +If you have a board that has a USB connector, a USB-serial convertor, and has |
| 46 | +the DTR and RTS pins wired in a special way then deploying the firmware should |
| 47 | +be easy as all steps can be done automatically. Boards that have such features |
| 48 | +include the Adafruit Feather HUZZAH and NodeMCU boards. |
| 49 | + |
| 50 | +For best results it is recommended to first erase the entire flash of your |
| 51 | +device before putting on new MicroPython firmware. |
| 52 | + |
| 53 | +Currently we only support esptool.py to copy across the firmware. You can find |
| 54 | +this tool here: `<https://github.com/themadinventor/esptool/>`__ . Any other |
| 55 | +flashing program should work, so feel free to try them out, or refer to the |
| 56 | +documentation for your board to see its recommendations. |
| 57 | + |
| 58 | +Using esptool.py you can erase the flash with the command:: |
| 59 | + |
| 60 | + esptool.py --port /dev/ttyUSB0 erase_flash |
| 61 | + |
| 62 | +And then deploy the new firmware using:: |
| 63 | + |
| 64 | + esptool.py --port /dev/ttyUSB0 --baud 460800 write_flash --flash_size=8m 0 mp-esp8266-firmware.bin |
| 65 | + |
| 66 | +You might need to change the "port" setting to something else relevant for your |
| 67 | +PC. You may also need to reduce the baudrate if you get errors when flashing |
| 68 | +(eg down to 115200). |
| 69 | + |
| 70 | +If you have a NodeMCU board, you may need to use the following command to deploy |
| 71 | +the firmware (note the "-fm dio" option):: |
| 72 | + |
| 73 | + esptool.py --port /dev/ttyUSB0 --baud 460800 write_flash --flash_size=8m -fm dio 0 mp-esp8266-firmware.bin |
| 74 | + |
| 75 | +If the above commands run without error then MicroPython should be installed on |
| 76 | +your board! |
| 77 | + |
| 78 | +Serial prompt |
| 79 | +------------- |
| 80 | + |
| 81 | +Once you have the firmware on the device you can access the REPL (Python prompt) |
| 82 | +over UART0 (GPIO1=TX, GPIO3=RX), which might be connected to a USB-serial |
| 83 | +convertor, depending on your board. The baudrate is 115200. The next part of |
| 84 | +the tutorial will discuss the prompt in more detail. |
| 85 | + |
| 86 | +WiFi |
| 87 | +---- |
| 88 | + |
| 89 | +After a fresh install and boot the device configures itself as a WiFi access |
| 90 | +point (AP) that you can connect to. The ESSID is of the form MicroPython-xxxxxx |
| 91 | +where the x's are replaced with part of the MAC address of your device (so will |
| 92 | +be the same everytime, and most likely different for all ESP8266 chips). The |
| 93 | +password for the WiFi is micropythoN (note the upper-case N). Its IP address |
| 94 | +will be 192.168.4.1 once you connect to its network. WiFi configuration will |
| 95 | +be discussed in more detail later in the tutorial. |
0 commit comments