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