Created
May 9, 2023 13:21
-
-
Save ishahak/12d87ba65431b31e5b29b1a9a0c64c34 to your computer and use it in GitHub Desktop.
Micropython demo for starting access-point and listening to both connections and serial commands
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import network | |
import time | |
import machine | |
import select | |
import sys | |
import os | |
local_IP = '192.168.31.10' | |
subnet = '255.255.255.0' | |
gateway = '192.168.31.1' | |
dns = '8.8.8.8' | |
ssid = 'pbio-board' | |
password = 'popeye31' | |
ap = network.WLAN(network.AP_IF) | |
if ap.isconnected(): | |
print('disconnecting previous network') | |
ap.active(False) | |
time.sleep(1) | |
ap.ifconfig([local_IP, subnet, gateway, dns]) | |
ap.config(essid=ssid, password=password, authmode=network.AUTH_WPA_WPA2_PSK) | |
ap.active(True) | |
while ap.active() == False: | |
pass | |
print('Connection successful') | |
print(ap.ifconfig()) | |
def web_page(): | |
return '<html><body><ol>~{}~</ol></body></html>'; | |
html = """<html><head><meta name="viewport" content="width=device-width, initial-scale=1"></head> | |
<body style="background-color:powderblue;"><h1 style="text-align:center; font-size:40px;color:#ed0e42;">precise-bio</h1><h2 style="text-align:center; font-size:20px;"></h2><ol>~{}~</ol></body></html>""" | |
return html | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
s.bind(('', 80)) | |
s.listen(5) | |
clients = [(local_IP, 'access-point')] | |
spoll = select.poll() | |
spoll.register(sys.stdin, select.POLLIN) | |
spoll.register(s, select.POLLIN) | |
def do_command(cmd): | |
if cmd == 'q': | |
print('exiting') | |
sys.exit(0) | |
else: | |
print('unknown command:', cmd) | |
cmd_str = '' | |
while True: | |
events = spoll.poll(400) | |
if len(events) > 0: | |
# print(events) | |
for obj, event in events: | |
# print(dir(obj)) | |
# print(dir(event)) | |
is_socket = type(obj) == socket.socket | |
# is_stdin = str(obj) == '<io.FileIO 0>' | |
# print(f'obj={obj}, event={event}, type=', end='') | |
if event & select.POLLIN: | |
# print("POLLIN") | |
if is_socket: | |
conn, addr = s.accept() | |
# print('Connection established from %s' % str(addr)) | |
request = conn.recv(2048).decode('utf-8') | |
request_lines = request.split('\r\n') | |
method, path, http_version = request_lines[0].split() | |
response = 'HTTP/1.1 200 OK\r\n' | |
# response += 'Content-Type: text/html\r\n' | |
# response += 'Connection: close\r\n' | |
response += '\r\n' | |
# print('request = %s' % request) | |
code = path.split('/') | |
# print('splt:', code) | |
code = code[1] if len(code) > 1 else 'none' | |
if code == '': | |
code = 'none' | |
ip = addr[0] | |
entry = (ip, code) | |
# print('entry:', entry) | |
if not entry in clients: | |
clients.append(entry) | |
print('>>> new entry added:', entry) | |
ip_list = ''.join([f'<li>{ip}</li>' for ip in clients]) | |
response += web_page().replace('~{}~', ip_list) | |
conn.sendall(response) | |
conn.close() | |
else: # we can assume it is stdin: | |
data = sys.stdin.read(1) | |
if data == '\n' or data == '\r': | |
if cmd_str != '': | |
do_command(cmd_str) | |
cmd_str = '' | |
else: | |
cmd_str += data | |
# print(f'got data: {data}') | |
elif event & select.POLLHUP: | |
print("<POLLHUP>") | |
elif event & select.POLLERR: | |
print("<POLLERR>") | |
else: | |
print("<??>") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment