Adding barometric pressure to other sensors

This is a special forum devoted to educators using Adafruit and Arduino products for teaching.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
dier02
 
Posts: 7
Joined: Fri Jul 24, 2015 8:21 am

Adding barometric pressure to other sensors

Post by dier02 »

I currently read data from a DHT22 and an LDR but want to add barometric pressure to the mix.

Code: Select all

// Libraries
#include <Adafruit_CC3000.h>
#include <SPI.h>
#include "DHT.h"
#include <avr/wdt.h>
 
// Define CC3000 chip pins
#define ADAFRUIT_CC3000_IRQ   3
#define ADAFRUIT_CC3000_VBAT  5
#define ADAFRUIT_CC3000_CS    10
 
// DHT sensor
#define DHTPIN 7
#define DHTTYPE DHT22
 
// Create CC3000 instances
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, 
ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT, SPI_CLOCK_DIV2);
                                         
// DHT instance
DHT dht(DHTPIN, DHTTYPE);
 
// WLAN parameters
#define WLAN_SSID       "myNetwork"
#define WLAN_PASS       "routerPassword"
#define WLAN_SECURITY   WLAN_SEC_WPA2

How can I add the BMP to the other code so I can see it all on Freeboard.io?

Code: Select all

 
// Dweet parameters
#define thing_name  "HSS_CodeKids"
 
// Variables to be sent
int temperature;
int humidity;
int light;
const int splSensor = A3;
 
uint32_t ip;
 
void setup(void)
{
  // Initialize
  Serial.begin(115200);
  
  Serial.println(F("\nInitializing..."));
  if (!cc3000.begin())
  {
    Serial.println(F("Couldn't begin()! Check your wiring?"));
    while(1);
  }
 
  // Connect to WiFi network
  Serial.print(F("Connecting to WiFi network ..."));
  cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY);
  Serial.println(F("done!"));
  
  /* Wait for DHCP to complete */
  Serial.println(F("Request DHCP"));
  while (!cc3000.checkDHCP())
  {
    delay(100);
  }  
}
 
void loop(void)
{ 
  
  // Measure from DHT
  float t = dht.readTemperature();
  float h = dht.readHumidity();
  temperature = (int)t;
  humidity = (int)h;
 
  // Measure light level
  float sensor_reading = analogRead(A0);
  light = (int)(sensor_reading/1024*100);
  Serial.println(F("Measurements done"));
  
  //measure from sound sensor that reid installed
    // the SPL output is connected to analog pin 3
 Serial.println(analogRead(splSensor), DEC);
  delay(200);  // delay to avoid overloading the serial port buffer

  
  
  // Start watchdog 
  wdt_enable(WDTO_8S); 
  
  // Get IP
  uint32_t ip = 0;
  Serial.print(F("www.dweet.io -> "));
  while  (ip  ==  0)  {
    if  (!  cc3000.getHostByName("www.dweet.io", &ip))  {
      Serial.println(F("Couldn't resolve!"));
    }
    delay(500);
  }  
  cc3000.printIPdotsRev(ip);
  Serial.println(F(""));
  
  // Reset watchdog
  wdt_reset();
  
  // Check connection to WiFi
  Serial.print(F("Checking WiFi connection ..."));
  if(!cc3000.checkConnected()){while(1){}}
  Serial.println(F("done."));
  wdt_reset();
  
  // Send request
  Adafruit_CC3000_Client client = cc3000.connectTCP(ip, 80);
  if (client.connected()) {
    Serial.print(F("Sending request... "));
    
    client.fastrprint(F("GET /dweet/for/"));
    client.print(thing_name);
    client.fastrprint(F("?temperature="));
    client.print(temperature);
    client.fastrprint(F("&humidity="));
    client.print(humidity);
    client.fastrprint(F("&light="));
    client.print(light);
    client.fastrprintln(F(" HTTP/1.1"));
    
    client.fastrprintln(F("Host: dweet.io"));
    client.fastrprintln(F("Connection: close"));
    client.fastrprintln(F(""));
    
    Serial.println(F("done."));
  } else {
    Serial.println(F("Connection failed"));    
    return;
  }
  
  // Reset watchdog
  wdt_reset();
  
  Serial.println(F("Reading answer..."));
  while (client.connected()) {
    while (client.available()) {
      char c = client.read();
      Serial.print(c);
    }
  }
  Serial.println(F(""));
  
  // Reset watchdog
  wdt_reset();
   
  // Close connection and disconnect
  client.close();
  Serial.println(F("Closing connection"));
  Serial.println(F(""));
  
  // Reset watchdog & disable
  wdt_reset();
  wdt_disable();
 
  // Wait 60 seconds until next update
  wait(60000);
    
}
 
// Wait for a given time using the watchdog
void wait(int total_delay) {
  
  int number_steps = (int)(total_delay/5000);
  wdt_enable(WDTO_8S);
  for (int i = 0; i < number_steps; i++){
    //Serial.println(F("Waiting for 5 seconds ..."));
    delay(5000);
    wdt_reset();
  }
  wdt_disable();
}

The code for the BMP180 is as follows:

Code: Select all

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085_U.h>
   
Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10085);
 
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);
  }
}
 
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 pressure in hPa */
    Serial.print("Pressure: "); Serial.print(event.pressure); Serial.println(" hPa");
  }
  else
  {
    Serial.println("Sensor error");
  }
  delay(250);
}
Last edited by adafruit_support_bill on Sat Aug 01, 2015 6:17 am, edited 2 times in total.
Reason: please use the </> button when submitting code. press </>, then paste your code between the [code] [/code] tags.

User avatar
adafruit_support_bill
 
Posts: 88097
Joined: Sat Feb 07, 2009 10:11 am

Re: Adding barometric pressure to other sensors

Post by adafruit_support_bill »

The BMP180 sketch is fairly simple, so it should be easy to merge:

Take the #include and "bmp" variable declaration from the BMP sketch and add that to the top of your DHT22 sketch.
Take the contents of the setup() function from BMP and add them into the setup() of your DHT22 sketch.
Then take the contents of the loop() from BMP and merge them into the loop() function of the DHT22 sketch.

In the last step, you may want to re-arrange things so that the output data is formatted the way you want to see it.

User avatar
dier02
 
Posts: 7
Joined: Fri Jul 24, 2015 8:21 am

Re: Adding barometric pressure to other sensors

Post by dier02 »

What about the includes for Wire.h and Adafruit_Sensor.h? Are these libraries needed? Is there going to be a problem with the differences in baud rate as one has 115200 and the (BMP) other has 9600?

User avatar
adafruit_support_bill
 
Posts: 88097
Joined: Sat Feb 07, 2009 10:11 am

Re: Adding barometric pressure to other sensors

Post by adafruit_support_bill »

What about the includes for Wire.h and Adafruit_Sensor.h? Are these libraries needed?
You need all the includes and variable declarations.
Is there going to be a problem with the differences in baud rate as one has 115200 and the (BMP) other has 9600?
No. Just choose one or the other and make sure that the setting on your Serial Monitor is the same.

Locked
Please be positive and constructive with your questions and comments.

Return to “For Educators”