1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【ESP32入門】CO2モニターを作る #3 Gmail通知編

1
Posted at

はじめに

前回(#2 CO2センサー編)でCO2濃度を取得できるようになりました。

今回はCO2濃度が高くなったときに Gmailで通知 する機能を追加します。


なぜGmail通知をつけるのか

CO2濃度が基準値を超えたときに「換気するタイミング」が一目でわかるようにしたかったからです。

数値を見続けるのは大変ですが、メールが届けば換気のサインだとすぐにわかります。今回は1000ppmを超えたら通知するようにしました。


使用するライブラリ

ライブラリマネージャから以下をインストールします。

  • ESP Mail Client by Mobizt

Gmailアプリパスワードの取得

通常のGmailパスワードでは送信できません。アプリパスワードという専用のパスワードが必要です。

以下の手順で取得します。

  1. Googleアカウントの2段階認証を有効にする
  2. https://myaccount.google.com/apppasswords にアクセス
  3. アプリ名を入力してパスワードを発行
  4. 表示された16桁のパスワードをメモする

このアプリパスワードをコードで使います。


secrets.h でパスワードを管理する

パスワードをコードに直接書くと危険なので、別ファイル secrets.h に分けます。

#define SECRET_SSID "your_wifi_name"
#define SECRET_PASSWORD "your_wifi_password"
#define SECRET_AUTHOR_EMAIL "your_gmail@gmail.com"
#define SECRET_AUTHOR_PASSWORD "your_app_password"
#define SECRET_RECIPIENT_EMAIL "recipient@gmail.com"

コード

#include <ESP_Mail_Client.h>
#include <HardwareSerial.h>
#include <MHZ19.h>
#include <WiFi.h>
#include "secrets.h"

MHZ19 myMHZ19;
HardwareSerial mySerial(2);
bool alertSent = false;

const char* ssid = SECRET_SSID;
const char* password = SECRET_PASSWORD;

#define SMTP_HOST "smtp.gmail.com"
#define SMTP_PORT 465
#define AUTHOR_EMAIL SECRET_AUTHOR_EMAIL
#define AUTHOR_PASSWORD SECRET_AUTHOR_PASSWORD
#define RECIPIENT_EMAIL SECRET_RECIPIENT_EMAIL

SMTPSession smtp;

void sendAlert(int co2Value) {
  ESP_Mail_Session session;
  session.server.host_name = SMTP_HOST;
  session.server.port = SMTP_PORT;
  session.login.email = AUTHOR_EMAIL;
  session.login.password = AUTHOR_PASSWORD;
  smtp.connect(&session);

  SMTP_Message message;
  message.sender.name = "CO2センサー";
  message.sender.email = AUTHOR_EMAIL;
  message.subject = "CO2アラート";
  message.addRecipient("", RECIPIENT_EMAIL);

  String textMsg = "CO2濃度が高くなっています:" + String(co2Value) + "ppm";
  message.text.content = textMsg.c_str();

  MailClient.sendMail(&smtp, &message);
}

void setup() {
  Serial.begin(115200);
  mySerial.begin(9600, SERIAL_8N1, 13, 14);
  myMHZ19.begin(mySerial);
  myMHZ19.autoCalibration(false);

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("Wi-Fi接続成功!");
}

void loop() {
  int co2 = myMHZ19.getCO2();
  Serial.print("CO2: ");
  Serial.println(co2);

  if (co2 > 1000 && alertSent == false) {
    sendAlert(co2);
    alertSent = true;
  }
  if (co2 <= 1000) {
    alertSent = false;
  }
  delay(5000);
}

コードのポイント

① 毎回接続し直す

smtp.connect(&session);

SMTPのセッションは一定時間で切れてしまうため、メールを送るたびに接続し直しています。これに気づくまで時間がかかりました。

② フラグで連続送信を防ぐ

if (co2 > 1000 && alertSent == false) {
  sendAlert(co2);
  alertSent = true;
}

alertSent というフラグを使い、一度送ったら1000ppm以下に下がるまで再送しないようにしています。これがないと5秒ごとにメールが大量に届いてしまいます。


動かしてみた

息を吹きかけてCO2を上げると、無事Gmailに通知が届きました。

件名:CO2アラート
本文:CO2濃度が高くなっています:1203ppm

換気のタイミングがメールでわかるようになりました。


まとめ

  • ESP Mail Clientでメール送信できた
  • Gmailはアプリパスワードが必要
  • secrets.hでパスワードを管理する
  • フラグで連続送信を防ぐ

次回はLCDにCO2濃度とレベルを表示します!


#4 LCD表示編へ続く

1
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?