Soil moisture and light sensor giving 0 output after wifi connection

both sensor is working when i comment these wifi connection related stuff, especially the one inside setup(), i really need help

#include <DHT.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>
#include <HTTPClient.h>

#define soil_pin 2
#define light_pin 15
#define led_red 18
#define led_blue 19
#define led_green 21
DHT dht(4,DHT11);
WebServer server(80);

const char* ssid = "wifiname";
const char* password =  "wifipass";

unsigned long prevnotif = 0;
unsigned long prevserial = 0;
const long kenotif = 60000;
const long keserial = 2000;

// void handleRoot() {
//   char msg[1500];

//   snprintf(msg, 1500,
//            "<html>\
//   <head>\
//     <meta http-equiv='refresh' content='4'/>\
//     <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'>\
//     <title>ESP32 Smart Plant!</title>\
//     <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; }\
//     .labels{ font-size: 1.5rem; vertical-align:middle; padding-bottom: 15px;}\
//     </style>\
//   </head>\
//   <body>\
//       <h2>ESP32 Smart Plant Server!</h2>\
//       <p>\
//         <span class='labels'>Temperature</span>\
//         <span>%.2f</span>\
//         <sup class='units'>&deg;C</sup>\
//       </p>\
//       <p>\
//         <span class='labels'>Soil Moisture</span>\
//         <span>%.2f</span>\
//         <sup class='units'>&percnt;</sup>\
//       </p>\
//       <p>\
//         <span class='labels'>Light</span>\
//         <span>%.2f</span>\
//         <sup class='units'>LUX</sup>\
//       </p>\
//   </body>\
// </html>",
//            readDHT(), readSoil(), readLight()
//           );
//   server.send(200, "text/html", msg);
// }

void setup() {
  pinMode(soil_pin, INPUT);
  pinMode(light_pin, INPUT);
  pinMode(led_red, OUTPUT);
  pinMode(led_blue, OUTPUT);
  pinMode(led_green, OUTPUT);
  dht.begin();
  delay(2000);
  Serial.begin(115200);
  // delay(10000);
  // WiFi.mode(WIFI_STA);
  // WiFi.begin(ssid, password);
  // Serial.println("");

  // while (WiFi.status() != WL_CONNECTED) {
  //   delay(500);
  //   Serial.print(".");
  // }

  // Serial.println("");
  // Serial.print("Connected to ");
  // Serial.println(ssid);
  // Serial.print("IP address: ");
  // Serial.println(WiFi.localIP());

  // if (MDNS.begin("esp32")) {
  //   Serial.println("MDNS responder started");
  // }
  // server.on("/", handleRoot);

  // server.begin();
  // Serial.println("HTTP server started");

}
void loop() {
  unsigned long curnotif = millis();
  unsigned long curserial = millis();
  float temp = dht.readTemperature();
  float LDR_Value = analogRead(light_pin);
  float lux = 250/(0.00080566406*LDR_Value)-50;

  if (lux >= 4000) {
    digitalWrite(led_green, LOW);
    digitalWrite(led_blue, HIGH);
    digitalWrite(led_red, LOW);
  } else if (lux >= 1000) {
    digitalWrite(led_green, HIGH);
    digitalWrite(led_blue, LOW);
    digitalWrite(led_red, LOW);
  } else {
    digitalWrite(led_green, LOW);
    digitalWrite(led_blue, LOW);
    digitalWrite(led_red, HIGH);
  }

  if (curserial - prevserial >= keserial) {
    prevserial = curserial;
    Serial.println();
    Serial.print("Soil Moisture : ");
    Serial.println(analogRead(soil_pin));
    Serial.print("Light Sensor  : ");
    Serial.println(lux);
    Serial.print("Temperature   : ");
    Serial.println(temp);
  }
  // server.handleClient();
  delay(1000);
}

float readSoil() {
  int sensorValue = analogRead(soil_pin);
  int minValue = 1800;
  int maxValue = 1000;
  if (sensorValue == 0) {    
    Serial.println("Failed to read from Soil Moisture sensor!");
    return -1;
  }else{
    int percent = map(sensorValue, maxValue, minValue, 0, 100);
    percent = constrain(percent, 0, 100);
    return percent;
  }
}

float readLight() {
  float LDR_Value = analogRead(light_pin);
  float lux = 250/(0.00080566406*LDR_Value)-50;
  if (LDR_Value == 0) {    
    Serial.println("Failed to read from Light sensor!");
    return -1;
  }else{
    return lux;
  }
}

float readDHT() {
  float t = dht.readTemperature();
  if (isnan(t)) {    
    Serial.println("Failed to read from DHT sensor!");
    return -1;
  }
  else {
    return t;
  }
}

Which arduino ?

Be careful on the ESP32 because it has two ADCs but One of them, ADC2, is actively used by the WiFi.

From the IDF documentation:

Since the ADC2 module is also used by the Wi-Fi, only one of them could get the preemption when using together, which means the adc2_get_raw() may get blocked until Wi-Fi stops, and vice versa.

So you can't use the ADC on any of the ADC2 channels while WiFi is on (so don’t use GPIO4, GPIO0, GPIO2, GPIO15, GPIO13, GPIO12, GPIO14, GPIO27, GPIO25, and GPIO26)

you can use ADC1 though (so pick any of GPIO36, GPIO37, GPIO38, GPIO39, GPIO32, GPIO33, GPIO34, and GPIO35).

1 Like

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