Moderator: igrr
kkozyrev wrote:Hello. I'm looking for working sketch with bmp085. Can somebody post a esp arduinoide sketch?
This is a very compact code for BMP085/BMP180 sensor
Temperature and pressure are displayed on the Serial Monitor every 2000ms:
// BMP180 basic sketch
#include <SFE_BMP180.h>
#include <Wire.h>
#define ALTITUDE 750.0 // Local altitude (meters)
SFE_BMP180 bmp180;
float temperature;
int pressure;
void setup(){
Wire.begin(0, 2); // for ESP-01, else pins 4(SDA) and 5(SCL)
Wire.setClock(400000);
Serial.begin(57600);
if (bmp180.begin()) {
delay(200);
Serial.println("BMP180 init success");
} else {
Serial.println("BMP180 init fail\n\n"); // check your setup with an I2C scanner
while(1) delay(1000);
}
}
void loop() {
getBMPdata(&temperature, &pressure);
Serial.print("\nBMP temperature: "); Serial.print(temperature,1); Serial.println(" C");
Serial.print("sea-level pressure: "); Serial.print(pressure); Serial.println(" mB");
delay(2000);
}
int getBMPdata(float* temp, int* pres) {
double T,P,p0;
char status;
status = bmp180.startTemperature();
if (status != 0) {
delay(status *2);
status = bmp180.getTemperature(T);
if (status != 0) {
status = bmp180.startPressure(1);
if (status != 0) {
delay(status *2);
status = bmp180.getPressure(P,T);
if (status != 0) {
p0 = bmp180.sealevel(P,ALTITUDE);
*temp = T;
*pres = int(p0+0.5);
return 1;
} else return 0;
} else return 0;
} else return 0;
} else return 0;
}
I use this code for my new Data Logger (T, P and battery level are sent to ThingSpeak)
** Full code available on request **
.
Hi Ivan, kudos for you hard work
Displays temperature and pressure relative to ground level every 2 seconds.
Although not entirely sure if I've got Altitude smoothing algorithm correct yet.
#include <Adafruit_BMP085.h>
#include <Wire.h>
Adafruit_BMP085 sensorData; //Create a sensor object
#define SDA_PIN 4
#define SCL_PIN 5
float tempC; //Temp reading in Celcius
float pressure = 0; //Pressure reading in Pascals
float altitude = 0; //Altitute in meters
float cal; //Calibration pressure constant
const int calReads = 50; //Number of pressure calibration reads to average
float calArray[calReads]; //Set up calibration pressure array
float calTotal = 0; //Running calibration pressure total
const int altReads = 5; //Number of altitude readings to average
float altArray[altReads]; //Setup altitude smoothing array
float altTotal = 0; //Running altitude smoothing total
int altIndex = 0;
void setup() {
Serial.begin(115200); //Start serial monitor
if (!sensorData.begin()) { //Check if there's a sensor attached
Serial.println("Could not find a valid BMP180 sensor, check wiring!");
while (1) {}
}
Wire.begin(SDA_PIN, SCL_PIN); //Map Ic2 bus pins
Wire.setClock(100000); //Set clock speed
sensorData.begin(); //Initialise sensor data
calibratePressure (); //Call calibrate pressure function
for (int altCurrent = 0; altCurrent < altReads; altCurrent++) { //Initialise altitude readings to 0
altArray[altCurrent] = 0;
}
}
void loop() {
Serial.println("");
tempC = sensorData.readTemperature(); //Read temparature
pressure = sensorData.readPressure(); //Read pressure
pressure = pressure/100; //Convert to Hectopascals
altitudeSmoothing(); //Call altitude Smoothing
Serial.print("The Temp is: "); //Print data
Serial.print(tempC);
Serial.println(" degrees C. ");
Serial.print("The Barometric Pressure is: ");
Serial.print(pressure);
Serial.println(" hPa. ");
Serial.print("The Altitude above ground level is: ");
Serial.print(altitude);
Serial.println(" Meters. ");
Serial.println("");
delay(2000);
}
void altitudeSmoothing () {
altTotal = altTotal - altArray[altIndex]; //Subtract last reading
altArray[altIndex] = sensorData.readAltitude(cal); //Read altitude
altTotal = altTotal + altArray[altIndex]; //Add reading to total
altIndex = altIndex + 1;
if (altIndex >= altReads) { //Check to see if at end of array
altIndex = 0;
}
altitude = altTotal / altReads; //Calculate average altitude
}
void calibratePressure (){
Serial.println("Sensor calibration, place at ground level, I'll wait 10 seconds while you do this!");
delay(10000);
Serial.println("");
Serial.println("Begin calibration, do not disturb until finished:");
Serial.println("");
for (int calCurrent = 0; calCurrent < calReads; calCurrent++) { //Initialise readings to 0
calArray[calCurrent] = 0;
}
for (int index = 0; index < calReads; index++) { //Check to see if at end of calArray
Serial.print(".");
calArray[index] = sensorData.readPressure(); //Read preasure
delay(150);
calTotal = calTotal + calArray[index]; //Add reading to total
}
cal = calTotal / calReads; //Calculate average calibration pressure
Serial.println("");
Serial.println("");
Serial.print("The calibration pressure is: ");
Serial.print(cal);
Serial.println(" Pascals ");
Serial.println("");
}
kas wrote:kkozyrev wrote:Hello. I'm looking for working sketch with bmp085. Can somebody post a esp arduinoide sketch?
This is a very compact code for BMP085/BMP180 sensor
Temperature and pressure are displayed on the Serial Monitor every 2000ms:Code: Select all// BMP180 basic sketch
#include <SFE_BMP180.h>
#include <Wire.h>
#define ALTITUDE 750.0 // Local altitude (meters)
SFE_BMP180 bmp180;
float temperature;
int pressure;
void setup(){
Wire.begin(0, 2); // for ESP-01, else pins 4(SDA) and 5(SCL)
Wire.setClock(400000);
Serial.begin(57600);
if (bmp180.begin()) {
delay(200);
Serial.println("BMP180 init success");
} else {
Serial.println("BMP180 init fail\n\n"); // check your setup with an I2C scanner
while(1) delay(1000);
}
}
void loop() {
getBMPdata(&temperature, &pressure);
Serial.print("\nBMP temperature: "); Serial.print(temperature,1); Serial.println(" C");
Serial.print("sea-level pressure: "); Serial.print(pressure); Serial.println(" mB");
delay(2000);
}
int getBMPdata(float* temp, int* pres) {
double T,P,p0;
char status;
status = bmp180.startTemperature();
if (status != 0) {
delay(status *2);
status = bmp180.getTemperature(T);
if (status != 0) {
status = bmp180.startPressure(1);
if (status != 0) {
delay(status *2);
status = bmp180.getPressure(P,T);
if (status != 0) {
p0 = bmp180.sealevel(P,ALTITUDE);
*temp = T;
*pres = int(p0+0.5);
return 1;
} else return 0;
} else return 0;
} else return 0;
} else return 0;
}
I use this code for my new Data Logger (T, P and battery level are sent to ThingSpeak)
** Full code available on request **
.
Hi Ivan, kudos for you hard work
I would love a copy of your code for the Data logger . I am unable to send PM's yet .
Bill