From cd015c8149b1c9c204cc9210dd5eb26e731bb880 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Tue, 27 Apr 2021 19:15:29 +0300 Subject: [PATCH] first results and a basic web server --- boot.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 boot.py diff --git a/boot.py b/boot.py new file mode 100644 index 0000000..52c2287 --- /dev/null +++ b/boot.py @@ -0,0 +1,54 @@ +from time import sleep +import machine +import network +import usocket as socket + +import esp +esp.osdebug(True) + +global server_socket + +wlan_ap = network.WLAN(network.AP_IF) +wlan_ap.config(dhcp_hostname="esp_root") # open +wlan_ap.active(True) +wlan_ap.config(essid="ESP32" , password="password" ) # open + + +station = network.WLAN(network.STA_IF) +station.active(True) +print(station.scan()) +station.config(dhcp_hostname='esp_root') +station.connect('TP-Link_Extender', '') +name = station.config('dhcp_hostname') +print("DNS " + name) + +addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1] + +server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +server_socket.bind(('', 80)) +server_socket.listen(5) + + +print(wlan_ap.ifconfig()) +print('Listening on:', addr) + + +while True: + + try: + conn, addr = server_socket.accept() + conn.settimeout(3.0) + print('Got a connection from %s' % str(addr)) + request = conn.recv(1024) + conn.settimeout(None) + request = str(request) + print('Content = %s' % request) + response = "Ho" + conn.send('HTTP/1.1 200 OK\n') + conn.send('Content-Type: text/html\n') + conn.send('Connection: close\n\n') + conn.sendall(response) + conn.close() + except OSError as e: + conn.close() + print('Connection closed')