Ejemplo n.º 1
0

def _closedCallback(webSocket):
    if WS_messages:
        _thread.list()
        print("WS CLOSED")


# ----------------------------------------------------------------------------

routeHandlers = [("/test", "GET", _httpHandlerTestGet),
                 ("/test", "POST", _httpHandlerTestPost)]

srv = MicroWebSrv(routeHandlers=routeHandlers)

# ------------------------------------------------------
# WebSocket configuration
srv.MaxWebSocketRecvLen = 256
# Run WebSocket in thread
srv.WebSocketThreaded = True
# If WS is running in thread, set the thread stack size
# For this example 4096 is enough, for more complex
# webSocket handling you may need to increase this size
srv.WebSocketStackSize = 4096
srv.AcceptWebSocketCallback = _acceptWebSocketCallback
# ------------------------------------------------------

srv.Start(threaded=True)

# ----------------------------------------------------------------------------
Ejemplo n.º 2
0
    dict['internal'] = machine.internal_temp()[1]  # Read ESP32 internal temp
    dict['time'] = rtc.now()  # Record current time
    # Convert data to JSON and send
    websocket.SendText(json.dumps(dict))


def cb_accept_ws(webSocket, httpClient):
    print("WS ACCEPT")
    webSocket.RecvTextCallback = cb_receive_text
    webSocket.RecvBinaryCallback = cb_receive_binary
    webSocket.ClosedCallback = cb_closed
    # Use lambda to pass websocket to timer callback
    cb = lambda timer: cb_timer(timer, webSocket)
    # Init and start timer to poll temperature sensor
    tm.init(period=3000, callback=cb)


mws = MicroWebSrv()  # TCP port 80 and files in /flash/www
mws.MaxWebSocketRecvLen = 256  # Default is set to 1024
mws.WebSocketThreaded = True  # WebSockets with new threads
mws.WebSocketStackSize = 4096
mws.AcceptWebSocketCallback = cb_accept_ws  # Function to receive WebSockets
mws.Start(threaded=False)  # Blocking call (CTRL-C to exit)

print('Cleaning up and exiting.')
mws.Stop()
tm.deinit()
rtc.clear()
ds.deinit()
ow.deinit()