aboutsummaryrefslogtreecommitdiff
path: root/posts/2020-09-06-esp-and-micropython.md
diff options
context:
space:
mode:
authorMitja Felicijan <mitja.felicijan@gmail.com>2022-08-27 14:05:48 +0200
committerMitja Felicijan <mitja.felicijan@gmail.com>2022-08-27 14:05:48 +0200
commit9f5454bda6299db43a4e9de5b3716471388b81d9 (patch)
tree1ceedf64a4517a372d70efc2b6f4bbd9478ce792 /posts/2020-09-06-esp-and-micropython.md
parente728c3a2cbd06d95cd1226d3b23473816bd0d67e (diff)
downloadmitjafelicijan.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.md209
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---
2Title: Getting started with MicroPython and ESP8266
3Description: Getting started with MicroPython and ESP8266
4Slug: esp8266-and-micropython-guide
5Listing: true
6Created: 2020-09-06
7Tags: []
8---
9
10**Table of contents**
11
121. [Introduction](#introduction)
132. [Flashing the SOC](#flashing-the-soc)
143. [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)
194. [Additional resources](#additional-resources)
20
21
22## Introduction
23
24A 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
26For 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![ESP8266 and ESP32 boards](/assets/esp8366-micropython/boards.jpg)
29
30This 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
39Plug your ESP8266 to USB port and check if the device was recognized with executing `dmesg | grep ch341-uart`.
40
41Then 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
45After 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
48mkdir esp8266-test
49cd esp8266-test
50
51wget https://micropython.org/resources/firmware/esp8266-20200902-v1.13.bin
52```
53
54After obtaining firmware we will need some tooling to flash the firmware to the board.
55
56```sh
57sudo pip3 install esptool
58```
59
60You can read more about `esptool` at [https://github.com/espressif/esptool/](https://github.com/espressif/esptool/).
61
62Before 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
65esptool.py --port /dev/ttyUSB0 erase_flash
66```
67
68If flash was successfully erased it is now time to flash the new firmware to it.
69
70```sh
71esptool.py --port /dev/ttyUSB0 --baud 460800 write_flash --flash_size=detect 0 esp8266-20200902-v1.13.bin
72```
73
74If 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
78When 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
85This should output a number representing a frequency of the CPU (mine was `80000000`).
86
87When 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
98Now, 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
100There 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
109sudo pip3 install adafruit-ampy
110```
111
112Listed below are some common commands I used.
113
114```bash
115
116# uploads file to flash
117ampy --delay 2 --port /dev/ttyUSB0 put boot.py
118
119# lists file on flash
120ampy --delay 2 --port /dev/ttyUSB0 ls
121
122# outputs contents of file on flash
123ampy --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
130Even 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
134sudo pip3 install rshell
135```
136
137Now that `rshell` is installed we can connect to the board.
138
139```bash
140rshell --buffer-size=30 -p /dev/ttyUSB0 -a
141```
142
143This 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
146m@turing ~/Junk/esp8266-test
147$ rshell --buffer-size=30 -p /dev/ttyUSB0 -a
148
149Using buffer-size of 30
150Connecting to /dev/ttyUSB0 (buffer-size 30)...
151Trying to connect to REPL connected
152Testing if ubinascii.unhexlify exists ... Y
153Retrieving root directories ... /boot.py/
154Setting time ... Sep 06, 2020 23:54:28
155Evaluating board_name ... pyboard
156Retrieving time epoch ... Jan 01, 2000
157Welcome to rshell. Use Control-D (or the exit command) to exit rshell.
158/home/m/Junk/esp8266-test> help
159
160Documented commands (type help <topic>):
161========================================
162args cat connect date edit filesize help mkdir rm shell
163boards cd cp echo exit filetype ls repl rsync
164
165Use 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
172To avoid copying files all the time I used `rsync` function from the inside of `rshell`.
173
174```bash
175rsync . /pyboard
176```
177
178#### Executing scripts
179
180It 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
182Lets assume we have `src/freq.py` file that displays CPU frequency of a remote device.
183
184```py
185# src/freq.py
186
187import machine
188print(machine.freq())
189```
190
191Now lets upload this and execute it.
192
193```bash
194# syncs files to remove device
195rsync ./src /pyboard
196
197# goes into REPL
198repl
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)