設計人:李鈺哲
將發行的token複製下來(待會要輸入在程式上面)
記得將LINE Notify加入到建立的群組裡面(不然會收不到訊息)
TridentTD_LineNotify
DHT sensor library
TridentTD_LineNotify
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <DHT.h>
#include <TridentTD_LineNotify.h>
#define DHTTYPE DHT22
#define DHTPIN 1
// 修改成上述寄到登入郵箱的 Token號碼
#define LINE_TOKEN "J8BH6Zd1Ix2X8uQpbktnAo1BRNYJFE283abyThOnlVy"
// 設定無線基地台SSID跟密碼
const char* ssid = "Justin's S22 Ultra";
const char* password = "88888888";
DHT dht(DHTPIN, DHTTYPE); // works fine for ESP8266
unsigned long previousMillis = 0; // will store last temp was read
const long interval = 2000; // interval at which to read sensor
// 用不到以下兩個變數
// const char* host = "notify-api.line.me";
// const int httpsPort = 443;
int chk;
float hum; //Stores humidity value
float temp_f; //Stores temperature value
float heatIndex;
float tempC;
void setup(void)
{
Serial.begin(9600); // 設定速率 感測器
dht.begin(); // 初始化
WiFi.mode(WIFI_STA);
// 連接無線基地台
WiFi.begin(ssid, password);
Serial.print("\n\r \n\rWorking to connect");
// 等待連線,並從 Console顯示 IP
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("DHT Weather Reading Server");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void loop(void)
{
// 量測間等待至少 2 秒
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= interval) {
// 將最後讀取感測值的時間紀錄下來
previousMillis = currentMillis;
// 讀取溫度大約 250 微秒!
hum = dht.readHumidity(); // 讀取濕度(百分比)
temp_f= dht.readTemperature(true); // 讀取溫度(華氏)
tempC =(( temp_f - 32)*5) / 9;//華氏轉攝氏
heatIndex = 1.07 * tempC + 0.2 * ( ((hum)/100) * 6.105 * exp((17.27 * tempC) / (237.7 + tempC) )) - 2.7 ;
Serial.println(hum);
Serial.println(temp_f);
Serial.println(heatIndex);
// 檢查兩個值是否為空值
if (isnan(hum) || isnan(temp_f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// 加入條件判斷,當體感溫度超過某個閾值時觸發額外的操作
if (heatIndex > 30.0) {
Serial.println("High heat index detected! Take necessary precautions.");
// 體感溫度超過 30 度,傳送 "會他媽熱死" 的通知
LINE.notify("會他媽熱死");
} else if (heatIndex > 25.0) {
Serial.println("High heat index detected! Today don't want to go out.");
// 體感溫度超過 25 度,傳送 "今天不想出門" 的通知
LINE.notify("今天不想出門");
}
}
String tempe="溫度:"+String((int)(temp_f-32)*5/9)+"℃";
String heatIdx = "體感溫度:" + String((int)heatIndex) + "℃";
String humid="濕度:"+String((int)hum)+"%";
// 顯示 Line版本
Serial.println(LINE.getVersion());
LINE.setToken(LINE_TOKEN);
// 先換行再顯示
LINE.notify("\n" + tempe + " ;" +"\n"+ heatIdx + " ;"+"\n"+ humid);
// 每十秒發送一次
delay(10000);
}
//DHT22
#include <DHT.h>; //import Libraries
#define DHTPIN 2 // what pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino
//Varibles
int chk;
float hum; //Stores humidity value
float temp; //Stores temperature value
//DHT22
void setup(){
Serial.begin(9600);
pinMode(ledPower,OUTPUT); //output pin is on D2(set up the function of pin D2)
//DHT22
dht.begin();
}
void loop(){
//DHT22
hum = dht.readHumidity();
temp= dht.readTemperature();
//DHT22
Serial.print(" Humidity: ");
Serial.print(hum);
Serial.print(" %, Temp: ");
Serial.print(temp);
Serial.println(" Celsius");
delay(3000);//delay
}
每十秒發送一次
接線的部分:
DHT22
VCC接到esp82665V上面
DAT接到esp8266TX上面
NC為空腳位
GND接到esp8266G上面
所需器材:
一條Micro USBA
ESP8266MOD(Wemos D1 mini)
DHT22溫溼度感測器
上面為體感溫度公式,由於此裝置皆為在室內使用,所以風速V皆設為0