ashutosh7i
ashutosh7i

Reputation: 7

MQ sensors giving 0 value when using WiFi.h ESP32 but works fine without it

i am creating an IOT project using MQ07 sensors and esp32 and Sr04 Ultrasonic sensors.

My project continuously reads sensor data and prints to serial console, there are some lights to show status as well, and this works fine, till i use wifi.h

The microcontroller board i am using is ESP32 Wroom32 firmware:

enter image description here

When i am using the code without wifi.h library everything works fine,but the moment i use wifi.h in void setup, my code gives 0 as sensor value.

Working code:

#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>   // Universal Telegram Bot Library
#include <ArduinoJson.h>

// Replace with your network credentials
const char* ssid = "wifiname";
const char* password = "PassWord";
  
// Initialize Telegram BOT
#define BOTtoken "53939jkj9:hHKJHkhUHohhoHKJHkjhoJHook"  // your Bot Token

// Chat ID
#define CHAT_ID "138080345"

WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);



// Checks for new messages every 1 second.
int botRequestDelay = 1000;
unsigned long lastTimeBotRan;

//temp
const int ledPin = 2;
bool ledState = LOW;
//temp


//led declaration
int green_led=18;   //step1 shows heating
int red_led=19; //step2 sensors ok
int rb_led=21;    //step3 system ok

//for sonar sensor
const int trigPin = 14;
const int echoPin = 12;
int a=20;    //4       //triggering distances
int b=15;    //3
int c=10;    //2
int d=5;    //1
int max_distance = 200;

//for gas sensors
int mq_02 = 25;        //pins for mq sensor analog input 
int mq_07 = 26;
int mq_135 = 27;


// Handle what happens when you receive new messages
void handleNewMessages(int numNewMessages) {
  Serial.println("handleNewMessages");
  Serial.println(String(numNewMessages));

  for (int i=0; i<numNewMessages; i++) {
    // Chat id of the requester
    String chat_id = String(bot.messages[i].chat_id);
    if (chat_id != CHAT_ID){
      bot.sendMessage(chat_id, "Unauthorized user", "");
      continue;
    }
    
    // Print the received message
    String text = bot.messages[i].text;
    Serial.println(text);

    String from_name = bot.messages[i].from_name;

    if (text == "/start") {
      String welcome = "Welcome, " + from_name + ".\n";
      welcome += "Use the following commands to control your outputs.\n\n";
      welcome += "/led_on to turn GPIO ON \n";
      welcome += "/led_off to turn GPIO OFF \n";
      welcome += "/state to request current GPIO state \n";
      bot.sendMessage(chat_id, welcome, "");
    }

    if (text == "/led_on") {
      bot.sendMessage(chat_id, "LED state set to ON", "");
      ledState = HIGH;
      digitalWrite(ledPin, ledState);
    }
    
    if (text == "/led_off") {
      bot.sendMessage(chat_id, "LED state set to OFF", "");
      ledState = LOW;
      digitalWrite(ledPin, ledState);
    }
    
    if (text == "/state") {
      if (digitalRead(ledPin)){
        bot.sendMessage(chat_id, "LED is ON", "");
      }
      else{
        bot.sendMessage(chat_id, "LED is OFF", "");
      }
    }
  }
}


void setup ()
{

    Serial.begin(115200);         //serial output for debugging

    pinMode (green_led,OUTPUT);
    pinMode (rb_led,OUTPUT);
    pinMode (red_led,OUTPUT);


    pinMode(18, OUTPUT);    //led pins
    pinMode(19, OUTPUT);
    pinMode(21, OUTPUT);

                
  delay(2000);
  Serial.print("Gas sensor Heating up!");           delay(5000);    //mq sensor heating time
  Serial.println (" ");
  Serial.println("Done!;Ready to go");              delay(2000);


  pinMode(mq_02, INPUT);
  pinMode(mq_07, INPUT);
  pinMode(mq_135, INPUT);


//>>>- Code works when this is commented and shows 0 as output

//    //telegram
//    pinMode(ledPin, OUTPUT);
//    digitalWrite(ledPin, ledState);
//    // Connect to Wi-Fi
//    WiFi.mode(WIFI_STA);
//    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());

}


void loop() {

  //for sensor
    //staring loop to continously update sensor value
    int mq_02_value = analogRead(mq_02);
    //int mq_05_value = analogRead(mq_05);
    int mq_07_value = analogRead(mq_07);
    int mq_135_value = analogRead(mq_135);

  
long duration, inches, cm;
pinMode(trigPin,OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);


Serial.println(duration);
inches = (duration / 74) / 2;
cm = (duration / 29) / 2;

if (inches <max_distance){
Serial.print("Distance= ");
Serial.print(inches);
Serial.print(" inch");
Serial.print(" / ");
Serial.print(cm);
Serial.print(" cm");
Serial.println();

}
//digitalWrite (ledA,HIGH);
if (inches >=a) { Serial.print(" Safe Level "); digitalWrite (green_led,HIGH); digitalWrite (red_led,LOW);   Serial.println();
  Serial.print ("Methane=");  Serial.print (" ");   Serial.print (mq_02_value);  Serial.print ("\t"); 
  Serial.print ("Carbon=");  Serial.print (" ");   Serial.print (mq_07_value);    Serial.print ("\t");
  Serial.print ("Ammonia/Quality=");  Serial.print (" ");   Serial.print (mq_135_value);    Serial.println ("\t");
  if (mq_135_value>1000){Serial.println (" "); Serial.println ("!Warning!"); Serial.println ("Fire Detected"); Serial.println ("!Warning!"); Serial.println (" "); delay(1000); digitalWrite (rb_led,HIGH); }
  else if (mq_02_value>1500) { Serial.println (" "); Serial.println ("High Methane Level"); Serial.println ("!Warning!"); Serial.println (" "); delay(1000); digitalWrite (rb_led,HIGH); }
  else if (mq_07_value>1000){Serial.println (" "); Serial.println ("!Warning!"); Serial.println ("Smoke Detected"); Serial.println ("!Warning!"); Serial.println (" "); delay(1000); digitalWrite (rb_led,HIGH);}
}  
else if (inches >=b){Serial.print(" Level Rising"); digitalWrite (green_led,HIGH); delay(500); digitalWrite (green_led,LOW); delay(500);   Serial.println();
  Serial.print ("Methane=");  Serial.print (" ");   Serial.print (mq_02_value);  Serial.print ("\t"); 
  Serial.print ("Carbon=");  Serial.print (" ");   Serial.print (mq_07_value);    Serial.print ("\t");
  Serial.print ("Ammonia/Quality=");  Serial.print (" ");   Serial.print (mq_135_value);    Serial.println ("\t");
   if (mq_135_value>1000){Serial.println (" "); Serial.println ("!Warning!"); Serial.println ("Fire Detected"); Serial.println ("!Warning!"); Serial.println (" "); delay(1000); digitalWrite (rb_led,HIGH);}
  else if (mq_02_value>1500) { Serial.println (" "); Serial.println ("High Methane Level"); Serial.println ("!Warning!"); Serial.println (" "); delay(1000); digitalWrite (rb_led,HIGH);}
  else if (mq_07_value>1000){Serial.println (" "); Serial.println ("!Warning!"); Serial.println ("Smoke Detected"); Serial.println ("!Warning!"); Serial.println (" "); delay(1000); digitalWrite (rb_led,HIGH);}
 } 
else if (inches >=c){Serial.print(" !Warning High Water Levels! "); digitalWrite (red_led,HIGH); delay(500); digitalWrite (red_led,LOW); delay(500);   Serial.println();
  Serial.print ("Methane=");  Serial.print (" ");   Serial.print (mq_02_value);  Serial.print ("\t"); 
  Serial.print ("Carbon=");  Serial.print (" ");   Serial.print (mq_07_value);    Serial.print ("\t");
  Serial.print ("Ammonia/Quality=");  Serial.print (" ");   Serial.print (mq_135_value);    Serial.println ("\t");
 if (mq_135_value>1000){Serial.println (" "); Serial.println ("!Warning!"); Serial.println ("Fire Detected"); Serial.println ("!Warning!"); Serial.println (" "); delay(1000); digitalWrite (rb_led,HIGH);}
  else if (mq_02_value>1500) { Serial.println (" "); Serial.println ("High Methane Level"); Serial.println ("!Warning!"); Serial.println (" "); delay(2000); digitalWrite (rb_led,HIGH);}
  else if (mq_07_value>1000){Serial.println (" "); Serial.println ("!Warning!"); Serial.println ("Smoke Detected"); Serial.println ("!Warning!"); Serial.println (" "); delay(1000); digitalWrite (rb_led,HIGH); }
  } 
else if (inches >=d){Serial.print(" !!!OverFlow!!!"); digitalWrite (green_led,LOW); digitalWrite (red_led,HIGH);   Serial.println();
  Serial.print ("Methane=");  Serial.print (" ");   Serial.print (mq_02_value);  Serial.print ("\t"); 
  Serial.print ("Carbon=");  Serial.print (" ");   Serial.print (mq_07_value);    Serial.print ("\t");
  Serial.print ("Ammonia/Quality=");  Serial.print (" ");   Serial.print (mq_135_value);    Serial.println ("\t");
    if (mq_135_value>1000){Serial.println (" "); Serial.println ("!Warning!"); Serial.println ("Fire Detected"); Serial.println ("!Warning!"); Serial.println (" "); delay(1000); digitalWrite (rb_led,HIGH);}
  else if (mq_02_value>1500) { Serial.println (" "); Serial.println ("High Methane Level"); Serial.println ("!Warning!"); Serial.println (" "); delay(2000); digitalWrite (rb_led,HIGH);}
  else if (mq_07_value>1000){Serial.println (" "); Serial.println ("!Warning!"); Serial.println ("Smoke Detected"); Serial.println ("!Warning!"); Serial.println (" "); delay(1000); digitalWrite (rb_led,HIGH); }
  } 

delay(1000);
}

Output when wifi.h is uncommented

enter image description here

output when wifi.h is uncommented

enter image description here

Upvotes: -1

Views: 1379

Answers (2)

ochieno Eliud
ochieno Eliud

Reputation: 51

I got this very same error when using ESP32-mini module with mq2 smoke sensor initially connected on pin 25. Doing some research around I realized this pin 25 falls under ADC2 category pins hence disabled when WiFi is enabled. I switched to use pin 32 on the module and now works well. ESP32 pinouts congifuration

ESP32 interfaced with MQ2-smoke sensor sample result

Upvotes: 0

romkey
romkey

Reputation: 7069

The ESP32 has a rather infamous problem related to WiFi and analog to digital conversion. When WiFI is in use you can't reliably use ADC2 (GPIO pins 0, 2, 4, 12, 13, 14, 15, 25, 26 and 27). You're using ADC2 by using pins 25, 26 and 27.

From ESP-IDF, "ADC Limitations":

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.

You can still use those pins for other devices, like an I2C controller or SPI port by mapping a device to those pins.

You should use ADC1 instead. Its GPIO pins are 32, 33, 34, 35, 36, 37, 38 and 39 although they may not all be expressed on your board. You should pick three of those and change your code and wiring to use them.

Random Nerd Tutorials has a very helpful guide to which pins are usable for what functions.

Upvotes: 3

Related Questions