|
| 1 | +# [ESP8266 Micropython][esp8266] |
| 2 | + |
| 3 | +![esp8266][esp8266-banner] |
| 4 | +## Board firmware installation |
| 5 | + |
| 6 | + - download latest micropython [esp8266 firmware][micropython-download] for your board |
| 7 | + - on your host system install python3 |
| 8 | + - install **[esptool][micropython-esptool]**, **[rshell][micropython-rshell]**, **[ampy][micropython-ampy]** on your host to communicate with esp32 board |
| 9 | + ```bash |
| 10 | + # Example on Linux |
| 11 | + sudo pip3 install esptool |
| 12 | + sudo pip3 install rshell |
| 13 | + sudo pip3 install adafruit-ampy |
| 14 | + ``` |
| 15 | + |
| 16 | + - connect board via USB. Run command: |
| 17 | + ```bash |
| 18 | + dmesg | grep tty |
| 19 | + ``` |
| 20 | + that will help to find ttyUSB connection port.This port will be used later for all communication operations |
| 21 | + |
| 22 | + - check board flash status. (In this example and below we assume that port=ttyUSB0) |
| 23 | + ```bash |
| 24 | + esptool.py --port /dev/ttyUSB0 flash_id |
| 25 | + ``` |
| 26 | + |
| 27 | + - erase board flash before new firmware uploading |
| 28 | + ```bash |
| 29 | + esptool.py --chip esp32 --port /dev/ttyUSB0 erase_flash |
| 30 | + ``` |
| 31 | + |
| 32 | + - burn new firmware |
| 33 | + ```bash |
| 34 | + esptool.py --chip esp32 --port /dev/ttyUSB0 write_flash -z 0x1000 [your esp32 firmware .bin] |
| 35 | + ``` |
| 36 | + |
| 37 | +## Board CLI |
| 38 | + |
| 39 | + - For board CLI access [rshell][micropython-rshell] can be used: |
| 40 | + ```bash |
| 41 | + rshell --buffer-size=30 -p /dev/ttyUSB0 |
| 42 | + ``` |
| 43 | + after board prompt appears ">" you will have access to some board commands: |
| 44 | + ```text |
| 45 | + args cat connect echo exit filetype ls repl rsync |
| 46 | + boards cd cp edit filesize help mkdir rm shell |
| 47 | + ``` |
| 48 | + - **[repl][micropython-repl]** command will start micropython interactive shell. |
| 49 | + |
| 50 | + Also this shell can be used for board soft reboots(Ctrl+D). |
| 51 | + |
| 52 | + Hard reboots can be done by board "RST" button. |
| 53 | + |
| 54 | + |
| 55 | +## Board files/directories operations |
| 56 | + |
| 57 | + - **[ampy][micropython-ampy]** utility can be used for storing/deleting files, |
| 58 | + directories creation/removing and scripts run |
| 59 | + ```bash |
| 60 | + export AMPY_PORT=/dev/ttyUSB0 |
| 61 | + ampy mkdir /lib |
| 62 | + ampy put blynklib.py /lib/blynklib.py |
| 63 | + ampy put test.py test.py |
| 64 | + ampy run test.py |
| 65 | + ``` |
| 66 | + |
| 67 | + |
| 68 | +## Libraries importing under ESP8266 Micropython |
| 69 | + |
| 70 | +The firmware including the MicroPython subsystem is stored in the onboard flash. |
| 71 | +The remaining capacity is available for use. For reasons connected with the physical architecture |
| 72 | +of the flash memory part of this capacity may be inaccessible as a filesystem. In such cases this space |
| 73 | +may be employed by incorporating user modules into a firmware build which is then flashed to the device. |
| 74 | + |
| 75 | +There are two ways to achieve this: |
| 76 | + - frozen modules |
| 77 | + - frozen bytecode. |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | +### Frozen module inside firmware |
| 82 | +Frozen modules store the Python source with the firmware. |
| 83 | + |
| 84 | +For custom esp8266 firmware build creation: |
| 85 | + - install docker to your host system. How to do it under different OS you may read [here][docker-install] |
| 86 | + - clone with git esp8266 docker repository. Virtual environment will be needed for firmware build operation. |
| 87 | + ```bash |
| 88 | + git clone https://github.com/enqack/docker-esp8266-micropython.git |
| 89 | + cd ./docker-esp8266-micropython |
| 90 | + ``` |
| 91 | + - place your library module to **docker-esp8266-micropython** root |
| 92 | + - modify Dockerfile. You need place your library module to esp8266 frozen modules directory. |
| 93 | + Thus **Copy** instruction in Dockerfile should be placed after **'RUN apt-get update'** and before **'USER micropython'** |
| 94 | + ```text |
| 95 | + RUN apt-get update ... |
| 96 | + ... |
| 97 | + COPY blynklib.py /micropython/ports/esp8266/modules/blynklib.py |
| 98 | + USER micropython |
| 99 | + ... |
| 100 | + ``` |
| 101 | + - follow **[this][esp8266-build-docker]** instructions to build and copy custom esp8266 firmware. |
| 102 | + |
| 103 | + Build process may take some time ~ 15-40 minutes. |
| 104 | + |
| 105 | + - after firmware created and copied locally - you can try to burn it with **esptool** to your ESP8266 board. |
| 106 | + - connect to board CLI with **rshell** and test **blynklib** availability within **repl** |
| 107 | + ```python |
| 108 | + import blynklib |
| 109 | + print(blynklib.LOGO) |
| 110 | + ``` |
| 111 | + |
| 112 | + |
| 113 | +### Frozen module inside firmware |
| 114 | +Frozen bytecode uses the cross compiler to convert the source to bytecode which is then stored with the firmware. |
| 115 | + |
| 116 | +Examine [this document][blynk-esp32-readme] to get more details how to compile *.py files into *.mpy bytecode |
| 117 | + |
| 118 | +***Note!!*** During custom firmware creation your libraries will be converted and adopted to esp8266 environment |
| 119 | +automatically. So you can create custom build and then just copy *.mpy files from docker system |
| 120 | +```bash |
| 121 | +docker cp micropython:/micropython/ports/esp8266/build/frozen_mpy/blinklib.mpy blinklib.mpy |
| 122 | +``` |
| 123 | + |
| 124 | +After *.mpy files can be placed to **/lib** directory of esp8266 board with **ampy** tool and simply imported |
| 125 | +as standard library |
| 126 | + |
| 127 | + |
| 128 | +## Wifi Connection |
| 129 | +Micropython allows to use core ***network*** module for WiFi connection setup. |
| 130 | + |
| 131 | +In script just place: |
| 132 | +```python |
| 133 | +import network |
| 134 | +
|
| 135 | +WIFI_SSID = 'YourWifiSSID' |
| 136 | +WIFI_PASS = 'YourWifiPassword' |
| 137 | +
|
| 138 | +wifi = network.WLAN(network.STA_IF) |
| 139 | +wifi.active(True) |
| 140 | +wifi.connect(WIFI_SSID, WIFI_PASS) |
| 141 | +
|
| 142 | +# check if board connected |
| 143 | +connect_status = wifi.isconnected() |
| 144 | +``` |
| 145 | + |
| 146 | + |
| 147 | + [esp8266]: https://en.wikipedia.org/wiki/ESP8266 |
| 148 | + [esp8266-banner]: http://arduino.ua/products_pictures/large_AOC400-1.jpg |
| 149 | + [micropython-download]: http://micropython.org/download#esp8266 |
| 150 | + [micropython-repl]: https://docs.micropython.org/en/latest/esp8266/tutorial/repl.html |
| 151 | + [micropython-ampy]: https://github.com/pycampers/ampy |
| 152 | + [micropython-rshell]: https://github.com/dhylands/rshell |
| 153 | + [micropython-esptool]: https://github.com/espressif/esptool |
| 154 | + [micropython-mpy-cross]: https://pypi.org/project/mpy-cross/ |
| 155 | + [esp8266-build-docker]: https://github.com/enqack/docker-esp8266-micropython |
| 156 | + [docker-install]: https://docs.docker.com/install/linux/docker-ce/ubuntu/ |
| 157 | + [blynk-esp32-readme]: https://github.com/blynkkk/lib-python/blob/master/examples/esp32/README.md |
0 commit comments