Raspberry Pi to ESP32

A backcopy of Hackspace showed how to use the Raspberry Pi to an ESP 32, in order to control it via Python. Unfortunately the article was too old and the build command didn’t work. Rather than try and unpick the thing, I went to find a newer, working tutorial.

I found a promising tutorial at Rototron

As always before working on the pi, you need to update it.

sudo apt-get update && sudo apt-get upgrade

They recommend using a tool called ESPTool to load Micropython into the ESP32.

sudo pip3 install esptool

Use dmesg to show the ring buffer of the Pi. More info on How To Geek. Use grep to just get your ESP32. It will be connected to a value starting with ttyUSB.

dmesg | grep ttyUSB

You can also look in the /dev folder. Plug in the ESP32 via USB and remove it – use ls and look for a device starting with ttyUSB. Mine is called ttyUSB0.

Ask your ESP32 what it is. Flash_id reads its identity information.

esptool.py --port /dev/ttyUSB0 flash_id

This deletes the old firmware:

esptool.py --port /dev/ttyUSB0 erase_flash

If you want to put Micropython on it, you need to download it. Go to this ESP32 download page. You will need the .bin file. It also gives you the install instruction. Change directory to your Downloads folder so it will find your bin file. It is important to specify the stating memory address of 0x1000.

esptool.py --chip esp32 --port /dev/ttyUSB0 --baud 460800 write_flash -z 0x1000 esp32-20190125-v1.10.bin

Haha fatal error, my chip is ESP8266 not ESP32. I sure wish that C++ would give you helpful error messages like this. Back to the Micropython page. This page links to a tutorial telling you how to install it.

esptool.py --port /dev/ttyUSB0 --baud 460800 write_flash --flash_size=detect 0 esp8266-20170108-v1.8.7.bin

That seems to have worked. Now you need a REPL terminal. This is so you can send commands to the ESP32 – or ESP8266. It’s all the same from here on.

Thonny

I’ve been told that Thonny is the best thing to use for a REPL – did I get the terminology wrong? Yup – it’s an IDE.

Just type thonny into Terminal. And it will open.

Select Tools -> Options. Go to the Interpreter tab and select MicroPython (ESP8266) or whatever chip you have. Press Enter and you will start seeing feedback in the Shell section of the IDE.

Ha it’ll be better to Select our board, not auto-select. Got some weird results.

Try this script in the Editor above the Shell. Save As name.py, in order to keep your work.

from machine import Pin
from time import sleep
led = Pin(2, Pin.OUT)
while True:
    led.value(not led.value())
    sleep(3)

It does work, but I’ve been told there is very little chance to get much python running on an ESP8266. Better to use an ESP32. I’m inclined to try this. Not much fun so far. More errors than anything else. I think I’ll try on rshell instead.

Leave a Reply

Your email address will not be published. Required fields are marked *