BPS_BMP280.ino 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. Description: Use BPS Unit to read atmospheric pressure, and display the data on the screen.
  3. */
  4. #include "bmp280.h"
  5. #include <M5Stack.h>
  6. #define P0 1013.25
  7. BMP280 bmp;
  8. void display_result(double t, double p, double a) {
  9. // M5.Lcd.
  10. M5.Lcd.drawString("Temperature: ", 20, 40, 4 );
  11. M5.Lcd.drawFloat(t, 2, 180, 40, 4);
  12. M5.Lcd.drawString("C", 280, 40, 4);
  13. M5.Lcd.drawString("Pressure: ", 20, 80, 4);
  14. M5.Lcd.drawFloat(p, 2, 180, 80, 4);
  15. M5.Lcd.drawString("Pa", 280, 80, 4);
  16. M5.Lcd.drawString("Altitude: ", 20, 120, 4);
  17. M5.Lcd.drawFloat(a, 2, 180, 120, 4);
  18. M5.Lcd.drawString("M", 280, 120, 4);
  19. }
  20. void setup()
  21. {
  22. M5.begin();
  23. if(!bmp.begin()){
  24. Serial.println("BMP init failed!");
  25. while(1);
  26. }
  27. else Serial.println("BMP init success!");
  28. bmp.setOversampling(4);
  29. M5.Lcd.drawString("BPS Example", 90, 0, 4);
  30. }
  31. void loop()
  32. {
  33. double T,P;
  34. char result = bmp.startMeasurment();
  35. if(result!=0){
  36. delay(result);
  37. result = bmp.getTemperatureAndPressure(T,P);
  38. if(result!=0)
  39. {
  40. double A = bmp.altitude(P,P0);
  41. Serial.print("T = \t");Serial.print(T,2); Serial.print(" degC\t");
  42. Serial.print("P = \t");Serial.print(P,2); Serial.print(" mBar\t");
  43. Serial.print("A = \t");Serial.print(A,2); Serial.println(" m");
  44. display_result(T, P, A);
  45. }
  46. else {
  47. Serial.println("Error.");
  48. }
  49. }
  50. else {
  51. Serial.println("Error.");
  52. }
  53. delay(2000);
  54. M5.Lcd.fillRect(160, 40, 100, 80, BLACK);
  55. }