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