wifi json
This commit is contained in:
parent
36a995c510
commit
bc99f15219
10
README.md
10
README.md
|
@ -62,6 +62,7 @@ Description=Hotspot Service
|
||||||
After=network.target
|
After=network.target
|
||||||
|
|
||||||
[Service]
|
[Service]
|
||||||
|
ExecPre=/bin/sleep 5
|
||||||
ExecStart=create_ap wlan0 eth0 Takway-Toys --no-virt
|
ExecStart=create_ap wlan0 eth0 Takway-Toys --no-virt
|
||||||
User=root
|
User=root
|
||||||
|
|
||||||
|
@ -174,7 +175,7 @@ nmcli dev wifi connect Innoxsz-Public password innox2023
|
||||||
- 断开Wi-Fi连接:
|
- 断开Wi-Fi连接:
|
||||||
|
|
||||||
```
|
```
|
||||||
nmcli dev disconnect iface wlan0
|
nmcli dev disconnect wlan0
|
||||||
```
|
```
|
||||||
|
|
||||||
- 扫描Wi-Fi:
|
- 扫描Wi-Fi:
|
||||||
|
@ -187,3 +188,10 @@ nmcli dev wifi
|
||||||
```
|
```
|
||||||
nmcli dev status
|
nmcli dev status
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### create_ap
|
||||||
|
|
||||||
|
```
|
||||||
|
sudo create_ap wlan0 eth0 Takway-Toys --no-virt
|
||||||
|
+```
|
|
@ -5,6 +5,7 @@ import psutil
|
||||||
import signal
|
import signal
|
||||||
import time
|
import time
|
||||||
import datetime
|
import datetime
|
||||||
|
import json
|
||||||
from flask import Flask, render_template, request, redirect, url_for, make_response
|
from flask import Flask, render_template, request, redirect, url_for, make_response
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -71,7 +72,7 @@ def check_wifi_connection():
|
||||||
if led_enabled:
|
if led_enabled:
|
||||||
orangepi.set_led_off('blue')
|
orangepi.set_led_off('blue')
|
||||||
return True, wifi_ssid
|
return True, wifi_ssid
|
||||||
return False
|
return False, None
|
||||||
|
|
||||||
def scan_wifi():
|
def scan_wifi():
|
||||||
subprocess.run(['nmcli', 'dev', 'wifi', 'rescan'], check=True)
|
subprocess.run(['nmcli', 'dev', 'wifi', 'rescan'], check=True)
|
||||||
|
@ -127,13 +128,25 @@ def scan_wifi():
|
||||||
if len(wifi_list) == 15:
|
if len(wifi_list) == 15:
|
||||||
break
|
break
|
||||||
# save wifi_list to file
|
# save wifi_list to file
|
||||||
with open('wifi_list.txt', 'w') as f:
|
with open('wifi_list.json', 'w') as f:
|
||||||
for wifi in wifi_list:
|
json.dump(wifi_list, f)
|
||||||
print(f"{wifi}")
|
|
||||||
f.write(f"{wifi}\n")
|
|
||||||
|
|
||||||
return wifi_list
|
return wifi_list
|
||||||
|
|
||||||
|
def load_saved_wifi():
|
||||||
|
try:
|
||||||
|
with open('wifi_list.json', 'r') as f:
|
||||||
|
wifi_list = json.load(f)
|
||||||
|
return wifi_list
|
||||||
|
except FileNotFoundError:
|
||||||
|
return []
|
||||||
|
|
||||||
|
def save_wifi(ssid, password):
|
||||||
|
wifi_list = load_saved_wifi()
|
||||||
|
wifi_list.append({'ssid': ssid, 'password': password})
|
||||||
|
with open('wifi_list.json', 'w') as f:
|
||||||
|
json.dump(wifi_list, f)
|
||||||
|
|
||||||
# 连接 Wi-Fi
|
# 连接 Wi-Fi
|
||||||
def connect_wifi(ssid, password):
|
def connect_wifi(ssid, password):
|
||||||
# 连接到用户选择的 Wi-Fi 网络
|
# 连接到用户选择的 Wi-Fi 网络
|
||||||
|
@ -142,6 +155,7 @@ def connect_wifi(ssid, password):
|
||||||
output_str = output.decode('utf-8') # 将输出转换为字符串
|
output_str = output.decode('utf-8') # 将输出转换为字符串
|
||||||
if "successfully" in output_str:
|
if "successfully" in output_str:
|
||||||
print(f"{datetime.datetime.now()}: Successfully connected to Wi-Fi: {ssid}")
|
print(f"{datetime.datetime.now()}: Successfully connected to Wi-Fi: {ssid}")
|
||||||
|
save_wifi(ssid, password) # 保存连接成功的Wi-Fi信息
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
print(f"{datetime.datetime.now()}: Error connecting to Wi-Fi: {output_str}")
|
print(f"{datetime.datetime.now()}: Error connecting to Wi-Fi: {output_str}")
|
||||||
|
@ -193,10 +207,13 @@ def submit():
|
||||||
if connect_wifi(ssid, password):
|
if connect_wifi(ssid, password):
|
||||||
close_app()
|
close_app()
|
||||||
|
|
||||||
if not check_wifi_connection():
|
connected, wifi_ssid = check_wifi_connection()
|
||||||
|
if not connected:
|
||||||
print(f"{datetime.datetime.now()}: Wi-Fi连接失败。")
|
print(f"{datetime.datetime.now()}: Wi-Fi连接失败。")
|
||||||
wifi_list = scan_wifi()
|
wifi_list = scan_wifi()
|
||||||
start_hotspot()
|
start_hotspot()
|
||||||
|
else:
|
||||||
|
save_wifi(wifi_ssid, password) # 保存连接成功的Wi-Fi信息
|
||||||
|
|
||||||
return redirect(url_for('index'))
|
return redirect(url_for('index'))
|
||||||
|
|
||||||
|
@ -218,13 +235,14 @@ if __name__ == '__main__':
|
||||||
if led_enabled:
|
if led_enabled:
|
||||||
orangepi.set_led_on('blue')
|
orangepi.set_led_on('blue')
|
||||||
|
|
||||||
if check_wifi_connection():
|
connected, wifi_ssid = check_wifi_connection()
|
||||||
|
if connected:
|
||||||
print(f"{datetime.datetime.now()}: 系统已自动连接到 Wi-Fi 网络,退出程序")
|
print(f"{datetime.datetime.now()}: 系统已自动连接到 Wi-Fi 网络,退出程序")
|
||||||
close_app()
|
close_app()
|
||||||
else:
|
else:
|
||||||
|
wifi_list = load_saved_wifi()
|
||||||
|
if not wifi_list:
|
||||||
wifi_list = scan_wifi()
|
wifi_list = scan_wifi()
|
||||||
print(f"{datetime.datetime.now()}: 未连接到 Wi-Fi 网络")
|
print(f"{datetime.datetime.now()}: 未连接到 Wi-Fi 网络")
|
||||||
start_hotspot()
|
start_hotspot()
|
||||||
app.run(host='0.0.0.0', port=80)
|
app.run(host='0.0.0.0', port=80)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue