macca-nz

Async ESP32 AP displays current millis

Feb 24th, 2024 (edited)
1,043
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 3.69 KB | Source Code | 0 0
  1. /*
  2.   Simple A-Sync Access Point Sketch that displays your ESP32's Current millis()
  3.   You can coose to have in RAW format or time format (uncomment Line 17)
  4. */
  5.  
  6. // Import required libraries
  7. #include <WiFi.h>
  8. //#include <AsyncTCP.h>
  9. #include <ESPAsyncWebServer.h>
  10.  
  11. //Choose millis() format
  12. //Comment out to have just the millis() value IE "Current Millis = 12345678"
  13. //otherwise it is formatted in days:hours:minutes:seconds.xxx
  14. #define FORMAT          
  15.  
  16. //AJAX refresh duration 1000mS or 1sec
  17. //#define PERIOD uint16_t(1000)            
  18.  
  19. /* Put your SSID & Password */
  20. const char* ssid = "ESP32-AP";          // Look for this SSID in your WiFi Networks
  21. const char* password = "123456789";     // Enter this Password to get access to the AP
  22.  
  23. /*  PConfigure Access Point IP Address details and it
  24.     MUST not conflict with your Home WiFi or any other devices  */
  25. IPAddress local_ip(192,168,4,1);        // After connecting Use http://192.168.4.1 in the device's web browser
  26. IPAddress gateway(192,168,4,1);
  27. IPAddress subnet(255,255,255,0);
  28.  
  29. // Create AsyncWebServer object on port 80
  30. AsyncWebServer server(80);
  31.  
  32. String getMillis() {
  33.   #ifdef FORMAT
  34.       // Read Current Millis in "days:hours:minutes:
  35.       unsigned long currentMillis = millis();
  36.       unsigned long seconds = currentMillis / 1000;
  37.       unsigned long minutes = seconds / 60;
  38.       unsigned long hours = minutes / 60;
  39.       unsigned long days = hours / 24;
  40.       currentMillis %= 1000;
  41.       seconds %= 60;
  42.       minutes %= 60;
  43.       hours %= 24;
  44.       char buf[20];
  45.       sprintf(buf,"%03u:%02u:%02u:%02u.%u", days, hours, minutes, seconds, currentMillis);
  46.       //Serial.println(buf);
  47.       return String(buf);
  48.   #else
  49.       return String(millis());
  50.   #endif
  51. }
  52.  
  53. const char index_html[] PROGMEM = R"rawliteral(
  54. <!DOCTYPE HTML><html>
  55. <head>
  56.  <meta name="viewport" content="width=device-width, initial-scale=1">
  57.  <style>
  58.    html {
  59.     font-family: Arial;
  60.     display: inline-block;
  61.     margin: 0px auto;
  62.     text-align: center;
  63.    }
  64.    h2 { font-size: 1.5rem; }
  65.    p { font-size: 1.5rem; }
  66.    .units { font-size: 1.1rem; }
  67.    .dht-labels{
  68.      font-size: 1.1rem;
  69.      vertical-align:middle;
  70.      padding-bottom: 15px;
  71.    }
  72.  </style>
  73. </head>
  74. <body>
  75.  <h2>ESP32 MILLIS AP Server</h2>
  76.  <p>
  77.    <span class="dht-labels">Current Millis = </span>
  78.    <span id="millis">%MILLIS%</span>
  79.  </p>
  80. </body>
  81. <script>
  82. setInterval(function ( ) {
  83.  var xhttp = new XMLHttpRequest();
  84.  xhttp.onreadystatechange = function() {
  85.    if (this.readyState == 4 && this.status == 200) {
  86.      document.getElementById("millis").innerHTML = this.responseText;
  87.    }
  88.  };
  89.  xhttp.open("GET", "/millis", true);
  90.  xhttp.send();
  91. }, 1000 ) ;
  92. </script>
  93. </html>)rawliteral";
  94.  
  95. // Replaces placeholder with DHT values
  96. String processor(const String& var){
  97.   //Serial.println(var);
  98.   if(var == "MILLIS"){
  99.     return getMillis();
  100.   }
  101.   return String();
  102. }
  103.  
  104. void setup(){
  105.   // Serial port for debugging purposes
  106.   Serial.begin(115200);
  107.   WiFi.mode(WIFI_AP);
  108.   WiFi.softAP(ssid, password);
  109.   WiFi.softAPConfig(local_ip, gateway, subnet);
  110.   delay(1000);
  111.   // Confirm Access Point IP is available
  112.   Serial.println();
  113.   Serial.print("IP address: ");
  114.   Serial.println(WiFi.softAPIP());
  115.  
  116.   // Route for root / web page
  117.   server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
  118.     request->send_P(200, "text/html", index_html, processor);
  119.   });
  120.   server.on("/millis", HTTP_GET, [](AsyncWebServerRequest *request){
  121.     request->send_P(200, "text/plain", getMillis().c_str());
  122.     String myString = getMillis();
  123.   });
  124.  
  125.   // Start server
  126.   server.begin();
  127.   Serial.println("\nThe HTTP server has started");
  128. }
  129.  
  130. void loop(){
  131. }
Advertisement
Add Comment
Please, Sign In to add comment