You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Also check out the new ESP32 MPY-Jama free IDE and the latest MicroWebSrv2 below:
MicroWebSrv is a micro HTTP Web server that supports WebSockets, html/python language templating and routing handlers, for MicroPython (principally used on ESP32 and Pycom modules. Now supports all variants of Pyboard D-series from the makers of Micropython)
Very easy to integrate and very light with 3 files only :
"microWebSrv.py" - The Web server
"microWebSocket.py" - The optional support of WebSockets
"microWebTemplate.py" - The optional templating language for .pyhtml rendered pages
Using optional module microWebSocket to connect WebSockets :
File "microWebSocket.py" must be present to activate WebSockets support
Enable and accept WebSockets :
frommicroWebSrvimportMicroWebSrvmws=MicroWebSrv() # TCP port 80 and files in /flash/wwwmws.MaxWebSocketRecvLen=256# Default is set to 1024mws.WebSocketThreaded=False# WebSockets without new threadsmws.AcceptWebSocketCallback=_acceptWebSocketCallback# Function to receive WebSocketsmws.Start(threaded=True) # Starts server in a new thread
Name
Function
Callback function to receive text message
ws.RecvTextCallback = func(webSocket, msg)
Callback function to receive binary data
ws.RecvBinaryCallback = func(webSocket, data)
Callback function when connection was closed
ws.ClosedCallback = func(webSocket)
Send a text message
ws.SendText(msg)
Send a binary message
ws.SendBinary(data)
Check connection state
ws.IsClosed()
Close the connection
ws.Close()
Basic example of callback functions :
def_acceptWebSocketCallback(webSocket, httpClient) :
print("WS ACCEPT")
webSocket.RecvTextCallback=_recvTextCallbackwebSocket.RecvBinaryCallback=_recvBinaryCallbackwebSocket.ClosedCallback=_closedCallbackdef_recvTextCallback(webSocket, msg) :
print("WS RECV TEXT : %s"%msg)
webSocket.SendText("Reply for %s"%msg)
def_recvBinaryCallback(webSocket, data) :
print("WS RECV DATA : %s"%data)
def_closedCallback(webSocket) :
print("WS CLOSED")
Using optional module microWebTemplate for .pyhtml rendered pages :
File "microWebTemplate.py" must be present to activate .pyhtml pages
Pages will be rendered in HTML with integrated MicroPython code
Instruction
Schema
PY
{{ py }}MicroPython code{{ end }}
IF
{{ ifMicroPython condition}}html bloc{{ end }}
ELIF
{{ elifMicroPython condition}}html bloc{{ end }}
ELSE
{{ else }}html bloc{{ end }}
FOR
{{ foridentifierinMicroPython iterator}}html bloc{{ end }}
INCLUDE
{{ includepyhtml_filename}}
?
{{MicroPython expression}}
Using {{ py }} :
{{ py }}
importmachinefromutimeimportsleeptest=123deftestFunc(x) :
return2*x
{{ end }}
{{ fortotoinrange(testFunc(3)) }}
<div>totox10equal {{ toto*10 }}</div><hr/>
{{ end }}
Using {{ include ... }} :
{{ includemyTemplate.pyhtml }}
Example of a .pyhtml file :
<html><head><title>TEST PYHTML</title></head><body><h1>BEGIN</h1>
{{ py }}
def _testFunction(x) :
return "IN TEST FUNCTION %s" % x
{{ end }}
<divstyle="background-color: black; color: white;">
{{ for toto in range(3) }}
This is an HTML test...<br />
TOTO = {{ toto + 1 }} !<br />
{{ for toto2 in range(3) }}
TOTO2 = {{ _testFunction(toto2) }}
{{ end }}
Ok good.<br />
{{ end }}
</div>
{{ _testFunction(100) }}<br />
<br />
{{ if 2+5 <3}}INIF(1){{elif10+15!= 25}}INELIF(2){{elif10+15 == 25}}INELIF(3){{else}}INELSE(4){{end}}</body></html>