|
38 | 38 |
|
39 | 39 | #include <WiFi.h> |
40 | 40 |
|
41 | | -const char *ssid = "Wokwi-GUEST"; |
42 | | -const char *password = ""; |
| 41 | +String ssid = ""; |
| 42 | +String password = ""; |
43 | 43 |
|
44 | 44 | // WARNING: This function is called from a separate FreeRTOS task (thread)! |
45 | 45 | void WiFiEvent(WiFiEvent_t event) { |
@@ -87,14 +87,56 @@ void WiFiGotIP(WiFiEvent_t event, WiFiEventInfo_t info) { |
87 | 87 | Serial.println(IPAddress(info.got_ip.ip_info.ip.addr)); |
88 | 88 | } |
89 | 89 |
|
| 90 | +void readWiFiCredentials() { |
| 91 | + Serial.println("Waiting for WiFi credentials..."); |
| 92 | + Serial.println("Send SSID:"); |
| 93 | + |
| 94 | + // Wait for SSID |
| 95 | + while (ssid.length() == 0) { |
| 96 | + if (Serial.available()) { |
| 97 | + ssid = Serial.readStringUntil('\n'); |
| 98 | + ssid.trim(); |
| 99 | + } |
| 100 | + delay(100); |
| 101 | + } |
| 102 | + |
| 103 | + Serial.println("Send Password:"); |
| 104 | + |
| 105 | + // Wait for password (allow empty password) |
| 106 | + bool password_received = false; |
| 107 | + while (!password_received) { |
| 108 | + if (Serial.available()) { |
| 109 | + password = Serial.readStringUntil('\n'); |
| 110 | + password.trim(); |
| 111 | + password_received = true; // Accept even empty password |
| 112 | + } |
| 113 | + delay(100); |
| 114 | + } |
| 115 | + |
| 116 | + Serial.print("SSID: "); |
| 117 | + Serial.println(ssid); |
| 118 | + Serial.print("Password: "); |
| 119 | + Serial.println(password); |
| 120 | +} |
| 121 | + |
90 | 122 | void setup() { |
91 | 123 | Serial.begin(115200); |
92 | 124 |
|
| 125 | + while (!Serial) { |
| 126 | + delay(100); |
| 127 | + } |
| 128 | + |
93 | 129 | // delete old config |
94 | 130 | WiFi.disconnect(true); |
95 | 131 |
|
96 | 132 | delay(1000); |
97 | 133 |
|
| 134 | + // Wait for test to be ready |
| 135 | + Serial.println("Device ready for WiFi credentials"); |
| 136 | + |
| 137 | + // Read WiFi credentials from serial |
| 138 | + readWiFiCredentials(); |
| 139 | + |
98 | 140 | // Examples of different ways to register wifi events; |
99 | 141 | // these handlers will be called from another thread. |
100 | 142 | WiFi.onEvent(WiFiEvent); |
@@ -134,7 +176,7 @@ void setup() { |
134 | 176 | // Delete the scan result to free memory for code below. |
135 | 177 | WiFi.scanDelete(); |
136 | 178 |
|
137 | | - WiFi.begin(ssid, password); |
| 179 | + WiFi.begin(ssid.c_str(), password.c_str()); |
138 | 180 |
|
139 | 181 | Serial.println(); |
140 | 182 | Serial.println(); |
|
0 commit comments