microWebsrv check client connection

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
alien1983
Posts: 17
Joined: Fri Sep 07, 2018 4:52 pm

microWebsrv check client connection

Post by alien1983 » Tue May 11, 2021 9:23 am

Hi,
I am building a soil sensor for the garden on the micropython esp32, I have a problem with the microWebsrv library .
I wanted to know when a client will connect to the server and when it will disconnect. I have not found any example of this:(


in short, my program is supposed to do this...
1.After pressing the button on the sensor, the esp32 will be reset,
2.Check machine.reset_cause() if HARD_RESET -- 2 or PWRON_RESET -- 1 then...
3.WIFI AP starts,
4.microWebserv strats (In threaded , websocket)
5.ReplWeb starts,
6.Some time is waiting for the connection (loop in new thread)
7. If not then import and run main2.py
8. else =>>>>>
"""needs some function that detects connected clients and the moment of disconnection (microWebsrv websocket) to break AP WIFI mode () and start normal mode"""

oryginal function from microWebsrv not work..

Code: Select all

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

Code: Select all

from microWebSrv.microWebSrv import MicroWebSrv
	mws = MicroWebSrv(webPath='www/')                                    # TCP port 80 and files in /flash/www
	#mws.SetNotFoundPageUrl(url='/www/index.html')
	mws.MaxWebSocketRecvLen     = 256                      # Default is set to 1024
	mws.WebSocketThreaded       = False                    # WebSockets without new threads
	mws.AcceptWebSocketCallback = _acceptWebSocketCallback # Function to receive WebSockets
	mws.Start(threaded=True)                               # Starts server in a new thread
	#mws.Start()
oryginal function from microWebsrv not work.. I can't see what is happening on the websocket?

alien1983
Posts: 17
Joined: Fri Sep 07, 2018 4:52 pm

Re: microWebsrv check client connection

Post by alien1983 » Tue May 11, 2021 1:03 pm

ok , after analyzing the code again here microWebsrv not a problem here.

if webrepl is connected it receives:
print(webrepl.client_s)
<socket>

if not connect:
print(webrepl.client_s)
none

I can get some details of this connection?

Post Reply