DHT111 Temp Sensor and Bridge Lib for Arduino YUN

Hi Folks,

Looks like the DHT111 Temp Sensor and Bridge Lib are not working together. In my setup area if I initialize the DHT sensors and Bridge I get an issue. Here is the code I am using. Any advice would be greatly appreciated.

Can these be used together? I am using the sample code for the bridge example that comes with the Arduino and sample code from ada fruit for DHT111.

dht.begin();
    Bridge.begin();
  // Listen for incoming connection only from localhost
  // (no one from the external network could connect)
    server.listenOnLocalhost();
    server.begin();

Full Code

#include "DHT.h"
#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>
#define DHTPIN 1     // 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
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 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

// Initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);
// NOTE: For working with a faster chip, like an Arduino Due or Teensy, you
// might need to increase the threshold for cycle counts considered a 1 or 0.
// You can do this by passing a 3rd parameter for this threshold.  It's a bit
// of fiddling to find the right value, but in general the faster the CPU the
// higher the value.  The default for a 16mhz AVR is a value of 6.  For an
// Arduino Due that runs at 84mhz a value of 30 works.
// Example to initialize DHT sensor for Arduino Due:
//DHT dht(DHTPIN, DHTTYPE, 30);

const int numReadings = 10;

float readings[numReadings]={0};      // the readings from the analog input
int readIndex = 0;              // the index of the current reading
float total = 0;                  // the running total
float maverage = 0;                // the average
int d2=2;
int d4=4;
int d7=7; //PIR sensor pin, when HIGH motion is detected
int MotionTrigger=0;
int LED=13;

// Listen on default port 5555, the webserver on the Yún
// will forward there all the HTTP requests for us.
YunServer server;

void setup() {
    Serial.begin(9600); 
    delay(1000);
    digitalWrite(LED,HIGH);
    Serial.println("DHTxx test!");
    Serial.println("Warming Up.........");
    delay(1000);
    for (int thisReading = 0; thisReading < numReadings; thisReading++)
    readings[thisReading] = 0;
    pinMode(LED,OUTPUT);
    pinMode(d2,OUTPUT);
    pinMode(d4,OUTPUT);
    pinMode(d7,INPUT);
    digitalWrite(LED,LOW);
    digitalWrite(d2,LOW);
    digitalWrite(d4,LOW);
    dht.begin();
    Bridge.begin();
  // Listen for incoming connection only from localhost
  // (no one from the external network could connect)
    server.listenOnLocalhost();
    server.begin();
    delay(2000);
    Serial.println("Ready to go!");
    digitalWrite(LED,LOW);
}

void loop() {
  // Wait a few seconds between measurements.
  delay(2000);
  
  // subtract the last reading:
  total = total - readings[readIndex];
  // read from the sensor:
  readings[readIndex] = dht.readHumidity();
  // add the reading to the total:
  total = total + readings[readIndex];
  // advance to the next position in the array:
  readIndex = readIndex + 1;

  // if we're at the end of the array...
  if (readIndex >= numReadings)
    // ...wrap around to the beginning:
    readIndex = 0;

  // calculate the average:
  maverage = total / numReadings;
  // send it to the computer as ASCII digits
  Serial.println("Moving Average of Humidity:");
  Serial.println(maverage);
  delay(1);        // delay in between reads for stability
  
  // 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();
  // Read temperature as Celsius
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit
  float f = dht.readTemperature(true);
  
  //Check if Motion has occured
  MotionTrigger=digitalRead(d7);
  if (MotionTrigger> 0 ) {
    digitalWrite(d2,HIGH);
    Serial.println("Motion has occured!");
    Serial.println(MotionTrigger);
  } else if (h>1.05*maverage)
  {
  digitalWrite (d4,HIGH);
  Serial.println("Humidity is 5% above the average!");
  Serial.println(h);
  }
  else
  {
    digitalWrite (d2,LOW);
    digitalWrite (d4,LOW);
    Serial.println("All systems are OK.");
    Serial.println(MotionTrigger);
  }
  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // Compute heat index
  // Must send in temp in Fahrenheit!
  float hi = dht.computeHeatIndex(f, h);

  Serial.print("Humidity: "); 
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Moving Average of Humidity: "); 
  Serial.print(maverage);
  Serial.print(" %\t");
  Serial.print("Temperature: "); 
  Serial.print(t);
  Serial.print(" *C ");
  Serial.print(f);
  Serial.print(" *F\t");
  Serial.print("Heat index: ");
  Serial.print(hi);
  Serial.println(" *F");

  // Get clients coming from server
  YunClient client = server.accept();

  // There is a new client?
  if (client) {
    // Process request
    process(client);

    // Close connection and free resources.
    client.stop();
  }

  delay(50); // Poll every 50ms


}

void process(YunClient client) {
  // read the command
  String command = client.readStringUntil('/');

  // is "digital" command?
  if (command == "digital") {
    digitalCommand(client);
  }

  // is "analog" command?
  if (command == "analog") {
    analogCommand(client);
  }

  // is "mode" command?
  if (command == "mode") {
    modeCommand(client);
  }
}

void digitalCommand(YunClient client) {
  int pin, value;

  // Read pin number
  pin = client.parseInt();

  // If the next character is a '/' it means we have an URL
  // with a value like: "/digital/13/1"
  if (client.read() == '/') {
    value = client.parseInt();
    digitalWrite(pin, value);
  }
  else {
    value = digitalRead(pin);
  }

  // Send feedback to client
  client.print(F("Pin D"));
  client.print(pin);
  client.print(F(" set to "));
  client.println(value);

  // Update datastore key with the current pin value
  String key = "D";
  key += pin;
  Bridge.put(key, String(value));
}

void analogCommand(YunClient client) {
  int pin, value;

  // Read pin number
  pin = client.parseInt();

  // If the next character is a '/' it means we have an URL
  // with a value like: "/analog/5/120"
  if (client.read() == '/') {
    // Read value and execute command
    value = client.parseInt();
    analogWrite(pin, value);

    // Send feedback to client
    client.print(F("Pin D"));
    client.print(pin);
    client.print(F(" set to analog "));
    client.println(value);

    // Update datastore key with the current pin value
    String key = "D";
    key += pin;
    Bridge.put(key, String(value));
  }
  else {
    // Read analog pin
    value = analogRead(pin);

    // Send feedback to client
    client.print(F("Pin A"));
    client.print(pin);
    client.print(F(" reads analog "));
    client.println(value);

    // Update datastore key with the current pin value
    String key = "A";
    key += pin;
    Bridge.put(key, String(value));
  }
}

void modeCommand(YunClient client) {
  int pin;

  // Read pin number
  pin = client.parseInt();

  // If the next character is not a '/' we have a malformed URL
  if (client.read() != '/') {
    client.println(F("error"));
    return;
  }

  String mode = client.readStringUntil('\r');

  if (mode == "input") {
    pinMode(pin, INPUT);
    // Send feedback to client
    client.print(F("Pin D"));
    client.print(pin);
    client.print(F(" configured as INPUT!"));
    return;
  }

  if (mode == "output") {
    pinMode(pin, OUTPUT);
    // Send feedback to client
    client.print(F("Pin D"));
    client.print(pin);
    client.print(F(" configured as OUTPUT!"));
    return;
  }

  client.print(F("error: invalid mode "));
  client.print(mode);
}