The code does not show the values

I use the board esp-32s (Nodemcu 32s)

this code does not work correectly whats wrong with the code.

#include <Wire.h>
#include <ESPAsyncWebServer.h>
#include <WebSocketsServer.h>
#include <OneWire.h>
#include <DallasTemperature.h>

const char *ssid = "your-ssid";
const char *password = "your-password";

AsyncWebServer server(80);
WebSocketsServer webSocket = WebSocketsServer(81);

#define TdsSensorPin 27
#define TurbiditySensorPin 26
#define TemperaturePin 13
#define TDS_VREF 3.3   // TDS sensor reference voltage(Volt)
#define TURB_VREF 5.0   // Turbidity sensor reference voltage(Volt)
#define SCOUNT 30       // sum of sample point
#define SERIAL_DELAY 2000  // Adjust this value to control the output frequency (in milliseconds)

int tdsAnalogBuffer[SCOUNT];         // TDS sensor analog values buffer
int turbidityAnalogBuffer[SCOUNT];   // Turbidity sensor analog values buffer
int analogBufferIndex = 0;
int copyIndex = 0;

float tdsValue = 0;
float turbidityValue_IEEE = 0;
float turbidityValue_WHO = 0;
float temperature = 25;  // current temperature for compensation

OneWire oneWire(TemperaturePin);
DallasTemperature sensors(&oneWire);

// median filtering algorithm
int getMedianNum(int bArray[], int iFilterLen) {
  int bTab[iFilterLen];
  for (int i = 0; i < iFilterLen; i++)
    bTab[i] = bArray[i];

  int i, j, bTemp;
  for (j = 0; j < iFilterLen - 1; j++) {
    for (i = 0; i < iFilterLen - j - 1; i++) {
      if (bTab[i] > bTab[i + 1]) {
        bTemp = bTab[i];
        bTab[i] = bTab[i + 1];
        bTab[i + 1] = bTemp;
      }
    }
  }

  if (iFilterLen % 2 == 0) {
    return (bTab[iFilterLen / 2] + bTab[iFilterLen / 2 - 1]) / 2;
  } else {
    return bTab[iFilterLen / 2];
  }
}

void sendSensorData(const char *sensorName, float sensorValue) {
  String json = "{\"" + String(sensorName) + "\":" + String(sensorValue) + "}";
  webSocket.broadcastTXT(json);
}

void setup() {
  Serial.begin(115200);
  pinMode(TdsSensorPin, INPUT);
  pinMode(TurbiditySensorPin, INPUT);
  sensors.begin();

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");

  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
    String html = "<html><style>table {border-collapse: collapse; width: 70%; margin: auto;}";
    html += "th, td {padding: 15px; text-align: left;}";
    html += "th {background-color: #f2f2f2;}</style><body>";

    html += "<h1 style='text-align:center;'>IOT WATER QUALITY MONITORING SYSTEM</h1>";
    html += "<h3 style='text-align:center;'>DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING ASONARGAON UNIVERSITY</h3>";

    html += "<table>";
    html += "<tr><th>Parameter</th><th>Value</th></tr>";
    html += "<tr><td>TDS (ppm)</td><td id='tds'>0</td></tr>";
    html += "<tr><td>Turbidity (IEEE) (NTU)</td><td id='turbidity_ieee'>0</td></tr>";
    html += "<tr><td>Turbidity (WHO) (NTU)</td><td id='turbidity_who'>0</td></tr>";
    html += "<tr><td>Temperature (&#8451;)</td><td id='temperature'>0</td></tr>";
    html += "</table>";

    html += "<script>function updateData() {";
    html += "fetch('/data').then(response => response.json()).then(data => {";
    html += "document.getElementById('tds').innerText = data.tds;";
    html += "document.getElementById('turbidity_ieee').innerText = data.turbidity_ieee;";
    html += "document.getElementById('turbidity_who').innerText = data.turbidity_who;";
    html += "document.getElementById('temperature').innerText = data.temperature;";
    html += "});";
    html += "} setInterval(updateData, 1000); </script>";
    html += "</body></html>";

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

  server.on("/data", HTTP_GET, [](AsyncWebServerRequest *request) {
    float sumVoltage;

    // TDS sensor calculations
    sumVoltage = 0;
    for (copyIndex = 0; copyIndex < SCOUNT; copyIndex++) {
      sumVoltage += tdsAnalogBuffer[copyIndex] * TDS_VREF / 4096.0;
    }
    float averageVoltageTDS = sumVoltage / SCOUNT;
    tdsValue = (133.42 * pow(averageVoltageTDS, 3) - 255.86 * pow(averageVoltageTDS, 2) + 857.39 * averageVoltageTDS) * 0.5;
    sendSensorData("tds", tdsValue);

    // Turbidity sensor calculations using IEEE standard
    sumVoltage = 0;
    for (copyIndex = 0; copyIndex < SCOUNT; copyIndex++) {
      sumVoltage += turbidityAnalogBuffer[copyIndex] * TURB_VREF / 4096.0;
    }
    float averageVoltageTurbidity = sumVoltage / SCOUNT;
    turbidityValue_IEEE = (-1120.4 * pow(averageVoltageTurbidity, 2) + 5742.3 * averageVoltageTurbidity - 4352.9) / 100.0;
    sendSensorData("turbidity_ieee", turbidityValue_IEEE);

    // Turbidity sensor calculations using WHO standard
    turbidityValue_WHO = 1000 / ((averageVoltageTurbidity / TURB_VREF) * 5.0 - 1);
    sendSensorData("turbidity_who", turbidityValue_WHO);

    sendSensorData("temperature", temperature);

    String json = "{\"tds\":" + String(tdsValue) + ",\"turbidity_ieee\":" + String(turbidityValue_IEEE) + ",\"turbidity_who\":" + String(turbidityValue_WHO) + ",\"temperature\":" + String(temperature) + "}";
    request->send(200, "application/json", json);
  });

  server.begin();
  webSocket.begin();
}

void loop() {
  static unsigned long analogSampleTimepoint = millis();

  if (millis() - analogSampleTimepoint > 40U) {
    analogSampleTimepoint = millis();
    tdsAnalogBuffer[analogBufferIndex] = analogRead(TdsSensorPin); // read TDS sensor value
    turbidityAnalogBuffer[analogBufferIndex] = analogRead(TurbiditySensorPin); // read turbidity sensor value
    sensors.requestTemperatures();
    temperature = sensors.getTempCByIndex(0);  // read temperature
    analogBufferIndex++;

    if (analogBufferIndex == SCOUNT) {
      analogBufferIndex = 0;
    }
  }

  webSocket.loop();
}

Is this part of your final exam?

@rafshan007, welcome

Please edit your post, select all code and click the <CODE> button to apply so-called code tags and next save your post. It makes it easier to read, easier to copy and the forum software will display it correctly. See How to get the best out of this forum

If you can compile and upload, you do not have a problem with IDE 1.x; hence your topic has been moved to a more suitable location on the forum.

1 Like

Sorry I cannot follow your ???. While following sterretje suggestions can you also post an annotated schematic showing exactly how you have wired this include all power, ground and power sources.

1 Like

who wrote this?
i believe to know what the problem is, but waiting you place sketch between code tags.

#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <WiFi.h>
#include <WebSocketsServer.h>

const char *ssid = "your-ssid";
const char *password = "your-password";

#define TdsSensorPin 27
#define TurbiditySensorPin 26
#define TemperaturePin 13
#define TDS_VREF 3.3
#define TURB_VREF 5.0
#define SCOUNT 30
#define SERIAL_DELAY 2000

int tdsAnalogBuffer[SCOUNT];
int turbidityAnalogBuffer[SCOUNT];
int analogBufferIndex = 0;
int copyIndex = 0;

float tdsValue = 0;
float turbidityValue_IEEE = 0;
float turbidityValue_WHO = 0;
float temperature = 25;

OneWire oneWire(TemperaturePin);
DallasTemperature sensors(&oneWire);

WebSocketsServer webSocket = WebSocketsServer(81);
WiFiServer server(80);

void setup() {
  Serial.begin(115200);

  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");

  // Set up WebSocket events
  webSocket.begin();
  server.begin();
}

void loop() {
  // Handle WebSocket events
  webSocket.loop();

  // Serve the HTML page
  WiFiClient client = server.available();
  if (client) {
    handleClient(client);
  }

  static unsigned long analogSampleTimepoint = millis();

  if (millis() - analogSampleTimepoint > 40U) {
    analogSampleTimepoint = millis();
    tdsAnalogBuffer[analogBufferIndex] = analogRead(TdsSensorPin);
    turbidityAnalogBuffer[analogBufferIndex] = analogRead(TurbiditySensorPin);
    sensors.requestTemperatures();
    temperature = sensors.getTempCByIndex(0);
    analogBufferIndex++;

    if (analogBufferIndex == SCOUNT) {
      analogBufferIndex = 0;

      // TDS sensor calculations
      float sumVoltageTDS = 0;
      for (copyIndex = 0; copyIndex < SCOUNT; copyIndex++) {
        sumVoltageTDS += tdsAnalogBuffer[copyIndex] * TDS_VREF / 4096.0;
      }
      float averageVoltageTDS = sumVoltageTDS / SCOUNT;
      tdsValue = (133.42 * pow(averageVoltageTDS, 3) - 255.86 * pow(averageVoltageTDS, 2) + 857.39 * averageVoltageTDS) * 0.5;

      // Turbidity sensor calculations using IEEE standard
      float sumVoltageTurbidity = 0;
      for (copyIndex = 0; copyIndex < SCOUNT; copyIndex++) {
        sumVoltageTurbidity += turbidityAnalogBuffer[copyIndex] * TURB_VREF / 4096.0;
      }
      float averageVoltageTurbidity = sumVoltageTurbidity / SCOUNT;
      turbidityValue_IEEE = (-1120.4 * pow(averageVoltageTurbidity, 2) + 5742.3 * averageVoltageTurbidity - 4352.9) / 100.0;

      // Turbidity sensor calculations using WHO standard
      turbidityValue_WHO = 1000 / ((averageVoltageTurbidity / TURB_VREF) * 5.0 - 1);

      // Send data to connected WebSocket clients
      sendSensorData();
    }
  }
}

void handleClient(WiFiClient client) {
  String request = client.readStringUntil('\r');
  client.flush();

  if (request.indexOf("/data") != -1) {
    sendHtmlData(client);
  } else {
    sendHtmlPage(client);
  }
}

void sendHtmlPage(WiFiClient client) {
  String html = "<html><style>table {border-collapse: collapse; width: 70%; margin: auto;}";
  html += "th, td {padding: 15px; text-align: left;}";
  html += "th {background-color: #f2f2f2;}</style><body>";

  html += "<h1 style='text-align:center;'>IOT WATER QUALITY MONITORING SYSTEM</h1>";
  html += "<h3 style='text-align:center;'>DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING ASONARGAON UNIVERSITY</h3>";

  html += "<table>";
  html += "<tr><th>Parameter</th><th>Value</th></tr>";
  html += "<tr><td>TDS (ppm)</td><td id='tds'>0</td></tr>";
  html += "<tr><td>Turbidity (IEEE) (NTU)</td><td id='turbidity_ieee'>0</td></tr>";
  html += "<tr><td>Turbidity (WHO) (NTU)</td><td id='turbidity_who'>0</td></tr>";
  html += "<tr><td>Temperature (&#8451;)</td><td id='temperature'>0</td></tr>";
  html += "</table>";

  html += "<script>";
  html += "function updateData() {";
  html += "fetch('/data').then(response => response.json()).then(data => {";
  html += "document.getElementById('tds').innerText = data.tds;";
  html += "document.getElementById('turbidity_ieee').innerText = data.turbidity_ieee;";
  html += "document.getElementById('turbidity_who').innerText = data.turbidity_who;";
  html += "document.getElementById('temperature').innerText = data.temperature;";
  html += "});";
  html += "}";
  html += "setInterval(updateData, 1000);";
  html += "</script>";
  html += "</body></html>";

  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println("Connection: close");
  client.print("Content-Length: ");
  client.println(html.length());
  client.println();
  client.println(html);
}

void sendHtmlData(WiFiClient client) {
  String json = "{"
                "\"tds\":" + String(tdsValue) +
                ",\"turbidity_ieee\":" + String(turbidityValue_IEEE) +
                ",\"turbidity_who\":" + String(turbidityValue_WHO) +
                ",\"temperature\":" + String(temperature) +
                "}";

  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: application/json");
  client.println("Connection: close");
  client.print("Content-Length: ");
  client.println(json.length());
  client.println();
  client.println(json);
}

void sendSensorData() {
  String json = "{"
                "\"tds\":" + String(tdsValue) +
                ",\"turbidity_ieee\":" + String(turbidityValue_IEEE) +
                ",\"turbidity_who\":" + String(turbidityValue_WHO) +
                ",\"temperature\:" + String(temperature) +
                "}";

  webSocket.broadcastTXT(json);
}

are you kidding ?

It's that time again!

Answering this question although not hard to do is not worth my time, I need some "secret" information so with that my answer well be kept secret!

yes

At what point in your testing, did the program begin to fail?

i am trying to test tds data pin number 27 ,turbidity data pin number 26 temperature data pin13
tds 3v
turbidity 5v
temprature 3v

temperature shows the realtime data but the other two thing does not.
thats why i post it here

test water tds turbidity temp

What do you see if you use serial.Print() to show you the value read from pin 27?

sorry about the sequence i've corrected the formatting

tds sensor value 0 data shows in serial monitor. and webpage with webserver not loaded

Yes, BUT! What is being read from pin 27? Why will you not tell us that?

the pin 27 is ADC pin i connect the tds sensor analog pin to the pin 27.
so that can masure the total disolved solid in the water.

If you cannot tell what value you get from the analog read, there is no reason to continue this conversation.

range tds 0 to 500ppm
turbidity 0 to -1000ntu

without the webpage connection it shows the right value but with webpage connection it does not shows the right value. it shows -43ppm does not increase or decrease

-1000 NTU does not increase or dwcrease