Add wifi ip and gateway
This commit is contained in:
parent
f23d040236
commit
df808f4ffa
|
@ -34,4 +34,3 @@
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
import wifi
|
|
|
@ -53,6 +53,12 @@
|
||||||
<label for="password">Wi-Fi Password:</label>
|
<label for="password">Wi-Fi Password:</label>
|
||||||
<input type="password" id="password" name="password">
|
<input type="password" id="password" name="password">
|
||||||
<br>
|
<br>
|
||||||
|
<label for="ip">Wi-Fi IP:</label>
|
||||||
|
<input type="ip" id="ip" name="ip" value="{ip}">
|
||||||
|
<br>
|
||||||
|
<label for="gateway">Wi-Fi Gateway:</label>
|
||||||
|
<input type="gateway" id="gateway" name="gateway" value="{gateway}">
|
||||||
|
<br>
|
||||||
<input type="submit" value="Save Wi-Fi Settings">
|
<input type="submit" value="Save Wi-Fi Settings">
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
|
11
src/main.js
11
src/main.js
|
@ -84,8 +84,17 @@ async function updateWifi(event) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
const ssid = document.getElementById('ssid').value;
|
const ssid = document.getElementById('ssid').value;
|
||||||
const password = document.getElementById('password').value;
|
const password = document.getElementById('password').value;
|
||||||
|
const ip = document.getElementById('ip').value;
|
||||||
|
const gateway = document.getElementById('gateway').value;
|
||||||
|
if(!ssid || !password) {
|
||||||
|
alert("Missing ssid or password");
|
||||||
|
return;
|
||||||
|
}
|
||||||
console.log(ssid, password);
|
console.log(ssid, password);
|
||||||
//await post('/num_leds', numLeds);
|
const response = await post('/wifi_settings', ssid+"\n"+password+"\n"+ip+"\n"+gateway);
|
||||||
|
if(response === 500) {
|
||||||
|
alert("Failed to connect to wifi");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function createPatternButtons(patterns) {
|
function createPatternButtons(patterns) {
|
||||||
|
|
62
src/main.py
62
src/main.py
|
@ -1,4 +1,4 @@
|
||||||
from machine import Pin
|
from machine import Pin, reset
|
||||||
from patterns import Patterns
|
from patterns import Patterns
|
||||||
from settings import Settings
|
from settings import Settings
|
||||||
import socket
|
import socket
|
||||||
|
@ -7,6 +7,7 @@ import json
|
||||||
import utime
|
import utime
|
||||||
import sys
|
import sys
|
||||||
import espnow
|
import espnow
|
||||||
|
import wifi
|
||||||
|
|
||||||
class LEDServer:
|
class LEDServer:
|
||||||
SETTINGS_FILE = "/settings.json" # Path should be adjusted for MicroPython's filesystem
|
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.css = self.read_file("/main.css").encode('utf-8')
|
||||||
self.patterns_json = json.dumps(list(self.patterns.patterns.keys()))
|
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,
|
"/delay": self.delay,
|
||||||
"/brightness": self.brightness,
|
"/brightness": self.brightness,
|
||||||
"/color": self.color,
|
"/color": self.color,
|
||||||
"/color2": self.color2
|
"/color2": self.color2,
|
||||||
|
"/wifi_settings": self.wifi_settings,
|
||||||
|
"set_leds": self.set_leds
|
||||||
}
|
}
|
||||||
|
|
||||||
if path in paths:
|
if path in paths:
|
||||||
|
@ -130,6 +141,37 @@ class LEDServer:
|
||||||
client_socket.send(b'HTTP/1.0 200 OK\r\n\r\n')
|
client_socket.send(b'HTTP/1.0 200 OK\r\n\r\n')
|
||||||
except:
|
except:
|
||||||
client_socket.send(b'HTTP/1.0 400 Bad request\r\n\r\n')
|
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):
|
def handle_get(self, path, client_socket):
|
||||||
if path == "/":
|
if path == "/":
|
||||||
|
@ -141,7 +183,12 @@ class LEDServer:
|
||||||
brightness=self.settings["brightness"],
|
brightness=self.settings["brightness"],
|
||||||
color=self.settings["color1"],
|
color=self.settings["color1"],
|
||||||
color2=self.settings["color2"],
|
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":
|
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(b'HTTP/1.0 200 OK\r\nContent-type: application/javascript\r\n\r\n')
|
||||||
client_socket.send(self.js)
|
client_socket.send(self.js)
|
||||||
|
@ -195,7 +242,10 @@ class LEDServer:
|
||||||
self.server_socket.close()
|
self.server_socket.close()
|
||||||
self.settings.save()
|
self.settings.save()
|
||||||
|
|
||||||
# Example of creating and starting the server
|
def main():
|
||||||
if __name__ == "__main__":
|
|
||||||
server = LEDServer()
|
server = LEDServer()
|
||||||
server.start()
|
server.start()
|
||||||
|
|
||||||
|
# Example of creating and starting the server
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
|
@ -60,6 +60,12 @@ class Patterns:
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def set(self, i, color):
|
||||||
|
self.n[i] = color
|
||||||
|
|
||||||
|
def write(self):
|
||||||
|
self.n.write()
|
||||||
|
|
||||||
def fill(self):
|
def fill(self):
|
||||||
for i in range(self.num_leds):
|
for i in range(self.num_leds):
|
||||||
self.n[i] = self.color1
|
self.n[i] = self.color1
|
||||||
|
|
46
src/wifi.py
46
src/wifi.py
|
@ -4,25 +4,36 @@ from config import *
|
||||||
from time import sleep
|
from time import sleep
|
||||||
import ubinascii
|
import ubinascii
|
||||||
|
|
||||||
def do_connect():
|
def do_connect(ssid, password, ip=None, gateway=None):
|
||||||
led = Pin(8, Pin.OUT)
|
led = Pin(8, Pin.OUT)
|
||||||
sta_if = network.WLAN(network.STA_IF)
|
try:
|
||||||
sta_if.ifconfig((ip, '255.255.255.0', gateway, '1.1.1.1'))
|
sta_if = network.WLAN(network.STA_IF)
|
||||||
if not sta_if.isconnected():
|
sta_if.disconnect()
|
||||||
print('connecting to network...')
|
print(ip, gateway)
|
||||||
sta_if.active(True)
|
if ip and gateway:
|
||||||
sta_if.connect(ssid, password)
|
print(ip, gateway)
|
||||||
led.on()
|
sta_if.ifconfig((ip, '255.255.255.0', gateway, '1.1.1.1'))
|
||||||
for i in range(10):
|
if not sta_if.isconnected():
|
||||||
if sta_if.isconnected():
|
print(f'connecting to network {ssid}')
|
||||||
print('network config:', sta_if.ifconfig())
|
sta_if.active(True)
|
||||||
led.off()
|
sta_if.connect(ssid, password)
|
||||||
break
|
led.on()
|
||||||
sleep(1)
|
for i in range(10):
|
||||||
|
print(f"Attempt {i}")
|
||||||
do_connect()
|
if sta_if.isconnected():
|
||||||
|
led.off()
|
||||||
|
return sta_if.ifconfig()
|
||||||
|
sleep(0.5)
|
||||||
|
return None
|
||||||
|
|
||||||
|
else:
|
||||||
|
return sta_if.ifconfig()
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Failed to connect to wifi {e}")
|
||||||
|
return None
|
||||||
|
|
||||||
def ap(password):
|
def ap(password):
|
||||||
|
pass
|
||||||
ap_if = network.WLAN(network.AP_IF)
|
ap_if = network.WLAN(network.AP_IF)
|
||||||
ap_if.active(True)
|
ap_if.active(True)
|
||||||
ap_mac = ap_if.config('mac')
|
ap_mac = ap_if.config('mac')
|
||||||
|
@ -35,3 +46,6 @@ def ap(password):
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue