Temperature, Humidity Sensor Tutorial for BMP180 Sensor

In this section we’ll walk through creating a Temperature Sensor using ESP32, ESP8266 and then view the temperature via Alexa, Google Home or SmartThings.

Prerequisites :

  1. ESP32, ESP8266 x 1.
  2. BMP180 x 1.
  3. Jumper Wires.

Quick introduction to BMP180 Temperature Sensor

The BMP180 sensor is a digital barometric pressure and temperature sensor from Bosch. The BMP180 sensor can measure barometric pressure within a range of 300 to 1100 hPa (9000 m to -500 m above sea level) with an accuracy of ±0.12 hPa. It can also measure temperature within a range of -40 to +85 °C with an accuracy of ±2 °C. The BMP180 sensor communicates with other devices using the I2C interface. It is a low-power sensor that consumes only 3 μA in standby mode.

Wiring for BMP180 with ESP8266

ESP8266 BMP180 Pin
3.3V Vin
GND GND
SCL 5 (D1)
SDA 4 (D2)

Please note that you must use I2C pins in ESP8266.

Sinric Pro ESP8266 I2C Pins

Wiring for BMP180 with ESP32

ESP32 BMP180 Pin
3.3V Vin
GND GND
SCL 22
SDA 21

Please note that you must use I2C pins in ESP32.

Sinric Pro ESP32 I2C Pins

Sinric Pro ESP32 I2C BMP180 Wiring

Let’s verify that temperature is wired correctly and working.

Step 1 : Install supporting libraries

  1. Install the BMP_085 library
  2. Install the Adafruit_Sensor library
  3. Install Sinric Pro Library

Sinric Pro Install the BMP180 library

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

Adafruit_BMP085 bmp; // Default I2C

void setup() {
  Serial.begin(9600);

  bool status = bmp.begin();  
  if (!status) {
    Serial.println("Could not find BMP180 sensor!");
    while (1);
  } 
}
 

void loop() { 
  Serial.print("Temperature = ");
  Serial.print(bmp.readTemperature());
  Serial.println(" *C");
  
  // Convert temperature to Fahrenheit
  /*Serial.print("Temperature = ");
  Serial.print(1.8 * bmp.readTemperature() + 32);
  Serial.println(" *F");*/
  
  Serial.print("Pressure = ");
  Serial.print(bmp.readPressure() / 100.0F);
  Serial.println(" hPa");

  Serial.print("Altitude (Approx) = ");
  Serial.print(bmp.readAltitude());
  Serial.println(" m");

  Serial.print("Pressure = ");
  Serial.print(bmp.readSealevelPressure());
  Serial.println(" Pa"); 

  Serial.println();
  delay(1000);
}

// Ref: https://github.com/adafruit/Adafruit-BMP085-Library/blob/master/examples/BMP085test/BMP085test.ino

View this code on GitHub Gist

Step 1 : Create a new device in Sinric Pro

Sinric Pro create device alexa

Sinric Pro temperature sensor device notifications

You can set the threshold here to receive a push notification via the Sinric Pro app when the temperature goes below or above a certain temperature. Use the Retrigger Time to set the delay between notifications.

Sinric Pro copy device id

Step 2 : Connect to Sinric Pro

You can generate the code using Zero Code feature or write it by your self. If you do not have programming experice, we recommend to use Zero Code feature in the Portal to generate the code, download and flash.

2.1 Complete Code

#ifdef ENABLE_DEBUG
  #define DEBUG_ESP_PORT Serial
  #define NODEBUG_WEBSOCKETS
  #define NDEBUG
#endif 

#include <Arduino.h>
#if defined(ESP8266)
  #include <ESP8266WiFi.h>
#elif defined(ESP32)
  #include <WiFi.h>
#endif

#include <Adafruit_BMP085.h>

#include "SinricPro.h"
#include "SinricProTemperaturesensor.h"

#define WIFI_SSID         ""   // Your WiFI SSID name 
#define WIFI_PASS         ""   // Your WiFi Password.
#define APP_KEY           ""   // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx" (Get it from Portal -> Secrets)
#define APP_SECRET        ""   // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx" (Get it from Portal -> Secrets)
#define TEMP_SENSOR_ID    ""   // Should look like "5dc1564130xxxxxxxxxxxxxx" (Get it from Portal -> Devices)
#define BAUD_RATE         115200              // Change baudrate to your need (used for serial monitor)
#define EVENT_WAIT_TIME   60000               // send event every 60 seconds

float temperature;                            // actual temperature
float humidity;                               // actual humidity
float lastTemperature;                        // last known temperature (for compare) 
 
Adafruit_BMP085 bmp;
 
void handleTemperaturesensor() {
  if (SinricPro.isConnected() == false) {
    Serial.printf("Not connected to Sinric Pro...!\r\n");
    return; 
  }

  static unsigned long last_millis;
  unsigned long        current_millis = millis();
  if (last_millis && current_millis - last_millis < EVENT_WAIT_TIME) return;
  last_millis = current_millis;
  
  float temperature = bmp.readTemperature();
  
  Serial.printf("Temperature: %2.1f Celsius\r\n", temperature);

  if (isnan(temperature)) { // reading failed... 
    Serial.printf("BME180 reading failed!\r\n");  // print error message
    return;                                    // try again next time
  }

  if (temperature == lastTemperature) {
    Serial.printf("Temperature did not changed. do nothing...!\r\n");
    return; 
  }

  SinricProTemperaturesensor &mySensor = SinricPro[TEMP_SENSOR_ID];  // get temperaturesensor device
  bool success = mySensor.sendTemperatureEvent(temperature, -1); // send event
  if (success) {  
    Serial.printf("Sent!\r\n");
  } else {
    Serial.printf("Something went wrong...could not send Event to server!\r\n"); // Enable ENABLE_DEBUG to see why
  }

  lastTemperature = temperature;  // save actual temperature for next compare   
}


// setup function for WiFi connection
void setupWiFi() {
  Serial.printf("\r\n[Wifi]: Connecting");

  #if defined(ESP8266)
    WiFi.setSleepMode(WIFI_NONE_SLEEP); 
    WiFi.setAutoReconnect(true);
  #elif defined(ESP32)
    WiFi.setSleep(false); 
    WiFi.setAutoReconnect(true);
  #endif

  WiFi.begin(WIFI_SSID, WIFI_PASS); 

  while (WiFi.status() != WL_CONNECTED) {
    Serial.printf(".");
    delay(250);
  }
  IPAddress localIP = WiFi.localIP();
  Serial.printf("connected!\r\n[WiFi]: IP-Address is %d.%d.%d.%d\r\n", localIP[0], localIP[1], localIP[2], localIP[3]);
}

bool onPowerState(const String &deviceId, bool &state) {
  return true; // request handled properly
}

// setup function for SinricPro
void setupSinricPro() {
  // add device to SinricPro
  SinricProTemperaturesensor &mySensor = SinricPro[TEMP_SENSOR_ID];
  mySensor.onPowerState(onPowerState);
  
  // setup SinricPro
  SinricPro.onConnected([](){ Serial.printf("Connected to SinricPro\r\n"); }); 
  SinricPro.onDisconnected([](){ Serial.printf("Disconnected from SinricPro\r\n"); });
  //SinricPro.restoreDeviceStates(true); // Uncomment to restore the last known state from the server.
  SinricPro.begin(APP_KEY, APP_SECRET);  
}

void setupBMP180() { 
  bool status = bmp.begin();  
  if (!status) {
    Serial.println("Could not find BMP180 sensor!");
    while (1);
  } 
}

// main setup function
void setup() {
  Serial.begin(BAUD_RATE); Serial.printf("\r\n\r\n");

  setupBMP180();
  setupWiFi();
  setupSinricPro();
}

void loop() {
  SinricPro.handle();
  handleTemperaturesensor();
}

View this code on GitHub Gist

Now you should be able to view the temperature via Sinric Pro App

Sinric Pro App Temperature Sensor

Please note that Google Home App shows the temperature sensor as a Thermostat due to Google Home limitations.

Troubleshooting

  1. This tutorial is for BMP180. Make sure it’s not BME280 module.

  2. Error: Could not find BMP180 sensor!. It’s likely you have not wired correctly. You can run the I2C scanner sketch to check whether the sensor is wired correctly.

  3. Please refer to our common Troubleshooting page for more details.

This document is open source. See a typo? Please create an issue