OPC-UA HTTP Webserver Arduino Ethernet ESP8266 ESP32 HTML Publish Subscribe

OPC UA - ESP8266


  • OPC UA has existed since 2008. It is a client -> server architecture that supports multiplatform applications.
  • The implementation offers the connection of clients to the web server. Clients communicate with the HTTP POST method and sell the data to the server in XML (SOAP) format.
  • The webserver can also respond to client requests with a value that can be used.
  • Architecture has applications in microprocessor communication, automation systems with PLCs, simple sensor networks.
  • In an advanced implementation, it is also possible to use an HTTPS connection to verify the WebServer / Client / Server certificate.
  • Demonstration implementation demonstrates easy connectivity between boards on the ESP8266 platform using an HTTP web server.
  • The ESP8266 server board also uses the AP access point implementation. The connected devices are thus in one network with the server and the network is encrypted under the WPA / WPA2 - PSK protocol.
  • ESP8266 broadcasts a WiFi network called ESP_WiFi with the password thereisnospoon , the server address is: 192.168.4.1 on port 80, assigning an address from the range 192.168.4.X s 24-bit network mask.
  • Server Pages:
  • / - The root location (192.168.4.1), the page can be used by a client on a computer, other device, visualizing real-time values ​​from clients
  • /send_data/ - page for receiving data, expecting XML data from clients
  • /get_data/ - specific information request page - webserver responds with a value on demand
  • Types of clients implemented in the project:
  • Publish_Client - sends data from ADC converter to server, prints received server response - acknowledges receipt of sent data
  • Publish_Client_UDP - send data from ADC converter to server, skip server response (process it, do not write received response, do not work with it)
  • Subscriber_Client - performs an HTTP query to the server - the server responds with a received value from the client, for example, the value can be a temperature. It is possible to work with the value and use it (ventilation, boiler switching, heating, irrigation, machine control)
  • OPC-UA implementation block diagram:

    Bloková schéma - OPC-UA - ESP8266, ESP32, Arduino + Ethernet Wiznet W5100 - XML

    (SOAP) XML ENCODED HTTP PAYLOAD - OPC-UA

    The webserver source code on the page does not contain XML (SOAP) parser. XML parser software versions and advanced implementations are not available for free. I offer software versions for ESP8266 / ESP32 / Arduino + Ethernet. If you are interested in buying a project, please contact: martinius96@gmail.com

    ESP8266 OPC-UA Webserver



    ESP8266 OPC-UA SOAP (XML) Webserver - AP MODE - NO XML SOAP PARSER


    //ESP8266 (NodeMCU) - OPC-UA (SOAP) webserver (NO XML parser) AP MODE
    //Autor: martinius96
    //Web: https://arduino.php5.sk
    
    #include <ESP8266WiFi.h>
    #include <ESP8266WebServer.h>
    ESP8266WebServer server(80);
    const char* ssid = "ESP_WiFi";
    const char* password = "thereisnospoon";
    
    void setup() {
      Serial.begin(115200);
      WiFi.softAP(ssid, password);
      IPAddress myIP = WiFi.softAPIP();
      Serial.println(myIP);
      server.on("/body", handleBody);
      server.begin();
      Serial.println("Server listening");
    }
    
    void loop() {
      server.handleClient();
    }
    
    void handleBody() {
      if (server.hasArg("plain") == false) {
        server.send(200, "text/plain", "Body not received");
        return;
      }
      String message = "Body received:\n";
      message += server.arg("plain");
      message += "\n";
      server.send(200, "text/plain", message);
      Serial.println(message);
    }
    

    ESP8266 OPC-UA SOAP (XML) Webserver - STA MODE - NO XML SOAP PARSER


    //ESP8266 (NodeMCU) - OPC-UA (SOAP) webserver (NO XML parser)
    //Autor: martinius96
    //Web: https://arduino.php5.sk
    
    #include <ESP8266WiFi.h>
    #include <ESP8266WebServer.h>
    ESP8266WebServer server(80);
    const char* ssid = "WIFI_NAME";
    const char* password =  "WIFI_PASSWORD";
    
    void setup() {
      Serial.begin(115200);
      WiFi.begin(ssid, password);
      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.println("Waiting to connect...");
      }
      Serial.print("IP address: ");
      Serial.println(WiFi.localIP());
      server.on("/body", handleBody);
      server.begin();
      Serial.println("Server listening");
    
    }
    
    void loop() {
      server.handleClient();
    }
    
    void handleBody() {
      if (server.hasArg("plain") == false) {
        server.send(200, "text/plain", "Body not received");
        return;
      }
      String message = "Body received:\n";
      message += server.arg("plain");
      message += "\n";
      server.send(200, "text/plain", message);
      Serial.println(message);
    }
    

    ESP8266 OPC-UA Client



    ESP8266 OPC-UA SOAP Client


    //ESP8266 (NodeMCU) - OPC-UA Client - XML (SOAP) data payload
    //Autor: martinius96
    //Web: https://arduino.php5.sk
    
    #include <ESP8266WiFi.h>
    const char * ssid = "WIFI_NAME";
    const char * password = "WIFI_PASSWORD";
    const char * host = "192.168.1.3"; //IP address of OPC-UA webserver --> second NodeMCU
    const int httpPort = 80; //http port
    unsigned long previousMillis = 0;
    const long interval = 5000;
    WiFiClient client;
    void setup() {
      Serial.begin(115200);
      WiFi.begin(ssid, password);
      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
      }
      Serial.println("");
      Serial.println("WiFi uspesne pripojene");
      Serial.println("IP adresa: ");
      Serial.println(WiFi.localIP());
      Serial.println("Ready");
    }
    
    void loop() {
      if (WiFi.status() != WL_CONNECTED) {
        WiFi.begin(ssid, password);
      }
      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
      }
      if (millis() - previousMillis >= interval) {
        previousMillis = millis();
        client.stop();
        String mac = WiFi.macAddress();
        String data = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
        data += "<ServiceRequest>\n";
        data += "<ID_BOARD>" + mac + "</ID_BOARD>\n";
        data += "<BME280_VALUE_PRESSURE>1013.48</BME280_VALUE_PRESSURE>\n";
        data += "<BME280_VALUE_TEMPERATURE>-20.34</BME280_VALUE_TEMPERATURE>\n";
        data += "<BME280_VALUE_HUMIDITY>48.55</BME280_VALUE_HUMIDITY>\n";
        data += "</ServiceRequest>\n";
        String url = "/body";
        if (client.connect(host, httpPort)) {
          client.println("POST " + url + " HTTP/1.0");
          client.println("Host: " + (String)host);
          client.println("User-Agent: ESP8266");
          client.println("Connection: close");
          client.println("Content-Type: application/x-www-form-urlencoded;");
          client.print("Content-Length: ");
          client.println(data.length());
          client.println();
          client.println(data);
          while (client.connected()) {
            String line = client.readStringUntil('\n');
            if (line == "\r") {
              break;
            }
          }
          String line = client.readStringUntil('\n');
        } else {
          Serial.println("Problem connecting to webserver");
        }
      }
    }