Add wifi ip and gateway

This commit is contained in:
2024-12-11 22:42:56 +13:00
parent f23d040236
commit df808f4ffa
6 changed files with 108 additions and 24 deletions

View File

@@ -1,4 +1,4 @@
from machine import Pin
from machine import Pin, reset
from patterns import Patterns
from settings import Settings
import socket
@@ -7,6 +7,7 @@ import json
import utime
import sys
import espnow
import wifi
class LEDServer:
SETTINGS_FILE = "/settings.json" # Path should be adjusted for MicroPython's filesystem
@@ -47,7 +48,15 @@ class LEDServer:
self.css = self.read_file("/main.css").encode('utf-8')
self.patterns_json = json.dumps(list(self.patterns.patterns.keys()))
ssid = self.settings["wifi"]["ssid"]
password = self.settings["wifi"]["password"]
ip = self.settings.get("wifi", {}).get("ip", None)
gateway = self.settings.get("wifi", {}).get("gateway", None)
wifi.ap(password="qwerty")
config = wifi.do_connect(ssid, password, ip, gateway)
if config is not None:
print(config)
@@ -67,7 +76,9 @@ class LEDServer:
"/delay": self.delay,
"/brightness": self.brightness,
"/color": self.color,
"/color2": self.color2
"/color2": self.color2,
"/wifi_settings": self.wifi_settings,
"set_leds": self.set_leds
}
if path in paths:
@@ -130,6 +141,37 @@ class LEDServer:
client_socket.send(b'HTTP/1.0 200 OK\r\n\r\n')
except:
client_socket.send(b'HTTP/1.0 400 Bad request\r\n\r\n')
def wifi_settings(self, client_socket, post_data):
try:
lines = post_data.split('\n')
ssid, password, ip, gateway = (lines + ["", "", None, None])[:4]
self.settings["wifi"]["ssid"] = ssid
self.settings["wifi"]["password"] = password
self.settings["wifi"]["ip"] = ip
self.settings["wifi"]["gateway"] = gateway
self.settings.save()
self.settings.load()
print(self.settings)
print(ssid, password)
config = wifi.do_connect(ssid, password, ip ,gateway)
if config is not None:
print(config)
client_socket.send(b'HTTP/1.0 200 OK\r\n\r\n')
else:
client_socket.send(b'HTTP/1.0 500 Failed to connect to wifi\r\n\r\n')
except ValueError:
client_socket.send(b'HTTP/1.0 400 Bad request\r\n\r\n')
def set_leds(self, client_socket, post_data):
if len(post_data == self.settings["num_leds"]*3):
print(post_data)
self.patterns.set(0, (255,255,255))
else:
print("Wrong number of leds")
def handle_get(self, path, client_socket):
if path == "/":
@@ -141,7 +183,12 @@ class LEDServer:
brightness=self.settings["brightness"],
color=self.settings["color1"],
color2=self.settings["color2"],
ssid=self.settings["wifi"]["ssid"]).encode())
ssid=self.settings["wifi"]["ssid"],
password=self.settings["wifi"]["password"],
ip=self.settings.get("wifi", {}).get("ip", ""),
gateway=self.settings.get("wifi", {}).get("gateway", "").encode() # Only encode if it's a string
))
elif path == "/main.js":
client_socket.send(b'HTTP/1.0 200 OK\r\nContent-type: application/javascript\r\n\r\n')
client_socket.send(self.js)
@@ -195,7 +242,10 @@ class LEDServer:
self.server_socket.close()
self.settings.save()
# Example of creating and starting the server
if __name__ == "__main__":
def main():
server = LEDServer()
server.start()
# Example of creating and starting the server
if __name__ == "__main__":
main()