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