Interface with a microcontroller from a web browser, using HTTPS and a simple Python server.
I love microcontrollers. I also love controlling hardware remotely, say, from another room of my house. The idea of being able to do this through a browser, like from my laptop or phone, rather than from an ssh console.
I’ve never worked with https or done any sort of web programming before. Luckily, I found this project that allowed me to get up and running quite quickly with an HTTPs server in Python. I was able to modify it to meet my needs for this very basic project. If you are looking to rebuild this project in any way, or just want to create your own python server, I recommend following the steps in the project linked above to create your own ‘server.pem’ file. I needed to add support for handling HTTP GET and POST requests. I was able to find an example project for GET and POST requests here.
As far as web programming goes, you will find that I’ve done only the basic CSS and html type scripting to create a very simple webpage. I’ll leave the web development to the more experienced people out there. However, even from this very basic example, you can see how it would be quite easy to create a more complex webpage and support more behavior on the microcontroller.
I am using the Digilent BasysMX3 which is basically a handy little development board for a PIC32. It’s got LEDs, an RGB LED, two Pmod ports, an in-system programmer and debugger, a 16×2 LCD screen, an integrated H-bridge, buttons, four seven segment displays, and a whole bunch of other fun things. I am only really using the LEDs and uart controllers for this project though. To connect to my python server, I am using the Pmod ESP32. This is basically Digilent’s own Pmod board featuring an ESP32 from Espressif. These ESP32 chips are great. They allow for WiFi/Bluetooth 4.2/BLE connections, have a whole host of peripherals, come with free RTOS, and can do pretty much anything.
How The Project Works
The Python server (simpleServer.py) creates an HTTPS server at the IP address and port number specified by the call:
httpd = BaseHTTPServer.HTTPServer(('192.168.1.107', 4443), MyHandler)
This allows you to visit the server by typing “https://192.168.1.107:4443” in your browser. Your browser may get mad at you for attempting to visit a page hosted locally, but it should let you still visit this page.
When you visit the address in the browser, there are three buttons present on the screen. Everytime one of these buttons is pressed, a hidden file is updated on the server, and then the page is reloaded.
On the microcontroller side, the ESP32 is already connected to the wifi network, and the ESP32 repeatedly attempts to create an SSL connect at the IP address and port number specified above. Once the SSL connection is established, the ESP32 submits an HTTP GET request for the hidden file described earlier. Based on the contents of the file, the ESP32 modifies the behavior of the LEDS.
If You Want To Build The Project
The MPLabXIDE project files are included in the linked github repository in the ‘BasysESP32.X’ directory. The corresponding files for the python server are also included in the repo.
You will need to make some changes to the server files and MPlab files in order to get it to work. Modify the ‘simpleServer.py’ file. So that it creates the server port on the IP address where you are hosting your server. This change will happen at line 95. For the MPlabX files, modify ‘main.c’ to connect the ESP32 to the IP address where you are hosting the server. This should be the same IP address that you just put in to the server code. This change occurs at line 30.
The server should look for a file called “server.pem” in the same directory as the server. I have included this in the repository, but if you want to run your own server. I recommend following the instructions shown here to create your own.pem file.
Connect the ESP32 to my wifi network before running the project, and so. I do not explicitly do this in software. I recommend you do the same. This way, you can test that the network connectivity is valid beforehand by pinging the device at its IP address. However, if you desire, there is an included function in the MPLabx project files that should allow you to connect the ESP32 to any wifi network. Additional utilities are provided in ESP32.c/.h.