#include <M5StickC.h>
#include "DHT12.h"
#include <Wire.h>
#include "Adafruit_Sensor.h"
#include <Adafruit_BMP280.h>
DHT12 dht12;
Adafruit_BMP280 bme;
#define refreshRate 5000
long timer = 0;
//WBGT計算テーブル
const int WBGT[20][17] = {{15,15,16,16,17,17,18,19,19,20,20,21,21,22,23,23,24},
{15,16,17,17,18,18,19,19,20,21,21,22,22,23,24,24,25},
{16,17,17,18,19,19,20,20,21,22,22,23,23,24,25,25,26},
{17,18,18,19,19,20,21,21,22,22,23,24,24,25,26,26,27},
{18,18,19,20,20,21,22,22,23,23,24,25,25,26,27,27,28},
{18,19,20,20,21,22,22,23,24,24,25,26,26,27,28,28,29},
{19,20,21,21,22,23,23,24,25,25,26,27,27,28,29,29,30},
{20,21,21,22,23,23,24,25,25,26,27,28,28,29,30,30,31},
{21,21,22,23,24,24,25,26,26,27,28,29,29,30,31,31,32},
{21,22,23,24,24,25,26,27,27,28,29,29,30,31,32,32,33},
{22,23,24,24,25,26,27,27,28,29,30,30,31,32,33,33,34},
{23,24,25,25,26,27,28,28,29,30,31,31,32,33,34,34,35},
{24,25,25,26,27,28,28,29,30,31,32,32,33,34,35,35,36},
{25,25,26,27,28,29,29,30,31,32,33,33,34,35,36,37,37},
{25,26,27,28,29,29,30,31,32,33,33,34,35,36,37,38,38},
{26,27,28,29,29,30,31,32,33,34,34,35,36,37,38,39,39},
{27,28,29,29,30,31,32,33,34,35,35,36,37,38,39,40,41},
{28,28,29,30,31,32,33,34,35,35,36,37,38,39,40,41,42},
{28,29,30,31,32,33,34,35,35,36,37,38,39,40,41,42,43},
{29,30,31,32,33,34,35,35,36,37,38,39,40,41,42,43,44}};
void setup() {
M5.begin();
Wire.begin(0,26);
M5.Lcd.setRotation(3);
M5.Lcd.setTextColor(BLACK);
pinMode(M5_BUTTON_HOME, INPUT);
if (!bme.begin(0x76)){
Serial.println("Could not find a valid BMP280 sensor, check wiring!");
while (1);
}
Serial.print("\n\rCalibrate done..");
M5.Lcd.fillRect( 0, 0, 32, 80, CYAN);
M5.Lcd.fillRect( 32, 0, 32, 80, GREEN);
M5.Lcd.fillRect( 64, 0, 32, 80, YELLOW);
M5.Lcd.fillRect( 96, 0, 32, 80, ORANGE);
M5.Lcd.fillRect(128, 0, 33, 80, RED);
timer = millis();
}
void loop() {
//表示モード 0:WBGT 1:不快指数(DI)
static int ViewMode = 0;
//温度データ取得
float tmp = dht12.readTemperature();
//温度データを小数点以下切り上げ
int tmp_i = (tmp-0.1) + 1;
//温度インデックス
int index_tmp = tmp_i - 21;
//湿度データ取得
float hum = dht12.readHumidity();
//湿度データを整数型に
int hum_i = hum;
//湿度インデックス
int index_hum = hum / 5 - 3;
//WBGT表示値
int WBGT_V = WBGT[index_tmp][index_hum];
//不快指数
int DI = 0.81*tmp+0.01*hum*(0.99*tmp-14.3)+46.3;
//M5ボタンでモード切替
if(digitalRead(M5_BUTTON_HOME) == LOW){
ViewMode++;
if(ViewMode==2) ViewMode=0;
switch(ViewMode){
case 0:
//WBGTモード
M5.Lcd.fillRect( 0, 0, 32, 80, CYAN);
M5.Lcd.fillRect( 32, 0, 32, 80, GREEN);
M5.Lcd.fillRect( 64, 0, 32, 80, YELLOW);
M5.Lcd.fillRect( 96, 0, 32, 80, ORANGE);
M5.Lcd.fillRect(128, 0, 32, 80, RED);
break;
case 1:
//不快指数モード
M5.Lcd.fillRect( 0, 0, 20,80,CYAN);
M5.Lcd.fillRect( 20, 0, 20,80,GREEN);
M5.Lcd.fillRect( 40, 0, 20,80,GREENYELLOW);
M5.Lcd.fillRect( 60, 0, 20,80,YELLOW);
M5.Lcd.fillRect( 80, 0, 20,80,ORANGE);
M5.Lcd.fillRect(100, 0, 20,80,RED);
M5.Lcd.fillRect(120, 0, 20,80,PURPLE);
M5.Lcd.fillRect(140, 0, 20,80,MAROON);
break;
}
while(digitalRead(M5_BUTTON_HOME)==LOW){}
//モード切替後、すぐに表示させる
timer -= 5000;
}
//画面描画
if(millis()>timer+refreshRate){
timer = millis();
switch(ViewMode){
case 0:
//WBGTモード
if(WBGT_V<=20){
M5.Lcd.fillRect(0,0,160,70,CYAN);
}else if(WBGT_V>20 && WBGT_V<=24){
M5.Lcd.fillRect(0,0,160,70,GREEN);
}else if(WBGT_V>24 && WBGT_V<=27){
M5.Lcd.fillRect(0,0,160,70,YELLOW);
}else if(WBGT_V>27 && WBGT_V<=30){
M5.Lcd.fillRect(0,0,160,70,ORANGE);
}else{
M5.Lcd.fillRect(0,0,160,70,RED);
}
M5.Lcd.setCursor(5, 50 ,2);
M5.Lcd.printf("WBGT-------------------");
M5.Lcd.setCursor(95, 15 ,6);
M5.Lcd.printf("%d",WBGT_V);
break;
case 1:
//不快指数モード
if(DI<=54){
M5.Lcd.fillRect(0,0,160,70,CYAN);
}else if(DI<=60){
M5.Lcd.fillRect(0,0,160,70,GREEN);
}else if(DI<=65){
M5.Lcd.fillRect(0,0,160,70,GREENYELLOW);
}else if(DI<=70){
M5.Lcd.fillRect(0,0,160,70,YELLOW);
}else if(DI<=75){
M5.Lcd.fillRect(0,0,160,70,ORANGE);
}else if(DI<=80){
M5.Lcd.fillRect(0,0,160,70,RED);
}else if(DI<=85){
M5.Lcd.fillRect(0,0,160,70,PURPLE);
}else{
M5.Lcd.fillRect(0,0,160,70,MAROON);
}
M5.Lcd.setCursor(5, 50 ,2);
M5.Lcd.printf("DI-----------------------");
M5.Lcd.setCursor(95, 15 ,6);
M5.Lcd.printf("%d",DI);
break;
}
M5.Lcd.setCursor(5, 10 ,2);
M5.Lcd.printf("Temp: %2.1fC", tmp);
M5.Lcd.setCursor(5, 30 ,2);
M5.Lcd.printf("Humid: %2.0f%%", hum);
}
}
コメント