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