diff options
Diffstat (limited to '_posts/2020-09-06-esp-and-micropython.md')
| -rw-r--r-- | _posts/2020-09-06-esp-and-micropython.md | 226 |
1 files changed, 226 insertions, 0 deletions
diff --git a/_posts/2020-09-06-esp-and-micropython.md b/_posts/2020-09-06-esp-and-micropython.md new file mode 100644 index 0000000..9a005af --- /dev/null +++ b/_posts/2020-09-06-esp-and-micropython.md | |||
| @@ -0,0 +1,226 @@ | |||
| 1 | --- | ||
| 2 | title: Getting started with MicroPython and ESP8266 | ||
| 3 | permalink: /esp8266-and-micropython-guide.html | ||
| 4 | date: 2020-09-06T12:00:00+02:00 | ||
| 5 | layout: post | ||
| 6 | type: post | ||
| 7 | draft: false | ||
| 8 | --- | ||
| 9 | |||
| 10 | ## Introduction | ||
| 11 | |||
| 12 | A while ago I bought some | ||
| 13 | [ESP8266](https://www.espressif.com/en/products/socs/esp8266) and | ||
| 14 | [ESP32](https://www.espressif.com/en/products/socs/esp32) dev boards to play | ||
| 15 | around with and I finally found a project to try it out. | ||
| 16 | |||
| 17 | For my project, I used [ESP32](https://www.espressif.com/en/products/socs/esp32) | ||
| 18 | but I could easily choose | ||
| 19 | [ESP8266](https://www.espressif.com/en/products/socs/esp8266). This guide | ||
| 20 | contains which tools I use and how I prepared my workspace to code for | ||
| 21 | [ESP8266](https://www.espressif.com/en/products/socs/esp8266). | ||
| 22 | |||
| 23 |  | ||
| 24 | |||
| 25 | This guide covers: | ||
| 26 | |||
| 27 | - flashing SOC | ||
| 28 | - install proper tooling | ||
| 29 | - deploying a simple script | ||
| 30 | |||
| 31 | > Make sure that you are using **a good USB cable**. I had some problems with | ||
| 32 | mine and once I replaced it everything started to work. | ||
| 33 | |||
| 34 | ## Flashing the SOC | ||
| 35 | |||
| 36 | Plug your ESP8266 to USB port and check if the device was recognized with | ||
| 37 | executing `dmesg | grep ch341-uart`. | ||
| 38 | |||
| 39 | Then check if the device is available under `/dev/` by running `ls | ||
| 40 | /dev/ttyUSB*`. | ||
| 41 | |||
| 42 | > **Linux users**: if a device is not available be sure you are in `dialout` | ||
| 43 | > group. You can check this by executing `groups $USER`. You can add a user to | ||
| 44 | > `dialout` group with `sudo adduser $USER dialout`. | ||
| 45 | |||
| 46 | After these conditions are meet go to the navigate to | ||
| 47 | [https://micropython.org/download/esp8266/](https://micropython.org/download/esp8266/) | ||
| 48 | and download `esp8266-20200902-v1.13.bin`. | ||
| 49 | |||
| 50 | ```sh | ||
| 51 | mkdir esp8266-test | ||
| 52 | cd esp8266-test | ||
| 53 | |||
| 54 | wget https://micropython.org/resources/firmware/esp8266-20200902-v1.13.bin | ||
| 55 | ``` | ||
| 56 | |||
| 57 | After obtaining firmware we will need some tooling to flash the firmware to the | ||
| 58 | board. | ||
| 59 | |||
| 60 | ```sh | ||
| 61 | sudo pip3 install esptool | ||
| 62 | ``` | ||
| 63 | |||
| 64 | You can read more about `esptool` at | ||
| 65 | [https://github.com/espressif/esptool/](https://github.com/espressif/esptool/). | ||
| 66 | |||
| 67 | Before flashing the firmware we need to erase the flash on device. Substitute | ||
| 68 | `USB0` with the device listed in output of `ls /dev/ttyUSB*`. | ||
| 69 | |||
| 70 | ```sh | ||
| 71 | esptool.py --port /dev/ttyUSB0 erase_flash | ||
| 72 | ``` | ||
| 73 | |||
| 74 | If flash was successfully erased it is now time to flash the new firmware to it. | ||
| 75 | |||
| 76 | ```sh | ||
| 77 | esptool.py --port /dev/ttyUSB0 --baud 460800 write_flash --flash_size=detect 0 esp8266-20200902-v1.13.bin | ||
| 78 | ``` | ||
| 79 | |||
| 80 | If everything went ok you can try accessing MicroPython REPL with ` screen | ||
| 81 | /dev/ttyUSB0 115200` or `picocom /dev/ttyUSB0 -b115200`. | ||
| 82 | |||
| 83 | > Sometimes you will need to press `ENTER` in `screen` or `picocom` to access | ||
| 84 | > REPL. | ||
| 85 | |||
| 86 | When you are in REPL you can test if all is working properly following steps. | ||
| 87 | |||
| 88 | ```py | ||
| 89 | > import machine | ||
| 90 | > machine.freq() | ||
| 91 | ``` | ||
| 92 | |||
| 93 | This should output a number representing a frequency of the CPU (mine was | ||
| 94 | `80000000`). | ||
| 95 | |||
| 96 | When you are in `screen` or `picocom` these can help you a bit. | ||
| 97 | |||
| 98 | | Key | Command | | ||
| 99 | | -------- | -------------------- | | ||
| 100 | | CTRL+d | preforms soft reboot | | ||
| 101 | | CTRL+a x | exits picocom | | ||
| 102 | | CTRL+a \ | exits screen | | ||
| 103 | |||
| 104 | |||
| 105 | ## Install better tooling | ||
| 106 | |||
| 107 | Now, to make our lives a little bit easier there are couple of additional tools | ||
| 108 | that will make this whole experience a little more bearable. | ||
| 109 | |||
| 110 | There are twq cool ways of uploading local files to SOC flash. | ||
| 111 | |||
| 112 | - ampy → [https://github.com/scientifichackers/ampy](https://github.com/scientifichackers/ampy) | ||
| 113 | - rshell → [https://github.com/dhylands/rshell](https://github.com/dhylands/rshell) | ||
| 114 | |||
| 115 | ### ampy | ||
| 116 | |||
| 117 | ```bash | ||
| 118 | # installing ampy | ||
| 119 | sudo pip3 install adafruit-ampy | ||
| 120 | ``` | ||
| 121 | |||
| 122 | Listed below are some common commands I used. | ||
| 123 | |||
| 124 | ```bash | ||
| 125 | # uploads file to flash | ||
| 126 | ampy --delay 2 --port /dev/ttyUSB0 put boot.py | ||
| 127 | |||
| 128 | # lists file on flash | ||
| 129 | ampy --delay 2 --port /dev/ttyUSB0 ls | ||
| 130 | |||
| 131 | # outputs contents of file on flash | ||
| 132 | ampy --delay 2 --port /dev/ttyUSB0 cat boot.py | ||
| 133 | ``` | ||
| 134 | |||
| 135 | > I added `delay` of 2 seconds because I had problems with executing commands. | ||
| 136 | |||
| 137 | ### rshell | ||
| 138 | |||
| 139 | Even though `ampy` is a cool tool I opted with `rshell` in the end since it's | ||
| 140 | much more polished and feature rich. | ||
| 141 | |||
| 142 | ```bash | ||
| 143 | # installing ampy | ||
| 144 | sudo pip3 install rshell | ||
| 145 | ``` | ||
| 146 | |||
| 147 | Now that `rshell` is installed we can connect to the board. | ||
| 148 | |||
| 149 | ```bash | ||
| 150 | rshell --buffer-size=30 -p /dev/ttyUSB0 -a | ||
| 151 | ``` | ||
| 152 | |||
| 153 | This will open a shell inside bash and from here you can execute multiple | ||
| 154 | commands. You can check what is supported with `help` once you are inside of a | ||
| 155 | shell. | ||
| 156 | |||
| 157 | ```bash | ||
| 158 | m@turing ~/Junk/esp8266-test | ||
| 159 | $ rshell --buffer-size=30 -p /dev/ttyUSB0 -a | ||
| 160 | |||
| 161 | Using buffer-size of 30 | ||
| 162 | Connecting to /dev/ttyUSB0 (buffer-size 30)... | ||
| 163 | Trying to connect to REPL connected | ||
| 164 | Testing if ubinascii.unhexlify exists ... Y | ||
| 165 | Retrieving root directories ... /boot.py/ | ||
| 166 | Setting time ... Sep 06, 2020 23:54:28 | ||
| 167 | Evaluating board_name ... pyboard | ||
| 168 | Retrieving time epoch ... Jan 01, 2000 | ||
| 169 | Welcome to rshell. Use Control-D (or the exit command) to exit rshell. | ||
| 170 | /home/m/Junk/esp8266-test> help | ||
| 171 | |||
| 172 | Documented commands (type help <topic>): | ||
| 173 | ======================================== | ||
| 174 | args cat connect date edit filesize help mkdir rm shell | ||
| 175 | boards cd cp echo exit filetype ls repl rsync | ||
| 176 | |||
| 177 | Use Control-D (or the exit command) to exit rshell. | ||
| 178 | ``` | ||
| 179 | |||
| 180 | > Inside a shell `ls` will display list of files on your machine. To get list | ||
| 181 | > of files on flash folder `/pyboard` is remapped inside the shell. To list files | ||
| 182 | > on flash you must perform `ls /pyboard`. | ||
| 183 | |||
| 184 | #### Moving files to flash | ||
| 185 | |||
| 186 | To avoid copying files all the time I used `rsync` function from the inside of | ||
| 187 | `rshell`. | ||
| 188 | |||
| 189 | ```bash | ||
| 190 | rsync . /pyboard | ||
| 191 | ``` | ||
| 192 | |||
| 193 | #### Executing scripts | ||
| 194 | |||
| 195 | It is a pain to continuously reboot the device to trigger `/pyboard/boot.py` and | ||
| 196 | there is a better way of testing local scripts on remote device. | ||
| 197 | |||
| 198 | Lets assume we have `src/freq.py` file that displays CPU frequency of a remote | ||
| 199 | device. | ||
| 200 | |||
| 201 | ```py | ||
| 202 | # src/freq.py | ||
| 203 | |||
| 204 | import machine | ||
| 205 | print(machine.freq()) | ||
| 206 | ``` | ||
| 207 | |||
| 208 | Now lets upload this and execute it. | ||
| 209 | |||
| 210 | ```bash | ||
| 211 | # syncs files to remove device | ||
| 212 | rsync ./src /pyboard | ||
| 213 | |||
| 214 | # goes into REPL | ||
| 215 | repl | ||
| 216 | |||
| 217 | # we import file by importing it without .py extension and this will run the script | ||
| 218 | > import freq | ||
| 219 | |||
| 220 | # CTRL+x will exit REPL | ||
| 221 | ``` | ||
| 222 | |||
| 223 | ## Additional resources | ||
| 224 | |||
| 225 | - https://randomnerdtutorials.com/getting-started-micropython-esp32-esp8266/ | ||
| 226 | - http://docs.micropython.org/en/latest/esp8266/quickref.html | ||
