Search code examples
arduinoesp32

ESP32 Access point interfearence with MQ2 module?


i have an esp32 code that is incharge of the detection of gas leak using mq2 module and also it is supposed to sent the result to another esp32. both part of access point and mq2 works fine, but whenever i uncomment the access point part code, the mq2 module doesnt seems to work and keeps showing "Gas sensor: 0" in serial monitor. also worth mentioning that even tho this happens, access point works fine but the gas detection doesn't.

here is the code:

#include "WiFi.h"
#include "ESPAsyncWebServer.h"
#include <Wire.h>

const char* ssid = "ESP32-Access-Point";
const char* password = "123456789";

int Sensor_input = 2;      
bool gasDetected = false;  

String readGas;

AsyncWebServer server(80);

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

//   Serial.print("Setting Access Point…");
//   WiFi.softAP(ssid, password);

//   IPAddress IP = WiFi.softAPIP();
//   Serial.print("AP IP address: ");
//   Serial.println(IP);

//   server.on("/gasdtc", HTTP_GET, [](AsyncWebServerRequest *request){
//   request->send_P(200, "text/plain", readGas.c_str());
//   });
  
//  bool status;
  
//   server.begin();
//   Serial.print("Server started. MQ2 detection innitializing:");
}
 
void loop(){
  int sensor_Aout = analogRead(Sensor_input);  
  Serial.print("Gas Sensor: ");
  Serial.print(sensor_Aout);  
  Serial.print("\t");
  Serial.print("\t");

//  Serial.print("Initial Free Heap: ");
//  Serial.println(ESP.getFreeHeap());


  if (sensor_Aout > 1800) {    
    Serial.println("Gas");
    gasDetected = true;
    readGas = "Gas detected: True";
  } else {
    Serial.println("No Gas");
    gasDetected = false;
    readGas = "Gas detected: False";
  }
  delay(1000);
}

tried almost everything other than changing the access point code (which i don't want to because it seems to work perfectly)


Solution

  • The ESP32 has a limitation the many people seem unaware of. It has two ADC (analog-to-digital converters), ADC1 and ADC2. ADC2 is unavailable while WiFi is active.

    ADC1's GPIO pins are 32, 33, 34, 35, 36, 37, 38, 39. ADC2's GPIO pins are 0, 2, 4, 12, 13, 14, 15, 25, 26, 27. Unfortunately, your project is using GPIO2 , which is on ADC2. So, as expected, GPIO2 stops working when WiFi is active.

    To solve the problem, change your wiring and code to use an ADC1 pin.