def httpserver_init(): global server server = MicroWebSrv(webPath='www/') server.MaxWebSocketRecvLen = 256 server.WebSocketThreaded = False server.AcceptWebSocketCallback = _acceptWebSocketCallback server.Start(threaded=False)
def setup_web_server(accept_socket_cb): "Setup http webserver." mws = MicroWebSrv() mws.MaxWebSocketRecvLen = 256 mws.WebSocketThreaded = False mws.AcceptWebSocketCallback = accept_socket_cb mws.Start()
def main(): ap = AccessPoint() srv = MicroWebSrv(webPath='www/') srv.MaxWebSocketRecvLen = 256 srv.WebSocketThreaded = False srv.AcceptWebSocketCallback = _acceptWebSocketCallback srv.Start() del ap
def start(): global gUart gUart = machine.UART(1, rx=14, tx=12) srv = MicroWebSrv(webPath='www/') #srv = MicroWebSrv(routeHandlers=routeHandlers, webPath='/www/') srv.MaxWebSocketRecvLen = 256 srv.WebSocketThreaded = True #False srv.AcceptWebSocketCallback = _acceptWebSocketCallback srv.Start()
def start_web_server(): global ipaddress epd.display_string_at(fb, 0, 60, "Connect to badge AP to configure.", font12, gxgde0213b1.COLORED) epd.display_string_at(fb, 0, 72, "Enter this URL in your browser:", font12, gxgde0213b1.COLORED) epd.display_string_at(fb, 0, 84, "http://" + ipaddress + "/setup", font12, gxgde0213b1.COLORED) epd.display_frame(fb) srv = MicroWebSrv(webPath='www/') srv.MaxWebSocketRecvLen = 256 srv.WebSocketThreaded = False #srv.AcceptWebSocketCallback = _acceptWebSocketCallback srv.Start(threaded=False)
def network_connect(): sta_if = network.WLAN(network.STA_IF) sta_if.active(True) ssid = ConfigFile.get('wifi', {}).get('ssid') if ssid: sta_if.connect(ssid, ConfigFile.get('wifi', {}).get('pwd')) if not wifi_connected(sta_if): ap = network.WLAN(network.AP_IF) # create access-point interface ap.active(True) # activate the interface ap.config(essid='micropython_ap') # set the ESSID of the access point @MicroWebSrv.route('/aps', 'GET') def scan_ap(http_client, http_response): sta_if.active(True) ap_list = sta_if.scan() http_response.WriteResponseJSONOk([ap[0] for ap in ap_list]) @MicroWebSrv.route('/connect', 'POST') def connect(http_client, http_response): params = json.loads(http_client.ReadRequestContent()) ssid = params.get('ssid') if not ssid: http_response.WriteResponseJSONOk({ 'Success': False, 'Message': 'ssid不能为空!' }) return sta_if = network.WLAN(network.STA_IF) sta_if.active(True) sta_if.connect(ssid, params.get('pwd')) # Connect to an AP if wifi_connected(sta_if): print('connect success!') wifi_config = ConfigFile.get('wifi', {}) wifi_config['ssid'] = ssid wifi_config['pwd'] = params.get('pwd') ConfigFile.set('wifi', wifi_config) machine.reset() http_response.WriteResponseJSONOk({ 'Success': False, 'Message': '连接失败!' }) srv = MicroWebSrv(webPath='/templates/') srv.MaxWebSocketRecvLen = 256 srv.WebSocketThreaded = False srv.Start()
def start_server(_relay=None, _dht11=None, _debugMode=False): """Run server at configured device""" print('Starting regular server...') global relay global dht11 global srv global DEBUG_MODE if (_relay!=None and _dht11!=None): relay = _relay dht11 = _dht11 DEBUG_MODE = _debugMode srv = MicroWebSrv(webPath='www/') srv.MaxWebSocketRecvLen = 256 srv.WebSocketThreaded = True srv.AcceptWebSocketCallback = _acceptWebSocketCallback srv.Start(threaded=True)
from microWebSrv import MicroWebSrv import esp32 # ---------------------------------------------------------------------------- @MicroWebSrv.route('/temperature') def _httpHandlerTempGet(httpClient, httpResponse): temp = (esp32.raw_temperature() - 32) * 5.0 / 9.0 httpResponse.WriteResponseOk(headers=({ 'Cache-Control': 'no-cache' }), contentType='text/html', contentCharset='UTF-8', content="Temperatur ist: {}".format(temp)) srv = MicroWebSrv(webPath='www/') srv.MaxWebSocketRecvLen = 256 srv.WebSocketThreaded = False srv.Start() # ----------------------------------------------------------------------------
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") # ---------------------------------------------------------------------------- # routeHandlers = [ # ( "/test", "GET", _httpHandlerTestGet ), # ( "/test", "POST", _httpHandlerTestPost ) # ] p = 80 if sys.platform == 'linux' else 8080 srv = MicroWebSrv(port=p, webPath='www/') srv.MaxWebSocketRecvLen = 256 srv.WebSocketThreaded = True srv.AcceptWebSocketCallback = _acceptWebSocketCallback srv.Start(threaded=True) while True: time.sleep(72000) # ----------------------------------------------------------------------------
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()
_thread.list() print("WS CLOSED") # ---------------------------------------------------------------------------- # You can set the path to the www directory, default is '/flash/www' #srv = MicroWebSrv(webPath='www_path') srv = MicroWebSrv() # ------------------------------------------------------ # WebSocket configuration srv.MaxWebSocketRecvLen = 256 # Run WebSocket in thread or not srv.WebSocketThreaded = ws_run_in_thread # If WebSocket is running in thread, set the thread stack size # For this example 4096 should be enough, for more complex # webSocket handling you may need to increase this size # If WebSocketS is NOT running in thread, and WebServer IS running in thread # make shure WebServer has enough stack size to handle also the WebSocket requests srv.WebSocketStackSize = 4096 srv.AcceptWebSocketCallback = _acceptWebSocketCallback # ------------------------------------------------------ # If WebSocketS used and NOT running in thread, and WebServer IS running in thread # make shure WebServer has enough stack size to handle also the WebSocket requests srv.Start(threaded=srv_run_in_thread, stackSize=8192) # ----------------------------------------------------------------------------
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
def start_webserver(): """start_webserver""" srv = MicroWebSrv(webPath='www/') srv.MaxWebSocketRecvLen = 256 srv.WebSocketThreaded = False srv.Start()
if __name__ == '__main__': if 'srv' in locals(): reset() pin13.write_digital(1) import time is_alive = False mws = MicroWebSrv() while True: if is_alive is False: if wifi.isconnected(): print(wifi.wlan.ifconfig()) display.scroll(wifi.wlan.ifconfig()[0]) mws.MaxWebSocketRecvLen = 32 mws.WebSocketThreaded = False mws.AcceptWebSocketCallback = _acceptWebSocketCallback try: is_alive = True mws.Start(threaded=True) except Exception as e: print(e) else: is_alive = False mws.Stop() pin13.write_digital(1) time.sleep(1) pin13.write_digital(0) time.sleep(1)