1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

AtomLiteで室温、湿度をLINEへ通知

Last updated at Posted at 2024-09-24

概要

 AtomLiteとM5Stack用温湿度気圧センサユニット(Ver.3)を使い、
 ボタンを押すと温度、湿度をLINEトークへ通知します。
image.png

image.png

環境など

 エディタ:VisualStudioCode(PlatformIO IDE)
 マイコン:ATOM Lite 
 温湿度センサ:M5Stack用温湿度気圧センサユニット Ver.3(ENV Ⅲ)
 フレームワーク:Aruduino

コード 

#include <Arduino.h>
#include <M5Unified.h>
#include <WiFiClientSecure.h>
#include "M5_ENV.h"

const char* ssid     = "";//環境に合わせて適宜設定
const char* password = "";//環境に合わせて適宜設定
const char* host     = "notify-api.line.me";
const char* token    = "";//発行したトークンを記入

// ENV3定義
SHT3X sht30;
QMP6988 qmp6988;
float tmp      = 0.0;
float hum      = 0.0;
float pres     = 0.0;

// ENV3センサー値読み取り
void readENV()
{
  pres = qmp6988.calcPressure();//Pa
  pres=pres/100;//hPa
  if (sht30.get() == 0) {
    tmp = sht30.cTemp;
    hum = sht30.humidity;
  } else {
    tmp = 0, hum = 0;
  }
}

boolean line_notify(String msg) {
  WiFiClientSecure client;
  client.setInsecure();
  if(!client.connect(host, 443)) {
    Serial.println("connect error!");
    return false;
  }
  String query = String("message=") + msg;
  String request = String("")
              + "POST /api/notify HTTP/1.1\r\n"
              + "Host: " + host + "\r\n"
              + "Authorization: Bearer " + token + "\r\n"
              + "Content-Length: " + String(query.length()) +  "\r\n"
              + "Content-Type: application/x-www-form-urlencoded\r\n\r\n"
              + query + "\r\n";
  client.print(request);
  client.flush();
  client.stop();
  return true;
}

void setup() {
  M5.begin();
  Serial.begin(115200);//USBシリアル

  // I2C設定
  Wire.begin(26,32,100000UL);

  // ENV3設定
  qmp6988.init();

  delay(5000);
  Serial.println("WiFi Start"+String(ssid));
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi Connected");
}

void loop() {
  M5.update();
  if(M5.BtnA.wasPressed()) {
    readENV();
    String msg = "\n温度:" + String(tmp,1) +"℃ 湿度:"+String(hum,0)+"%";
    Serial.println(msg);
    String msg_notify = "\n温度:" + String(tmp,1) +"%E2%84%83 湿度:"+String(hum,0)+"%25";//URLエンコード %E2%84%83⇒℃ %25⇒%
    line_notify(msg_notify);
    Serial.println("LINE-NOTIFY FINISH");
  }
}

plathomeio.ini設定

[env:m5stack-atom]
platform = espressif32
board = m5stack-atom
framework = arduino
monitor_speed = 115200
lib_deps =
m5stack/M5Unified@^0.1.14
m5stack/M5Unit-ENV@^0.0.7

参考サイト

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?