async-web-server-without-style

uum-iot-lab/web-server-esp32/async-web-server-without-style

No description
async-web-server-without-style
@/async-web-server-without-style
INETjesuso/esp32-wifi/esp32-inet
CONNpulse
Establish the connection
TITLEstring
HTML Title
OPstring
Operation to be done on the web server
STYLEstring
HTML code to style the web server
RUNpulse
async-web-server-without-style
IP
DONE
INET
CONN
TITLE
OP
STYLE
RUN
DONEpulse
Pulse when get the IP address
IPxod/net/ip-address
IP address for the web server
To use the node in your project you should have the uum-iot-lab/web-server-esp32 library installed. Use the “File → Add Library” menu item in XOD IDE if you don’t have it yet. See Using libraries for more info.

C++ implementation

#pragma XOD require "https://github.com/me-no-dev/ESPAsyncWebServer"
#pragma XOD require "https://github.com/me-no-dev/AsyncTCP"

#pragma XOD error_raise enable

#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>

AsyncWebServer server(80);

const char index_html[] PROGMEM = R"rawliteral(
    <!DOCTYPE HTML><html>
    <head>
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="icon" href="data:,">
       %TITLE%
       %STYLE%
    </head>
    <body>
        <div id="operation">%OPERATION%</div>
    </body>
    <script>
    setInterval(function ( ) {
      var xhttp = new XMLHttpRequest();
      xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
          document.getElementById("operation").innerHTML = this.responseText;
        }
      };
      xhttp.open("GET", "/operation", true);
      xhttp.send();
    }, 2000 ) ;
    </script>
    </html>
    )rawliteral";

namespace input {
    String operation = "";
    String style = "";
    String title = "";
}

String processor(const String& var){
      if(var == "TITLE"){
        return String(input::title);
      } else if(var == "STYLE"){
        return String(input::style);
      } else if(var == "OPERATION"){
        return String(input::operation);
      }
      return String();
}

node {
    #define WIFI_TIMEOUT_MS 20000

    void evaluate(Context ctx) {
        if (isInputDirty<input_CONN>(ctx)){

            auto tt = getValue<input_TITLE>(ctx);
            char _tt[length(tt) + 1] = { 0 };
            dump(tt, _tt);
            int lengthtt = length(tt) + 1;

            String ttl = "";
            for (int i = 0; i < lengthtt; i++) {
                ttl += _tt[i];
            }
            input::title = "<title>" + ttl + "</title>";

            server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
              request->send_P(200, "text/html", index_html, processor);
            });

            server.on("/operation", HTTP_GET, [](AsyncWebServerRequest *request){
                request->send_P(200, "text/plain", String(input::operation).c_str());
            });

            server.begin();

            auto mode = WiFi.getMode();
            if(WiFi.getMode() == 1) {
                auto ip = WiFi.localIP();
                emitValue<output_IP>(ctx, ip);
            }
            emitValue<output_DONE>(ctx, 1);

        }

        if (isInputDirty<input_RUN>(ctx)){
            auto op = getValue<input_OP>(ctx);
            char _op[length(op) + 1] = { 0 };
            dump(op, _op);
            int lengthchar = length(op) + 1;

            String opp = "";
            for (int i = 0; i < lengthchar; i++) {
                opp += _op[i];
            }
            input::operation = opp;

            auto st = getValue<input_STYLE>(ctx);
            char _st[length(st) + 1] = { 0 };
            dump(st, _st);
            int lengthst = length(st) + 1;

            String sty = "";
            for (int i = 0; i < lengthst; i++) {
                sty += _st[i];
            }
            input::style = sty;
        }


    }
}