Skip to content

Commit b87040f

Browse files
committed
disconnect wifi if already connected and get ntp time
1 parent e60c0c4 commit b87040f

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

gurgleapps_webserver.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ def __init__(self, wifi_ssid, wifi_password, port=80, timeout=20, doc_root="/www
7070
def connect_wifi(self, ssid, password):
7171
# Deactivate AP mode
7272
self.wlan_ap.active(False)
73+
if self.wlan_sta.isconnected():
74+
print("Already connected to Wi-Fi. IP: "+self.wlan_sta.ifconfig()[0])
75+
self.wlan_sta.disconnect()
76+
self.wlan_sta.active(False)
77+
time.sleep(1)
78+
print("Disconnected from Wi-Fi.")
79+
7380
# Activate Wi-Fi mode and connect
7481
self.wlan_sta.active(True)
7582
self.wlan_sta.connect(ssid, password)
@@ -84,10 +91,12 @@ def connect_wifi(self, ssid, password):
8491
print("Failed to connect to Wi-Fi.")
8592
return False
8693

87-
def connect_access_point(self, ssid, password=None):
94+
def start_access_point(self, ssid, password=None):
95+
#def connect_access_point(self, ssid, password=None, ip='192.168.1.1', subnet='255.255.255.0', gateway='192.168.1.1', dns='8.8.8.8'):
8896
# Deactivate Wi-Fi mode
89-
self.wlan_sta.active(False)
90-
# Activate AP mode
97+
# self.wlan_sta.active(False)
98+
# Set the IP configuration for the AP mode
99+
#self.wlan_ap.ifconfig((ip, subnet, gateway, dns))
91100
self.wlan_ap.config(essid=ssid, password=password)
92101
self.wlan_ap.active(True)
93102
self.ip_address = self.wlan_ap.ifconfig()[0]

main.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from gurgleapps_webserver import GurgleAppsWebserver
99
import config
1010
import utime as time
11+
import ntptime
1112
import uasyncio as asyncio
1213
from machine import Pin
1314
import ujson as json
@@ -72,6 +73,21 @@ async def start_flashing(request, response):
7273
status = True
7374
await send_status(request, response)
7475

76+
async def get_time(request, response):
77+
if not server.wlan_sta.isconnected():
78+
response_string = json.dumps({"error": True, "message": "Not connected to wifi", "time": time.localtime()})
79+
await response.send_json(response_string, 200)
80+
return
81+
try:
82+
ntptime.host = "pool.ntp.org"
83+
#ntptime.host = "time.nist.gov"
84+
ntptime.settime()
85+
response_string = json.dumps({"error": False, "time": time.localtime()})
86+
await response.send_json(response_string, 200)
87+
except Exception as e:
88+
response_string = json.dumps({"error": True, "message": str(e), "time": time.localtime()})
89+
await response.send_json(response_string, 200)
90+
7591
async def stop_server(request, response):
7692
global shutdown
7793
await response.send_html("Server stopping")
@@ -121,6 +137,7 @@ async def main():
121137
server.add_function_route("/hello/<name>", say_hello)
122138
server.add_function_route("/stop-server", stop_server)
123139
server.add_function_route("/run-as-access-point", run_as_access_point)
140+
server.add_function_route("/get-time", get_time)
124141

125142
asyncio.run(server.start_server_with_background_task(main))
126143
print('DONE')

0 commit comments

Comments
 (0)