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 main(): import uos import json import time import _thread import network from m5stack import lcd, buttonA from microWebSrv import MicroWebSrv from micropython import const def wifiCheck(): sta_if = network.WLAN(network.STA_IF) if sta_if.active() == False: sta_if.active(True) wifi = json.load(open('/flash/etc/wlan.json', 'r')) ssid = wifi['wifi']['ssid'] pwd = wifi['wifi']['password'] sta_if.connect(ssid, pwd) while sta_if.ifconfig()[0] == '0.0.0.0': time.sleep(.5) lcd.println('Connect to WLAN.') def __acceptWebSocketCallback(webSocket, httpClient): print("WS ACCEPT") webSocket.SendText(json.dumps(['login', 'SyncSyncSync'])) webSocket.RecvTextCallback = __recvTextCallback # webSocket.RecvBinaryCallback = self.__recvBinaryCallback # webSocket.ClosedCallback = self.__closedCallback def __recvTextCallback(webSocket, msg): msg = eval(msg) # ['command', 'parameter'] cmd = msg[0] para = msg[1] print('Receive command: %s' % cmd) if cmd == 'ls': webSocket.SendText(json.dumps(['ls', uos.listdir()])) elif cmd == 'cd': uos.chdir(para) webSocket.SendText(json.dumps(['cd', uos.listdir()])) elif cmd == 'get': with open(para, 'r') as o: webSocket.SendText(json.dumps(['get', o.read()])) elif cmd == 'post': with open(para, 'w') as o: o.write(msg[2]) webSocket.SendText(json.dumps(['post', 1])) wifiCheck() ws = MicroWebSrv(port=8000) ws.MaxWebSocketRecvLen = 256 # ws.WebSocketThreaded = False ws.AcceptWebSocketCallback = __acceptWebSocketCallback ws.Start(threaded=False)
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 _closedCallback(webSocket): with _thread.allocate_lock(): websockets.remove(webSocket) gc.collect() print('closing stale websocket') print('number of connections: {}'.format(len(websockets))) print('free mem: {}'.format(gc.mem_free())) def _acceptWebSocketCallback(webSocket, httpClient): with _thread.allocate_lock(): websockets.append(webSocket) gc.collect() print('new connection from {}'.format(httpClient.GetIPAddr())) print('number of connections: {}'.format(len(websockets))) webSocket.RecvTextCallback = _recvTextCallback webSocket.ClosedCallback = _closedCallback webSocket.SendText(str(hue)) print('free mem: {}'.format(gc.mem_free())) mws = MicroWebSrv(webPath='/www') mws.MaxWebSocketRecvLen = 256 mws.AcceptWebSocketCallback = _acceptWebSocketCallback mws.Start() _thread.start_new_thread(run_main_loop, ())
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()
def _recvBinaryCallback(webSocket, data): print(data) try: new_sequence = open("/sd/" + playfile + "/dat.dat", 'a') new_sequence.write(data) new_sequence.close() webSocket.SendText("OK") except: webSocket.SendText("FAIL") def _closedCallback(webSocket): print("WS Closed, now play") animation() # ------------------[Start Server]-------------------------------------------- print("Start Server") srv = MicroWebSrv(webPath='www/') srv.MaxWebSocketRecvLen = 512 #srv.WebSocketThreaded = True srv.AcceptWebSocketCallback = _acceptWebSocketCallback srv.Start(threaded=True) # ---------------------------------------------------------------------------- # ------------------[Auto Play]----------------------------------------------- print("Auto Play") animation() # ----------------------------------------------------------------------------
def start_webserver(): """start_webserver""" srv = MicroWebSrv(webPath='www/') srv.MaxWebSocketRecvLen = 256 srv.WebSocketThreaded = False srv.Start()