A Micro HTTP Web Server and language templating for MicroPython

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
User avatar
jczic
Posts: 13
Joined: Fri Sep 01, 2017 2:10 am
Location: Paris, France
Contact:

A Micro HTTP Web Server and language templating for MicroPython

Post by jczic » Fri Sep 01, 2017 2:48 am

Hello,
Just if you want a Micro HTTP Web Server and language templating for MicroPython (used on Pycom modules) :
https://github.com/jczic/MicroWebSrv
;)

loboris
Posts: 344
Joined: Fri Oct 02, 2015 6:19 pm

Re: A Micro HTTP Web Server and language templating for MicroPython

Post by loboris » Fri Sep 01, 2017 9:30 pm

Thanks for the information.
I have included it with minor modifications into my MicroPython port as a frozen module and it works very well.
Are you planning to implement WebSockets protocol and posted multipart data mentioned in "issues" ?

User avatar
rdagger
Posts: 143
Joined: Tue Feb 28, 2017 6:16 pm
Contact:

Re: A Micro HTTP Web Server and language templating for MicroPython

Post by rdagger » Sun Sep 03, 2017 11:39 pm

jczic wrote:Hello,
Just if you want a Micro HTTP Web Server and language templating for MicroPython (used on Pycom modules) :
https://github.com/jczic/MicroWebSrv
;)
Your library looks great! Is it compatible with an ESP32? Have you tried implementing websockets?

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: A Micro HTTP Web Server and language templating for MicroPython

Post by OutoftheBOTS_ » Sun Feb 04, 2018 9:48 am

@jczic

I am trying to get websockets to work on my ESP32 using your library.

I have tried all your examples and they work fine on the firmware I am using (laboris port)

This is the code that I tried to start the webserver and send websocket messages, I also have a htlm file in the flash/www/ directory that is for receiving the websockets message and display it on the screen.

I get the following error
AttributeError: 'MicroWebSrv' object has no attribute 'SendText'
with this code

Code: Select all

from microWebSrv import MicroWebSrv
import time

def _acceptWebSocketCallback(webSocket, httpClient) :
  print("WS ACCEPT")
  webSocket.RecvTextCallback   = _recvTextCallback
  webSocket.RecvBinaryCallback = _recvBinaryCallback
  webSocket.ClosedCallback     = _closedCallback

def _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")

mws = MicroWebSrv()                                    # TCP port 80 and files in /flash/www
mws.MaxWebSocketRecvLen     = 256                      # Default is set to 1024
mws.WebSocketThreaded       = False                    # WebSockets without new threads
mws.AcceptWebSocketCallback = _acceptWebSocketCallback# Function to receive WebSockets
mws.Start() 

t=0
while True:
  print(t)
  mws.SendText(str(t))
  time.sleep(1)
  t+=1


OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: A Micro HTTP Web Server and language templating for MicroPython

Post by OutoftheBOTS_ » Sun Feb 04, 2018 11:27 am

Ok I found the wstest.html and am playing with it now and it works.

I think that I maybe better understanding how it work. The server can only send a message in response the the client sending a message first???

I want to graph live data from my robot and new data will be available from a timer event on the ESP32 every 100ms so I assume then I have to have java script client side that sends a message every 100ms for the esp32 to respond with new data to???

edit

ok I seem to have worked it out by using setInterval(myFunction, 100); and am now successfully sending the needed data every 100ms.

Now to find some open source javascript for plotting the data :)

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: A Micro HTTP Web Server and language templating for MicroPython

Post by OutoftheBOTS_ » Sun Feb 04, 2018 11:45 pm

Ok I am getting further but need a little advice.

So I think it works like this. The client needs to establish the websocket but once this is done the the client doesn't need to request a message the server can push out messages.

For the server to push out the message it needs a pointer to the websocket. The only way that I seem to be able to get a pointer to the websocket is have the client send a message first then make a global variable point to the websocket.

Once the client executes this javascript websocket = new WebSocket(wsUri); the webscoket is made, is there a way at the server side to detect this and get a pointer to it??

here is the round about way I made it work but it far from pretty coding

Code: Select all

from microWebSrv import MicroWebSrv
import utime

wsflag = False

def _acceptWebSocketCallback(webSocket, httpClient) :
  print("WS ACCEPT")
  webSocket.RecvTextCallback   = _recvTextCallback
  webSocket.RecvBinaryCallback = _recvBinaryCallback
  webSocket.ClosedCallback     = _closedCallback

def _recvTextCallback(webSocket, msg) :
  print("WS RECV TEXT : %s" % msg)
  webSocket.SendText("Reply for %s" % msg+ str(t))
  global wsflag
  global ws
  ws = webSocket
  wsflag = True

def _recvBinaryCallback(webSocket, data) :
  print("WS RECV DATA : %s" % data)

def _closedCallback(webSocket) :
  print("WS CLOSED")
  

mws = MicroWebSrv()                                    # TCP port 80 and files in /flash/www
mws.MaxWebSocketRecvLen     = 256                      # Default is set to 1024
mws.WebSocketThreaded       = False                    # WebSockets without new threads
mws.AcceptWebSocketCallback = _acceptWebSocketCallback# Function to receive WebSockets
mws.Start() 

t=0
while not wsflag : pass 

while True:
  print(t)
  if  not ws.IsClosed() : ws.SendText(str(t))
  utime.sleep_ms(100)
  t+=1


Post Reply