Advertisement
At-M

WeMos D1 Mini BME280 (original: bme280 by tyler glenn)

May 27th, 2018
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.39 KB | None | 0 0
  1. /*
  2. BME280 I2C Test.ino
  3.  
  4. This code shows how to record data from the BME280 environmental sensor
  5. using I2C interface. This file is an example file, part of the Arduino
  6. BME280 library.
  7.  
  8. GNU General Public License
  9.  
  10. Written: Dec 30 2015.
  11. Comments modified by At-M: May 27 2018.
  12. Last Updated: Oct 07 2017.
  13.  
  14. Connecting the BME280 Sensor:
  15. Sensor              ->  Board
  16. -----------------------------
  17. Vin (Voltage In)    ->  3.3V
  18. Gnd (Ground)        ->  Gnd
  19. SDA (Serial Data)   ->  D2 WeMos D1 Mini
  20. SCK (Serial Clock)  ->  D1 WeMos D1 Mini
  21.  
  22. Getting the library:
  23. It might be included in the Arduino IDE Search, search for "BME280", Author: Tyler Glenn
  24. or visit https://www.github.com/finitespace/BME280 and install manually
  25.  
  26.  */
  27.  
  28. #include <BME280I2C.h>
  29. #include <Wire.h>
  30.  
  31. #define SERIAL_BAUD 115200
  32.  
  33. BME280I2C bme;    // Default : forced mode, standby time = 1000 ms
  34.                   // Oversampling = pressure ×1, temperature ×1, humidity ×1, filter off,
  35.  
  36. //////////////////////////////////////////////////////////////////
  37. void setup()
  38. {
  39.   Serial.begin(SERIAL_BAUD);
  40.  
  41.   while(!Serial) {} // Wait
  42.  
  43.   Wire.begin();
  44.  
  45.   while(!bme.begin())
  46.   {
  47.     Serial.println("Could not find BME280 sensor!");
  48.     delay(1000);
  49.   }
  50.  
  51.   // bme.chipID(); // Deprecated. See chipModel().
  52.   switch(bme.chipModel())
  53.   {
  54.      case BME280::ChipModel_BME280:
  55.        Serial.println("Found BME280 sensor! Success.");
  56.        break;
  57.      case BME280::ChipModel_BMP280:
  58.        Serial.println("Found BMP280 sensor! No Humidity available.");
  59.        break;
  60.      default:
  61.        Serial.println("Found UNKNOWN sensor! Error!");
  62.   }
  63. }
  64.  
  65. //////////////////////////////////////////////////////////////////
  66. void loop()
  67. {
  68.    printBME280Data(&Serial);
  69.    delay(500);
  70. }
  71.  
  72. //////////////////////////////////////////////////////////////////
  73. void printBME280Data
  74. (
  75.    Stream* client
  76. )
  77. {
  78.    float temp(NAN), hum(NAN), pres(NAN);
  79.  
  80.    BME280::TempUnit tempUnit(BME280::TempUnit_Celsius);
  81.    BME280::PresUnit presUnit(BME280::PresUnit_Pa);
  82.  
  83.    bme.read(pres, temp, hum, tempUnit, presUnit);
  84.  
  85.    client->print("Temp: ");
  86.    client->print(temp);
  87.    client->print("°"+ String(tempUnit == BME280::TempUnit_Celsius ? 'C' :'F'));
  88.    client->print("\t\tHumidity: ");
  89.    client->print(hum);
  90.    client->print("% RH");
  91.    client->print("\t\tPressure: ");
  92.    client->print(pres);
  93.    client->println(" Pa");
  94.  
  95.    delay(1000);
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement