M5StickC

電子工作メモ

連休最終日に、電子工作のめも 
久々に部品を繋いで、プログラミング
この夏の記録を残そう
次のチャレンジは。https://beebotte.com/で、可視化をめざす。
それとRaspberry
 Piを利用する
そうそう、Node-Readで可視化も…。




  • DHT11

  • M5StickC


//--------------------------------------------------------------------------------
//  M5StickC 温湿度センサー制御 テストプログラム
//     M5StickC temperature / humidity sensor control test program.
//  
//  ボタン制御:なし
//
//  表示制御
//    温度、湿度を26ピクセルASCIIフォントで表示する
//
//--------------------------------------------------------------------------------

// ------------------------------------------------------------
// ライブラリインクルード部 Library include section.
// ------------------------------------------------------------ #include  <M5StickC.h>                             // M5StickC 用ライブラリ #include  <DHT.h>                                  // DHTxx(温度センサー)制御用ライブラリ


// ------------------------------------------------------------
// 定数/変数 定義部 Constant / variable definition section.
// ------------------------------------------------------------
const uint8_t TEMP_MEAN = 26;                     // 温度測定結果の取得 Acquisition of temperature measurement results.
const int DHT_TYPE = DHT11;                       // センサーのタイプを定義
DHT dht(TEMP_MEAN, DHT_TYPE);                     // センサー初期化



float temp_val  = 0.0f;                           // 温度(℃)格納用
float himi_val  = 0.0f;                           // 湿度(%)格納用
float heatIndex  = 0.0f;                          // heatIndex
float discomfortIndex  = 0.0f;                    // discomfortIndex


// ------------------------------------------------------------
// 温度測定の関数 Temperature measurement function.
// Check_TEMP()
// ------------------------------------------------------------
void Check_TEMP() {
  delay(3000);

  bool isFahrenheit = true;
  float percentHumidity = dht.readHumidity();     // 湿度の取得
  float temperature = dht.readTemperature();      // 温度の取得

  // 温度と湿度どちらかの値が不正である場合はエラーをシリアルモニタへ出力
  if (isnan(percentHumidity) || isnan(temperature)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // 温度(℃)の変換
  temp_val = temperature;                         // 温度(℃)の格納
  himi_val = percentHumidity;                     // 湿度(%)の格納

  
  // 温度(セ氏)と湿度(%)から Heat Index の算出
  // Heat Index とはアメリカ海洋大気庁(NOAA)が出している熱中症に関連する指標のようです。
  // see https://en.wikipedia.org/wiki/Heat_index
  heatIndex = dht.computeHeatIndex(temperature, percentHumidity, false);
  // 不快指数
//  discomfortIndex = computeDiscomfortIndex(temperature, percentHumidity);
  discomfortIndex = 0.81 * temp_val + 0.01 * himi_val * (0.99 * temp_val - 14.3) + 46.3;  //不快指数の算出

}

// ------------------------------------------------------------
// ディスプレイ出力用の関数 Function for Display output.
// display_output()
// ------------------------------------------------------------
void display_output(){

  // プログラムタイトル表示部分
  M5.Lcd.setTextSize(1);                          // テキストサイズの指定
  M5.Lcd.fillScreen(BLACK);                       // 画面の塗りつぶし Screen fill.
  M5.Lcd.setTextColor(WHITE, BLACK);              // テキストカラーの設定
  M5.Lcd.setCursor(0, 0, 2);                      // カーソル位置とフォントの設定
  M5.Lcd.print("Temp/Himi Sensor Ctrl");          // ディスプレイに表示(プログラム名)

  M5.Lcd.setTextColor(GREEN, BLACK);              // テキストカラーの指定
  M5.Lcd.setCursor(0, 20, 4);                     // カーソル位置とフォントカラーの設定
  M5.Lcd.print(String(temp_val, 1));              // ディスプレイに表示(温度)
  
  M5.Lcd.setTextColor(WHITE, BLACK);              // テキストカラーの指定
  M5.Lcd.setCursor(55, 25, 2);                    // カーソル位置とフォントカラーの設定
  M5.Lcd.print("C");                              // ディスプレイに表示(温度)
  
  M5.Lcd.setTextColor(GREEN, BLACK);              // テキストカラーの指定
  M5.Lcd.setCursor(80, 20, 4);                    // カーソル位置とフォントカラーの設定
  M5.Lcd.print(String(himi_val, 1));              // ディスプレイに表示(湿度)
 
  M5.Lcd.setTextColor(WHITE, BLACK);              // テキストカラーの指定
  M5.Lcd.setCursor(140, 25, 2);                   // カーソル位置とフォントカラーの設定
  M5.Lcd.print("%");                              // ディスプレイに表示(湿度)

  M5.Lcd.setTextColor(WHITE, BLACK);              // テキストカラーの設定
  M5.Lcd.setCursor(0, 42, 2);                    // カーソル位置とフォントの設定
  M5.Lcd.print("HeatIndex/DiscomfortIndex");      // ディスプレイに表示(プログラム名)

  M5.Lcd.setTextColor(MAGENTA, BLACK);              // テキストカラーの設定
  M5.Lcd.setCursor(0, 57, 2);                    // カーソル位置とフォントカラーの設定
  M5.Lcd.print(String(heatIndex, 1));              // ディスプレイに表示(湿度)
  M5.Lcd.setCursor(80, 57, 2);                    // カーソル位置とフォントカラーの設定
  M5.Lcd.print(String(discomfortIndex, 1));              // ディスプレイに表示(湿度)
 
}

void setup() {
  // --------------------------------------------------
  // 初期化 Initialize
  // --------------------------------------------------
  // M5StickCの初期化と動作設定 Initialization and operation settings of M5StickC.
  M5.begin();                       // 開始
  M5.Lcd.setRotation(3);            // 画面の向きを変更(右横向き)Change screen orientation (left landscape orientation).
  M5.Axp.ScreenBreath(9);           // 液晶バックライト電圧設定 LCD backlight voltage setting.
  M5.Lcd.fillScreen(BLACK);         // 画面の塗りつぶし Screen fill.

  // シリアルコンソールの開始 Start serial console.
  Serial.begin(115200);
  delay(500);

  dht.begin();                                    //温湿度センサー開始

}

void loop() {

  delay(2000);                                    // 遅延(2秒)
  Check_TEMP();                                   // 温度・湿度の取得
  display_output();                               // ディスプレイ表示更新
  
}

この記事が気に入ったらサポートをしてみませんか?