def start(_sdb): global dns, web, sdb sdb = _sdb ## Starting Wifi Access Poijnt wlan.active(1) ## Setting Up Capitve portal ip = wlan.ifconfig()[0] dns = MicroDNSSrv() web = MicroWebSrv() web.SetNotFoundPageUrl("http://my-fajr-clock.wifi/status") dns.SetDomainsList({"*": ip}) dns.Start() web.Start(True)
import os import machine def reset(): machine.reset() def ls(path=''): for f in os.listdir(path): print(f) if not sta.active() and ap.active(): ip = ap.ifconfig()[0] # CAPTIVE PORTAL # MicroDNSSrv.Create({ '*' : ip }) elif sta.active() and not ap.active(): ip = sta.ifconfig()[0] try: if srv: srv.Stop() except NameError as err: srv = MicroWebSrv(webPath='') srv.SetNotFoundPageUrl("http://{}/index.html".format(ip)) srv.Start() kit.set_pixel(0, 0, [10, 10, 10]) kit.render()
def run_web(threaded): from microWebSrv import MicroWebSrv def delayed_reboot(delay=3.5): from time import sleep from machine import reset sleep(delay) reset() ####################################### URL Route handling ################################# @MicroWebSrv.route('/config') def _httpHandlerConfig(httpClient, httpResponse, args={}) : from utils import sys_config_html content = """\ <!DOCTYPE html> <html lang=en> <head> <meta charset="UTF-8" /> <title>CONFIG DUMP</title> </head> <body><PRE>""" content += sys_config_html() content += '<div style="alignt: center; text-align: center;">\n' content += '<P><h3><a href="/stats">status</a></h3></P>' content += "<br /><h5><a href=\"/config/reboot\">REBOOT?</A></h5>\n" content += "</div>" content += """\ </PRE> </body> </html> """ httpResponse.WriteResponseOk( headers = None, contentType = "text/html", contentCharset = "UTF-8", content = content ) ############################################## @MicroWebSrv.route('/config/reboot') @MicroWebSrv.route('/config/restart') def _httpHandlerReboot(httpClient, httpResponse, args={}) : from _thread import start_new_thread start_new_thread(delayed_reboot, ()) content = """\ <!DOCTYPE html> <html lang=en> <head> <meta charset="UTF-8" /> <meta http-equiv="refresh" content="8;url=/" /> <title>C U NEXT TUESDAY...</title> </head> <body><BR /><HR /><br /> <P><center><img src="/reboot.gif"></center></P> <BR /><HR /><br /></body> </html> """ httpResponse.WriteResponseOk( headers = None, contentType = "text/html", contentCharset = "UTF-8", content = content ) ############################################################ @MicroWebSrv.route('/config/toggleAP') def _httpHandlerToggle(httpClient, httpResponse, args={}) : from os import rename apMode = None try: x = open('/client.cfg') x.close() apMode = False rename("/client.cfg","/client.disable") except: try: x = open('client.disable') x.close() apMode = True rename("/client.disable","/client.cfg") except: return() content = """\ <!DOCTYPE html> <html lang=en> <head> <meta charset="UTF-8" /> <title>TEST EDIT</title> </head> <body> """ if(apMode): content += "<H2>Switched to Wifi Client Mode</H2>" content += '<div style="alignt: center; text-align: center;">\n' content += '<P><h3><a href="/config/">config</a></h3></P>' content += "<br /><h5><a href=\"/config/reboot\">REBOOT?</A></h5>\n" content += "</div>" else: content += "<H2>Switched to Wifi AP Mode</H2>" content += '<div style="alignt: center; text-align: center;">\n' content += '<P><h3><a href="/config/">config</a></h3></P>' content += "<br /><h5><a href=\"/config/reboot\">REBOOT?</A></h5>\n" content += "</div>" content += """\ </body> </html> """ httpResponse.WriteResponseOk( headers = None, contentType = "text/html", contentCharset = "UTF-8", content = content ) ############################################## @MicroWebSrv.route('/config/<param>/<value>') def _httpHandlerEditParams(httpClient, httpResponse, args={}) : from utils import replaceLine, findValue content = """\ <!DOCTYPE html> <html lang=en> <head> <meta charset="UTF-8" /> <title>-=DAT CONFIG=-</title> </head> <body> """ content += "<h3>CONFIG UPDATE</h3>" if 'param' in args : param = args['param'].strip() oldline = findValue('/config.py',param) content += "<p>[config: {}]</p>".format(oldline) content += "<br />" if 'value' in args : if(type(args['value']) == int): value = str(args['value']) else: value = args['value'].strip() content += "<p>[new value is {}]</p>".format(value) content += "<br />" if( ('value' in args) and ('param' in args) ): #re = args['param'].strip() + " = ([A-Za-z\"]+)" re = oldline if((value == str(1)) or ('rue' in value)): value = "True" elif((value == str(0)) or ('alse' in value)): value = "False" else: value = '"' + value + '"' newline = args['param'].strip() + " = " + value + "\n" content += "<P>" + newline + "</P><BR />" print("CONFIGURE:",param,' - ',value,' - ', re, '\n',newline) replaceLine("/config.py",re,newline) content += "<p>parameters updated.</p>" content += '<div style="alignt: center; text-align: center;">\n' content += '<P><h3><a href="/config/">config</a></h3></P>' content += "<br /><h5><a href=\"/config/reboot\">REBOOT?</A></h5>\n" content += "</div>" content += """ </body> </html> """ httpResponse.WriteResponseOk( headers = None, contentType = "text/html", contentCharset = "UTF-8", content = content ) ############################################################ @MicroWebSrv.route('/config/<param>') def _httpHandlerEditParams(httpClient, httpResponse, args={}) : from utils import findValue content = """\ <!DOCTYPE html> <html lang=en> <head> <meta charset="UTF-8" /> <title>-=DAT CONFIG=-</title> </head> <body> """ content += "<h3>CONFIG REQUEST</h3>" if 'param' in args : param = args['param'].strip() oldline = findValue('/config.py',param) content += "<p>[config: {}]</p>".format(oldline) content += "<br />" content += '<div style="alignt: center; text-align: center;">\n' content += "<P><h5><a href='/config/" + param + "/1'>enable</a> / <a href='/config/" + param + "/0'>disable</a></h5></P>" content += '<P><h3><a href="/config/">config</a></h3></P>' content += "</div>" content += """ </body> </html> """ httpResponse.WriteResponseOk( headers = None, contentType = "text/html", contentCharset = "UTF-8", content = content ) ############################################################ @MicroWebSrv.route('/stats') @MicroWebSrv.route('/status') def _httpHandlerStats(httpClient, httpResponse, args={}) : from utils import sys_stats, sys_config global MyName, debug, debugDNS, ip, runREPL, runDNS, runWeb, threaded content = """\ <!DOCTYPE html> <html lang=en> <head> <meta charset="UTF-8" /> <meta http-equiv="refresh" content="2" /> <title>STATUS O****M</title> </head> <body> """ content += "<h2>SYSTEM STATS</h2>\n<PRE>" + sys_stats() + "</PRE>\n" content += "<H2>SYSTEM CONFIG</H2>\n<PRE>" + sys_config() + "</PRE>\n" content += "<h5><a href='/config'>configure</a></h5>" content += "<h3><a href='/'>HOME</a></h5>" content += """ </body> </html> """ httpResponse.WriteResponseOk( headers = None, contentType = "text/html", contentCharset = "UTF-8", content = content ) ############################################ End of Routes ################################################## mws = MicroWebSrv(port=80, bindIP='0.0.0.0', webPath="/www") mws.SetNotFoundPageUrl("http://"+ip) print("web server starting at http://" + ip + "/ .") mws.Start(threaded=threaded)
contentType="text/plain", contentCharset="UTF-8", content="") @MicroWebSrv.route('/api/log', 'GET') def get_logfile(httpClient, httpResponse): reset_causes = { machine.PWRON_RESET: 'PWRON', # Press reset button on FiPy machine.HARD_RESET: 'HARD', machine.WDT_RESET: 'WDT', # Upload and restart from USB or machine.reset() machine.DEEPSLEEP_RESET: 'DEEPSLEEP', machine.SOFT_RESET: 'SOFT', machine.BROWN_OUT_RESET: 'BROWN_OUT' } data = {} data['reset_cause'] = reset_causes[machine.reset_cause()] try: with open('/sd/hiverizelog/logging.csv') as f: data['logfile'] = f.read() except OSError as err: data['logfile'] = "Could not open logfile: {}".format(err) return httpResponse.WriteResponseJSONOk(obj=data, headers=_headers) print("in webserver.py") mws = MicroWebSrv() mws.SetNotFoundPageUrl("http://hiverize.wifi") MicroDNSSrv.Create({'*': '192.168.4.1'})
<body> <div class="colorpicker"></div> <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script> <script src='/tinycolor.min.js'></script> <script src="/index.js"></script> </body> </html> """ httpResponse.WriteResponseOk(headers=None, contentType="text/html", contentCharset="UTF-8", content=content) @MicroWebSrv.route('/test/', 'POST') def test_(httpClient, httpResponse): data = httpClient.ReadRequestContentAsJSON() r = int(data['code']['rgb']['r'] / 10) g = int(data['code']['rgb']['g'] / 10) b = int(data['code']['rgb']['b'] / 10) print(r, g, b) set_led((r, g, b)) httpResponse.WriteResponseRedirect('/test/') srv = MicroWebSrv(webPath="/") srv.SetNotFoundPageUrl(url="/test") srv.Start(threaded=True)
<h1>WiFi Config</h1> SSID = %s<br /> </body> </html> """ % (MicroWebSrv.HTMLEscape(ssid)) httpResponse.WriteResponseOk(headers=None, contentType="text/html", contentCharset="UTF-8", content=content) prefixes = ["!", "#", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/"] lines = ["Erp", "Arp", "Urp"] ap = network.WLAN(network.AP_IF) ap.active(True) ap.config(essid=">>>ESP<<<", authmode=0) ap.ifconfig(('10.0.0.1', '255.255.255.0', '10.0.0.1', '10.0.0.1')) srv = MicroWebSrv(webPath='www/') srv.SetNotFoundPageUrl("/test") while (1): srv.Stop() for line in lines: ap.config(essid=line, authmode=0) utime.sleep(10) srv.Start(threaded=False) utime.sleep(30) # ----------------------------------------------------------------------------