|
| 1 | +/**************************************************************************************************************************** |
| 2 | + EthernetWebServer_BigData.ino |
| 3 | +
|
| 4 | + EthernetWebServer is a library for the Ethernet shields to run WebServer |
| 5 | +
|
| 6 | + Based on and modified from ESP8266 https://github.com/esp8266/Arduino/releases |
| 7 | + Built by Khoi Hoang https://github.com/khoih-prog/EthernetWebServer |
| 8 | + Licensed under MIT license |
| 9 | + *****************************************************************************************************************************/ |
| 10 | + |
| 11 | +#include "defines.h" |
| 12 | + |
| 13 | +EthernetWebServer server(80); |
| 14 | + |
| 15 | +// Adjust according to your board's heap size. Too large => crash |
| 16 | +#if (ESP32 || ESP8266) || (ETHERNET_USE_NRF528XX) || (ETHERNET_USE_RPIPICO) |
| 17 | + #define MULTIPLY_FACTOR 3.0f |
| 18 | + |
| 19 | +#else |
| 20 | + #define MULTIPLY_FACTOR 1.0f |
| 21 | +#endif |
| 22 | + |
| 23 | +// In bytes |
| 24 | +#define STRING_SIZE (8192 * MULTIPLY_FACTOR) |
| 25 | + |
| 26 | +#define BUFFER_SIZE 512 |
| 27 | +char temp[BUFFER_SIZE]; |
| 28 | + |
| 29 | +void createPage(String &pageInput) |
| 30 | +{ |
| 31 | + int sec = millis() / 1000; |
| 32 | + int min = sec / 60; |
| 33 | + int hr = min / 60; |
| 34 | + int day = hr / 24; |
| 35 | + |
| 36 | + snprintf(temp, BUFFER_SIZE - 1, |
| 37 | + "<html>\ |
| 38 | +<head>\ |
| 39 | +<meta http-equiv='refresh' content='5'/>\ |
| 40 | +<title>EthernetWebServer_BigData-%s</title>\ |
| 41 | +<style>\ |
| 42 | +body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\ |
| 43 | +</style>\ |
| 44 | +</head>\ |
| 45 | +<body>\ |
| 46 | +<h2>EthernetWebServer_W5500!</h2>\ |
| 47 | +<h3>running on %s</h3>\ |
| 48 | +<p>Uptime: %d d %02d:%02d:%02d</p>\ |
| 49 | +</body>\ |
| 50 | +</html>", BOARD_NAME, BOARD_NAME, day, hr % 24, min % 60, sec % 60); |
| 51 | + |
| 52 | + pageInput = temp; |
| 53 | +} |
| 54 | + |
| 55 | +String out; |
| 56 | + |
| 57 | +void handleRoot() |
| 58 | +{ |
| 59 | + //out.reserve(STRING_SIZE); |
| 60 | + |
| 61 | + // clear the String to start over |
| 62 | + out = String(); |
| 63 | + |
| 64 | + createPage(out); |
| 65 | + |
| 66 | + out += "<html><body>\r\n<table><tr><th>INDEX</th><th>DATA</th></tr>"; |
| 67 | + |
| 68 | + for (uint16_t lineIndex = 0; lineIndex < (100 * MULTIPLY_FACTOR); lineIndex++) |
| 69 | + { |
| 70 | + out += "<tr><td>"; |
| 71 | + out += String(lineIndex); |
| 72 | + out += "</td><td>"; |
| 73 | + out += "WiFiWebServer_BigData_ABCDEFGHIJKLMNOPQRSTUVWXYZ</td></tr>"; |
| 74 | + } |
| 75 | + |
| 76 | + out += "</table></body></html>\r\n"; |
| 77 | + |
| 78 | + Serial.print(F("String Len = ")); Serial.println(out.length()); |
| 79 | + |
| 80 | + server.send(200, F("text/html"), out); |
| 81 | +} |
| 82 | + |
| 83 | +void handleNotFound() |
| 84 | +{ |
| 85 | + String message = F("File Not Found\n\n"); |
| 86 | + |
| 87 | + message += F("URI: "); |
| 88 | + message += server.uri(); |
| 89 | + message += F("\nMethod: "); |
| 90 | + message += (server.method() == HTTP_GET) ? F("GET") : F("POST"); |
| 91 | + message += F("\nArguments: "); |
| 92 | + message += server.args(); |
| 93 | + message += F("\n"); |
| 94 | + |
| 95 | + for (uint8_t i = 0; i < server.args(); i++) |
| 96 | + { |
| 97 | + message += " " + server.argName(i) + ": " + server.arg(i) + "\n"; |
| 98 | + } |
| 99 | + |
| 100 | + server.send(404, F("text/plain"), message); |
| 101 | +} |
| 102 | + |
| 103 | +void initEthernet() |
| 104 | +{ |
| 105 | +#if USE_ETHERNET_PORTENTA_H7 |
| 106 | + ET_LOGWARN(F("======== USE_PORTENTA_H7_ETHERNET ========")); |
| 107 | +#elif USE_NATIVE_ETHERNET |
| 108 | + ET_LOGWARN(F("======== USE_NATIVE_ETHERNET ========")); |
| 109 | +#elif USE_ETHERNET_GENERIC |
| 110 | + ET_LOGWARN(F("=========== USE_ETHERNET_GENERIC ===========")); |
| 111 | +#elif USE_ETHERNET_ESP8266 |
| 112 | + ET_LOGWARN(F("=========== USE_ETHERNET_ESP8266 ===========")); |
| 113 | +#elif USE_ETHERNET_ENC |
| 114 | + ET_LOGWARN(F("=========== USE_ETHERNET_ENC ===========")); |
| 115 | +#else |
| 116 | + ET_LOGWARN(F("=========================")); |
| 117 | +#endif |
| 118 | + |
| 119 | +#if !(USE_NATIVE_ETHERNET || USE_ETHERNET_PORTENTA_H7) |
| 120 | + |
| 121 | +#if (USING_SPI2) |
| 122 | +#if defined(CUR_PIN_MISO) |
| 123 | + ET_LOGWARN(F("Default SPI pinout:")); |
| 124 | + ET_LOGWARN1(F("MOSI:"), CUR_PIN_MOSI); |
| 125 | + ET_LOGWARN1(F("MISO:"), CUR_PIN_MISO); |
| 126 | + ET_LOGWARN1(F("SCK:"), CUR_PIN_SCK); |
| 127 | + ET_LOGWARN1(F("SS:"), CUR_PIN_SS); |
| 128 | + ET_LOGWARN(F("=========================")); |
| 129 | +#endif |
| 130 | +#else |
| 131 | + ET_LOGWARN(F("Default SPI pinout:")); |
| 132 | + ET_LOGWARN1(F("MOSI:"), MOSI); |
| 133 | + ET_LOGWARN1(F("MISO:"), MISO); |
| 134 | + ET_LOGWARN1(F("SCK:"), SCK); |
| 135 | + ET_LOGWARN1(F("SS:"), SS); |
| 136 | + ET_LOGWARN(F("=========================")); |
| 137 | +#endif |
| 138 | + |
| 139 | +#if defined(ESP8266) |
| 140 | + // For ESP8266, change for other boards if necessary |
| 141 | +#ifndef USE_THIS_SS_PIN |
| 142 | +#define USE_THIS_SS_PIN D2 // For ESP8266 |
| 143 | +#endif |
| 144 | + |
| 145 | + ET_LOGWARN1(F("ESP8266 setCsPin:"), USE_THIS_SS_PIN); |
| 146 | + |
| 147 | +#if ( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC ) |
| 148 | + // For ESP8266 |
| 149 | + // Pin D0(GPIO16) D1(GPIO5) D2(GPIO4) D3(GPIO0) D4(GPIO2) D8 |
| 150 | + // EthernetGeneric X X X X X 0 |
| 151 | + // Ethernet_ESP8266 0 0 0 0 0 0 |
| 152 | + // D2 is safe to used for Ethernet, Ethernet2, Ethernet3, EthernetLarge libs |
| 153 | + // Must use library patch for Ethernet, EthernetLarge libraries |
| 154 | + Ethernet.init (USE_THIS_SS_PIN); |
| 155 | + |
| 156 | +#elif USE_CUSTOM_ETHERNET |
| 157 | + |
| 158 | + // You have to add initialization for your Custom Ethernet here |
| 159 | + // This is just an example to setCSPin to USE_THIS_SS_PIN, and can be not correct and enough |
| 160 | + Ethernet.init(USE_THIS_SS_PIN); |
| 161 | + |
| 162 | +#endif //( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC ) |
| 163 | + |
| 164 | +#elif defined(ESP32) |
| 165 | + |
| 166 | + // You can use Ethernet.init(pin) to configure the CS pin |
| 167 | + //Ethernet.init(10); // Most Arduino shields |
| 168 | + //Ethernet.init(5); // MKR ETH shield |
| 169 | + //Ethernet.init(0); // Teensy 2.0 |
| 170 | + //Ethernet.init(20); // Teensy++ 2.0 |
| 171 | + //Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet |
| 172 | + //Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet |
| 173 | + |
| 174 | +#ifndef USE_THIS_SS_PIN |
| 175 | +#define USE_THIS_SS_PIN 5 //22 // For ESP32 |
| 176 | +#endif |
| 177 | + |
| 178 | + ET_LOGWARN1(F("ESP32 setCsPin:"), USE_THIS_SS_PIN); |
| 179 | + |
| 180 | + // For other boards, to change if necessary |
| 181 | +#if ( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC ) |
| 182 | + // Must use library patch for Ethernet, EthernetLarge libraries |
| 183 | + // ESP32 => GPIO2,4,5,13,15,21,22 OK with Ethernet, Ethernet2, EthernetLarge |
| 184 | + // ESP32 => GPIO2,4,5,15,21,22 OK with Ethernet3 |
| 185 | + |
| 186 | + //Ethernet.setCsPin (USE_THIS_SS_PIN); |
| 187 | + Ethernet.init (USE_THIS_SS_PIN); |
| 188 | + |
| 189 | +#elif USE_CUSTOM_ETHERNET |
| 190 | + |
| 191 | + // You have to add initialization for your Custom Ethernet here |
| 192 | + // This is just an example to setCSPin to USE_THIS_SS_PIN, and can be not correct and enough |
| 193 | + Ethernet.init(USE_THIS_SS_PIN); |
| 194 | + |
| 195 | +#endif //( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC ) |
| 196 | + |
| 197 | +#elif ETHERNET_USE_RPIPICO |
| 198 | + |
| 199 | + pinMode(USE_THIS_SS_PIN, OUTPUT); |
| 200 | + digitalWrite(USE_THIS_SS_PIN, HIGH); |
| 201 | + |
| 202 | + // ETHERNET_USE_RPIPICO, use default SS = 5 or 17 |
| 203 | +#ifndef USE_THIS_SS_PIN |
| 204 | +#if defined(ARDUINO_ARCH_MBED) |
| 205 | +#define USE_THIS_SS_PIN 5 // For Arduino Mbed core |
| 206 | +#else |
| 207 | +#define USE_THIS_SS_PIN 17 // For E.Philhower core |
| 208 | +#endif |
| 209 | +#endif |
| 210 | + |
| 211 | + ET_LOGWARN1(F("RPIPICO setCsPin:"), USE_THIS_SS_PIN); |
| 212 | + |
| 213 | + // For other boards, to change if necessary |
| 214 | +#if ( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC ) |
| 215 | + // Must use library patch for Ethernet, EthernetLarge libraries |
| 216 | + // For RPI Pico using Arduino Mbed RP2040 core |
| 217 | + // SCK: GPIO2, MOSI: GPIO3, MISO: GPIO4, SS/CS: GPIO5 |
| 218 | + // For RPI Pico using E. Philhower RP2040 core |
| 219 | + // SCK: GPIO18, MOSI: GPIO19, MISO: GPIO16, SS/CS: GPIO17 |
| 220 | + // Default pin 5/17 to SS/CS |
| 221 | + |
| 222 | + //Ethernet.setCsPin (USE_THIS_SS_PIN); |
| 223 | + Ethernet.init (USE_THIS_SS_PIN); |
| 224 | + |
| 225 | +#endif //( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC ) |
| 226 | + |
| 227 | +#else //defined(ESP8266) |
| 228 | + // unknown board, do nothing, use default SS = 10 |
| 229 | +#ifndef USE_THIS_SS_PIN |
| 230 | +#define USE_THIS_SS_PIN 10 // For other boards |
| 231 | +#endif |
| 232 | + |
| 233 | +#if defined(BOARD_NAME) |
| 234 | + ET_LOGWARN3(F("Board :"), BOARD_NAME, F(", setCsPin:"), USE_THIS_SS_PIN); |
| 235 | +#else |
| 236 | + ET_LOGWARN1(F("Unknown board setCsPin:"), USE_THIS_SS_PIN); |
| 237 | +#endif |
| 238 | + |
| 239 | + // For other boards, to change if necessary |
| 240 | +#if ( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC || USE_NATIVE_ETHERNET ) |
| 241 | + // Must use library patch for Ethernet, Ethernet2, EthernetLarge libraries |
| 242 | + |
| 243 | + Ethernet.init (USE_THIS_SS_PIN); |
| 244 | + |
| 245 | +#elif USE_CUSTOM_ETHERNET |
| 246 | + |
| 247 | + // You have to add initialization for your Custom Ethernet here |
| 248 | + // This is just an example to setCSPin to USE_THIS_SS_PIN, and can be not correct and enough |
| 249 | + Ethernet.init(USE_THIS_SS_PIN); |
| 250 | + |
| 251 | +#endif //( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC ) |
| 252 | + |
| 253 | +#endif // defined(ESP8266) |
| 254 | + |
| 255 | +#endif // #if !(USE_NATIVE_ETHERNET) |
| 256 | + |
| 257 | + // start the ethernet connection and the server: |
| 258 | + // Use DHCP dynamic IP and random mac |
| 259 | + uint16_t index = millis() % NUMBER_OF_MAC; |
| 260 | + // Use Static IP |
| 261 | + //Ethernet.begin(mac[0], ip); |
| 262 | + Ethernet.begin(mac[index]); |
| 263 | + |
| 264 | +#if !(USE_NATIVE_ETHERNET || USE_ETHERNET_PORTENTA_H7) |
| 265 | + ET_LOGWARN(F("=========================")); |
| 266 | + |
| 267 | +#if ( defined( ESP32 ) && USE_ETHERNET_GENERIC ) |
| 268 | + // Just info to know how to connect correctly |
| 269 | + // To change for other SPI |
| 270 | + ET_LOGWARN(F("Currently Used SPI pinout:")); |
| 271 | + ET_LOGWARN1(F("MOSI:"), PIN_MOSI); |
| 272 | + ET_LOGWARN1(F("MISO:"), PIN_MISO); |
| 273 | + ET_LOGWARN1(F("SCK:"), PIN_SCK); |
| 274 | + ET_LOGWARN1(F("SS:"), PIN_SS); |
| 275 | +#else |
| 276 | +#if defined(CUR_PIN_MISO) |
| 277 | + ET_LOGWARN(F("Currently Used SPI pinout:")); |
| 278 | + ET_LOGWARN1(F("MOSI:"), CUR_PIN_MOSI); |
| 279 | + ET_LOGWARN1(F("MISO:"), CUR_PIN_MISO); |
| 280 | + ET_LOGWARN1(F("SCK:"), CUR_PIN_SCK); |
| 281 | + ET_LOGWARN1(F("SS:"), CUR_PIN_SS); |
| 282 | +#else |
| 283 | + ET_LOGWARN(F("Currently Used SPI pinout:")); |
| 284 | + ET_LOGWARN1(F("MOSI:"), MOSI); |
| 285 | + ET_LOGWARN1(F("MISO:"), MISO); |
| 286 | + ET_LOGWARN1(F("SCK:"), SCK); |
| 287 | + ET_LOGWARN1(F("SS:"), SS); |
| 288 | +#endif |
| 289 | +#endif |
| 290 | + |
| 291 | + ET_LOGWARN(F("=========================")); |
| 292 | + |
| 293 | +#elif (USE_ETHERNET_PORTENTA_H7) |
| 294 | + |
| 295 | + if (Ethernet.hardwareStatus() == EthernetNoHardware) |
| 296 | + { |
| 297 | + SerialDebug.println("No Ethernet found. Stay here forever"); |
| 298 | + |
| 299 | + while (true) |
| 300 | + { |
| 301 | + delay(1); // do nothing, no point running without Ethernet hardware |
| 302 | + } |
| 303 | + } |
| 304 | + |
| 305 | + if (Ethernet.linkStatus() == LinkOFF) |
| 306 | + { |
| 307 | + SerialDebug.println("Not connected Ethernet cable"); |
| 308 | + } |
| 309 | + |
| 310 | +#endif |
| 311 | + |
| 312 | + SerialDebug.print(F("Using mac index = ")); |
| 313 | + SerialDebug.println(index); |
| 314 | + |
| 315 | + SerialDebug.print(F("Connected! IP address: ")); |
| 316 | + SerialDebug.println(Ethernet.localIP()); |
| 317 | +} |
| 318 | + |
| 319 | +void setup() |
| 320 | +{ |
| 321 | + out.reserve(STRING_SIZE); |
| 322 | + |
| 323 | + //Initialize serial and wait for port to open: |
| 324 | + Serial.begin(115200); |
| 325 | + |
| 326 | + while (!Serial && millis() < 5000); |
| 327 | + |
| 328 | + delay(200); |
| 329 | + |
| 330 | + Serial.print(F("\nStart EthernetWebServer_BigData on ")); |
| 331 | + SerialDebug.print(BOARD_NAME); |
| 332 | + SerialDebug.print(F(" with ")); |
| 333 | + SerialDebug.println(SHIELD_TYPE); |
| 334 | + SerialDebug.println(ETHERNET_WEBSERVER_VERSION); |
| 335 | + |
| 336 | + initEthernet(); |
| 337 | + |
| 338 | + server.on(F("/"), handleRoot); |
| 339 | + |
| 340 | + server.on(F("/inline"), []() |
| 341 | + { |
| 342 | + server.send(200, F("text/plain"), F("This works as well")); |
| 343 | + }); |
| 344 | + |
| 345 | + server.onNotFound(handleNotFound); |
| 346 | + |
| 347 | + server.begin(); |
| 348 | +} |
| 349 | + |
| 350 | +void loop() |
| 351 | +{ |
| 352 | + server.handleClient(); |
| 353 | +} |
0 commit comments