Ethernet Shield With Barometric Pressure Sensor

Hi all,

I am working on a project to monitor different parameters of my greenhouse using an Arduino Uno and an ethernet shield, like water temerature and barometric pressure. Right now, my code runs and has the ability to turn my grow lights on/off, measure air temerature, and relative humidity. My code worked fine until today, when my barometric sensor arrived. It is a BMP180. I downloaded the libraries from Adafruit and started coding it into my greenhouse code with the help of the example code.

Here is where I hit a wall…the line of code under the loop that gets the events makes the entire HTML page not work at all. It is only that line of code and I have no idea as to why it is an issue. As soon as I remove it, the page loads again. Any ideas as to what is going on here?

Here is my code:

//libraries
#include <SPI.h>
#include <Ethernet.h>
#include <dht.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085_U.h>


//ethernet
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };// mac address
byte ip[] = { 10, 0, 0, 60 }; // IP address in LAN
byte gateway[] = { 10, 0, 0, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); 

//strings
String readString;

//integers
int ledPin = 2;
const int temperaturePin = A0;

//pin definitions
#define DHT11_PIN A1
dht DHT;

//conversions
float hum;

Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10085);

void setup() {

  pinMode(ledPin, OUTPUT); //pin selected to control
  digitalWrite(ledPin, HIGH);
  delay(500);
  digitalWrite(ledPin, LOW); 
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
}

void loop() {
  int reading = analogRead(temperaturePin);
  
  float voltage = reading * 5.0;
  voltage /= 1024.0;

  float temperatureC = (voltage - 0.5) * 100;
  float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
  
  int chk = DHT.read11(DHT11_PIN);
  hum = DHT.humidity;

  sensors_event_t event;
  bmp.getEvent(&event);
  
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string
          readString += c;
        }

        //if HTTP request has ended– 0x0D is Carriage Return \n ASCII
        if (c == 0x0D) {
          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");
          client.println();

          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE> Greenhouse Monitor</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");
          client.println("<hr>");
          client.println("
");
          client.println("<H1 style=\"color:Green;\">Greenhouse Monitor</H1>");
          client.println("<hr>");
          client.println("
");

          client.println("<H1 style=\"color:Green;\">Grow Lights:</H1>");

          client.println("<H2><a href=\"/?GROWON\"\">Turn On Growlights</a>
</H2>");
          client.println("<H2><a href=\"/?GROWOFF\"\">Turn Off Growlights</a>
</H2>");

          client.println("<H1 style=\"color:Green;\">Air Temperature:</H1>");
          client.println(temperatureC);
          client.println("C");
          client.println("
");
          client.println(temperatureF);
          client.println("F");
          client.println("
");
          
          client.println("<H1 style=\"color:Green;\">Relative Humidity:</H1>");
          client.println(hum);
          client.println("%");
          client.println("
");

          client.println("<H1 style=\"color:Green;\">Barometric Pressure:</H1>");
          client.println(event.pressure);
          client.println(" hPa");
          client.println("
");

          client.println("</BODY>");
          client.println("</HTML>");

          delay(10);
          //stopping client
          client.stop();

          // control grow lights
          if (readString.indexOf("?GROWON") > -1)
          {
            digitalWrite(ledPin, HIGH);
          }
          else {
            if (readString.indexOf("?GROWOFF") > -1) 
            {
              digitalWrite(ledPin, LOW);
            }
          }
          //clearing string for next read
          readString = "";

        }
      }
    }
  }
}

Here is the line that is causing the problem:

bmp.getEvent(&event);

Not sure what "bmp.getEvent(&event);" does,l but have you tried using in simple test code where it will run and maybe print what ever it does to the serial monitor?

zoomkat:
Not sure what “bmp.getEvent(&event);” does,l but have you tried using in simple test code where it will run and maybe print what ever it does to the serial monitor?

Here is the example code that Adafruit has for the BMP180:

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

/* This driver uses the Adafruit unified sensor library (Adafruit_Sensor),
   which provides a common 'type' for sensor data and some helper functions.
   
   To use this driver you will also need to download the Adafruit_Sensor
   library and include it in your libraries folder.

   You should also assign a unique ID to this sensor for use with
   the Adafruit Sensor API so that you can identify this particular
   sensor in any data logs, etc.  To assign a unique ID, simply
   provide an appropriate value in the constructor below (12345
   is used by default in this example).
   
   Connections
   ===========
   Connect SCL to analog 5
   Connect SDA to analog 4
   Connect VDD to 3.3V DC
   Connect GROUND to common ground
    
   History
   =======
   2013/JUN/17  - Updated altitude calculations (KTOWN)
   2013/FEB/13  - First version (KTOWN)
*/
   
Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10085);

/**************************************************************************/
/*
    Displays some basic information on this sensor from the unified
    sensor API sensor_t type (see Adafruit_Sensor for more information)
*/
/**************************************************************************/
void displaySensorDetails(void)
{
  sensor_t sensor;
  bmp.getSensor(&sensor);
  Serial.println("------------------------------------");
  Serial.print  ("Sensor:       "); Serial.println(sensor.name);
  Serial.print  ("Driver Ver:   "); Serial.println(sensor.version);
  Serial.print  ("Unique ID:    "); Serial.println(sensor.sensor_id);
  Serial.print  ("Max Value:    "); Serial.print(sensor.max_value); Serial.println(" hPa");
  Serial.print  ("Min Value:    "); Serial.print(sensor.min_value); Serial.println(" hPa");
  Serial.print  ("Resolution:   "); Serial.print(sensor.resolution); Serial.println(" hPa");  
  Serial.println("------------------------------------");
  Serial.println("");
  delay(500);
}

/**************************************************************************/
/*
    Arduino setup function (automatically called at startup)
*/
/**************************************************************************/
void setup(void) 
{
  Serial.begin(9600);
  Serial.println("Pressure Sensor Test"); Serial.println("");
  
  /* Initialise the sensor */
  if(!bmp.begin())
  {
    /* There was a problem detecting the BMP085 ... check your connections */
    Serial.print("Ooops, no BMP085 detected ... Check your wiring or I2C ADDR!");
    while(1);
  }
  
  /* Display some basic information on this sensor */
  displaySensorDetails();
}

/**************************************************************************/
/*
    Arduino loop function, called once 'setup' is complete (your own code
    should go here)
*/
/**************************************************************************/
void loop(void) 
{
  /* Get a new sensor event */ 
  sensors_event_t event;
  bmp.getEvent(&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                   */
     
    /* First we get the current temperature from the BMP085 */
    float temperature;
    bmp.getTemperature(&temperature);
    Serial.print("Temperature: ");
    Serial.print(temperature);
    Serial.println(" C");

    /* Then convert the atmospheric pressure, and SLP to altitude         */
    /* Update this next line with the current SLP for better results      */
    float seaLevelPressure = SENSORS_PRESSURE_SEALEVELHPA;
    Serial.print("Altitude:    "); 
    Serial.print(bmp.pressureToAltitude(seaLevelPressure,
                                        event.pressure)); 
    Serial.println(" m");
    Serial.println("");
  }
  else
  {
    Serial.println("Sensor error");
  }
  delay(1000);
}

I’m not 100% sure what that line of code does…but I find it weird that it makes my HTML page go from perfectly fine to not being able to load at all. Unless, the arduino uses the SCL and SDA lines fro the ethernet shield communication.

It has been a long time, but the ethernet shield may be particular about things being connected to pins 9 and 10 on boot up.

zoomkat: It has been a long time, but the ethernet shield may be particular about things being connected to pins 9 and 10 on boot up.

The BMP180 uses the SCL and SDA pins to communicate (which is A4 and A5), but I do see SCL and SDA pins on the digital side of the board (next to the AREF pin). Not sure if the I2C pins are fighting with the ethernet shield or not. It also definitely seems like this is the case because as soon as I add that line of code back (which permits the I2C communication to begin), it stops the HTML page completely.

josephm3502: . Not sure if the I2C pins are fighting with the ethernet shield

I can't see how that can happen. Ethernet uses SPI. About the only thing they obviously have in common is the power supply, which is something you might consider checking.

Nick_Pyner: I can't see how that can happen. Ethernet uses SPI. About the only thing they obviously have in common is the power supply, which is something you might consider checking.

Well that proves that my guess is indeed incorrect haha. Anyways, the barometric sensor only draws about 5uA. I am not sure how much current the ethernet shield draws, but I even tried uploading the code and not connecting the sensor, and it still yielded the same result...

You might leave the code the same and physically disconnect the module and see if the issue still happens.

Tried that as well, and still the same result. I'm at a loss here lol...

"Tried that as well, and still the same result. I'm at a loss here lol..."

Does the module example code you posted work without the ethernet shield? Is the module somehow stacked on top of the ethernet shield?

zoomkat:
“Tried that as well, and still the same result. I’m at a loss here lol…”

Does the module example code you posted work without the ethernet shield? Is the module somehow stacked on top of the ethernet shield?

If I use just the example code that it came with it works just fine. It gives me barometric pressure and temperature. The moment I add components of the example code for the sensor to my HTML code, it stops working.

I have the Ethernet shield atop of the Uno and then I have the power jumpers and I2C jumpers plugged into the headers on the Ethernet shield.

"If I use just the example code that it came with it works just fine. It gives me barometric pressure and temperature. The moment I add components of the example code for the sensor to my HTML code, it stops working."

When the example code works, is all the hardware installed at that time? This might tell if there is a hardware conflict from the start, or there is some code/hardware conflict when the new code is run with the hardware installed.

zoomkat: "If I use just the example code that it came with it works just fine. It gives me barometric pressure and temperature. The moment I add components of the example code for the sensor to my HTML code, it stops working."

When the example code works, is all the hardware installed at that time? This might tell if there is a hardware conflict from the start, or there is some code/hardware conflict when the new code is run with the hardware installed.

Yes all the hardware is installed at that time. All I did was just upload the example code with everything still attached...

It might also be worth noting that I have also have tried 6 different libraries for the barometric pressure sensor with the same exact result for each one.