//Created by Barbu Vulc!
//Libraries:
#include <SoftwareSerialTX.h> //UART protocol...
#include <WiFi.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <stdio.h>

// Replace with your network credentials
//const char* ssid = "YOUR_SSID";
//const char* password = "YOUR_PASSWORD";

// Set web server port number to 80
WiFiServer server(80);
/* Set the following constants to 1 or 0 to define which tasks to include and
exclude.*/ 
#define mainCREATE_SIMPLE_UDP_CLIENT_SERVER_TASKS     1
#define mainCREATE_TCP_ECHO_TASKS_SINGLE              0
#define mainCREATE_TCP_ECHO_SERVER_TASK               0

//Initiate variables:
String temp = "";
String hum = "";

#define TX 21
#define RX 22
SoftwareSerial mySerial(RX, TX);

void setup() {
  mySerial.begin(9600);
  Serial.begin(9600);

  // Connect to Wi-Fi network with SSID and password
  Serial.print("Connecting to ");
  WiFi.begin("Wokwi-GUEST", "", 6);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500); 
    Serial.print(".");
  }
  // Print local IP address and start web server
  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  server.begin();
  
  if(pdFALSE == xTaskCreate(MyServeClientTask, "Client", 16384, NULL, 1, NULL)){
    Serial.print("Client task_1 failed!");
  } else {
    Serial.print("Client task_1 OK!");
  }

  if(pdFALSE == xTaskCreate(SerialTask, "Serial", 16384, NULL, 1, NULL)){
    Serial.print("Client task_1 failed!");
  } else {
    Serial.print("Client task_1 OK!");
  }
}

void MyServeClientTask(void *pvParameters) {
  TickType_t xLastWakeTime;
  const TickType_t xFrequency = 1000 / portTICK_PERIOD_MS;
  xLastWakeTime = xTaskGetTickCount();
  BaseType_t xWasDelayed;

  String header;
  String currentLine;
  if (xWasDelayed) {}

  for (;;) {
    xWasDelayed = xTaskDelayUntil(&xLastWakeTime, xFrequency);
    WiFiClient client = server.accept();  // Listen for incoming clients

    if (client) {
      while (client.connected()) {  
        if (client.available()) {                                 // if there's bytes to read from the client,
          char c = client.read();                                 // read a byte, then
          Serial.write(c);                                        // print it out the serial monitor
          header += c;
          if (c == '\n') {  // if the byte is a newline character
            // if the current line is blank, you got two newline characters in a row.
            // that's the end of the client HTTP request, so send a response:
            if (currentLine.length() == 0) {
              // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
              // and a content-type so the client knows what's coming, then a blank line:
              client.println("HTTP/1.1 200 OK");  
              client.println();
              
              if (header.indexOf("GET / HTTP/1.1") != -1) {
                client.println();
                client.println("<p><ul><li><a href=\"/temperature\">temperature</a></li> <li><a href=\"/humidity\">humidity</a></li> <li><a href=\"/realtime\">Monitorizare in timp real!</a></li></ul></p>");
              } else {
                if (header.indexOf("GET /temperature HTTP/1.1") != -1) {
                  client.println();
                  //Temperature:
                  client.println("<html><body><p><div><br/></div> <div id=\"temp\"></div></p> <script>let x = \'" + temp + "\' + \' &#8451\'; document.getElementById(\"temp\").innerHTML = x; </script> </body></html>");
                } else if (header.indexOf("GET /humidity HTTP/1.1") != -1) {
                  client.println();
                  //Humidity:
                  client.println("<html><body><p><div><br/></div> <div id=\"hum\"></div></p> <script>let x = \'" + hum + "\' + \' %\'; document.getElementById(\"hum\").innerHTML = x; </script> </body></html>");
                } else  if (header.indexOf("GET /realtime HTTP/1.1") != -1) {
                  client.println();
                  //Real-time monitoring:
                  client.println("<html><meta http-equiv=\"refresh\" content=\"2\"><body> <p><div><br/></div> <div id=\"temp\"></div> <script>let x = \'" + temp + "\' + \' &#8451\'; document.getElementById(\"temp\").innerHTML = x; </script></p> <p><div><br/></div> <div id=\"hum\"></div> <script>let y = \'" + hum + "\' + \' %\'; document.getElementById(\"hum\").innerHTML = y; </script></p> </body></html>");
                }
              }
              vTaskDelay(50 / portTICK_PERIOD_MS);
              client.stop();
              currentLine = "";
              header = "";
            } else if (c != '\r') {  // if you got anything else but a carriage return character,
              currentLine += c;      // add it to the end of the currentLine
            } else {
              currentLine = "";  
            }
          }
        }
      }
      Serial.println("MSD Header: " + header);
    }
  }
}

void SerialTask(void *pvParameters) {
  TickType_t xLastWakeTime;
  const TickType_t xFrequency = 100 / portTICK_PERIOD_MS;
  xLastWakeTime = xTaskGetTickCount();
  BaseType_t xWasDelayed;

  String header;
  String currentLine;
  if (xWasDelayed) {}

  for (;;) {
    xWasDelayed = xTaskDelayUntil(&xLastWakeTime, xFrequency);
    if(mySerial.available()){
      String strser = mySerial.readStringUntil('\n');
      if (strser.startsWith("T=")) {
        temp = strser.substring(2, 7);
        Serial.println("TT="+temp);
      } else if(strser.startsWith("H=")){
        hum = strser.substring(2, 7);
        Serial.println("HH="+hum);
      }
    }
  }
}

void loop() { /*Is not used!!! =))*/ }
$abcdeabcde151015202530354045505560fghijfghij