pawi552

BME280 test

Oct 24th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.09 KB | None | 0 0
  1. /***************************************************************************
  2.   This is a library for the BME280 humidity, temperature & pressure sensor
  3.  
  4.   Designed specifically to work with the Adafruit BME280 Breakout
  5.   ----> http://www.adafruit.com/products/2650
  6.  
  7.   These sensors use I2C or SPI to communicate, 2 or 4 pins are required
  8.   to interface. The device's I2C address is either 0x76 or 0x77.
  9.  
  10.   Adafruit invests time and resources providing this open source code,
  11.   please support Adafruit andopen-source hardware by purchasing products
  12.   from Adafruit!
  13.  
  14.   Written by Limor Fried & Kevin Townsend for Adafruit Industries.
  15.   BSD license, all text above must be included in any redistribution
  16.  ***************************************************************************/
  17.  
  18. #include <Wire.h>
  19. #include <ESP8266WiFi.h>
  20. #include <SPI.h>
  21. #include <Adafruit_Sensor.h>
  22. #include <Adafruit_BME280.h>
  23.  
  24. #define SEALEVELPRESSURE_HPA (1013.25)
  25.  
  26. Adafruit_BME280 bme; // I2C
  27.  
  28.  
  29. unsigned long delayTime;
  30.  
  31. void setup() {
  32.     Serial.begin(9600);
  33.     Serial.println(F("BME280 test"));
  34.     WiFi.mode(WIFI_STA);
  35.     Wire.begin(0, 2);
  36.     bool status;
  37.    
  38.     // default settings
  39.     status = bme.begin();
  40.     if (!status) {
  41.         Serial.println("Could not find a valid BME280 sensor, check wiring!");
  42.         while (1);
  43.     }
  44.    
  45.     Serial.println("-- Default Test --");
  46.     delayTime = 1000;
  47.  
  48.     Serial.println();
  49. }
  50.  
  51.  
  52. void loop() {
  53.     printValues();
  54.     delay(delayTime);
  55. }
  56.  
  57.  
  58. void printValues() {
  59.     Serial.print("Temperature = ");
  60.     Serial.print(bme.readTemperature());
  61.     Serial.println(" *C");
  62.  
  63.     if(isnan(bme.readTemperature())){
  64.       Serial.println("Failed to read data from BME280 sensor!");
  65.     }
  66.  
  67.     Serial.print("Pressure = ");
  68.  
  69.     Serial.print(bme.readPressure() / 100.0F);
  70.     Serial.println(" hPa");
  71.  
  72.     Serial.print("Approx. Altitude = ");
  73.     Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
  74.     Serial.println(" m");
  75.  
  76.     Serial.print("Humidity = ");
  77.     Serial.print(bme.readHumidity());
  78.     Serial.println(" %");
  79.  
  80.     Serial.println();
  81. }
Advertisement
Add Comment
Please, Sign In to add comment