Problem with Home Automation Project

Hi there, i have created this really simple code to send some useful data like temperature, humidity, soil moisture and gas quality to nodemcu via the internet. I am using gas sensor MQ2, soil moisture sensor and DHT11 sensor for temp and humidity. But unfortunately soil sensor and MQ2 is not sending any data. PLZ help ME!!
CODE-

    #define BLYNK_PRINT Serial


    #include <ESP8266WiFi.h>
    #include <BlynkSimpleEsp8266.h>
    #include <DHT.h>

    // You should get Auth Token in the Blynk App.
    // Go to the Project Settings (nut icon).
    char auth[] = "a9c89af929024044a8146779d7111aef";

    // Your WiFi credentials.
    // Set password to "" for open networks.
    char ssid[] = "BHUTANI";
    char pass[] = "#198s6i8";

    #define DHTPIN 2          // What digital pin temperature and humidity sensor is connected to
    #define soilPin 0         // What digital pin soil moisture sensor is connected to
    #define gasPin 4          // What digital pin gas sensor is connected to

    // Uncomment whatever type you're using!
    #define DHTTYPE DHT11     // DHT 11
 
    DHT dht(DHTPIN, DHTTYPE);
    BlynkTimer timer;

 
    void sendSensor()
    {
    float h = dht.readHumidity();
    float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

    if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
    }
    
    Blynk.virtualWrite(V5, h);
    Blynk.virtualWrite(V6, t);
    }

    void sendsoilSensor()
    {
    float s = analogRead(soilPin);

     if (isnan(s)) {
     Serial.println("Failed to read from Gas sensor!");
    return;
    }
  
     Blynk.virtualWrite(V7, s);
     }

     void sendgasSensor()
     {
     float g = analogRead(gasPin);

     if (isnan(g)) {
    Serial.println("Failed to read from Gas sensor!");
    return;
     }
 
     Blynk.virtualWrite(V8, g);
    }
    void setup()
    {
    // Debug console
    Serial.begin(9600);

    Blynk.begin(auth, ssid, pass);

    dht.begin();

    // Setup a function to be called every second
    timer.setInterval(1000L, sendSensor);
    }

    void loop()
    {
    Blynk.run();
    timer.run();
    }
1 Like

please, would you mind turning off the bold characters?

what esp board are you using?

nodemcu only have 1 analog port A1, so you are only able to read 1 analog value.

1 Like

esp8266

and since when has the esp8266 2 analog pins?

How can i do it?

I thought GPIO pins can take analog data too!

You need to google “analogue multiplexer” if you want to find ways of using more than one analogue input with a NodeMCU/RSP8266.

Personally, I’d probably use multiple NodeMCU/ESP8266 devices with one analogue sensor per device. This approach might even make your setup simpler, if there is a physical distance between your gas, soil and humidity monitoring locations.

Pete.

There is another way tell me if possible? I can use the analog pin for gas sensor and use a digital pin with soil sensor in order to just know if the soil is moist or nor!

You could use one of these:

Pete.

actually, there are lots of modules even for gas and moisture sensors, which have also a digital output, and you can set the threshold level with a trimpot on the module. (like the one @PeteKnight linked)

thus, you can make the whole project using digital pins only. also, take care not to supply the gas sensors from digital pins, they consume ~150mA!

Why not to use an I2C ADC?

https://www.aliexpress.com/item/32819279709/32819279709.html

For example…

Thanks a lot! I have finally succeeded! I hooked the gas sensor to adc00 (A0) and digital pin of soil moisture sensor to a digital pin on nodemcu and it is finally working!