Need help with code, Web browser related

I wrote a code to obtain and receive load cell data and be able to control a relay. Both of these on a web browser. This was written combing many examples and right now I've managed to clear all the errors, however, I am still facing a problem. The web browser can never be reached. This is the code:

#include <Arduino.h>
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <WebSerial.h>
#include <HX711_ADC.h>

TaskHandle_t Task1;
TaskHandle_t Task2;

const int HX711_dat = 18;
const int HX711_clk = 19;
HX711_ADC LoadCell(HX711_dat, HX711_clk);

const int relay_pin = 26;
const char* wifi_name = "AntanasWIFI";
const char* wifi_pass = "12345678";

void task1Code(void* pvParameters);
void task2Code(void* pvParameters);

AsyncWebServer server(80);  // Create an instance of AsyncWebServer

void setup() {
  Serial.begin(57600);
  pinMode(relay_pin, OUTPUT);
  WiFi.mode(WIFI_STA);
  WiFi.begin(wifi_name, wifi_pass);
  if (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.printf("WiFi Failed!\n");
    return;
  }
  Serial.print("IP Address: ");
  Serial.println(WiFi.localIP());

  WebSerial.begin(&server);

  float calibrationValue = 23.09;

  LoadCell.begin();
  unsigned long stabilizingtime = 2000;
  boolean _tare = true;
  LoadCell.start(stabilizingtime, _tare);
  if (LoadCell.getTareTimeoutFlag()) {
    Serial.println("Timeout, check MCU>HX711 wiring and pin designations");
  } else {
    LoadCell.setCalFactor(calibrationValue);
    Serial.println("Startup is complete");
  }

  xTaskCreatePinnedToCore(
    task1Code,
    "Task1",
    10000,
    NULL,
    1,
    &Task1,
    0);

  xTaskCreatePinnedToCore(
    task2Code,
    "Task2",
    10000,
    &server,  // Pass the server object as a parameter
    1,
    &Task2,
    1);

  delay(150);
}

void loop() {
  // Allow the tasks to execute
  delay(10);
}

void task1Code(void* pvParameters) {
  static unsigned long t = 0;
  static boolean newDataReady = 0;
  const int serialPrintInterval = 30;  // increase value to slow down serial print activity

  while (1) {
    // check for new data/start next conversion:
    if (LoadCell.update()) newDataReady = true;

    // get smoothed value from the dataset:
    if (newDataReady) {
      if (millis() > t + serialPrintInterval) {
        float i = LoadCell.getData();
        WebSerial.print("Load cell output value: ");
        WebSerial.println(i);
        newDataReady = 0;
        t = millis();
      }
    }

    // receive command from serial terminal, send 't' to initiate tare operation:
    if (Serial.available() > 0) {
      char inByte = Serial.read();
      if (inByte == 't') LoadCell.tareNoDelay();
    }

    // check if last tare operation is complete:
    if (LoadCell.getTareStatus() == true) {
      Serial.println("Tare complete");
    }

    // Allow the main task to execute Arduino library functions
    delay(10);
  }
}

void task2Code(void* pvParameters) {
  AsyncWebServer* server = static_cast<AsyncWebServer*>(pvParameters);
  Serial.print("Task2 running on core ");
  Serial.println(xPortGetCoreID());

  server->on("/", HTTP_GET, [](AsyncWebServerRequest* request) {
    String html = "<!DOCTYPE HTML>";
    html += "<html><body><center><h2>ESP32 Web Server</h2>";
    html += "<h3>Click to turn ON/OFF the Relay</h3>";
    html += "<a href=\"/RELAYON\"><button>RELAY ON</button></a>";
    html += "<a href=\"/RELAYOFF\"><button>RELAY OFF</button></a>";
    html += "</center></body></html>";

    request->send(200, "text/html", html);
  });

  server->on("/RELAYON", HTTP_GET, [](AsyncWebServerRequest* request) {
    digitalWrite(relay_pin, HIGH);
    request->send(200, "text/plain", "Relay turned ON");
  });

  server->on("/RELAYOFF", HTTP_GET, [](AsyncWebServerRequest* request) {
    digitalWrite(relay_pin, LOW);
    request->send(200, "text/plain", "Relay turned OFF");
  });

  while (1) {
    // Allow the main task to execute Arduino library functions
    delay(10);
  }
}

The WiFi credentials are my mobile phone hotspot's info. Both my pc and esp32 are connected - no error here I believe.

But how have you proved that?

Can your Arduino ping the server?

Have you used curl or wget or similar to verify that your request is will-formed?

1 Like

What I meant to say is that I can see that both the ESP and my computer are connected to my hotspot. Regarding what you've said - the code here is at fault since I've run a simple web server sketch with the same credentials and its fine. Correct me if I misunderstood your fix

Are you sure you aren't blowing through setup and then doing nothing in your main loop? Instead of xTaskCreatePinnedToCore(), try just running it in the main loop?

the loop function is empty because the main logic is implemented within the tasks Task1 and Task2.Both of these tasks run concurrently, independant of each other

They're supposed to, I understand. But since you're having issues with the code maybe it's best to simplify it as much as possible first until it's working.

Thank you, I've resolved the issue. Following your advice of simplifying the code I found the error. Now, however, only one issue remains. How fast can data appear on the web serial monitor? Is there a max data limit it can display before the data starts queing up or can I influence it?

Do you mean how fast is it updated to the web server you're running in task2Code and viewable by a device?

I don't see anywhere that you're storing data, just refreshing it as called. Maybe you're referring to TCP packets filling up a buffer? Can you clarify your questions please.

As I understand if the data being sent from the microcontroller arrives at the web browser faster than it can be processed, the TCP packets can fill up the buffer on the web browser's side. This can result in data being queued up. My question is: Is there a way to ensure that the web browser can handle the incoming data at the desired rate. Should that be possible, my goal would be data every 20ms.

I've seen data sent to the Arduino get queued up and overflow and how to handle that, but I'm not sure how to address that issue or if you even need to.

Out of curiosity, when you open and view the web page on a device, it doesn't lag or behave strangely does it?

Well, I'm not really familiar with the whole web serial concept as this is the first time I'm using it. However, the web serial monitor does seem to display the values fairly slow, but I couldn't say its lagging on behaving strangely, other than it getting queued up after a min. Would you know which direction to point me to in orded to handle this?

I've never used that functionality so no, I don't know what to recommend. Be patient, someone else here probably knows.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.