เขียนโปรแกรม Arduino เพื่อวัดอุณหภูมิและความชื้น ให้เซนเซอร์เตือนแจ้งผ่าน Line
เขียนโปรแกรม Arduino เพื่อวัดอุณหภูมิและความชื้น ให้เซนเซอร์เตือนแจ้งผ่าน Line
อุปกรณ์ Arduino เพียบ https://bit.ly/3SJuQNM
สำหรับการเขียนโปรแกรม Arduino เพื่อให้เซนเซอร์เตือนแจ้งผ่าน Line คุณต้องใช้ไลบรารี WiFiClient
และ HTTPClient
เพื่อเชื่อมต่อกับ Line Notify API และส่งข้อมูลการแจ้งเตือนไปที่ Line ของคุณได้ นอกจากนี้คุณต้องมี Access Token จาก Line Notify เพื่อทำการยืนยันตัวตนในการส่งข้อความ นอกจากนี้ยังต้องใช้เซนเซอร์ที่ต้องการเพื่อตรวจจับข้อมูล
นี้คือตัวอย่างโค้ดที่ใช้ Arduino และเซนเซอร์เรียบร้อยแล้ว:
โปรแกรมนี้ใช้ BME280 Sensor เพื่อวัดอุณหภูมิและความชื้น หากค่าที่วัดเกินค่าที่กำหนด (temperature > 30.0 หรือ humidity > 80.0) จะทำการส่ง Line Notify แจ้งเตือนผ่าน API ของ Line Notify
โปรดแทนค่า Your_WiFi_SSID
, Your_WiFi_Password
, และ Your_Line_Notify_Access_Token
ด้วยข้อมูลที่คุณใช้งานจริง ๆ ของคุณ
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <WiFi.h>
#include <HTTPClient.h>
#define WIFI_SSID "Your_WiFi_SSID"
#define WIFI_PASSWORD "Your_WiFi_Password"
#define LINE_TOKEN "Your_Line_Notify_Access_Token"
// BME280 Sensor
Adafruit_BME280 bme;
void connectWiFi() {
Serial.println("Connecting to WiFi...");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void setup() {
Serial.begin(115200);
connectWiFi();
bme.begin();
}
void loop() {
delay(2000);
float temperature = bme.readTemperature();
float humidity = bme.readHumidity();
// ตรวจสอบเงื่อนไขของเซนเซอร์
if (temperature > 30.0) {
sendLineNotify("Temperature is too high!");
}
if (humidity > 80.0) {
sendLineNotify("Humidity is too high!");
}
}
void sendLineNotify(String message) {
Serial.println("Sending Line Notify...");
HTTPClient http;
http.begin("https://notify-api.line.me/api/notify");
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
http.addHeader("Authorization", "Bearer " + String(LINE_TOKEN));
String payload = "message=" + message;
int httpResponseCode = http.POST(payload);
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println(response);
} else {
Serial.print("Error on Line Notify request. HTTP Response code: ");
Serial.println(httpResponseCode);
}
http.end();
}
อุปกรณ์ Arduino เพียบ https://bit.ly/3SJuQNM
- ดูระบบ PHP ทั้งหมด
- อ่านบทความทั้งหมด
- ติดต่อเรา