|
| 1 | +#include <WiFi.h> |
| 2 | +#include <WiFiClient.h> |
| 3 | +#include <WebServer.h> |
| 4 | +#include <ESPmDNS.h> |
| 5 | + |
| 6 | +/* |
| 7 | + * MultiHomedServers |
| 8 | + * |
| 9 | + * MultiHomedServers tests support for multi-homed servers, i.e. a distinct web servers on each IP interface. |
| 10 | + * It only tests the case n=2 because on a basic ESP32 device, we only have two IP interfaces, namely |
| 11 | + * the WiFi station interfaces and the WiFi soft AP interface. |
| 12 | + * |
| 13 | + * For this to work, the WebServer and the WiFiServer classes must correctly handle the case where an |
| 14 | + * IP address is passed to their relevant constructor. It also requires WebServer to work with multiple, |
| 15 | + * simultaneous instances. |
| 16 | + * |
| 17 | + * Testing the WebServer and the WiFiServer constructors was the primary purpose of this script. |
| 18 | + * The part of WebServer used by this sketch does seem to work with multiple, simultaneous instances. |
| 19 | + * However there is much functionality in WebServer that is not tested here. It may all be well, but |
| 20 | + * that is not proven here. |
| 21 | + * |
| 22 | + * This sketch starts the mDNS server, as did HelloServer, and it resolves esp32.local on both interfaces, |
| 23 | + * but was not otherwise tested. |
| 24 | + * |
| 25 | + * This script also tests that a server not bound to a specific IP address still works. |
| 26 | + * |
| 27 | + * We create three, simultaneous web servers, one specific to each interface and one that listens on both: |
| 28 | + * |
| 29 | + * name IP Address Port |
| 30 | + * ---- ---------- ---- |
| 31 | + * server0 INADDR_ANY 8080 |
| 32 | + * server1 station address 8081 |
| 33 | + * server2 soft AP address 8081 |
| 34 | + * |
| 35 | + * The expected responses to a brower's requests are as follows: |
| 36 | + * |
| 37 | + * 1. when client connected to the same WLAN as the station: |
| 38 | + * Request URL Response |
| 39 | + * ----------- -------- |
| 40 | + * http://stationaddress:8080 "hello from server0" |
| 41 | + * http://stationaddress:8081 "hello from server1" |
| 42 | + * |
| 43 | + * 2. when client is connected to the soft AP: |
| 44 | + * |
| 45 | + * Request URL Response |
| 46 | + * ----------- -------- |
| 47 | + * http://softAPaddress:8080 "hello from server0" |
| 48 | + * http://softAPaddress:8081 "hello from server2" |
| 49 | + * |
| 50 | + * 3. Repeat 1 and 2 above with esp32.local in place of stationaddress and softAPaddress, respectively. |
| 51 | + * |
| 52 | + * MultiHomedServers was originally based on HelloServer. |
| 53 | + */ |
| 54 | + |
| 55 | +const char* ssid = "........"; |
| 56 | +const char* password = "........"; |
| 57 | +const char *apssid = "ESP32"; |
| 58 | + |
| 59 | +WebServer *server0, *server1, *server2; |
| 60 | + |
| 61 | +const int led = 13; |
| 62 | + |
| 63 | +void handleRoot(WebServer *server, const char *content) { |
| 64 | + digitalWrite(led, 1); |
| 65 | + server->send(200, "text/plain", content); |
| 66 | + digitalWrite(led, 0); |
| 67 | +} |
| 68 | + |
| 69 | +void handleRoot0() { |
| 70 | + handleRoot(server0, "hello from server0"); |
| 71 | +} |
| 72 | + |
| 73 | +void handleRoot1() { |
| 74 | + handleRoot(server1, "hello from server1"); |
| 75 | +} |
| 76 | + |
| 77 | +void handleRoot2() { |
| 78 | + handleRoot(server2, "hello from server2"); |
| 79 | +} |
| 80 | + |
| 81 | +void handleNotFound(WebServer *server) { |
| 82 | + digitalWrite(led, 1); |
| 83 | + String message = "File Not Found\n\n"; |
| 84 | + message += "URI: "; |
| 85 | + message += server->uri(); |
| 86 | + message += "\nMethod: "; |
| 87 | + message += (server->method() == HTTP_GET) ? "GET" : "POST"; |
| 88 | + message += "\nArguments: "; |
| 89 | + message += server->args(); |
| 90 | + message += "\n"; |
| 91 | + for (uint8_t i = 0; i < server->args(); i++) { |
| 92 | + message += " " + server->argName(i) + ": " + server->arg(i) + "\n"; |
| 93 | + } |
| 94 | + server->send(404, "text/plain", message); |
| 95 | + digitalWrite(led, 0); |
| 96 | +} |
| 97 | + |
| 98 | +void handleNotFound0() { |
| 99 | + handleNotFound(server0); |
| 100 | +} |
| 101 | + |
| 102 | +void handleNotFound1() { |
| 103 | + handleNotFound(server1); |
| 104 | +} |
| 105 | + |
| 106 | +void handleNotFound2() { |
| 107 | + handleNotFound(server2); |
| 108 | +} |
| 109 | + |
| 110 | +void setup(void) { |
| 111 | + pinMode(led, OUTPUT); |
| 112 | + digitalWrite(led, 0); |
| 113 | + Serial.begin(115200); |
| 114 | + WiFi.mode(WIFI_STA); |
| 115 | + WiFi.begin(ssid, password); |
| 116 | + Serial.println(""); |
| 117 | + |
| 118 | + // Wait for connection |
| 119 | + while (WiFi.status() != WL_CONNECTED) { |
| 120 | + delay(500); |
| 121 | + Serial.print("."); |
| 122 | + } |
| 123 | + Serial.println(""); |
| 124 | + Serial.print("Connected to "); |
| 125 | + Serial.println(ssid); |
| 126 | + Serial.print("IP address: "); |
| 127 | + Serial.println(WiFi.localIP()); |
| 128 | + if (!WiFi.softAP(apssid)) { |
| 129 | + Serial.println("failed to start softAP"); |
| 130 | + for (;;) { |
| 131 | + digitalWrite(led, 1); |
| 132 | + delay(100); |
| 133 | + digitalWrite(led, 0); |
| 134 | + delay(100); |
| 135 | + } |
| 136 | + } |
| 137 | + Serial.print("Soft AP: "); |
| 138 | + Serial.print(apssid); |
| 139 | + Serial.print(" IP address: "); |
| 140 | + Serial.println(WiFi.softAPIP()); |
| 141 | + |
| 142 | + if (MDNS.begin("esp32")) { |
| 143 | + Serial.println("MDNS responder started"); |
| 144 | + } |
| 145 | + |
| 146 | + server0 = new WebServer(8080); |
| 147 | + server1 = new WebServer(WiFi.localIP(), 8081); |
| 148 | + server2 = new WebServer(WiFi.softAPIP(), 8081); |
| 149 | + |
| 150 | + server0->on("/", handleRoot0); |
| 151 | + server1->on("/", handleRoot1); |
| 152 | + server2->on("/", handleRoot2); |
| 153 | + |
| 154 | + server0->onNotFound(handleNotFound0); |
| 155 | + server1->onNotFound(handleNotFound1); |
| 156 | + server2->onNotFound(handleNotFound2); |
| 157 | + |
| 158 | + server0->begin(); |
| 159 | + Serial.println("HTTP server0 started"); |
| 160 | + server1->begin(); |
| 161 | + Serial.println("HTTP server1 started"); |
| 162 | + server2->begin(); |
| 163 | + Serial.println("HTTP server2 started"); |
| 164 | +} |
| 165 | + |
| 166 | +void loop(void) { |
| 167 | + server0->handleClient(); |
| 168 | + server1->handleClient(); |
| 169 | + server2->handleClient(); |
| 170 | + delay(2);//allow the cpu to switch to other tasks |
| 171 | +} |
0 commit comments