LoginSignup
2
0

More than 1 year has passed since last update.

LINE通知機能付きCO2濃度検出器をM5stackで作ってみた(博多弁編)

Last updated at Posted at 2023-01-21

大学3年の研究室配属直後に,自由工作課題として制作。2020年度というのもあり,新型コロナウイルス関連で制作してみました。電子工作初心者でも,かんたんに制作できるデバイスを用いているのでおススメです。

準備するもの

はじめに概要紹介

動作概要紹介

スライド4.jpg

  • CO2濃度を随時センサから取得し,M5stack画面に表示させる
  • CO2濃度が複数ある基準値を超えた場合,Wi-Fiを通じてLINEに通知される
    ※初心者作成図なので,誤りのある解釈となっているかもしれません

完成図

スライド9.PNG

動作フローチャート

スライド5.jpg

  • CO2濃度基準値は厚生労働省や文科省の基準に準じています
  • 福岡出身のアイデンティティをちょこちょこ出していく

制作

回路図

スライド6.jpg
 上記の回路図を示します。M5stackは下画像のように接続子の対応が表記されているので,わかりやすいです。
IMG_6464.jpg

Wi-Fi環境接続

M5stackを使用する室内Wi-Fi環境に接続します。私はこちらの記事をもとに作成しました。『ESP32 Wifi経由でIFTTTへ接続しLINEへ通知
LINEへの通知は,多少のメールアドレス等の登録が必要となっています。添付記事を参考にしてみてください。

Wi-Fi環境設定
#include <MHZ19_uart.h>
#include <M5Stack.h>
#include <WiFi.h>
#include <WiFiClient.h>

const int rx_pin = 16; //Serial rx pin no
const int tx_pin = 17; //Serial tx pin no
MHZ19_uart mhz19;
//接続する室内Wi-FiのIDとパスワード
const char* ssid     = "NNN~NNN";
const char* password = "XXX~~XXX";

String makerEvent = "co2alert2"; // Maker Webhooks
String makerKey = "〇〇〇・・・〇〇〇"; // Maker Webhooks

const char* server = "maker.ifttt.com";  // Server URL
WiFiClient client;

bool checkWifiConnected() {
  // attempt to connect to Wifi network:
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    // wait 1 second for re-trying
    delay(1000);
  }

  Serial.print("Connected to ");
  Serial.println(ssid);
  return true;
}

void send(String value1, String value2, String value3) {
  while (!checkWifiConnected()) {
    Serial.print("Attempting to connect to WiFi");
    WiFi.begin(ssid, password);
  }

  Serial.println("\nStarting connection to server...");
  if (!client.connect(server, 80)) {
    Serial.println("Connection failed!");
  } else {
    Serial.println("Connected to server!");
    // Make a HTTP request:
    String url = "/trigger/" + makerEvent + "/with/key/" + makerKey;
    url += "?value1=" + value1 + "&value2=" + value2 + "&value3=" + value3;
    client.println("GET " + url + " HTTP/1.1");
    client.print("Host: ");
    client.println(server);
    client.println("Connection: close");
    client.println();
    Serial.print("Waiting for response "); //WiFiClientSecure uses a non blocking implementation

    int count = 0;
    while (!client.available()) {
      delay(50); //
      Serial.print(".");
    }
    // if there are incoming bytes available
    // from the server, read them and print them:
    while (client.available()) {
      char c = client.read();
      Serial.write(c);
    }

    // if the server's disconnected, stop the client:
    if (!client.connected()) {
      Serial.println();
      Serial.println("disconnecting from server.");
      client.stop();
    }
  }
}

CO2濃度取得&稼働!

void setup() {
  //Initialize serial and wait for port to open:
  //Serial.begin(115200);
  mhz19.begin(rx_pin, tx_pin);
  mhz19.setAutoCalibration(false);

  Serial.println("MH-Z19 is warming up now.");
  delay(3 * 1000); //
  
  M5.begin();
  M5.Lcd.setBrightness(100);
  M5.Lcd.fillScreen(WHITE);

  M5.Lcd.setCursor(0,0);
  M5.Lcd.setTextColor(BLACK,WHITE);
  M5.Lcd.setTextSize(4);
  M5.Lcd.print("CO2");

   WiFi.begin(ssid, password);
  while (!checkWifiConnected()) {
    WiFi.begin(ssid, password);
  }
}

int orange = 0;
int red = 0;
  
void loop() {

  int co2ppm = mhz19.getCO2PPM();
  
  M5.Lcd.setCursor(48,40);
  M5.Lcd.setTextSize(5);
  if(co2ppm > 10){
      M5.Lcd.fillRect(0,40,270,50,WHITE);
      M5.Lcd.fillRect(150,0,150,40,WHITE);
    if(co2ppm < 1000){
      M5.Lcd.setTextColor(GREEN,WHITE);//白地に緑色の文字
      M5.Lcd.print(co2ppm);//取得したCO2濃度値の表示
      M5.Lcd.print("ppm");//文字列"ppm"を表示
      orange = red = 0;
      M5.Lcd.drawJpgFile(SD,"/goodair.jpg",0,90,320,150,0,90);//SDカードに保存している"goodair.jpg"を表示
      }
    else if(co2ppm < 1500){
      M5.Lcd.setTextColor(GREENYELLOW,WHITE);
      M5.Lcd.print(co2ppm);
      M5.Lcd.print("ppm");
      orange = red = 0;
      M5.Lcd.drawJpgFile(SD,"/normalair.jpg",0,90,320,150,0,90);
      }
    else if(co2ppm < 3000){
      M5.Lcd.setTextColor(ORANGE,WHITE);
      M5.Lcd.print(co2ppm);
      M5.Lcd.print("ppm");
      M5.Lcd.drawJpgFile(SD,"/badair.jpg",0,90,320,150,0,90);
      red = 0;
      if(orange == 0){
        send("CO2Alert!!","Over1500!", "VENTILATE."); //任意の文字列3つ
        M5.Lcd.setCursor(150,0);
        M5.Lcd.setTextColor(ORANGE,WHITE);
        M5.Lcd.setTextSize(4);
        M5.Lcd.println("send");
        orange = 1;
        }
      }
    else{
      M5.Lcd.setTextColor(RED,WHITE);
      M5.Lcd.print(co2ppm);
      M5.Lcd.print("ppm");
      M5.Lcd.drawJpgFile(SD,"/worstair.jpg",0,90,320,150,0,90);
      if(red == 0){
        send("CO2Alert!!!!!","Over3000!!!!!", "VENTILATE!!!!!"); //任意の文字列3つ
        M5.Lcd.setCursor(150,0);
        M5.Lcd.setTextColor(ORANGE,WHITE);
        M5.Lcd.setTextSize(4);
        M5.Lcd.println("send");
        red = 1;
        }
      }
    }

    delay(1000);

}
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0