2021年5月9日日曜日

BME280 温度ロガー…その6

DIY Weather Station
BME280 気圧センサーを接続してみました。温度ロガーの6回目です…暇があれば センサーを眺めているのです…(^^ゞ


2021年4月19日の記事→GY-BMP280

前回BMP280では全く動かなかった(センサーを認識しなかった)ので 新たに買い直したのです…BME280は2個1990円と高い授業料になりました…気圧センサーが動かなかった本当の原因は別の場所に有ったのです。
※気圧センサが動かなかった本当の原因とは…Arduinoボードだったかも知れません。
UNO R3はSCL SDAのピンが出ていますが、LCDシールドを載せるとSCL SDAが隠れて使えなくなるのです。アナログ入力の5番と4番がSCL SDAに割り当てられているのですが…何故か?気圧センサーが認識されないのでした。ボードを交換したら動くようになったのです。
※気圧センサーをつないでも認識されない原因はピンソケットのイモ半田でした…半田鏝を当ててしっかりと付け直しています。


もう一つの原因はライブラリーの使い方に慣れていないのでBME280とBMP280の区別がつかなかった事です。BME280のアドレスは0x77と0x76の2つが有って、デフォルトは0x76になっているのでアドレスを指定しないとエラーが出ます。


※I2Cアドレスを調べるにはArduino IDEに収録されているi2c_scannerを使って調べます…正常に認識していれば 0x76(赤枠)と表示されます。0x57と0x68はDS3231 RTCモジュールです。


相変わらずコピペするしか出来ないのですが…必要な部分をコピペして張り合わせていけば一つの形(スケッチ)になることが分かってきたのです。

/****************************************************
  2021/05/09
  BMP280 Logger
  BMx280Library使用
****************************************************/
// DHT センサーライブラリー
#include <DHT.h>
// RTCモジュール
#include <RTClib.h>
//温湿度センサーの定義
#define DHTPIN 2     //白リード線はD2番につなぐ
//#define DHTTYPE DHT11   // DHT 11 
#define DHTTYPE DHT22   // DHT 22に変更
// DHT センサーのイニシャライズ(初期化)
DHT dht(DHTPIN, DHTTYPE);

//aitendoのLCDシールド定義
#include <ShiftedLCD.h>
LiquidCrystal lcd(10);
int LCD_BL = 6;
//
#include <SPI.h> //SPI通信を行う

//
#include <Arduino.h>
#include <Wire.h>
#include <BMx280I2C.h>
#define I2C_ADDRESS 0x76
//create a BMx280I2C object using the I2C interface with I2C Address 0x76
BMx280I2C bmx280(I2C_ADDRESS);

// RTC Module
RTC_DS3231 rtc;


void setup() {
//DHT sensor
dht.begin();
 
 
 float t = dht.readTemperature();
 float h = dht.readHumidity();
  //LCDのクリア
  lcd.begin(16, 2);           /* LCD設定(16文字2行) */
  lcd.clear();                /* LCDのクリア */


  // シリアル通信
  Serial.begin(9600);

  //wait for serial connection to open (only necessary on some boards)
  while (!Serial);

  Wire.begin();

  //begin() checks the Interface, reads the sensor ID (to differentiate between BMP280 and BME280)
  //and reads compensation parameters.
  if (!bmx280.begin())
  {
    Serial.println("begin() failed. check your BMx280 Interface and I2C Address.");
    while (1);
  }

  if (bmx280.isBME280())
    Serial.println("BMP280 Logger");

  //reset sensor to default parameters.
  bmx280.resetToDefaults();

  //by default sensing is disabled and must be enabled by setting a non-zero
  //oversampling setting.
  //set an oversampling setting for pressure and temperature measurements.
  bmx280.writeOversamplingPressure(BMx280MI::OSRS_P_x16);
  bmx280.writeOversamplingTemperature(BMx280MI::OSRS_T_x16);

  //if sensor is a BME280, set an oversampling setting for humidity measurements.
  if (bmx280.isBME280())
    bmx280.writeOversamplingHumidity(BMx280MI::OSRS_H_x16);
}

void loop() {
  // put your main code here, to run repeatedly:

  delay(2000);

  //start a measurement
  if (!bmx280.measure())
  {
    Serial.println("could not start measurement, is a measurement already running?");
    return;
  }

  //wait for the measurement to finish
  do
  {
    delay(100);
  } while (!bmx280.hasValue());

  Serial.print("Pressure: "); Serial.println(bmx280.getPressure() / 100);
  //Serial.print("Pressure (64 bit): "); Serial.println(bmx280.getPressure64());
  Serial.print("Temperature: "); Serial.println(bmx280.getTemperature());
Serial.print("RH: ");
  //Serial.print(p);
  Serial.println(" *%");
  
  
  //LCD表示
  lcd.setCursor(0, 0);
  lcd.print("Tem:" + String(bmx280.getTemperature()) + "c");
  lcd.setCursor(0, 1);
  lcd.print("Prs:" + String(bmx280.getPressure() / 100) + "hP   ");
  
  //----
  //important: measurement data is read from the sensor in function hasValue() only.
  //make sure to call get*() functions only after hasValue() has returned true.
  if (bmx280.isBME280())
  {
    Serial.print("Humidity: ");
    Serial.println(bmx280.getHumidity());

  }
}