ESP32 soil moisture sensor reading error when trying to send data to server

Hi. I'm making wifi plant irrigator using ESP32 dev module and Capacitive Soil Moisture Sensor v1.2. I'm quite new at programming . I have problem with soil moisture data reading. While I'm using this code it shows correct data.

const int pin = 4;
const int Air = 3970;
const int Water = 1620;
int soilread;
int soilpercent;

void setup() {

  Serial.begin(115200);
  pinMode(pin, INPUT);
 }

void loop() {

  
  soilread = analogRead(pin);
  soilpercent = map(soilread, Air, Water, 0, 100);
  
  if(soilpercent>100){
    Serial.println("Soil Moisture 100%");
  }

  if(soilpercent<0){
    Serial.println("Soil Moisture 0%");
  }
 
  if(soilpercent>=0 && soilpercent <=100){
    Serial.println("Soil Moisture");
    Serial.print(soilpercent);
    Serial.print("[%]");
  }
  delay(1000);
}

But when I use code with sending data to web server it always show 169, no matter if sensor is in the water or in the air.

#include "WiFi.h"
#include "ESPAsyncWebServer.h"
#include <Adafruit_Sensor.h>
#include <DHT.h>

const char* ssid = " ";
const char* password = " ";
const int air = 3970;
const int water = 1620;

int soilread;     //analogRead soilpin
int soilpercent;  
int soilpin = 13;

#define DHTPIN 4    
#define DHTTYPE DHT11    
DHT dht(DHTPIN, DHTTYPE);

AsyncWebServer server(80);

String readSoilMoisture() {

  if (soilpercent <0){
    return "0";
  }

  if (soilpercent >100){
    return "100";
  } 

  if (soilpercent>=0 && soilpercent <=100){
    return String(soilpercent);
  }  
}


String readDHTTemperature() {

  float t = dht.readTemperature();

  if (isnan(t)) {    
    Serial.println("Failed to read from DHT sensor!");
    return "--";
  }
  else {
    Serial.println(t);
    return String(t);
  }
}

String readDHTHumidity() {

  float h = dht.readHumidity();
  if (isnan(h)) {
    Serial.println("Failed to read from DHT sensor!");
    return "--";
  }
  else {
    Serial.println(h);
    return String(h);
  }
}

const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
  <style>
    html {
     font-family: Arial;
     display: inline-block;
     margin: 0px auto;
     text-align: center;
    }
    h2 { font-size: 3.0rem; }
    p { font-size: 3.0rem; }
    .units { font-size: 1.2rem; }
    .dht-labels{
      font-size: 1.5rem;
      vertical-align:middle;
      padding-bottom: 15px;
    }
  </style>
</head>
<body>
  <h2>Sensor data</h2>
  <p>
    <i class="fas fa-seedling"" style="color:#09993e;"></i>   
    <span class="soil-labels">Soil Moisture</span>
    <span id="soilmoisture">%SOILMOISTURE%</span>
    <sup class="units">&percnt;</sup>
  </p>
  <p>
    <i class="fas fa-thermometer-half" style="color:#059e8a;"></i> 
    <span class="dht-labels">Air Temperature</span> 
    <span id="temperature">%TEMPERATURE%</span>
    <sup class="units">&deg;C</sup>
  </p>
  <p>
    <i class="fas fa-tint" style="color:#00add6;"></i> 
    <span class="dht-labels">Air Humidity</span>
    <span id="humidity">%HUMIDITY%</span>
    <sup class="units">&percnt;</sup>
  </p>
</body>
<script>
setInterval(function ( ) {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("soilmoisture").innerHTML = this.responseText;
    }
  };
  xhttp.open("GET", "/soilmoisture", true);
  xhttp.send();
}, 5000 ) ;

setInterval(function ( ) {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("temperature").innerHTML = this.responseText;
    }
  };
  xhttp.open("GET", "/temperature", true);
  xhttp.send();
}, 10000 ) ;

setInterval(function ( ) {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("humidity").innerHTML = this.responseText;
    }
  };
  xhttp.open("GET", "/humidity", true);
  xhttp.send();
}, 10000 ) ;
</script>
</html>)rawliteral";


String processor(const String& var){
  //Serial.println(var);
  if(var == "TEMPERATURE"){
    return readDHTTemperature();
  }
  else if(var == "HUMIDITY"){
    return readDHTHumidity();
  }
  else if(var == "SOILMOISTURE"){
    return readSoilMoisture();
  }
  return String();
}

void setup(){

  Serial.begin(115200);
  pinMode(soilpin, INPUT);
  dht.begin();
  
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi..");
  }

  // Print ESP32 Local IP Address
  Serial.println(WiFi.localIP());

  // Route for root / web page
  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/html", index_html, processor);
  });
  server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", readDHTTemperature().c_str());
  });
  server.on("/humidity", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", readDHTHumidity().c_str());
  });
  server.on("/soilmoisture", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", readSoilMoisture().c_str());
  });

  // Start server
  server.begin();
}
 
void loop(){
  soilread = analogRead(soilpin);
  soilpercent = map(soilread, air, water, 0, 100);
  Serial.println(soilpercent);
  delay(1000);

}

This code is from ESP32 DHT11/DHT22 Web Server using Arduino IDE | Random Nerd Tutorials
I changed it that way that it send soil moisture data to web server. But as I mentioned received sensor data is wrong.
How can I fix this.

Please give some examples of incorrect data and the corresponding correct data

Please don't post pictures, copy and paste the text in code tags. So much easier to read and deal with

Which figures are which ?

Sorry for that. This screen with only 169 is from second code ( this which send data to server, and to board are connected DHT11 and soil moisture sensor).
Second screen (where it displays "xx[%] Soil Moisture" is response for first code( code without sending data to server and using only soil moisture sensor).

Ok, I fixed this. I looked once again to pinout and chosen another pin. Here's the code.

#include "WiFi.h"
#include "ESPAsyncWebServer.h"
#include <Adafruit_Sensor.h>
#include <DHT.h>

const char* ssid = "wrota tele :-)";
const char* password = "abcde12345";
const int air = 3970;
const int water = 1620;

int soilread;     //analogRead soilpin
int soilpercent;  
int soilpin = 34;

#define DHTPIN 2    
#define DHTTYPE DHT11    
DHT dht(DHTPIN, DHTTYPE);

AsyncWebServer server(80);

String readSoilMoisture() {

 if (soilpercent <0){
   return "0";
 }

 if (soilpercent >100){
   return "100";
 } 

 if (soilpercent>=0 && soilpercent <=100){
   return String(soilpercent);
 }  
}


String readDHTTemperature() {

 float t = dht.readTemperature();

 if (isnan(t)) {    
   Serial.println("Failed to read from DHT sensor!");
   return "--";
 }
 else {
   Serial.println(t);
   return String(t);
 }
}

String readDHTHumidity() {

 float h = dht.readHumidity();
 if (isnan(h)) {
   Serial.println("Failed to read from DHT sensor!");
   return "--";
 }
 else {
   Serial.println(h);
   return String(h);
 }
}

const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html>
<head>
 <meta name="viewport" content="width=device-width, initial-scale=1">
 <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
 <style>
   html {
    font-family: Arial;
    display: inline-block;
    margin: 0px auto;
    text-align: center;
   }
   h2 { font-size: 3.0rem; }
   p { font-size: 3.0rem; }
   .units { font-size: 1.2rem; }
   .dht-labels{
     font-size: 1.5rem;
     vertical-align:middle;
     padding-bottom: 15px;
   }
 </style>
</head>
<body>
 <h2>Sensor data</h2>
 <p>
   <i class="fas fa-seedling"" style="color:#09993e;"></i>   
   <span class="soil-labels">Soil Moisture</span>
   <span id="soilmoisture">%SOILMOISTURE%</span>
   <sup class="units">&percnt;</sup>
 </p>
 <p>
   <i class="fas fa-thermometer-half" style="color:#059e8a;"></i> 
   <span class="dht-labels">Air Temperature</span> 
   <span id="temperature">%TEMPERATURE%</span>
   <sup class="units">&deg;C</sup>
 </p>
 <p>
   <i class="fas fa-tint" style="color:#00add6;"></i> 
   <span class="dht-labels">Air Humidity</span>
   <span id="humidity">%HUMIDITY%</span>
   <sup class="units">&percnt;</sup>
 </p>
</body>
<script>
setInterval(function ( ) {
 var xhttp = new XMLHttpRequest();
 xhttp.onreadystatechange = function() {
   if (this.readyState == 4 && this.status == 200) {
     document.getElementById("soilmoisture").innerHTML = this.responseText;
   }
 };
 xhttp.open("GET", "/soilmoisture", true);
 xhttp.send();
}, 5000 ) ;

setInterval(function ( ) {
 var xhttp = new XMLHttpRequest();
 xhttp.onreadystatechange = function() {
   if (this.readyState == 4 && this.status == 200) {
     document.getElementById("temperature").innerHTML = this.responseText;
   }
 };
 xhttp.open("GET", "/temperature", true);
 xhttp.send();
}, 10000 ) ;

setInterval(function ( ) {
 var xhttp = new XMLHttpRequest();
 xhttp.onreadystatechange = function() {
   if (this.readyState == 4 && this.status == 200) {
     document.getElementById("humidity").innerHTML = this.responseText;
   }
 };
 xhttp.open("GET", "/humidity", true);
 xhttp.send();
}, 10000 ) ;
</script>
</html>)rawliteral";


String processor(const String& var){
 //Serial.println(var);
 if(var == "TEMPERATURE"){
   return readDHTTemperature();
 }
 else if(var == "HUMIDITY"){
   return readDHTHumidity();
 }
 else if(var == "SOILMOISTURE"){
   return readSoilMoisture();
 }
 return String();
}

void setup(){

 Serial.begin(115200);
 pinMode(soilpin, INPUT);
 dht.begin();
 
 WiFi.begin(ssid, password);
 while (WiFi.status() != WL_CONNECTED) {
   delay(1000);
   Serial.println("Connecting to WiFi..");
 }

 // Print ESP32 Local IP Address
 Serial.println(WiFi.localIP());

 // Route for root / web page
 server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
   request->send_P(200, "text/html", index_html, processor);
 });
 server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest *request){
   request->send_P(200, "text/plain", readDHTTemperature().c_str());
 });
 server.on("/humidity", HTTP_GET, [](AsyncWebServerRequest *request){
   request->send_P(200, "text/plain", readDHTHumidity().c_str());
 });
 server.on("/soilmoisture", HTTP_GET, [](AsyncWebServerRequest *request){
   request->send_P(200, "text/plain", readSoilMoisture().c_str());
 });

 // Start server
 server.begin();
}

void loop(){
 soilread = analogRead(soilpin);
 soilpercent = map(soilread, air, water, 0, 100);
 Serial.println(soilpercent);
 delay(1000);

}

In the housing it says ESP32-WROOM-32

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