Hello how can i combined two sensor

hello how can i combined tow sensor
i want to connect dht11 and hc s40 to my esp nodmcu
any help?

What type of sensor is the hc s40? I’ve never heard of it and google doesn’t return any sensible results.

Pete.

1 Like

sory it’s hc sr04 “ultrasonic”

it should work. how did you tried and where it failed?
please provide properly formatted code and error log (if any).

1 Like

These ultrasonic sensors have their own timing requirements, as does Blynk.
Search the forum and get the ultrasonic code running with Blynk then look at adding the DHT11 (which is probably the worst temp/humidity sensor out there).

Don’t be tempted to try taking readings from the DHT11 more than once every 5 seconds and don’t take readings from the ultrasonic sensor too often either.

Pete.

2 Likes

thank for all
i used this code and it’s work

// defines pins numbers
const int trigPin = 12;
const int echoPin = 15;
// defines variables
long duration;
int distance;
float h;


#define BLYNK_PRINT Serial
#define DHTPIN D4   
#define DHTTYPE DHT11   // DHT 11
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include "DHT.h"


char auth[] = "";

BlynkTimer timer;
DHT dht(DHTPIN, DHTTYPE);
void myTimerEvent()
{
  float humi = dht.readHumidity();
  float temper = dht.readTemperature();

  if (isnan(humi) || isnan(temper)) 
  {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  else
  {
    Serial.print("Humidity: ");
    Serial.print(humi);
    Serial.print(" %\t");
    Serial.print("Temperature: ");
    Serial.println(temper);
    Blynk.virtualWrite(V4, humi); 
    Blynk.virtualWrite(V5, temper);
  }
}
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
Blynk.begin(auth, "Dude", "ar451626", "blynk-cloud.com", 8442);
timer.setInterval(2000L, myTimerEvent);
}


int data()
{
 // Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
delay(10);



Blynk.virtualWrite(1, distance);
return(distance);


}


void loop()
{
    Blynk.run();
   data();
      Blynk.virtualWrite(1, distance);
timer.run();

}

Putting Blynk.virtualWrite in your void loop is totally the wrong thing to do.
The other stuff in there isn’t a great idea either and as I said earlier, trying to read the DHT11 every 2 seconds isn’t recommended.

Pete.

1 Like

@amjad Please format posted code properly as required… I fixed your last post.

Blynk - FTFC

1 Like

i change it to 5 sec and remove Blynk.virtualWrite in void loop
is it better ?

// defines pins numbers
const int trigPin = 12;
const int echoPin = 15;
// defines variables
long duration;
int distance;
float h;


#define BLYNK_PRINT Serial
#define DHTPIN D4   
#define DHTTYPE DHT11   // DHT 11
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include "DHT.h"


char auth[] = "";

BlynkTimer timer;
DHT dht(DHTPIN, DHTTYPE);
void myTimerEvent()
{
  float humi = dht.readHumidity();
  float temper = dht.readTemperature();

  if (isnan(humi) || isnan(temper)) 
  {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  else
  {
    Serial.print("Humidity: ");
    Serial.print(humi);
    Serial.print(" %\t");
    Serial.print("Temperature: ");
    Serial.println(temper);
    Blynk.virtualWrite(V4, humi); 
    Blynk.virtualWrite(V5, temper);
  }
}
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
Blynk.begin(auth, "Dude", "ar451626", "blynk-cloud.com", 8442);
timer.setInterval(5000L, myTimerEvent);
}


int data()
{
 // Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
delay(10);



Blynk.virtualWrite(1, distance);
return(distance);


}


void loop()
{
    Blynk.run();
   data();/
timer.run();

}

and btw why Putting Blynk.virtualWrite in void loop is totally the wrong?

Because the void loop() runs thousands of times a second.

1 Like

thnx

And having your data() function in loud loop also means that the program flow is trying to run the process which takes an ultrasonic measurement thousands of time per second as well. Except it can’t, because you have a delay() in there.

You should be running this from a timer as well.

Pete.

ok thank u
u r great