CanSat

HEHHE
mail@pastecode.io avatar
unknown
c_cpp
a year ago
2.2 kB
3
Indexable
#include "DFRobot_BMP280.h"
#include "Wire.h"
#include "SD.h"
#include "SPI.h"

const int chipSelect = 28; // 28 for mkr zero, 4 mkr wifi 1010

typedef DFRobot_BMP280_IIC    BMP;    // ******** use abbreviations instead of full names ********

BMP   bmp(&Wire, BMP::eSdoLow);

#define SEA_LEVEL_PRESSURE    1011.0f   // sea level pressure 

// show last sensor operate status
void printLastOperateStatus(BMP::eStatus_t eStatus)
{
  switch(eStatus) {
  case BMP::eStatusOK:    
    Serial.println("everything ok"); 
    break;
  case BMP::eStatusErr:   
    Serial.println("unknow error"); 
    break;
  case BMP::eStatusErrDeviceNotDetected:    
    Serial.println("device not detected"); 
    break;
  case BMP::eStatusErrParameter:    
    Serial.println("parameter error"); 
    break;
  default: 
    Serial.println("unknown status"); 
    break;
  }
}

void setup()
{
  Serial.begin(9600);
  bmp.reset();
  Serial.println("bmp read data test");
  while(bmp.begin() != BMP::eStatusOK) {
    Serial.println("bmp begin failed");
    printLastOperateStatus(bmp.lastOperateStatus);
    delay(2000);
  }
  Serial.println("bmp begin success");
  delay(100);

Serial.print("Initializing SD card...");
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1);
  }
  Serial.println("card initialized.");
}

void loop()
{
  float   temp = bmp.getTemperature();
  uint32_t    press = bmp.getPressure();
  float   alti = bmp.calAltitude(SEA_LEVEL_PRESSURE, press);

  Serial.println();
  Serial.println("======== start print ========");
  Serial.print("temperature (unit Celsius): "); Serial.println(temp);
  Serial.print("pressure (unit pa):         "); Serial.println(press);
  Serial.print("altitude (unit meter):      "); Serial.println(alti);
  Serial.println("========  end print  ========");

  File dataFile = SD.open("log.txt", FILE_WRITE);
  dataFile.print(temp);
  dataFile.print(",");
  dataFile.print(press);
  dataFile.print(",");
  dataFile.println(alti);
  dataFile.close();
    
 
  delay(250);
}