From 9180ce6ca23b1833300bdd18042f0bf110f1a273 Mon Sep 17 00:00:00 2001 From: LeightonGinty Date: Sat, 16 Nov 2024 16:28:14 +0000 Subject: Add server --- alcogotchi-server/alcogotchi.py | 68 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 alcogotchi-server/alcogotchi.py diff --git a/alcogotchi-server/alcogotchi.py b/alcogotchi-server/alcogotchi.py new file mode 100644 index 0000000..8f5a033 --- /dev/null +++ b/alcogotchi-server/alcogotchi.py @@ -0,0 +1,68 @@ +from future import * +try: + import usocket as socket +except: + import socket +import network + +import esp +esp.osdebug(None) + +import gc +gc.collect() + +ssid = 'MicroPython-AP' +password = '123456789' + +ap = network.WLAN(network.AP_IF) +ap.active(True) +ap.config(essid=ssid, password=password) + +while ap.active() == False: + pass + +print('Connection successful') +print(ap.ifconfig()) + +def web_page(led_successful): + if led_successful == 1: + gpio_state="Drinking Booze" + else: + gpio_state="Not Drinking Booze :(" + + html = """ ESP Web Server +

ESP Web Server

+

Drinking state: """ + gpio_state + """

+

""" + return html + +s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +s.bind(('', 80)) +s.listen(5) + +while True: + conn, addr = s.accept() + print('Got a connection from %s' % str(addr)) + request = conn.recv(1024) + request = str(request) + print('Content = %s' % request) + drinking_booze = request.find('/?booze=yes') + not_drinking_booze = request.find('/?booze=no') + if drinking_booze == 6: + print('Drinking booze') + response = web_page(1) + + # led.value(1) + if not_drinking_booze == 6: + print('Not drinking booze') + response = web_page(0) + + # led.value(0) + 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() \ No newline at end of file -- cgit v1.2.3-70-g09d2