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