diff options
Diffstat (limited to 'content/posts/2020-09-06-esp-and-micropython.md')
| -rw-r--r-- | content/posts/2020-09-06-esp-and-micropython.md | 68 |
1 files changed, 47 insertions, 21 deletions
diff --git a/content/posts/2020-09-06-esp-and-micropython.md b/content/posts/2020-09-06-esp-and-micropython.md index 481be0a..ac79a6d 100644 --- a/content/posts/2020-09-06-esp-and-micropython.md +++ b/content/posts/2020-09-06-esp-and-micropython.md | |||
| @@ -1,34 +1,45 @@ | |||
| 1 | --- | 1 | --- |
| 2 | title: Getting started with MicroPython and ESP8266 | 2 | title: Getting started with MicroPython and ESP8266 |
| 3 | url: esp8266-and-micropython-guide.html | 3 | url: esp8266-and-micropython-guide.html |
| 4 | date: 2020-09-06 | 4 | date: 2020-09-06T12:00:00+02:00 |
| 5 | draft: false | 5 | draft: false |
| 6 | --- | 6 | --- |
| 7 | 7 | ||
| 8 | ## Introduction | 8 | ## Introduction |
| 9 | 9 | ||
| 10 | 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. | 10 | A while ago I bought some [ESP8266](https://www.espressif.com/en/products/socs/esp8266) |
| 11 | and [ESP32](https://www.espressif.com/en/products/socs/esp32) dev boards to | ||
| 12 | play around with and I finally found a project to try it out. | ||
| 11 | 13 | ||
| 12 | 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). | 14 | For my project, I used [ESP32](https://www.espressif.com/en/products/socs/esp32) |
| 15 | but I could easily choose [ESP8266](https://www.espressif.com/en/products/socs/esp8266). | ||
| 16 | This guide contains which tools I use and how I prepared my workspace to code | ||
| 17 | for [ESP8266](https://www.espressif.com/en/products/socs/esp8266). | ||
| 13 | 18 | ||
| 14 |  | 19 |  |
| 15 | 20 | ||
| 16 | This guide covers: | 21 | This guide covers: |
| 22 | |||
| 17 | - flashing SOC | 23 | - flashing SOC |
| 18 | - install proper tooling | 24 | - install proper tooling |
| 19 | - deploying a simple script | 25 | - deploying a simple script |
| 20 | 26 | ||
| 21 | > 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. | 27 | > Make sure that you are using **a good USB cable**. I had some problems with |
| 28 | mine and once I replaced it everything started to work. | ||
| 22 | 29 | ||
| 23 | ## Flashing the SOC | 30 | ## Flashing the SOC |
| 24 | 31 | ||
| 25 | Plug your ESP8266 to USB port and check if the device was recognized with executing `dmesg | grep ch341-uart`. | 32 | Plug your ESP8266 to USB port and check if the device was recognized with |
| 33 | executing `dmesg | grep ch341-uart`. | ||
| 26 | 34 | ||
| 27 | Then check if the device is available under `/dev/` by running `ls /dev/ttyUSB*`. | 35 | Then check if the device is available under `/dev/` by running `ls /dev/ttyUSB*`. |
| 28 | 36 | ||
| 29 | > **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`. | 37 | > **Linux users**: if a device is not available be sure you are in `dialout` |
| 38 | > group. You can check this by executing `groups $USER`. You can add a user to | ||
| 39 | > `dialout` group with `sudo adduser $USER dialout`. | ||
| 30 | 40 | ||
| 31 | 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`. | 41 | After these conditions are meet go to the navigate to [https://micropython.org/download/esp8266/](https://micropython.org/download/esp8266/) |
| 42 | and download `esp8266-20200902-v1.13.bin`. | ||
| 32 | 43 | ||
| 33 | ```sh | 44 | ```sh |
| 34 | mkdir esp8266-test | 45 | mkdir esp8266-test |
| @@ -37,7 +48,8 @@ cd esp8266-test | |||
| 37 | wget https://micropython.org/resources/firmware/esp8266-20200902-v1.13.bin | 48 | wget https://micropython.org/resources/firmware/esp8266-20200902-v1.13.bin |
| 38 | ``` | 49 | ``` |
| 39 | 50 | ||
| 40 | After obtaining firmware we will need some tooling to flash the firmware to the board. | 51 | After obtaining firmware we will need some tooling to flash the firmware to |
| 52 | the board. | ||
| 41 | 53 | ||
| 42 | ```sh | 54 | ```sh |
| 43 | sudo pip3 install esptool | 55 | sudo pip3 install esptool |
| @@ -45,7 +57,8 @@ sudo pip3 install esptool | |||
| 45 | 57 | ||
| 46 | You can read more about `esptool` at [https://github.com/espressif/esptool/](https://github.com/espressif/esptool/). | 58 | You can read more about `esptool` at [https://github.com/espressif/esptool/](https://github.com/espressif/esptool/). |
| 47 | 59 | ||
| 48 | Before flashing the firmware we need to erase the flash on device. Substitute `USB0` with the device listed in output of `ls /dev/ttyUSB*`. | 60 | Before flashing the firmware we need to erase the flash on device. Substitute |
| 61 | `USB0` with the device listed in output of `ls /dev/ttyUSB*`. | ||
| 49 | 62 | ||
| 50 | ```sh | 63 | ```sh |
| 51 | esptool.py --port /dev/ttyUSB0 erase_flash | 64 | esptool.py --port /dev/ttyUSB0 erase_flash |
| @@ -57,9 +70,11 @@ If flash was successfully erased it is now time to flash the new firmware to it. | |||
| 57 | esptool.py --port /dev/ttyUSB0 --baud 460800 write_flash --flash_size=detect 0 esp8266-20200902-v1.13.bin | 70 | esptool.py --port /dev/ttyUSB0 --baud 460800 write_flash --flash_size=detect 0 esp8266-20200902-v1.13.bin |
| 58 | ``` | 71 | ``` |
| 59 | 72 | ||
| 60 | If everything went ok you can try accessing MicroPython REPL with `screen /dev/ttyUSB0 115200` or `picocom /dev/ttyUSB0 -b115200`. | 73 | If everything went ok you can try accessing MicroPython REPL with ` |
| 74 | screen /dev/ttyUSB0 115200` or `picocom /dev/ttyUSB0 -b115200`. | ||
| 61 | 75 | ||
| 62 | > Sometimes you will need to press `ENTER` in `screen` or `picocom` to access REPL. | 76 | > Sometimes you will need to press `ENTER` in `screen` or `picocom` to access |
| 77 | > REPL. | ||
| 63 | 78 | ||
| 64 | When you are in REPL you can test if all is working properly following steps. | 79 | When you are in REPL you can test if all is working properly following steps. |
| 65 | 80 | ||
| @@ -68,7 +83,8 @@ When you are in REPL you can test if all is working properly following steps. | |||
| 68 | > machine.freq() | 83 | > machine.freq() |
| 69 | ``` | 84 | ``` |
| 70 | 85 | ||
| 71 | This should output a number representing a frequency of the CPU (mine was `80000000`). | 86 | This should output a number representing a frequency of the CPU (mine was |
| 87 | `80000000`). | ||
| 72 | 88 | ||
| 73 | When you are in `screen` or `picocom` these can help you a bit. | 89 | When you are in `screen` or `picocom` these can help you a bit. |
| 74 | 90 | ||
| @@ -81,7 +97,8 @@ When you are in `screen` or `picocom` these can help you a bit. | |||
| 81 | 97 | ||
| 82 | ## Install better tooling | 98 | ## Install better tooling |
| 83 | 99 | ||
| 84 | 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. | 100 | Now, to make our lives a little bit easier there are couple of additional tools |
| 101 | that will make this whole experience a little more bearable. | ||
| 85 | 102 | ||
| 86 | There are twq cool ways of uploading local files to SOC flash. | 103 | There are twq cool ways of uploading local files to SOC flash. |
| 87 | 104 | ||
| @@ -113,7 +130,8 @@ ampy --delay 2 --port /dev/ttyUSB0 cat boot.py | |||
| 113 | 130 | ||
| 114 | ### rshell | 131 | ### rshell |
| 115 | 132 | ||
| 116 | Even though `ampy` is a cool tool I opted with `rshell` in the end since it's much more polished and feature rich. | 133 | Even though `ampy` is a cool tool I opted with `rshell` in the end since it's |
| 134 | much more polished and feature rich. | ||
| 117 | 135 | ||
| 118 | ```bash | 136 | ```bash |
| 119 | # installing ampy | 137 | # installing ampy |
| @@ -126,7 +144,9 @@ Now that `rshell` is installed we can connect to the board. | |||
| 126 | rshell --buffer-size=30 -p /dev/ttyUSB0 -a | 144 | rshell --buffer-size=30 -p /dev/ttyUSB0 -a |
| 127 | ``` | 145 | ``` |
| 128 | 146 | ||
| 129 | 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. | 147 | This will open a shell inside bash and from here you can execute multiple |
| 148 | commands. You can check what is supported with `help` once you are inside of | ||
| 149 | a shell. | ||
| 130 | 150 | ||
| 131 | ```bash | 151 | ```bash |
| 132 | m@turing ~/Junk/esp8266-test | 152 | m@turing ~/Junk/esp8266-test |
| @@ -151,11 +171,14 @@ boards cd cp echo exit filetype ls repl rsync | |||
| 151 | Use Control-D (or the exit command) to exit rshell. | 171 | Use Control-D (or the exit command) to exit rshell. |
| 152 | ``` | 172 | ``` |
| 153 | 173 | ||
| 154 | > 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`. | 174 | > Inside a shell `ls` will display list of files on your machine. To get list |
| 175 | > of files on flash folder `/pyboard` is remapped inside the shell. To list files | ||
| 176 | > on flash you must perform `ls /pyboard`. | ||
| 155 | 177 | ||
| 156 | #### Moving files to flash | 178 | #### Moving files to flash |
| 157 | 179 | ||
| 158 | To avoid copying files all the time I used `rsync` function from the inside of `rshell`. | 180 | To avoid copying files all the time I used `rsync` function from the |
| 181 | inside of `rshell`. | ||
| 159 | 182 | ||
| 160 | ```bash | 183 | ```bash |
| 161 | rsync . /pyboard | 184 | rsync . /pyboard |
| @@ -163,9 +186,11 @@ rsync . /pyboard | |||
| 163 | 186 | ||
| 164 | #### Executing scripts | 187 | #### Executing scripts |
| 165 | 188 | ||
| 166 | 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. | 189 | It is a pain to continuously reboot the device to trigger `/pyboard/boot.py` |
| 190 | and there is a better way of testing local scripts on remote device. | ||
| 167 | 191 | ||
| 168 | Lets assume we have `src/freq.py` file that displays CPU frequency of a remote device. | 192 | Lets assume we have `src/freq.py` file that displays CPU frequency of a |
| 193 | remote device. | ||
| 169 | 194 | ||
| 170 | ```py | 195 | ```py |
| 171 | # src/freq.py | 196 | # src/freq.py |
| @@ -191,5 +216,6 @@ repl | |||
| 191 | 216 | ||
| 192 | ## Additional resources | 217 | ## Additional resources |
| 193 | 218 | ||
| 194 | - [https://randomnerdtutorials.com/getting-started-micropython-esp32-esp8266/](https://randomnerdtutorials.com/getting-started-micropython-esp32-esp8266/) | 219 | - https://randomnerdtutorials.com/getting-started-micropython-esp32-esp8266/ |
| 195 | - [http://docs.micropython.org/en/latest/esp8266/quickref.html](http://docs.micropython.org/en/latest/esp8266/quickref.html) | 220 | - http://docs.micropython.org/en/latest/esp8266/quickref.html |
| 221 | |||
