Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import gc
- import network
- gc.collect()
- import machine
- gc.collect()
- import ubinascii
- gc.collect()
- import ujson
- gc.collect()
- import uos
- gc.collect()
- import utime
- gc.collect()
- import socket
- gc.collect()
- import select
- gc.collect()
- html = """<!DOCTYPE html>
- <html>
- <head> <title>Ouroboros IoT Login</title> </head>
- <body>
- <form action="configure.html" method="POST">
- Username : <input type="text" name="username"></br>
- Password: <input type="password" name="password" ></br>
- <input type="submit" value="submit" name="submit">
- </form>
- </body>
- </html>
- """
- login_fail_html = """<!DOCTYPE html>
- <html>
- <head> <title>Ouroboros IoT Login</title> </head>
- <body>
- <h2>Incorrect Credentials!</h2><br>Please login<br>
- <form action="configure.html" method="POST">
- Username : <input type="text" name="username"></br>
- Password: <input type="password" name="password" ></br>
- <input type="submit" value="submit" name="submit">
- </form>
- </body>
- </html>
- """
- # Check if file exists
- def fileExists(fileName):
- try:
- uos.stat(fileName)
- print("File " + fileName + " found!")
- return True
- except OSError:
- print("No file " + fileName + " found!")
- return False
- # Turns WiFi ON for configuration
- def turn_wifi_on():
- # Setup the AP interface
- ap_if = network.WLAN(network.AP_IF)
- ap_if.active(False)
- ap_if.active(True)
- # Get the MACADDRESS - without any spaces
- macaddress = ubinascii.hexlify(ap_if.config('mac'),'').decode()
- ap_if.config(essid="OUB1_"+macaddress, password="12345678")
- #ap_if.config(essid="OUB1_"+macaddress)
- ap_if.ifconfig(('192.168.0.1', '255.255.255.0', '192.168.0.1', '192.168.0.1'))
- # Configure the AP to static IPs
- def turn_wifi_off():
- ap_if = network.WLAN(network.AP_IF)
- ap_if.active(False)
- # Find out the stored IoT secret content
- def get_iot_secret():
- fileName = 'alpha.txt'
- if fileExists(fileName):
- f = open(fileName)
- content_str = f.read()
- f.close()
- return content_str
- else:
- return 'asdasrefwefefergf9rerf3n4r23irn1n32f'
- # Find out the stored home network credential if exist
- def get_wifi_config():
- fileName = 'wifi.conf'
- if fileExists(fileName):
- f = open(fileName)
- content_str = f.read()
- f.close()
- content = ujson.loads(content_str)
- return content
- else:
- return None
- # Set the home network credentials
- def save_wifi_config(essid, passphrase):
- f = open('wifi.conf', 'w')
- config = {'essid':essid, 'passphrase':passphrase}
- config_str = ujson.dumps(config)
- f.write(config_str)
- f.close()
- # Find out the stored login credentials
- def get_login_config():
- fileName = 'login.conf'
- if fileExists(fileName):
- f = open(fileName)
- content_str = f.read()
- f.close()
- content = ujson.loads(content_str)
- return content
- else:
- # No file exists so far, so use the admin/admin credentials
- return {'user':'admin','password':'admin'}
- # Set the login credentials
- def save_login_config(user, password):
- f = open('login.conf', 'w')
- config = {'user':user, 'password':password}
- config_str = ujson.dumps(config)
- f.write(config_str)
- f.close()
- def turn_gpio_on(device_num):
- # Device Num to Pin Mapping
- if device_num == 0:
- pin_num = 0
- elif device_num == 1:
- pin_num = 2
- # Check Pin
- pin = machine.Pin(pin_num)
- if pin.value() == 0:
- pin.on()
- # else it is already at HIGH state, nothing to do
- def turn_gpio_off(device_num):
- # Device Num to Pin Mapping
- if device_num == 0:
- pin_num = 0
- elif device_num == 1:
- pin_num = 2
- # Check Pin
- pin = machine.Pin(pin_num)
- if pin.value() == 1:
- pin.off()
- # else it is already at LOW state, nothing to do
- def init_pin(device_num):
- # Device Num to Pin Mapping
- if device_num == 0:
- pin_num = 0
- elif device_num == 1:
- pin_num = 2
- #open GPIO0 in output mode & turn it off by default
- pin = machine.Pin(pin_num, machine.Pin.OUT)
- # Turn off both GPIO initially
- turn_gpio_off(device_num)
- # Find out the post parameters in a dictionary
- def get_post_params(req):
- print("Inside GET POST PARAMS : req = " + req)
- post_params = req.split('rn')[-1:][0]
- # Check if the post body contains the necessary fields
- # Split the post_params by &
- # params : ['username=', 'password=', 'method=POST', 'url=http%3A%2F%2Ftwig-me.com%2Fv1%2Fusergroups%2FWKMUYXELA9LCC', 'jsondata=', 'submit=submit']
- print("post_params : " + post_params)
- params = post_params.split('&')
- print("Params")
- print(params)
- # Initialize the key value pair dict
- post_dict = {}
- # Iterate on each param
- for param in params:
- # Each param would be like 'method=POST', etc
- key_val = param.split('=')
- print("Key Val :")
- print(key_val)
- key = key_val[0]
- val = key_val[1]
- # Update post_dict
- post_dict[key] = val
- return post_dict
- # This web server takes care of the WiFi configuration
- # max_run_sec
- def web_server(max_run_sec = None):
- # Turn wifi interface ON
- turn_wifi_on()
- # Create server socket
- addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
- s = socket.socket()
- # TODO : If both the wifi and sta are operating simultaneously, then bind only to WiFi
- s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
- s.bind(addr)
- s.listen(1)
- # s.settimeout(1)
- poller = select.poll()
- poller.register(s, select.POLLIN)
- # Get the current time since epoch
- startTimeEpoch = utime.time()
- while True:
- events = poller.poll(200) # time in milliseconds
- if events:
- try:
- gc.collect()
- res = s.accept()
- client_s = res[0]
- client_addr = res[1]
- req = ''
- #while True:
- # data = client_s.recv(200)
- # if data:
- # req += str(data, 'utf8')
- # else:
- # break
- # utime.sleep_ms(50)
- req = client_s.recv(4096)
- req = req.decode()
- print(req)
- req = str(req)
- # Came here means that there has been some connection!
- # Reset the start time epoch in such a case:
- startTimeEpoch = utime.time()
- # Check route now
- if req.find('configure.html') != -1:
- print("Got configure request!rn")
- # Check if the username and password are correct, if not, configure:
- login_config = get_login_config()
- username = login_config['user']
- pwd = login_config['password']
- print("Username : " + username + ", pwd : " + pwd)
- # Find the POST PARAMETERS sent
- # There would be just one entry in the array, so get the 0th index directly
- # post_params : 'username=&password=&method=POST&url=http%3A%2F%2Ftwig-me.com%2Fv1%2Fusergroups%2FWKMUYXELA9LCC&jsondata=&submit=submit'
- print("Came here A")
- post_dict = get_post_params(req)
- # Now check if the post_dict has the key and value for username and password as needed?
- username_post = post_dict['username']
- password_post = post_dict['password']
- print("Came here B")
- # Check if the password is same as expected
- if (username_post == username) and (password_post == pwd):
- hidden_input = '<input type="hidden" name="username" value="' + username + '"><input type="hidden" name="passphrase" value="' + pwd + '">'
- # Send the login username and password inside the hidden input field
- configure_html = "<!DOCTYPE html><html><head> <title>Ouroboros IoT WiFi Configuration Page</title> </head><body><form action="configure_wifi.html" method="POST">WiFi SSID : <input type="text" name="essid"></br>WiFi Password: <input type="password" name="passphrase" ></br>" + hidden_input + "<input type="submit" value="submit" name="submit"></form></body></html>"
- # TODO : Also show link to webpage, where from we can change the login credentials
- client_s.send(configure_html)
- else:
- client_s.send(login_fail_html)
- elif req.find('configure_wifi.html') != -1:
- # Check if the username and password are correct, if not, configure:
- login_config = get_login_config()
- username = login_config['user']
- pwd = login_config['password']
- # Get post parameters
- post_dict = get_post_params(req)
- # Now check if the post_dict has the key and value for username and password as needed?
- username_post = post_dict['username']
- password_post = post_dict['password']
- # Check if the password is same as expected
- if (username_post == username) and (password_post == pwd):
- # Do some sanity check for handling the new wifi ssid and password
- new_wifi_ssid = post_dict['essid']
- new_wifi_passphrase = post_dict['passphrase']
- # Set the wifi credentials
- save_wifi_config(new_wifi_ssid, new_wifi_passphrase)
- client_s.send('<!DOCTYPE html><html><head> <title>Ouroboros IoT WiFi Configuration Success</title> </head><body>Configuration successful!<br>Device would go into reboot now!</body></html>')
- # Reboot device now
- machine.reset()
- else:
- client_s.send(login_fail_html)
- elif req.find('index.html') != -1:
- print("Got index.html request!rn")
- client_s.send(html)
- else :
- # Do nothing
- print("Invalid request received! Show the login page again!rn")
- client_s.send(html)
- client_s.close()
- machine.idle()
- except OSError:
- # Got no request and it timedout!
- print("Timed-out, no request received!rn")
- except Exception as e:
- print("Got some exceptionrn")
- print(str(e))
- finally:
- if max_run_sec is not None:
- elapsedTime = utime.time() - startTimeEpoch
- if elapsedTime > max_run_sec:
- # Max run time of web server has elapsed, time to exit this mode!
- break
- utime.sleep_ms()
- machine.idle()
- # When while loop ends!
- s.close()
- # Turn wifi interface OFF
- turn_wifi_off()
- # Starts a thread which runs the web server to handle WiFi
- def start_web_server(max_run_sec = None):
- # start_new_thread(web_server, (max_run_sec))
- web_server(max_run_sec)
- ############# MAIN ##########################
- # Initialize two pins to INPUT and OFF by default
- init_pin(0)
- init_pin(1)
- #turn_wifi_off()
- # Check if the home wifi network has been setup
- # Check if home wifi config is valid, if so, connect to it
- # If home wifi is not configured, then use the Web server all the time.
- if get_wifi_config() is None:
- # Came here means the wifi is not configured
- # Start the web server
- print("Starting web server")
- start_web_server()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement