Skip to content

Commit eec83d6

Browse files
authored
#158 Re-Calculate URLs on WiFi reconnect (#159)
1 parent 7a29a31 commit eec83d6

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

esp32-cam-webserver.ino

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,26 @@ void printLocalTime(bool extraData=false) {
263263
}
264264
}
265265

266+
void calcURLs() {
267+
// Set the URL's
268+
#if defined(URL_HOSTNAME)
269+
if (httpPort != 80) {
270+
sprintf(httpURL, "http://%s:%d/", URL_HOSTNAME, httpPort);
271+
} else {
272+
sprintf(httpURL, "http://%s/", URL_HOSTNAME);
273+
}
274+
sprintf(streamURL, "http://%s:%d/", URL_HOSTNAME, streamPort);
275+
#else
276+
Serial.println("Setting httpURL");
277+
if (httpPort != 80) {
278+
sprintf(httpURL, "http://%d.%d.%d.%d:%d/", ip[0], ip[1], ip[2], ip[3], httpPort);
279+
} else {
280+
sprintf(httpURL, "http://%d.%d.%d.%d/", ip[0], ip[1], ip[2], ip[3]);
281+
}
282+
sprintf(streamURL, "http://%d.%d.%d.%d:%d/", ip[0], ip[1], ip[2], ip[3], streamPort);
283+
#endif
284+
}
285+
266286
void WifiSetup() {
267287
// Feedback that we are now attempting to connect
268288
flashLED(300);
@@ -389,6 +409,7 @@ void WifiSetup() {
389409
Serial.printf("IP address: %d.%d.%d.%d\r\n",ip[0],ip[1],ip[2],ip[3]);
390410
Serial.printf("Netmask : %d.%d.%d.%d\r\n",net[0],net[1],net[2],net[3]);
391411
Serial.printf("Gateway : %d.%d.%d.%d\r\n",gw[0],gw[1],gw[2],gw[3]);
412+
calcURLs();
392413
// Flash the LED to show we are connected
393414
for (int i = 0; i < 5; i++) {
394415
flashLED(50);
@@ -434,6 +455,7 @@ void WifiSetup() {
434455
gw = WiFi.gatewayIP();
435456
strcpy(apName, stationList[0].ssid);
436457
Serial.printf("IP address: %d.%d.%d.%d\r\n",ip[0],ip[1],ip[2],ip[3]);
458+
calcURLs();
437459
// Flash the LED to show we are connected
438460
for (int i = 0; i < 5; i++) {
439461
flashLED(150);
@@ -693,21 +715,6 @@ void setup() {
693715
// Now we have a network we can start the two http handlers for the UI and Stream.
694716
startCameraServer(httpPort, streamPort);
695717

696-
#if defined(URL_HOSTNAME)
697-
if (httpPort != 80) {
698-
sprintf(httpURL, "http://%s:%d/", URL_HOSTNAME, httpPort);
699-
} else {
700-
sprintf(httpURL, "http://%s/", URL_HOSTNAME);
701-
}
702-
sprintf(streamURL, "http://%s:%d/", URL_HOSTNAME, streamPort);
703-
#else
704-
if (httpPort != 80) {
705-
sprintf(httpURL, "http://%d.%d.%d.%d:%d/", ip[0], ip[1], ip[2], ip[3], httpPort);
706-
} else {
707-
sprintf(httpURL, "http://%d.%d.%d.%d/", ip[0], ip[1], ip[2], ip[3]);
708-
}
709-
sprintf(streamURL, "http://%d.%d.%d.%d:%d/", ip[0], ip[1], ip[2], ip[3], streamPort);
710-
#endif
711718
if (critERR.length() == 0) {
712719
Serial.printf("\r\nCamera Ready!\r\nUse '%s' to connect\r\n", httpURL);
713720
Serial.printf("Stream viewer available at '%sview'\r\n", streamURL);

0 commit comments

Comments
 (0)