Pc-Trace

<< 2024年5月 >>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31






Pc-Trace
PC-Trace

< next arduinoの記事 prev >

BM180の気象データを表示

LEONARDをメインボード、SSD1306をディスプレイとして、気圧、気温データを表示。

出力データは、USBで出力するシリアルデータ以外、以下の8項目をOLEDに表示
気圧表示前に、測定位置の高度を設定する必要があるので、以下の部分を修正する。

#define ALTITUDE 26.0 // 測定位置の高度設定 (メートル)

1.. 気温 摂氏(deg C)、華氏((9.0/5.0)*T+32.0,2 deg C)
2.. 絶対圧力(hPa)、水銀柱インチ(inHg)圧力✕0.0295333727で換算
3.. 相対圧力(hPa:海面圧力)、水銀柱インチ(inHg)圧力✕0.0295333727で換算
4.. 高度: 測定高度(メートルと、フィート表示)

撮影画像は、絶対圧力(ABS、hPa)を表示中



ディスプレイの解像度は、ワンコインで隠れてしまう、128✕32。
表示はそれなりに工夫をしたつもりでも、あくまで個人の主観。

シリアル出力では、mb 表示だが、OLED側は hPa ( mb = hPa )

表示切り替えは、物理的なスイッチではなく、時間による切り替え。

ソース内の、WSDLリンク:

自己責任で...

Libraryを必要に応じて揃える必要があり、今回はSSD1306対象のLibraryを含めた以下の5つ

SFE_BMP180.h
Wire.h
Adafruit_GFX.h
Adafruit_SSD1306.h
Adafruit_BMP085.h


Any Arduino pins labeled: SDA SCL
Uno, Redboard, Pro: A4 A5
Mega2560, Due: 20 21
Leonardo: 2 3




/* SFE_BMP180 library example sketch

This sketch shows how to use the SFE_BMP180 library to read the
Bosch BMP180 barometric pressure sensor.
https://www.sparkfun.com/products/11824

Like most pressure sensors, the BMP180 measures absolute pressure.
This is the actual ambient pressure seen by the device, which will
vary with both altitude and weather.

Before taking a pressure reading you must take a temparture reading.
This is done with startTemperature() and getTemperature().
The result is in degrees C.

Once you have a temperature reading, you can take a pressure reading.
This is done with startPressure() and getPressure().
The result is in millibar (mb) aka hectopascals (hPa).

If you'll be monitoring weather patterns, you will probably want to
remove the effects of altitude. This will produce readings that can
be compared to the published pressure readings from other locations.
To do this, use the sealevel() function. You will need to provide
the known altitude at which the pressure was measured.

If you want to measure altitude, you will need to know the pressure
at a baseline altitude. This can be average sealevel pressure, or
a previous pressure reading at your altitude, in which case
subsequent altitude readings will be + or - the initial baseline.
This is done with the altitude() function.

Hardware connections:

- (GND) to GND
+ (VDD) to 3.3V

(WARNING: do not connect + to 5V or the sensor will be damaged!)

You will also need to connect the I2C pins (SCL and SDA) to your
Arduino. The pins are different on different Arduinos:

Any Arduino pins labeled: SDA SCL
Uno, Redboard, Pro: A4 A5
Mega2560, Due: 20 21
Leonardo: 2 3

Leave the IO (VDDIO) pin unconnected. This pin is for connecting
the BMP180 to systems with lower logic levels such as 1.8V

Have fun! -Your friends at SparkFun.

The SFE_BMP180 library uses floating-point equations developed by the
Weather Station Data Logger project: http://wmrx00.sourceforge.net/

Our example code uses the "beerware" license. You can do anything
you like with this code. No really, anything. If you find it useful,
buy me a beer someday.

V10 Mike Grusin, SparkFun Electronics 10/24/2013
V1.1.2 Updates for Arduino 1.6.4 5/2015
*/

// Your sketch must #include this library, and the Wire library.
// (Wire is a standard library included with Arduino.):

#include
#include
#include
#include
#include

// ここでは「pressure」と呼ばれる SFE_BMP180 オブジェクトを作成する必要があります。

// OLEDディスプレイのピン設定
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

SFE_BMP180 pressure;

#define ALTITUDE 26.0 // 測定位置の高度設定 (メートル)

void setup()
{
Serial.begin(9600);
Serial.println("REBOOT");

// センサーを初期化 (デバイスに保存されている校正値を取得することが重要)。
// OLEDディスプレイの初期化 ----------------------------------------
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
delay(2000);
display.clearDisplay();

if (pressure.begin())
Serial.println("BMP180 init success");
else
{

Serial.println("BMP180 init fail\n\n");
while(1); // 一時停止
}
}

//======================= loop 開始 ============================

void loop()
{
char status;
double T,P,p0,a;

// ここでループして、一定 秒ごとに圧力測定値を取得
// 天気予報で使用される、海面補正された気圧が必要な場合は、
// 測定が行われる高度を知る必要があります。
// このスケッチでは ALTITUDE という定数を使用。

Serial.println();
Serial.print("provided altitude: ");
Serial.print(ALTITUDE,0);
Serial.print(" meters, ");
Serial.print(ALTITUDE*3.28084,0);
Serial.println(" feet");

// 気圧ではなく高度を測定したい場合は、代わりに次のものが必要です。
// 既知のベースライン圧力を提供します。 これはスケッチの最後に示されています。

// 圧力の読み取りを実行するには、まず温度を測定する必要があります。

// 温度測定を開始します:
// リクエストが成功すると、待機するミリ秒数が返されます。
// リクエストが失敗した場合は 0 が返されます。

status = pressure.startTemperature();
if (status != 0)
{
// 測定が完了するまで待ちます。
delay(status);

// 完了した温度測定値を取得します。
// 測定値は変数 T に保存されることに注意してください。
// 関数は成功した場合は 1 を返し、失敗した場合は 0 を返します。

status = pressure.getTemperature(T);
if (status != 0)
{
// 測定結果をプリント
Serial.print("temperature: ");
Serial.print(T,2);
Serial.print(" deg C, ");
Serial.print((9.0/5.0)*T+32.0,2);
Serial.println(" deg F");

// 圧力測定を開始します:
// パラメータは 0 〜 3 (最高解像度、最長待機) のオーバーサンプリング設定です。
// リクエストが成功すると、待機するミリ秒数が返されます。
// リクエストが失敗した場合は 0 が返されます。

status = pressure.startPressure(3);
if (status != 0)
{
// Wait for the measurement to complete:
delay(status);

// 完了した圧力測定値を取得します。
// 測定値は変数 P に保存されることに注意してください。
// この関数には以前の温度測定 (T) が必要であることにも注意してください。
// (温度が安定している場合は、複数の圧力測定に対して 1 回の温度測定を行うことができます。)
// 関数は成功した場合は 1 を返し、失敗した場合は 0 を返します。

status = pressure.getPressure(P,T);
if (status != 0)
{

// 測定結果をprint
Serial.print("absolute pressure: ");
Serial.print(P,2);
Serial.print(" mb, ");
Serial.print(P*0.0295333727,2);
Serial.println(" inHg");

// 圧力センサーは、高度によって変化する絶対圧力を返します。
// 高度の影響を取り除くには、海抜関数と現在の高度を使用します。
// この数値は天気予報でよく使用されます。
// パラメータ: P = 絶対気圧 (mb)、ALTITUDE = 現在の高度 (m)。
// 結果: p0 = 海面補償圧力 (MB)

p0 = pressure.sealevel(P,ALTITUDE); // 設定高度 meters (Boulder, CO)
Serial.print("relative (sea-level) pressure: ");
Serial.print(p0,2);
Serial.print(" mb, ");
Serial.print(p0*0.0295333727,2);
Serial.println(" inHg");

// 一方、気圧の測定値から高度を決定したい場合は、
// ベースライン気圧 (海抜またはその他) とともに高度関数を使用します。
// パラメータ: P = 絶対圧力 (mb)、p0 = ベースライン圧力 (mb)。
// 結果: a = 高度 (m)。

a = pressure.altitude(P,p0);
Serial.print("computed altitude: ");
Serial.print(a,0);
Serial.print(" meters, ");
Serial.print(a*3.28084,0);
Serial.println(" feet");

// ---SSD1306 表示開始 ----------------

display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);

// ------- 温度・華氏------------------

display.setCursor(0,4);
display.print("TMP:");
display.setCursor(32,0);
display.setTextSize(2);
display.print(T,2);
display.setTextSize(1);
display.setCursor(90,8);
display.println(" deg C");

display.setCursor(32,18);
display.setTextSize(2);
display.print((9.0/5.0)*T+32.0,2);
display.setCursor(90,24);
display.setTextSize(1);
display.println(" deg F");

display.display();
delay(4000);

// ----------絶対圧力・水銀柱--------------

display.clearDisplay();
display.setCursor(0,0);
display.print("ABS:");
display.setCursor(0,8);
display.print("hPa");
display.setCursor(32,0);
display.setTextSize(2);
display.print(P,2);

display.setCursor(32,18);
display.setTextSize(2);
display.print(P*0.0295333727,2);
display.setTextSize(1);
display.setCursor(90,24);
display.println(" inHg");

display.display();
delay(4000);

// ------相対 (海面) 圧力・水銀柱------------

display.clearDisplay();
display.setCursor(0,0);
display.print("SEA:");
display.setCursor(0,8);
display.print("hPa");
display.setCursor(32,0);
display.setTextSize(2);
display.print(p0,2);

display.setCursor(32,18);
display.setTextSize(2);
display.print(p0*0.0295333727,2);
display.setTextSize(1);
display.setCursor(90,24);
display.println(" inHg");

display.display();
delay(5000);

// ------計測高度(メートル・フィート)----------------

display.clearDisplay();
display.setCursor(0,4);
display.print("Computed: ");

int currentX1 = display.getCursorX(); // X位置を取得

display.setCursor(currentX1,0);
display.setTextSize(2);
display.print(a,0);

display.setTextSize(1);

int currentX2 = display.getCursorX(); // X位置を取得

display.setCursor(currentX2, 8);
display.println(" m");

display.setCursor(currentX1, 18);
display.setTextSize(2);
display.print(a*3.28084,0);

int currentX3 = display.getCursorX(); //X位置を取得

display.setTextSize(1);
display.setCursor(currentX3,24);
display.println(" feet");

display.display();

//-------------------------------------------------------------

}
else Serial.println("error retrieving pressure measurement\n");
}
else Serial.println("error starting pressure measurement\n");
}
else Serial.println("error retrieving temperature measurement\n");
}
else Serial.println("error starting temperature measurement\n");

delay(3000); // Pause for 5 seconds.
}
添付ファイル 添付ファイル


< next prev >