Wednesday, 29 January 2020

MicroPython

Micropython was invented by an Australian programmer Damien George in 2013.  It is a lightweight version of python, compatible with version 3, suitable for microcontrollers.  It strikes me that I should concentrate more on python programming and this is part of the suite.  In particular I can use it for  ESP8266, ESP32, Arduino and micro:bits.

micro:bit


It couldn't be simpler to get started with micropython as explained at microbit.org.  Just use an on-line editor to create your program - initially you can amend the sample provided.  Once happy with the program it is downloaded to your PC as a hex file.  You connect the micro:bit to the PC which gives you a new drive.  Copy the .HEX file across to the micro:bit drive and your program runs.

ESP32


Install a python utility called esptool in my Windows environment using pip
download and install firmware as described at micropython.org
Install the Thonny IDE as described by RandomNerdTutorials.

Now we select micropython on ESP32 in Thonny options, select the ESP32 com port and see the Thonny shell running on ESP32.


First ESP32 Project

I really want projects to be web based to reduce connection hassles.  So first of all I want to establish wifi connectivity.
RandomNerdTutorials.com have a very simple project which starts wifi and establishes a webserver.
Two files called boot.py and main.py are downloaded to the ESP32.  These are run automatically at reset.  In this case boot.py establishes the wifi connection and main.py causes a webserver to listen for and process connections.
Connections from a browser cause a webpage to be displayed allowing control of a GPIO.
The GPIO in question is the inbuilt LED (pin 2) so clicking a button causes a response to be sent to the webserver turning it on or off.
This gives us everything we need to use the ESP32 as a basic standalone webserver reporting on its local environment.

Webserver

J-Christophe Bos has created a wonderful small webserver which is contained in 3 files:


It allows you to serve static pages, server-side scripting (pyyhon/html), websockets and supports GET/PUT, JSON, AJAX all in 65KB of program.  ESP32 has 2MB flash for filespace so this leaves plenty of room for data and static pages.


There is a microwebsrv2 update on github but as of December 2019 there seems to be a little problem making it trickier (for newbies like me) to setup. 

No comments:

Post a Comment