aboutsummaryrefslogtreecommitdiff
path: root/content/posts/2020-09-06-esp-and-micropython.md
diff options
context:
space:
mode:
Diffstat (limited to 'content/posts/2020-09-06-esp-and-micropython.md')
-rw-r--r--content/posts/2020-09-06-esp-and-micropython.md68
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---
2title: Getting started with MicroPython and ESP8266 2title: Getting started with MicroPython and ESP8266
3url: esp8266-and-micropython-guide.html 3url: esp8266-and-micropython-guide.html
4date: 2020-09-06 4date: 2020-09-06T12:00:00+02:00
5draft: false 5draft: false
6--- 6---
7 7
8## Introduction 8## Introduction
9 9
10A 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. 10A while ago I bought some [ESP8266](https://www.espressif.com/en/products/socs/esp8266)
11and [ESP32](https://www.espressif.com/en/products/socs/esp32) dev boards to
12play around with and I finally found a project to try it out.
11 13
12For 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). 14For my project, I used [ESP32](https://www.espressif.com/en/products/socs/esp32)
15but I could easily choose [ESP8266](https://www.espressif.com/en/products/socs/esp8266).
16This guide contains which tools I use and how I prepared my workspace to code
17for [ESP8266](https://www.espressif.com/en/products/socs/esp8266).
13 18
14![ESP8266 and ESP32 boards](/assets/esp8366-micropython/boards.jpg) 19![ESP8266 and ESP32 boards](/assets/esp8366-micropython/boards.jpg)
15 20
16This guide covers: 21This 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
28mine and once I replaced it everything started to work.
22 29
23## Flashing the SOC 30## Flashing the SOC
24 31
25Plug your ESP8266 to USB port and check if the device was recognized with executing `dmesg | grep ch341-uart`. 32Plug your ESP8266 to USB port and check if the device was recognized with
33executing `dmesg | grep ch341-uart`.
26 34
27Then check if the device is available under `/dev/` by running `ls /dev/ttyUSB*`. 35Then 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
31After 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`. 41After these conditions are meet go to the navigate to [https://micropython.org/download/esp8266/](https://micropython.org/download/esp8266/)
42and download `esp8266-20200902-v1.13.bin`.
32 43
33```sh 44```sh
34mkdir esp8266-test 45mkdir esp8266-test
@@ -37,7 +48,8 @@ cd esp8266-test
37wget https://micropython.org/resources/firmware/esp8266-20200902-v1.13.bin 48wget https://micropython.org/resources/firmware/esp8266-20200902-v1.13.bin
38``` 49```
39 50
40After obtaining firmware we will need some tooling to flash the firmware to the board. 51After obtaining firmware we will need some tooling to flash the firmware to
52the board.
41 53
42```sh 54```sh
43sudo pip3 install esptool 55sudo pip3 install esptool
@@ -45,7 +57,8 @@ sudo pip3 install esptool
45 57
46You can read more about `esptool` at [https://github.com/espressif/esptool/](https://github.com/espressif/esptool/). 58You can read more about `esptool` at [https://github.com/espressif/esptool/](https://github.com/espressif/esptool/).
47 59
48Before flashing the firmware we need to erase the flash on device. Substitute `USB0` with the device listed in output of `ls /dev/ttyUSB*`. 60Before 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
51esptool.py --port /dev/ttyUSB0 erase_flash 64esptool.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.
57esptool.py --port /dev/ttyUSB0 --baud 460800 write_flash --flash_size=detect 0 esp8266-20200902-v1.13.bin 70esptool.py --port /dev/ttyUSB0 --baud 460800 write_flash --flash_size=detect 0 esp8266-20200902-v1.13.bin
58``` 71```
59 72
60If everything went ok you can try accessing MicroPython REPL with `screen /dev/ttyUSB0 115200` or `picocom /dev/ttyUSB0 -b115200`. 73If everything went ok you can try accessing MicroPython REPL with `
74screen /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
64When you are in REPL you can test if all is working properly following steps. 79When 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
71This should output a number representing a frequency of the CPU (mine was `80000000`). 86This should output a number representing a frequency of the CPU (mine was
87`80000000`).
72 88
73When you are in `screen` or `picocom` these can help you a bit. 89When 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
84Now, to make our lives a little bit easier there are couple of additional tools that will make this whole experience a little more bearable. 100Now, to make our lives a little bit easier there are couple of additional tools
101that will make this whole experience a little more bearable.
85 102
86There are twq cool ways of uploading local files to SOC flash. 103There 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
116Even though `ampy` is a cool tool I opted with `rshell` in the end since it's much more polished and feature rich. 133Even though `ampy` is a cool tool I opted with `rshell` in the end since it's
134much 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.
126rshell --buffer-size=30 -p /dev/ttyUSB0 -a 144rshell --buffer-size=30 -p /dev/ttyUSB0 -a
127``` 145```
128 146
129This 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. 147This will open a shell inside bash and from here you can execute multiple
148commands. You can check what is supported with `help` once you are inside of
149a shell.
130 150
131```bash 151```bash
132m@turing ~/Junk/esp8266-test 152m@turing ~/Junk/esp8266-test
@@ -151,11 +171,14 @@ boards cd cp echo exit filetype ls repl rsync
151Use Control-D (or the exit command) to exit rshell. 171Use 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
158To avoid copying files all the time I used `rsync` function from the inside of `rshell`. 180To avoid copying files all the time I used `rsync` function from the
181inside of `rshell`.
159 182
160```bash 183```bash
161rsync . /pyboard 184rsync . /pyboard
@@ -163,9 +186,11 @@ rsync . /pyboard
163 186
164#### Executing scripts 187#### Executing scripts
165 188
166It 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. 189It is a pain to continuously reboot the device to trigger `/pyboard/boot.py`
190and there is a better way of testing local scripts on remote device.
167 191
168Lets assume we have `src/freq.py` file that displays CPU frequency of a remote device. 192Lets assume we have `src/freq.py` file that displays CPU frequency of a
193remote 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