 {
   // You can open the Arduino IDE Serial Monitor window to see what the code is doing
   Serial.begin(115200);  // Serial connection from ESP-01 via 3.3v console cable
+  // Power on the DHT22 sensor first.
+  pinMode(DHTPWR, OUTPUT);
+  digitalWrite(DHTPWR, HIGH);
   dht.begin();           // initialize temperature sensor
-
+  
   // Connect to WiFi network
   WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
   Serial.print("\n\r \n\rWorking to connect");
   // Wait for connection
   while (WiFi.status() != WL_CONNECTED) {
-    delay(500);
+    delay(250);
     Serial.print(".");
   }
   Serial.println("");
+  Wire.pins(12, 14);
+  if (!bmp.begin()) {
+    Serial.println("BMP init error!");
+  }
   Serial.println("DHT Weather Reading Client");
   Serial.print("Connected to ");
   Serial.println(WIFI_SSID);

// This is for the ESP8266 processor on ESP-01 
DHT dht(DHTPIN, DHTTYPE, 11); // 11 works fine for ESP8266

// Generally, you should use "unsigned long" for variables that hold time
unsigned long previousMillis = 0;        // will store last temp was read
const long interval = 2000;              // interval at which to read sensor

  client.stop();
}

bool isValidHumidity(float humidity) {
  return (!isnan(humidity) && humidity >= 0 && humidity <= 100);
}

bool isValidTemp(float temp) {
  return (!isnan(temp) && temp >= -100 && temp <= 212);
}

float pascalsToInchesHg(int64 pascals) {
  return pascals * 0.000295299830714;
}

float tempCtoTempF(float tempC) {
  return (tempC + 32) * 9 / 5;
}

void loop(void)
{
  float temp, humidity;
  for (int i = 0; i < 4; i++) {
    temp = dht.readTemperature(true);
    if (!isValidTemp(temp)) {
      delay(250);
      continue;
    }
    break;
  }
  for (int i = 0; i < 4; i++) {
    humidity = dht.readHumidity();
    if (!isValidHumidity(humidity)) {
      delay(250);
      continue;
    }
    break;
  }
  
  String out;
  if (isValidHumidity(humidity)) {
    out += makeSensorVar("humidity") + " " + String(humidity) + "\n";
  }
  if (isValidTemp(temp)) {
    out +=  makeSensorVar("tempF") + " " + String(temp) + "\n";
  }
  out += makeSensorVar("battery_millivolts") + " " + String(ESP.getVcc()) + "\n";
  out += makeSensorVar("free_heap") + " " + String(ESP.getFreeHeap()) + "\n";
  out += makeSensorVar("bmp_tempC") + " " + String(bmp.readTemperature()) + "\n";
  out += makeSensorVar("pressure") + " " + String(bmp.readPressure()) + "\n";
  out += makeSensorVar("altitude") + " " + String(bmp.readAltitude()) + "\n";
  Serial.println(out);
  sendMetrics(out);
  // Power off the sensor while the esp is in deep sleep.
  digitalWrite(DHTPWR, LOW);
  ESP.deepSleep(120000000, WAKE_RF_DEFAULT);
  // It can take a while for the ESP to actually go to sleep.
  // When it wakes up we start again in setup().
  delay(5000);
} 

