|
| 1 | +/**************************************************************************************************************************** |
| 2 | + ATWebServer_BigData.ino |
| 3 | +
|
| 4 | + For ESP8266/ESP32 AT-command running shields |
| 5 | +
|
| 6 | + ESP8266_AT_WebServer is a library for the ESP8266/ESP32 AT-command shields to run WebServer |
| 7 | + Based on and modified from ESP8266 https://github.com/esp8266/Arduino/releases |
| 8 | + Built by Khoi Hoang https://github.com/khoih-prog/ESP8266_AT_WebServer |
| 9 | + Licensed under MIT license |
| 10 | +
|
| 11 | + *****************************************************************************************************************************/ |
| 12 | + |
| 13 | +// Credits of [Miguel Alexandre Wisintainer](https://github.com/tcpipchip) for this simple yet effective method |
| 14 | +// For some STM32, there is only definition of Serial in variant.h, and is used for Serial/USB Debugging |
| 15 | +// For example, in Nucleo-144 F767ZI original variant.h |
| 16 | +// |
| 17 | +// #define SERIAL_PORT_MONITOR Serial |
| 18 | +// #define SERIAL_PORT_HARDWARE Serial |
| 19 | +// |
| 20 | +// To use ESP8266/ESP32-AT, we need another Serial, such as Serial1 |
| 21 | +// To do this, first, in corresponding variant.h, modify as follows: |
| 22 | + |
| 23 | +// #define SERIAL_PORT_HARDWARE Serial1 |
| 24 | +// |
| 25 | +// then assign pins D0 = RX/D1 = TX to be Hardware Serial1 by putting in sketch as follows: |
| 26 | +// |
| 27 | +// #define EspSerial SERIAL_PORT_HARDWARE //Serial1 |
| 28 | +// HardwareSerial Serial1(D0, D1); |
| 29 | +// |
| 30 | +// This must be included in defines.h for each board you'd like to use ESPSerial as Serial1 |
| 31 | +// |
| 32 | +// The pin usage must be modified according to your boards. |
| 33 | + |
| 34 | +#include "defines.h" |
| 35 | + |
| 36 | +int status = WL_IDLE_STATUS; // the Wifi radio's status |
| 37 | +int reqCount = 0; // number of requests received |
| 38 | + |
| 39 | +#define WEBSERVER_PORT 80 |
| 40 | + |
| 41 | +ESP8266_AT_WebServer server(WEBSERVER_PORT); |
| 42 | + |
| 43 | +// Adjust according to your board's heap size. Too large => crash |
| 44 | +#if (ESP8266_AT_USE_NRF528XX) || (ESP8266_AT_USE_RPIPICO) |
| 45 | + #define MULTIPLY_FACTOR 3.0f |
| 46 | +#else |
| 47 | + #define MULTIPLY_FACTOR 1.0f |
| 48 | +#endif |
| 49 | + |
| 50 | +// In bytes |
| 51 | +#define STRING_SIZE (8192 * MULTIPLY_FACTOR) |
| 52 | + |
| 53 | +#define BUFFER_SIZE 512 |
| 54 | +char temp[BUFFER_SIZE]; |
| 55 | + |
| 56 | +void createPage(String &pageInput) |
| 57 | +{ |
| 58 | + int sec = millis() / 1000; |
| 59 | + int min = sec / 60; |
| 60 | + int hr = min / 60; |
| 61 | + int day = hr / 24; |
| 62 | + |
| 63 | + snprintf(temp, BUFFER_SIZE - 1, |
| 64 | + "<html>\ |
| 65 | +<head>\ |
| 66 | +<meta http-equiv='refresh' content='5'/>\ |
| 67 | +<title>ATWebServer_BigData-%s</title>\ |
| 68 | +<style>\ |
| 69 | +body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\ |
| 70 | +</style>\ |
| 71 | +</head>\ |
| 72 | +<body>\ |
| 73 | +<h2>ATWebServer_BigData!</h2>\ |
| 74 | +<h3>running on %s</h3>\ |
| 75 | +<p>Uptime: %d d %02d:%02d:%02d</p>\ |
| 76 | +</body>\ |
| 77 | +</html>", BOARD_NAME, BOARD_NAME, day, hr % 24, min % 60, sec % 60); |
| 78 | + |
| 79 | + pageInput = temp; |
| 80 | +} |
| 81 | + |
| 82 | +String out; |
| 83 | + |
| 84 | +void handleRoot() |
| 85 | +{ |
| 86 | + //out.reserve(STRING_SIZE); |
| 87 | + |
| 88 | + // clear the String to start over |
| 89 | + out = String(); |
| 90 | + |
| 91 | + createPage(out); |
| 92 | + |
| 93 | + out += "<html><body>\r\n<table><tr><th>INDEX</th><th>DATA</th></tr>"; |
| 94 | + |
| 95 | + for (uint16_t lineIndex = 0; lineIndex < (100 * MULTIPLY_FACTOR); lineIndex++) |
| 96 | + { |
| 97 | + out += "<tr><td>"; |
| 98 | + out += String(lineIndex); |
| 99 | + out += "</td><td>"; |
| 100 | + out += "ATWebServer_BigData_ABCDEFGHIJKLMNOPQRSTUVWXYZ</td></tr>"; |
| 101 | + } |
| 102 | + |
| 103 | + out += "</table></body></html>\r\n"; |
| 104 | + |
| 105 | + Serial.print(F("String Len = ")); Serial.println(out.length()); |
| 106 | + |
| 107 | + server.send(200, F("text/html"), out); |
| 108 | +} |
| 109 | + |
| 110 | +void handleNotFound() |
| 111 | +{ |
| 112 | + String message = F("File Not Found\n\n"); |
| 113 | + |
| 114 | + message += F("URI: "); |
| 115 | + message += server.uri(); |
| 116 | + message += F("\nMethod: "); |
| 117 | + message += (server.method() == HTTP_GET) ? F("GET") : F("POST"); |
| 118 | + message += F("\nArguments: "); |
| 119 | + message += server.args(); |
| 120 | + message += F("\n"); |
| 121 | + |
| 122 | + for (uint8_t i = 0; i < server.args(); i++) |
| 123 | + { |
| 124 | + message += " " + server.argName(i) + ": " + server.arg(i) + "\n"; |
| 125 | + } |
| 126 | + |
| 127 | + server.send(404, F("text/plain"), message); |
| 128 | +} |
| 129 | + |
| 130 | +void setup() |
| 131 | +{ |
| 132 | + out.reserve(STRING_SIZE); |
| 133 | + |
| 134 | + //Initialize serial and wait for port to open: |
| 135 | + Serial.begin(115200); |
| 136 | + |
| 137 | + while (!Serial && millis() < 5000); |
| 138 | + |
| 139 | + delay(200); |
| 140 | + |
| 141 | + Serial.print(F("\nStart ATWebServer_BigData on ")); |
| 142 | + Serial.print(BOARD_NAME); |
| 143 | + Serial.print(F(" with ")); |
| 144 | + Serial.println(SHIELD_TYPE); |
| 145 | + Serial.println(ESP8266_AT_WEBSERVER_VERSION); |
| 146 | + |
| 147 | + // initialize serial for ESP module |
| 148 | + EspSerial.begin(115200); |
| 149 | + // initialize ESP module |
| 150 | + WiFi.init(&EspSerial); |
| 151 | + |
| 152 | + Serial.println(F("WiFi shield init done")); |
| 153 | + |
| 154 | + // check for the presence of the shield |
| 155 | + if (WiFi.status() == WL_NO_SHIELD) |
| 156 | + { |
| 157 | + Serial.println(F("WiFi shield not present")); |
| 158 | + |
| 159 | + // don't continue |
| 160 | + while (true); |
| 161 | + } |
| 162 | + |
| 163 | + // attempt to connect to WiFi network |
| 164 | + while ( status != WL_CONNECTED) |
| 165 | + { |
| 166 | + Serial.print(F("Connecting to WPA SSID: ")); |
| 167 | + Serial.println(ssid); |
| 168 | + // Connect to WPA/WPA2 network |
| 169 | + status = WiFi.begin(ssid, pass); |
| 170 | + } |
| 171 | + |
| 172 | + server.on(F("/"), handleRoot); |
| 173 | + |
| 174 | + server.on(F("/inline"), []() |
| 175 | + { |
| 176 | + server.send(200, F("text/plain"), F("This works as well")); |
| 177 | + }); |
| 178 | + |
| 179 | + server.onNotFound(handleNotFound); |
| 180 | + |
| 181 | + server.begin(); |
| 182 | + |
| 183 | + Serial.print(F("HTTP server started @ ")); |
| 184 | + Serial.print(WiFi.localIP()); |
| 185 | + Serial.print(F(", Port = ")); |
| 186 | + Serial.println(WEBSERVER_PORT); |
| 187 | +} |
| 188 | + |
| 189 | +void loop() |
| 190 | +{ |
| 191 | + server.handleClient(); |
| 192 | +} |
0 commit comments