|
| 1 | +/**************************************************************************************************************************** |
| 2 | + AsyncMultiWebServer_ESP32_ENC.ino |
| 3 | +
|
| 4 | + For ENC28J60 Ethernet in ESP32 (ESP32 + ENC28J60) |
| 5 | +
|
| 6 | + AsyncWebServer_ESP32_ENC is a library for the Ethernet ENC28J60 in ESSP32 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_ENC |
| 10 | + Licensed under GPLv3 license |
| 11 | + *****************************************************************************************************************************/ |
| 12 | + |
| 13 | +#if !( defined(ESP32) ) |
| 14 | + #error This code is designed for (ESP32 + ENC28J60) 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 | +#include <AsyncTCP.h> |
| 57 | + |
| 58 | +#include <AsyncWebServer_ESP32_ENC.h> |
| 59 | + |
| 60 | +// Optional values to override default settings |
| 61 | +//#define SPI_HOST 1 |
| 62 | +//#define SPI_CLOCK_MHZ 8 |
| 63 | + |
| 64 | +// Must connect INT to GPIOxx or not working |
| 65 | +//#define INT_GPIO 4 |
| 66 | + |
| 67 | +//#define MISO_GPIO 19 |
| 68 | +//#define MOSI_GPIO 23 |
| 69 | +//#define SCK_GPIO 18 |
| 70 | +//#define CS_GPIO 5 |
| 71 | +// Optional values to override default settings |
| 72 | + |
| 73 | +unsigned int analogReadPin [] = { 12, 13, 14 }; |
| 74 | + |
| 75 | +#define BUFFER_SIZE 500 |
| 76 | + |
| 77 | +#define HTTP_PORT1 8080 |
| 78 | +#define HTTP_PORT2 8081 |
| 79 | +#define HTTP_PORT3 8082 |
| 80 | + |
| 81 | +AsyncWebServer* server1; |
| 82 | +AsyncWebServer* server2; |
| 83 | +AsyncWebServer* server3; |
| 84 | + |
| 85 | +AsyncWebServer* multiServer [] = { server1, server2, server3 }; |
| 86 | +uint16_t http_port [] = { HTTP_PORT1, HTTP_PORT2, HTTP_PORT3 }; |
| 87 | + |
| 88 | +#define NUM_SERVERS ( sizeof(multiServer) / sizeof(AsyncWebServer*) ) |
| 89 | + |
| 90 | +unsigned int serverIndex; |
| 91 | + |
| 92 | +String createBuffer() |
| 93 | +{ |
| 94 | + char temp[BUFFER_SIZE]; |
| 95 | + |
| 96 | + memset(temp, 0, sizeof(temp)); |
| 97 | + |
| 98 | + int sec = millis() / 1000; |
| 99 | + int min = sec / 60; |
| 100 | + int hr = min / 60; |
| 101 | + int day = hr / 24; |
| 102 | + |
| 103 | + snprintf(temp, BUFFER_SIZE - 1, |
| 104 | + "<html>\ |
| 105 | +<head>\ |
| 106 | +<meta http-equiv='refresh' content='5'/>\ |
| 107 | +<title>%s</title>\ |
| 108 | +<style>\ |
| 109 | +body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\ |
| 110 | +</style>\ |
| 111 | +</head>\ |
| 112 | +<body>\ |
| 113 | +<h1>Hello from %s</h1>\ |
| 114 | +<h2>running AsyncMultiWebServer_ESP32_ENC</h2>\ |
| 115 | +<h3>on %s</h3>\ |
| 116 | +<h3>Uptime: %d d %02d:%02d:%02d</h3>\ |
| 117 | +</body>\ |
| 118 | +</html>", BOARD_NAME, BOARD_NAME, SHIELD_TYPE, day, hr, min % 60, sec % 60); |
| 119 | + |
| 120 | + return temp; |
| 121 | +} |
| 122 | + |
| 123 | + |
| 124 | +void handleRoot(AsyncWebServerRequest * request) |
| 125 | +{ |
| 126 | + String message = createBuffer(); |
| 127 | + request->send(200, F("text/html"), message); |
| 128 | +} |
| 129 | + |
| 130 | +String createNotFoundBuffer(AsyncWebServerRequest * request) |
| 131 | +{ |
| 132 | + String message; |
| 133 | + |
| 134 | + message.reserve(500); |
| 135 | + |
| 136 | + message = F("File Not Found\n\n"); |
| 137 | + |
| 138 | + message += F("URI: "); |
| 139 | + message += request->url(); |
| 140 | + message += F("\nMethod: "); |
| 141 | + message += (request->method() == HTTP_GET) ? F("GET") : F("POST"); |
| 142 | + message += F("\nArguments: "); |
| 143 | + message += request->args(); |
| 144 | + message += F("\n"); |
| 145 | + |
| 146 | + for (uint8_t i = 0; i < request->args(); i++) |
| 147 | + { |
| 148 | + message += " " + request->argName(i) + ": " + request->arg(i) + "\n"; |
| 149 | + } |
| 150 | + |
| 151 | + return message; |
| 152 | +} |
| 153 | + |
| 154 | +void handleNotFound(AsyncWebServerRequest * request) |
| 155 | +{ |
| 156 | + String message = createNotFoundBuffer(request); |
| 157 | + request->send(404, F("text/plain"), message); |
| 158 | +} |
| 159 | + |
| 160 | +void setup() |
| 161 | +{ |
| 162 | + Serial.begin(115200); |
| 163 | + |
| 164 | + while (!Serial && millis() < 5000); |
| 165 | + |
| 166 | + delay(200); |
| 167 | + |
| 168 | + Serial.print(F("\nStart AsyncMultiWebServer_ESP32_ENC on ")); |
| 169 | + Serial.print(BOARD_NAME); |
| 170 | + Serial.print(F(" with ")); |
| 171 | + Serial.println(SHIELD_TYPE); |
| 172 | + Serial.println(ASYNC_WEBSERVER_ESP32_ENC_VERSION); |
| 173 | + |
| 174 | + AWS_LOGWARN(F("Default SPI pinout:")); |
| 175 | + AWS_LOGWARN1(F("MOSI:"), MOSI_GPIO); |
| 176 | + AWS_LOGWARN1(F("MISO:"), MISO_GPIO); |
| 177 | + AWS_LOGWARN1(F("SCK:"), SCK_GPIO); |
| 178 | + AWS_LOGWARN1(F("CS:"), CS_GPIO); |
| 179 | + AWS_LOGWARN1(F("INT:"), INT_GPIO); |
| 180 | + AWS_LOGWARN1(F("SPI Clock (MHz):"), SPI_CLOCK_MHZ); |
| 181 | + AWS_LOGWARN(F("=========================")); |
| 182 | + |
| 183 | + /////////////////////////////////// |
| 184 | + |
| 185 | + // To be called before ETH.begin() |
| 186 | + ESP32_ENC_onEvent(); |
| 187 | + |
| 188 | + // start the ethernet connection and the server: |
| 189 | + // Use DHCP dynamic IP and random mac |
| 190 | + uint16_t index = millis() % NUMBER_OF_MAC; |
| 191 | + |
| 192 | + //bool begin(int MISO_GPIO, int MOSI_GPIO, int SCLK_GPIO, int CS_GPIO, int INT_GPIO, int SPI_CLOCK_MHZ, |
| 193 | + // int SPI_HOST, uint8_t *ENC28J60_Mac = ENC28J60_Default_Mac); |
| 194 | + //ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, SPI_HOST ); |
| 195 | + ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, SPI_HOST, mac[index] ); |
| 196 | + |
| 197 | + // Static IP, leave without this line to get IP via DHCP |
| 198 | + //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0); |
| 199 | + ETH.config(myIP, myGW, mySN, myDNS); |
| 200 | + |
| 201 | + ESP32_ENC_waitForConnect(); |
| 202 | + |
| 203 | + /////////////////////////////////// |
| 204 | + |
| 205 | + |
| 206 | + Serial.print("\nConnected to network. IP = "); |
| 207 | + Serial.println(ETH.localIP()); |
| 208 | + |
| 209 | + for (serverIndex = 0; serverIndex < NUM_SERVERS; serverIndex++) |
| 210 | + { |
| 211 | + multiServer[serverIndex] = new AsyncWebServer(http_port[serverIndex]); |
| 212 | + |
| 213 | + if (multiServer[serverIndex]) |
| 214 | + { |
| 215 | + Serial.printf("Initialize multiServer OK, serverIndex = %d, port = %d\n", serverIndex, http_port[serverIndex]); |
| 216 | + } |
| 217 | + else |
| 218 | + { |
| 219 | + Serial.printf("Error initialize multiServer, serverIndex = %d\n", serverIndex); |
| 220 | + |
| 221 | + while (1); |
| 222 | + } |
| 223 | + |
| 224 | + multiServer[serverIndex]->on("/", HTTP_GET, [](AsyncWebServerRequest * request) |
| 225 | + { |
| 226 | + handleRoot(request); |
| 227 | + }); |
| 228 | + |
| 229 | + multiServer[serverIndex]->on("/hello", HTTP_GET, [](AsyncWebServerRequest * request) |
| 230 | + { |
| 231 | + String message = F("Hello from AsyncMultiWebServer_ESP32_ENC using ENC28J60 Ethernet, running on "); |
| 232 | + message += BOARD_NAME; |
| 233 | + |
| 234 | + request->send(200, "text/plain", message); |
| 235 | + }); |
| 236 | + |
| 237 | + multiServer[serverIndex]->onNotFound([](AsyncWebServerRequest * request) |
| 238 | + { |
| 239 | + handleNotFound(request); |
| 240 | + }); |
| 241 | + |
| 242 | + multiServer[serverIndex]->begin(); |
| 243 | + |
| 244 | + Serial.printf("HTTP server started at ports %d\n", http_port[serverIndex]); |
| 245 | + } |
| 246 | +} |
| 247 | + |
| 248 | +void loop() |
| 249 | +{ |
| 250 | +} |
0 commit comments