How to make ESP32 as HTTP webserver using MicroPython ?
|In IoT, there are are two components – Embedded device (edge) and application on cloud. Normally server components are hosted in cloud to have better performance as explained in this article. However to increase the edge computing powers, there is also need to host a webserver on edge nodes. This article explains how to run the webserver which uses HTTP protocol. There are two different ways of doing this.
- Using picoweb package
- Typical Socket programming
Required hardware : ESP32 development kit, Led, DHT 22 sensor,
Required software: Micropython firmware, pycraft IDE
Using Picoweb package
What is picoweb : It is a “micro” web micro-framework (thus, “pico-framework”) for radically unbloated web applications using radically unbloated Python implementation, MicroPython
Features: Asynchronous from the start, Small memory usage, API affinity for similar web frameworks like flask
From REPL prompt of micropython, install picoweb
upip.install(‘picoweb’)
upip.install(‘utemplate’)
Objective : To get the real temperature whenever is request is posted to webserver
Connection: DHT22 is connected to GPIO pin 13 and v3.3 and ground are connected
Approach: Build sensor template which will give the format for presentation layer.
That template is called while making response
yield from picoweb.start_response(resp, content_type = “text/html”)
yield from app.render_template(resp, “sensor.tpl”, (sensor,))
In case of DHCP enabled routers, IP address of ESP32 development kit will be dynamic in nature. So get the ip address and pass it to host name
Code:
Template need to be created and placed inside templates folder
Using Socket programming
Other way of creating webserver is to use sockets and make socket connection,
Objective: Make a connected LED to turn on or off from browser client.
Connection: Connect LED to GPIO 16
Approach: Create socket and listen to the socket.
Code:
Though I personally prefer using picoweb for the benefits of small memory foot print, similarity with flask, depending on the need developers can choose any specific method.
full code is available on git repo
References:
https://github.com/pfalcon/picoweb/
http://crufti.com/getting-started-with-iot-development-using-micropython/
Why is ESP8266 400 Rupees, and ESP 32 – 1600 rupees !
So, if you can send me the link to the processor on Amazon.co.in I will know what exactly you are using ?
Is this what you are using
https://www.amazon.in/Robocraze-Development-Bluetooth-Ultra-Low-Consumption/dp/B075H1BJNG/ref=sr_1_2?ie=UTF8&qid=1506231119&sr=8-2&keywords=esp32
ESP32 is way better chipset than ESP8266. It has two cores. It has BLE as additional communication. ESP32 has more GPIO pins. For this webserver component of micropython, this code will work in ESP8266 as well. It has 16 MB flash. Refer this link for more details.
https://espressif.com/en/products/hardware/esp32/overview
On your board query, I used something similar like the one in your query,
Hello,
I would like to use ESP32 (with micropython) and picoweb to made some thermostat. I will get and set temperature via picoweb “web app”. To communicate I will use simple json messages… but I need to check temperature every e.g. 2 minutes, compare it to stored one and turn on or off my heater.
So my idea is:
in file “init.py” I will add second scheduled job:
loop.create_task(asyncio.start_server(self._handle, host, port))
loop.create_task(check_temperature())
then define this function (outside WebApp class) and put some code to read temperature and turn on/off heater
async def check_temperature():
while True:
#check temperature
# some IFs
# turn on/off heater
await asyncio.sleep(120)
And the question is… Is this the right way to do this? Any better solution?
Best regards,
MvincM
We need to test and confirm. However I would suggest using MQTT will be the better solution. Separate embedded component (Esp32) and cloud component via middleware broker like cloudmqtt will be elegant solution. Advantages: Flexible and extensible.
Refer to this blog http://icircuit.net/building-iot-application-using-esp32-micropython-10-steps/2077 for more details.
Thanks for comment, update after you complete your experiment,
Hello.
I was thinking about MQTT and some OpenHub server but… If you are really looking for stable and some kind embedded solution for keeping your house from freezing it must be working without any internet connection. It must be very stable and do not depend on internet.
So any additional fireworks like nice gui in could is rather COULD HAVE option but first of all thermostat must be standalone device.
So I would be very grateful for your opinion how to do this best way. My proposal works but… I don’t if it best way.
Best regards
MvincM
Hello, very nice info, I am getting an error when I try to upip
Error installing ‘picoweb’: list index out of range, packages may be partially installed
Thanks
try uninstalling it fully and re-install picoweb again
I don’t kow if you still have this problem, but others might. You are seeing this error either because your ESP is not connected to internet or because RAM is full. First case is easy to fix while second case is not. I’ve managed to solve it. If you have a code in your boot.py then it occupies some RAM and this doesn’t allow upip to install picoweb. Steps I did:
1) erased and reflashed device (I don’t know if there is a better way to disable code in boot.py);
2) Opened screen, connected device to Wi Fi;
3) import upip and upip.install(‘picoweb’)
Picoweb now should be available on your ESP.
hi, i try to run upip on esp8266, and i have 4MB space of internal filesystem
>>> import upip
>>> gc.collect()
>>> upip.install(‘picoweb’)
Installing to: /lib/
Warning: pypi.python.org SSL certificate is not validated
Error installing ‘picoweb’: memory allocation failed, allocating 7406 bytes, packages may be partially installed
>>> import os
>>> os.listdir()
[‘boot.py’, ‘main.py’, ‘webrepl_cfg.py’]
how can i fix it? thank you.
i find a discuss on forum.
https://forum.micropython.org/viewtopic.php?f=16&t=4080&hilit=upip
maybe the only solution is to recompile picoweb into the firmware before burning onto the board. any better solution?
Yes, burning a new firmware is better solution. to have upip to work, internet connection is must. So ensure, esp32 is connected to internet first.
Hello
Is there a way to read and write GPIO from the same webpage
Regards
Yes, You could use JavaScript to send the post request and get the status of the GPIO
Hi, picoweb requires ‘ulooging’ lib. How do I install it on an ESP32?
upip.install(‘logging’) fails 🙁
I have also same problem with ebolise. Pls help!!
For those that still have this problem the library has now changed to:
upip.install(‘micropython-ulogging’)