Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Simple A-Sync Access Point Sketch that displays your ESP32's Current millis()
- You can coose to have in RAW format or time format (uncomment Line 17)
- */
- // Import required libraries
- #include <WiFi.h>
- //#include <AsyncTCP.h>
- #include <ESPAsyncWebServer.h>
- //Choose millis() format
- //Comment out to have just the millis() value IE "Current Millis = 12345678"
- //otherwise it is formatted in days:hours:minutes:seconds.xxx
- #define FORMAT
- //AJAX refresh duration 1000mS or 1sec
- //#define PERIOD uint16_t(1000)
- /* Put your SSID & Password */
- const char* ssid = "ESP32-AP"; // Look for this SSID in your WiFi Networks
- const char* password = "123456789"; // Enter this Password to get access to the AP
- /* PConfigure Access Point IP Address details and it
- MUST not conflict with your Home WiFi or any other devices */
- IPAddress local_ip(192,168,4,1); // After connecting Use http://192.168.4.1 in the device's web browser
- IPAddress gateway(192,168,4,1);
- IPAddress subnet(255,255,255,0);
- // Create AsyncWebServer object on port 80
- AsyncWebServer server(80);
- String getMillis() {
- #ifdef FORMAT
- // Read Current Millis in "days:hours:minutes:
- unsigned long currentMillis = millis();
- unsigned long seconds = currentMillis / 1000;
- unsigned long minutes = seconds / 60;
- unsigned long hours = minutes / 60;
- unsigned long days = hours / 24;
- currentMillis %= 1000;
- seconds %= 60;
- minutes %= 60;
- hours %= 24;
- char buf[20];
- sprintf(buf,"%03u:%02u:%02u:%02u.%u", days, hours, minutes, seconds, currentMillis);
- //Serial.println(buf);
- return String(buf);
- #else
- return String(millis());
- #endif
- }
- const char index_html[] PROGMEM = R"rawliteral(
- <!DOCTYPE HTML><html>
- <head>
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <style>
- html {
- font-family: Arial;
- display: inline-block;
- margin: 0px auto;
- text-align: center;
- }
- h2 { font-size: 1.5rem; }
- p { font-size: 1.5rem; }
- .units { font-size: 1.1rem; }
- .dht-labels{
- font-size: 1.1rem;
- vertical-align:middle;
- padding-bottom: 15px;
- }
- </style>
- </head>
- <body>
- <h2>ESP32 MILLIS AP Server</h2>
- <p>
- <span class="dht-labels">Current Millis = </span>
- <span id="millis">%MILLIS%</span>
- </p>
- </body>
- <script>
- setInterval(function ( ) {
- var xhttp = new XMLHttpRequest();
- xhttp.onreadystatechange = function() {
- if (this.readyState == 4 && this.status == 200) {
- document.getElementById("millis").innerHTML = this.responseText;
- }
- };
- xhttp.open("GET", "/millis", true);
- xhttp.send();
- }, 1000 ) ;
- </script>
- </html>)rawliteral";
- // Replaces placeholder with DHT values
- String processor(const String& var){
- //Serial.println(var);
- if(var == "MILLIS"){
- return getMillis();
- }
- return String();
- }
- void setup(){
- // Serial port for debugging purposes
- Serial.begin(115200);
- WiFi.mode(WIFI_AP);
- WiFi.softAP(ssid, password);
- WiFi.softAPConfig(local_ip, gateway, subnet);
- delay(1000);
- // Confirm Access Point IP is available
- Serial.println();
- Serial.print("IP address: ");
- Serial.println(WiFi.softAPIP());
- // Route for root / web page
- server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
- request->send_P(200, "text/html", index_html, processor);
- });
- server.on("/millis", HTTP_GET, [](AsyncWebServerRequest *request){
- request->send_P(200, "text/plain", getMillis().c_str());
- String myString = getMillis();
- });
- // Start server
- server.begin();
- Serial.println("\nThe HTTP server has started");
- }
- void loop(){
- }
Advertisement
Add Comment
Please, Sign In to add comment