top of page

NodeMCU BMP180

Updated: Dec 4, 2021



#include <SFE_BMP180.h>
#include <Wire.h>

#include <ESP8266WiFi.h> //The library we will use for the wifi connection
#include <PubSubClient.h> // The library we will use for the mqtt connection

//mqtt connection library settings
WiFiClient espClient;
PubSubClient client(espClient);
//


//settings for internet and mqtt connection
const char* ssid = ""; // wifi SSID
const char* password =  ""; // wifi password 
const char* mqttServer = ""; // mqtt server ip  to be sent by smartespiot by mail 
const int mqttPort = ; // mqtt port   to be sent by smartespiot by mail 
const char* mqttUser = ""; // mqtt username  to be sent by smartespiot by mail 
const char* mqttPassword = ""; // mqtt pass to be sent by smartespiot by mail 



//


SFE_BMP180 pressure;

double baseline; // baseline pressure

void setup()
{
  Serial.begin(9600);
  Wire.begin(14, 12); // d6-14 scl d5-12 sda
  Serial.println("REBOOT");

  // Initialize the sensor (it is important to get calibration values stored on the device).

  if (pressure.begin())
    Serial.println("BMP180 init success");
  else
  {
    // Oops, something went wrong, this is usually a connection problem,
    // see the comments at the top of this sketch for the proper connections.

    Serial.println("BMP180 init fail (disconnected?)\n\n");
    while (1); // Pause forever.
  }

  // Get the baseline pressure:

  baseline = getPressure();

  Serial.print("baseline pressure: ");
  Serial.print(baseline);
  Serial.println(" mb");

  WiFi.begin(ssid, password); // start the wifi connection process

  while (WiFi.status() != WL_CONNECTED) { //
    delay(500); //wait 500ms
    Serial.println("Connecting to wifi..."); // wait until the wifi process connects
  }
  Serial.println("Wifi connection ready :)"); // print the connection made information to the serial port

  client.setServer(mqttServer, mqttPort);//start connecting to the mqtt server from the specified port

  while (!client.connected()) { //wait until we connect to mqtt
    Serial.println("Connecting to MQTT server...");

    if (client.connect("SmartEspIOT", mqttUser, mqttPassword )) {//login to mqtt server with username and password with smartespIOT name

      Serial.println("MQTT connection established :) ");

    } else {

      Serial.print("Unable to connect... ");// If the connection cannot be established, let's give a warning
      Serial.print(client.state());             // print the problem description
      // We can understand the problem according to the information returned from the clients state.
      //-4 : MQTT_CONNECTION_TIMEOUT - the server did not respond in the ongoing time
      //-3 : MQTT_CONNECTION_LOST - network connection lost
      //-2 : MQTT_CONNECT_FAILED - network connection failed
      //-1 : MQTT_DISCONNECTED - disconnected
      //0 : MQTT_CONNECTED - connection established
      //1 : MQTT_CONNECT_BAD_PROTOCOL - The server does not support the requested MQTT version
      //2 : MQTT_CONNECT_BAD_CLIENT_ID - The server did not accept your information
      //3 : MQTT_CONNECT_UNAVAILABLE - The server did not accept the connectionetmed
      //4 : MQTT_CONNECT_BAD_CREDENTIALS - username password not accepted
      //5 : MQTT_CONNECT_UNAUTHORIZED - you are not authorized to connect


      delay(2000);   // wait two seconds and try connecting again

    }
  }

}

void loop()
{
  double a, P;

  // Get a new pressure reading:

  P = getPressure();

  // Show the relative altitude difference between
  // the new reading and the baseline reading:

  a = pressure.altitude(P, baseline);

  Serial.print("relative altitude: ");
  if (a >= 0.0) Serial.print(" "); // add a space for positive numbers
  Serial.print(a, 1);
  Serial.print(" meters, ");
  if (a >= 0.0) Serial.print(" "); // add a space for positive numbers
  Serial.print(a * 3.28084, 0);
  Serial.println(" feet");
//Publish message ///
  client.publish("devicename/meters", String(a).c_str());// send meters
  client.publish("devicename/feet", String(a * 3.28084).c_str());// send feed
  delay(2000); // wait two seconds and send the message again
  //
}


double getPressure()
{
  char status;
  double T, P, p0, a;

  // You must first get a temperature measurement to perform a pressure reading.

  // Start a temperature measurement:
  // If request is successful, the number of ms to wait is returned.
  // If request is unsuccessful, 0 is returned.

  status = pressure.startTemperature();
  if (status != 0)
  {
    // Wait for the measurement to complete:

    delay(status);

    // Retrieve the completed temperature measurement:
    // Note that the measurement is stored in the variable T.
    // Use '&T' to provide the address of T to the function.
    // Function returns 1 if successful, 0 if failure.

    status = pressure.getTemperature(T);
    if (status != 0)
    {
      // Start a pressure measurement:
      // The parameter is the oversampling setting, from 0 to 3 (highest res, longest wait).
      // If request is successful, the number of ms to wait is returned.
      // If request is unsuccessful, 0 is returned.

      status = pressure.startPressure(3);
      if (status != 0)
      {
        // Wait for the measurement to complete:
        delay(status);

        // Retrieve the completed pressure measurement:
        // Note that the measurement is stored in the variable P.
        // Use '&P' to provide the address of P.
        // Note also that the function requires the previous temperature measurement (T).
        // (If temperature is stable, you can do one temperature measurement for a number of pressure measurements.)
        // Function returns 1 if successful, 0 if failure.

        status = pressure.getPressure(P, T);
        if (status != 0)
        {
          return (P);
        }
        else Serial.println("error retrieving pressure measurement\n");
      }
      else Serial.println("error starting pressure measurement\n");
    }
    else Serial.println("error retrieving temperature measurement\n");
  }
  else Serial.println("error starting temperature measurement\n");
}




BMP180_smartespiot_web
.zip
Download ZIP • 2KB


31 views0 comments

Recent Posts

See All