|  | 
|  | 1 | +/**************************************************************************************************************************** | 
|  | 2 | + AsyncMultiWebServer_ESP32_W6100.ino | 
|  | 3 | +
 | 
|  | 4 | + For W6100 LwIP Ethernet in ESP32 (ESP32 + W6100) | 
|  | 5 | +
 | 
|  | 6 | + AsyncWebServer_ESP32_W6100 is a library for the LwIP Ethernet W6100 in ESP32 to run AsyncWebServer | 
|  | 7 | +
 | 
|  | 8 | + Based on and modified from ESPAsyncWebServer (https://github.com/me-no-dev/ESPAsyncWebServer) | 
|  | 9 | + Built by Khoi Hoang https://github.com/khoih-prog/AsyncWebServer_ESP32_W6100 | 
|  | 10 | + Licensed under GPLv3 license | 
|  | 11 | + *****************************************************************************************************************************/ | 
|  | 12 | + | 
|  | 13 | +#if !( defined(ESP32) ) | 
|  | 14 | + #error This code is designed for (ESP32 + W6100) to run on ESP32 platform! Please check your Tools->Board setting. | 
|  | 15 | +#endif | 
|  | 16 | + | 
|  | 17 | +#include <Arduino.h> | 
|  | 18 | + | 
|  | 19 | +#define _ASYNC_WEBSERVER_LOGLEVEL_ 2 | 
|  | 20 | + | 
|  | 21 | +// Enter a MAC address and IP address for your controller below. | 
|  | 22 | +#define NUMBER_OF_MAC 20 | 
|  | 23 | + | 
|  | 24 | +byte mac[][NUMBER_OF_MAC] = | 
|  | 25 | +{ | 
|  | 26 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 }, | 
|  | 27 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 }, | 
|  | 28 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 }, | 
|  | 29 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 }, | 
|  | 30 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 }, | 
|  | 31 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 }, | 
|  | 32 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 }, | 
|  | 33 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 }, | 
|  | 34 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 }, | 
|  | 35 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A }, | 
|  | 36 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B }, | 
|  | 37 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C }, | 
|  | 38 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D }, | 
|  | 39 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E }, | 
|  | 40 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F }, | 
|  | 41 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 }, | 
|  | 42 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 }, | 
|  | 43 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 }, | 
|  | 44 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 }, | 
|  | 45 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 }, | 
|  | 46 | +}; | 
|  | 47 | + | 
|  | 48 | +// Select the IP address according to your local network | 
|  | 49 | +IPAddress myIP(192, 168, 2, 232); | 
|  | 50 | +IPAddress myGW(192, 168, 2, 1); | 
|  | 51 | +IPAddress mySN(255, 255, 255, 0); | 
|  | 52 | + | 
|  | 53 | +// Google DNS Server IP | 
|  | 54 | +IPAddress myDNS(8, 8, 8, 8); | 
|  | 55 | + | 
|  | 56 | +////////////////////////////////////////////////////////// | 
|  | 57 | + | 
|  | 58 | +// Optional values to override default settings | 
|  | 59 | +// Don't change unless you know what you're doing | 
|  | 60 | +//#define ETH_SPI_HOST SPI3_HOST | 
|  | 61 | +//#define SPI_CLOCK_MHZ 25 | 
|  | 62 | + | 
|  | 63 | +// Must connect INT to GPIOxx or not working | 
|  | 64 | +//#define INT_GPIO 4 | 
|  | 65 | + | 
|  | 66 | +//#define MISO_GPIO 19 | 
|  | 67 | +//#define MOSI_GPIO 23 | 
|  | 68 | +//#define SCK_GPIO 18 | 
|  | 69 | +//#define CS_GPIO 5 | 
|  | 70 | + | 
|  | 71 | +////////////////////////////////////////////////////////// | 
|  | 72 | + | 
|  | 73 | +#include <AsyncTCP.h> | 
|  | 74 | + | 
|  | 75 | +#include <AsyncWebServer_ESP32_W6100.h> | 
|  | 76 | + | 
|  | 77 | +unsigned int analogReadPin [] = { 12, 13, 14 }; | 
|  | 78 | + | 
|  | 79 | +#define BUFFER_SIZE 500 | 
|  | 80 | + | 
|  | 81 | +#define HTTP_PORT1 8080 | 
|  | 82 | +#define HTTP_PORT2 8081 | 
|  | 83 | +#define HTTP_PORT3 8082 | 
|  | 84 | + | 
|  | 85 | +AsyncWebServer* server1; | 
|  | 86 | +AsyncWebServer* server2; | 
|  | 87 | +AsyncWebServer* server3; | 
|  | 88 | + | 
|  | 89 | +AsyncWebServer* multiServer [] = { server1, server2, server3 }; | 
|  | 90 | +uint16_t http_port [] = { HTTP_PORT1, HTTP_PORT2, HTTP_PORT3 }; | 
|  | 91 | + | 
|  | 92 | +#define NUM_SERVERS ( sizeof(multiServer) / sizeof(AsyncWebServer*) ) | 
|  | 93 | + | 
|  | 94 | +unsigned int serverIndex; | 
|  | 95 | + | 
|  | 96 | +String createBuffer() | 
|  | 97 | +{ | 
|  | 98 | + char temp[BUFFER_SIZE]; | 
|  | 99 | + | 
|  | 100 | + memset(temp, 0, sizeof(temp)); | 
|  | 101 | + | 
|  | 102 | + int sec = millis() / 1000; | 
|  | 103 | + int min = sec / 60; | 
|  | 104 | + int hr = min / 60; | 
|  | 105 | + int day = hr / 24; | 
|  | 106 | + | 
|  | 107 | + snprintf(temp, BUFFER_SIZE - 1, | 
|  | 108 | + "<html>\ | 
|  | 109 | +<head>\ | 
|  | 110 | +<meta http-equiv='refresh' content='5'/>\ | 
|  | 111 | +<title>%s</title>\ | 
|  | 112 | +<style>\ | 
|  | 113 | +body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\ | 
|  | 114 | +</style>\ | 
|  | 115 | +</head>\ | 
|  | 116 | +<body>\ | 
|  | 117 | +<h1>Hello from %s</h1>\ | 
|  | 118 | +<h2>running AsyncMultiWebServer_ESP32_W6100</h2>\ | 
|  | 119 | +<h3>on %s</h3>\ | 
|  | 120 | +<h3>Uptime: %d d %02d:%02d:%02d</h3>\ | 
|  | 121 | +</body>\ | 
|  | 122 | +</html>", ARDUINO_BOARD, ARDUINO_BOARD, SHIELD_TYPE, day, hr, min % 60, sec % 60); | 
|  | 123 | + | 
|  | 124 | + return temp; | 
|  | 125 | +} | 
|  | 126 | + | 
|  | 127 | + | 
|  | 128 | +void handleRoot(AsyncWebServerRequest * request) | 
|  | 129 | +{ | 
|  | 130 | + String message = createBuffer(); | 
|  | 131 | + request->send(200, F("text/html"), message); | 
|  | 132 | +} | 
|  | 133 | + | 
|  | 134 | +String createNotFoundBuffer(AsyncWebServerRequest * request) | 
|  | 135 | +{ | 
|  | 136 | + String message; | 
|  | 137 | + | 
|  | 138 | + message.reserve(500); | 
|  | 139 | + | 
|  | 140 | + message = F("File Not Found\n\n"); | 
|  | 141 | + | 
|  | 142 | + message += F("URI: "); | 
|  | 143 | + message += request->url(); | 
|  | 144 | + message += F("\nMethod: "); | 
|  | 145 | + message += (request->method() == HTTP_GET) ? F("GET") : F("POST"); | 
|  | 146 | + message += F("\nArguments: "); | 
|  | 147 | + message += request->args(); | 
|  | 148 | + message += F("\n"); | 
|  | 149 | + | 
|  | 150 | + for (uint8_t i = 0; i < request->args(); i++) | 
|  | 151 | + { | 
|  | 152 | + message += " " + request->argName(i) + ": " + request->arg(i) + "\n"; | 
|  | 153 | + } | 
|  | 154 | + | 
|  | 155 | + return message; | 
|  | 156 | +} | 
|  | 157 | + | 
|  | 158 | +void handleNotFound(AsyncWebServerRequest * request) | 
|  | 159 | +{ | 
|  | 160 | + String message = createNotFoundBuffer(request); | 
|  | 161 | + request->send(404, F("text/plain"), message); | 
|  | 162 | +} | 
|  | 163 | + | 
|  | 164 | +void setup() | 
|  | 165 | +{ | 
|  | 166 | + Serial.begin(115200); | 
|  | 167 | + | 
|  | 168 | + while (!Serial && millis() < 5000); | 
|  | 169 | + | 
|  | 170 | + delay(200); | 
|  | 171 | + | 
|  | 172 | + Serial.print(F("\nStart AsyncMultiWebServer_ESP32_W6100 on ")); | 
|  | 173 | + Serial.print(ARDUINO_BOARD); | 
|  | 174 | + Serial.print(F(" with ")); | 
|  | 175 | + Serial.println(SHIELD_TYPE); | 
|  | 176 | + Serial.println(ASYNC_WEBSERVER_ESP32_W6100_VERSION); | 
|  | 177 | + | 
|  | 178 | + AWS_LOGWARN(F("Default SPI pinout:")); | 
|  | 179 | + AWS_LOGWARN1(F("SPI_HOST:"), ETH_SPI_HOST); | 
|  | 180 | + AWS_LOGWARN1(F("MOSI:"), MOSI_GPIO); | 
|  | 181 | + AWS_LOGWARN1(F("MISO:"), MISO_GPIO); | 
|  | 182 | + AWS_LOGWARN1(F("SCK:"), SCK_GPIO); | 
|  | 183 | + AWS_LOGWARN1(F("CS:"), CS_GPIO); | 
|  | 184 | + AWS_LOGWARN1(F("INT:"), INT_GPIO); | 
|  | 185 | + AWS_LOGWARN1(F("SPI Clock (MHz):"), SPI_CLOCK_MHZ); | 
|  | 186 | + AWS_LOGWARN(F("=========================")); | 
|  | 187 | + | 
|  | 188 | + /////////////////////////////////// | 
|  | 189 | + | 
|  | 190 | + // To be called before ETH.begin() | 
|  | 191 | + ESP32_W6100_onEvent(); | 
|  | 192 | + | 
|  | 193 | + // start the ethernet connection and the server: | 
|  | 194 | + // Use DHCP dynamic IP and random mac | 
|  | 195 | + //bool begin(int MISO_GPIO, int MOSI_GPIO, int SCLK_GPIO, int CS_GPIO, int INT_GPIO, int SPI_CLOCK_MHZ, | 
|  | 196 | + // int SPI_HOST, uint8_t *W6100_Mac = W6100_Default_Mac); | 
|  | 197 | + ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST ); | 
|  | 198 | + //ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST, mac[millis() % NUMBER_OF_MAC] ); | 
|  | 199 | + | 
|  | 200 | + // Static IP, leave without this line to get IP via DHCP | 
|  | 201 | + //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0); | 
|  | 202 | + //ETH.config(myIP, myGW, mySN, myDNS); | 
|  | 203 | + | 
|  | 204 | + ESP32_W6100_waitForConnect(); | 
|  | 205 | + | 
|  | 206 | + /////////////////////////////////// | 
|  | 207 | + | 
|  | 208 | + Serial.print("\nConnected to network. IP = "); | 
|  | 209 | + Serial.println(ETH.localIP()); | 
|  | 210 | + | 
|  | 211 | + for (serverIndex = 0; serverIndex < NUM_SERVERS; serverIndex++) | 
|  | 212 | + { | 
|  | 213 | + multiServer[serverIndex] = new AsyncWebServer(http_port[serverIndex]); | 
|  | 214 | + | 
|  | 215 | + if (multiServer[serverIndex]) | 
|  | 216 | + { | 
|  | 217 | + Serial.printf("Initialize multiServer OK, serverIndex = %d, port = %d\n", serverIndex, http_port[serverIndex]); | 
|  | 218 | + } | 
|  | 219 | + else | 
|  | 220 | + { | 
|  | 221 | + Serial.printf("Error initialize multiServer, serverIndex = %d\n", serverIndex); | 
|  | 222 | + | 
|  | 223 | + while (1); | 
|  | 224 | + } | 
|  | 225 | + | 
|  | 226 | + multiServer[serverIndex]->on("/", HTTP_GET, [](AsyncWebServerRequest * request) | 
|  | 227 | + { | 
|  | 228 | + handleRoot(request); | 
|  | 229 | + }); | 
|  | 230 | + | 
|  | 231 | + multiServer[serverIndex]->on("/hello", HTTP_GET, [](AsyncWebServerRequest * request) | 
|  | 232 | + { | 
|  | 233 | + String message = F("Hello from AsyncMultiWebServer_ESP32_W6100 using W6100 Ethernet, running on "); | 
|  | 234 | + message += ARDUINO_BOARD; | 
|  | 235 | + | 
|  | 236 | + request->send(200, "text/plain", message); | 
|  | 237 | + }); | 
|  | 238 | + | 
|  | 239 | + multiServer[serverIndex]->onNotFound([](AsyncWebServerRequest * request) | 
|  | 240 | + { | 
|  | 241 | + handleNotFound(request); | 
|  | 242 | + }); | 
|  | 243 | + | 
|  | 244 | + multiServer[serverIndex]->begin(); | 
|  | 245 | + | 
|  | 246 | + Serial.printf("HTTP server started at ports %d\n", http_port[serverIndex]); | 
|  | 247 | + } | 
|  | 248 | +} | 
|  | 249 | + | 
|  | 250 | +void loop() | 
|  | 251 | +{ | 
|  | 252 | +} | 
0 commit comments