Combining Codes.

Hey all,

I was doing a project for school, and I need to use the BMP085 Pressure Sensor and the DHT-11 Humidity Sensor. Is there any way to combine these two codes to get the humidity, temperature, and pressure on my serial monitor?

Thanks,
Connor

Yes, there is a way to do it. Typically you combine the two setup() functions, combine the two loop() functions, and include all the other stuff from outside those two.

Great, thanks.

That worked for the most part. The only problem it that the altitude and pressure numbers are not turning up. Here is my sketch:

// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain

#include "DHT.h"
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085_U.h>

#define DHTPIN 2 // what pin we're connected to

// Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)

// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

DHT dht(DHTPIN, DHTTYPE);

void setup() {
Serial.begin(9600);
Serial.println("DHTxx test!");
Serial.println("Pressure Sensor Test"); Serial.println("");

dht.begin();
}

void loop() {
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
float t = dht.readTemperature();

// check if returns are valid, if they are NaN (not a number) then something went wrong!
if (isnan(t) || isnan(h)) {
Serial.println("Failed to read from DHT");
} else {
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" C");
/
Get a new sensor event */
sensors_event_t event;

/* Display the results (barometric pressure is measure in hPa) /
if (event.pressure)
{
/
Display atmospheric pressue in hPa */
Serial.print("Pressure: ");
Serial.print(event.pressure);
Serial.println(" hPa");

/* Calculating altitude with reasonable accuracy requires pressure *

  • sea level pressure for your position at the moment the data is *
  • converted, as well as the ambient temperature in degress *
  • celcius. If you don't have these values, a 'generic' value of *
  • 1013.25 hPa can be used (defined as SENSORS_PRESSURE_SEALEVELHPA *
  • in sensors.h), but this isn't ideal and will give variable *
  • results from one day to the next. *
  • You can usually find the current SLP value by looking at weather *
  • websites or from environmental information centers near any major *
  • airport. *
  • For example, for Paris, France you can check the current mean *
  • pressure and sea level at: http://bit.ly/16Au8ol */

/* Then convert the atmospheric pressure, SLP and temp to altitude /
/
Update this next line with the current SLP for better results */
float seaLevelPressure = SENSORS_PRESSURE_SEALEVELHPA;
Serial.print("Altitude: ");

Serial.println(" m");
Serial.println("");
}
else
{
Serial.println("Sensor error");
}
delay(1000);
}
}

Here is what it is showing:

Screen Shot 2014-05-10 at 1.50.38 PM.png

I suggest that, rather than trying to combine the two loop() functions you put each one into its own function and call them from a new loop() function like this

void loop() {
  bmp085Loop();
  dht11Loop();
}

void bmp085Loop() {
  // stuff from the loop part of the bmpo85 sketch
}

void dht11Loop() {
   // stuff from the loop part of the dht11 sketch
}

You will probably need to make some changes to the code to make the merged code work and keeping each part separate will make it easier to see what needs to be done.

...R

PS wrote this before reading your sketch looks like I was mistaken and you didn't post a sketch - sorry for any confusion

Edit to change erroneous ; to a { ...R

I see what you are saying, but I get tons of errors. How do I change that?

Connor

connordrone:
I see what you are saying, but I get tons of errors. How do I change that?

Connor

By reading the error messages and correcting them.

By reading the error messages and correcting them.
[/quote]

Thats the thing, I don't know how to. I am just a beginner and I don't know what they mean.

DHTtester.pde: In function 'void setup()':
DHTtester:28: error: a function-definition is not allowed here before '{' token
DHTtester:99: error: expected `}' at end of input

That is what the error said

It looks like you are trying to define a function inside the setup function.
It normally is that you have not got got matching braces or a missing semicolon.

Okay, thanks. Now another problem. It says that a bunch of stuff was not included in the scope. What does that mean and how do I change that?

connordrone:
That worked for the most part. The only problem it that the altitude and pressure numbers are not turning up. Here is my sketch:

The value in event.pressure is printing out as "ovf" which may mean some kind of overflow.

There is no code in your sketch for calculating altitude and you don't print it either which is why it doesn't show up in your output. Perhaps you left out some of the pressure sensor code.

  void loop();
  bmp085Loop();
  dht11Loop();
}

@Robin,
Silly question but is there a curly brace missing here ?

raschemmel:

  void loop();

bmp085Loop();
  dht11Loop();
}




@Robin,
Silly question but is there a curly brace missing here ?

That should be:

  void loop() {
  bmp085Loop();
  dht11Loop();
}

raschemmel:
Silly question but is there a curly brace missing here ?

Not silly at all - it has been corrected. Thanks for spotting it.

...R

connordrone:
Okay, thanks. Now another problem. It says that a bunch of stuff was not included in the scope. What does that mean and how do I change that?

Normally it is when you have not got some libary installed correctly.
It could also be you have defined your variables in the wrong place.
It will tell you what is in not in scope so look at where you use that and how it is defined.