dht.h interferes with softwareserial?

Hi,

I have a project with a serial gps and a DHT22 sensor.

I use the gps with softwareserial and (I think) the DHT22 uses an interrupt (but I am not sure as I do not have enough knowledge to interpret the DHT.h library).

When I try to read data from DHT and then from GPS:

  • the GPS does not give any serial output.
  • DHT gives output

When I comment out the DHT part GPS output works fine...

Any ideas?

I use:
(I did not paste full sketch as it is part of a much larger sketch...)

#include <dht.h>             //Temp and humidity
int 	PIN_TempOutside	        = 	22;
//Initialize GPS software serial port
SoftwareSerial GPSSerial(53, 52); // Mega RX = digital pin 53, TX = digital pin 52

//Declare temperature library instance
dht DHT;

I run this DHT related code in setup():

pinMode(	PIN_TempOutside	        ,	INPUT);
  GPSSerial.begin(9600);  //GPS serial

I run this in loop()

getGPSValues();
delay (2000);
getSensorValues();

void getGPSValues(){
    // This sketch displays information every time a new sentence is correctly encoded.
      GPSSerial.listen();  //When using more softwareserial this command enables the needed software serial ports
      while (GPSSerial.available() > 0){
      if (gps.encode(GPSSerial.read()))
      {
          if (gps.location.isValid())
          {
              GPSLocLat = gps.location.lat();
              GPSLocLng = gps.location.lng();
              GPSSpeed = gps.speed.kmph();
              GPSCourse = gps.course.deg();
              GPSAltitude = gps.altitude.meters();
              GPSNrOfSat = gps.satellites.value();
          }
      }
     if (millis() > 5000 && gps.charsProcessed() < 10)
     {
          Serial.println(F("No GPS detected: check wiring."));
          GPSError = true;
          while(true);
     }
    }
}

void getSensorValues(){

//Temp and humidty outside
    int chk = DHT.read22(PIN_TempOutside);
    switch (chk)
    {
      case DHTLIB_OK:  
  		//Serial.print("OK,\t"); 
  		break;
      case DHTLIB_ERROR_CHECKSUM: 
  		Serial.print("Checksum error,\t"); 
  		break;
      case DHTLIB_ERROR_TIMEOUT: 
  		Serial.print("Time out error,\t"); 
  		break;
      case DHTLIB_ERROR_CONNECT:
          Serial.print("Connect error,\t");
          break;
      case DHTLIB_ERROR_ACK_L:
          Serial.print("Ack Low error,\t");
          break;
      case DHTLIB_ERROR_ACK_H:
          Serial.print("Ack High error,\t");
          break;
      default: 
  	Serial.print("Unknown error,\t"); 
  	break;
      }
        TempOutside= DHT.temperature;
        HumidityOutside= DHT.humidity;
   }

What happens if you choose two different pins for Software Serial?

You need to post a complete program that demonstrates the problem.

...R

Hi, I do not understand your question.

GPS is software serial on pin 52 and 53
DHT is connected on pin 22, it is not using software serial but it is done from within the library DHT.h include (which I cannot interpret...)

I understand the need for posting the whole sketch, probem is it is just a part of a much, much larger sketch.

I have found a way on the forum to manage multiple software serial inputs with something like:

portOne.listen();
// while there is data coming in, read it
// and send to the hardware serial port:
while (portOne.available() > 0)

....

portTwo.listen();
// while there is data coming in, read it
// and send to the hardware serial port:
while (portTwo.available() > 0)
....

But as the DHT does not (visibly) use software serial I am unable to apply this.

Regards,
Jeroen

vorstendom:
Hi, I do not understand your question.

GPS is software serial on pin 52 and 53

Because you have not posted a complete program I cannot tell whether the use of 52 and 53 may be the cause of your problem. It would take you less than 3 minutes to change and see what happens.

...R

dht disables interrupts. Does that help?

GPS is software serial on pin 52 and 53

Why are you using SoftwareSerial on the Mega? What are the 4 hardware serial ports doing? If ANY of them is not being used, DO NOT USE SOFTWARE SERIAL!.

PaulS:
Why are you using SoftwareSerial on the Mega? What are the 4 hardware serial ports doing? If ANY of them is not being used, DO NOT USE SOFTWARE SERIAL!.

Blindingly obvious !! Why did I not notice that (no, don't answer that).

...R

thanx for te replies.

I use the tinygps++ library which requires to use software serial

And I also have
-a bluetooth module
-a gsm module
-a 2,4 GHz transceiver

So I am also running out of serial hardware ports

I use the tinygps++ library which requires to use software serial

It most certainly does NOT!

@PaulS: Thx, True, just found it in a forum myself. But in the examples softwareserial is used.... That is why I thought it is mandatory. I got it working now on a hardware serial.

But then new problems rise.

I run a number of main funtions in loop:

//Read, interpret and store all sensor values in variables, this includes also onewire sensors
getSensorValues();
//Read, interpret GPS data and store in variables; hardware serial
getGPSValues();
//Read touch events from nextion touch screen; hardware serial defined in the library
nexLoop(nex_listen_list);
//Send data from the earlier stored variables to nextion screen; hardware serial defined in the library
sendScreen0Data();
//Receive Bluetooth data; hardware serial
receiveBluetooth();
//Send data from the earlier stored variables to bluetooth; hardware serial
sendToBluetooth();
//write data from the earlier stored variables serial for development & Debug purposes
serialOutput();

When I run like above the GPS does not show any data anymore. When I comment out all code except the GPS and serialOutput() it works fine.

Reading and thinking about it it looks like I get time outs or interrupts interfering with reading data when executing the rather time consuming functions, but I have no clue how test or tackle this...

Any ideas or maybe best practices how to run extensive code and multiple serial in/outputs?

thx

vorstendom:
But then new problems rise.

Post the latest version of your code so we can keep up with you.

...R