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