code

Friday, December 13, 2019

How to run MicroPython on ESP8266 HUZZAH Feather and run code on startup (Complete tutorial)





I've been playing a bit with ESP8266 and related family of microchips lately, and since I love Python, what could be better than to have a fun intro into the embedded world, while trying to run it on the chip. However it turned out to be a bit more difficult than your usual 'I'll have code running in 1 hour' which usually works for me with new Python projects.

I stumbled in quite a few problem trying to flash the MicroPython build into it, and then to make it run python program on boot automatically, so I'll go over the steps here for anyone trying to do the same:

I started with downloading the driver for Windows (my host OS is Windows, I did try to connect it to a Ubuntu VM since most of the examples I saw were on Linux machines, but it wasn't detected for some reason, so I'll show you how I did it on Windows).

First download the driver from here:

I downloaded version 6.7 for my Windows 10-64 bit machine (since the newer ones didn't work for me) and installed it. Now if you like me, when trying to plug the chip to the USB input of your machine and open windows device manager, you'll get a little yellow triangle with exclamation mark stating the driver is not detected (so I guessed I needs to be manually installed).Instead, it appeared as "CP210x USB to UART Bridge", now what you really want is for it to appear with some COM port at the end of the name, and under Ports section, so you can processed with the flashing ( for me it was "CP210x USB to UART Bridge(COM3)" ).  
In order to do that, right click on the undetected driver --> update driver --> Browse my computer for driver software --> Let me pick from a list of available drivers on my computer --> now you should have a list of manufacturers on the left, click on Silicon Laboratories. Then on the right windows click on "Silicon Labs CP210x USB to UART Bridge) --> click next and you’re done.







Now on the device manager, the device should appear under "Ports (COM & LPT)". Write down the COM port number because we'll use it next.
We're now ready to download MicroPython and flash it on the device! Open http://micropython.org/download and look for ESP8266, if you don’t know what to download then you should probably download the latest build bin.

Next, we'll need some tool in order to flash it to our device, so "pip install esptool" (some guides says this tool only works with python2 but I guess they're old, since I installed it with my Python3.7 and it worked). This will install esptool.py.exe on your python Scripts path. If you have it on your enviroment path you're good to go, otherwise, cd to your Scripts directory.
Run: (change to your COM port number you wrote earlier, if you have different one on your machine)
"esptool.py.exe --port COM3 erase_flash"
"esptool.py.exe --port COM3 --baud 460800 write_flash --flash_size=detect 0 esp8266-20190529-v1.11.bin" (change according to your MicroPython version)

If you've done all the steps correctly, you now have MicroPython flashed on your ESP8266, you can connect to it using some Serial connection client (I'm using MobaXterm), don't forget to choose the right serial port, Speed(bps) should be 115200 , if it doesn’t work try other value.
Great! Now you can try to play with MicroPython on your ESP8266 a bit, if you're coming from normal Python like me, you'll notice a lot of default libs are missing so you'll need to read the docs and understand what you can and can't use. 
There are also other libs relevant to this chip (for instance, with network lib you can connect the chip to WIFI).
But, without a way to run your program on the device startup, it doesn't really worth much, so in order to do that, we'll need another tool: "pip install adafruit-ampy" ,after it has finished installing, we can use the "ampy" tool to put our program on the device to run on boot.

Run: "ampy –port COM3 put 'your_python_program.py' /main.py". The file that is copied into the chip has to be called "main.py". Make sure to disconnect any serial console you have connected to the chip before running this command. It doesn't work if the device is busy!
If no errors appear, the file was copied to the device successfully, now after you disconnect then reconnect your device to the USB input, your script should run.

Here's a little script I wrote, which sends an HTTP GET command to a local Python HTTP server (with flask) I have running on a different machine in my network.




See you next time !



No comments:

Post a Comment

What happens when you combine a Flipper Zero with Gameboy Color?

      Hello my fellow vintage geeks . If you're a 90s kid and had style you probably owned an A...