เเจ้งเตือนไฟไหม้ผ่านไลน์ ESP8266 + MQ--2
วงจร
//** เเจ้งเตือนไฟไหม้ผ่านไลน์ **//
#include <ESP8266WiFi.h>
#include <WiFiClientSecureAxTLS.h>
void Line_Notify(String message) ;
#define WIFI_SSID "wasanshow"//ชื่อไวไฟของเรา
#define WIFI_PASSWORD "wasanshow"//รหัสผ่านไวไฟของเรา
#define LINE_TOKEN "UB8qXzKCGMQsy9Lg06tCZJq4ZRnjpOjNBhOQPMYwUVC"//ไลน์ TOKEN ของเรา
int redLed = 4;
int greenLed = 5;
int buzzer = 16;
int smokeA0 = A0;
// Your threshold value
int sensorThres = 700;
void setup() {
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(buzzer, OUTPUT);
pinMode(smokeA0, INPUT);
Serial.begin(9600);
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("connecting");
while (WiFi.status() != WL_CONNECTED) {
Serial.println(".");
delay(500);
}
Serial.println();
Serial.print("connected: ");
Serial.println(WiFi.localIP());
}
void loop()
{
int analogSensor = analogRead(smokeA0);
Serial.print("Pin A0: ");
Serial.println(analogSensor);
// Checks if it has reached the threshold value
if (analogSensor > sensorThres)
{
digitalWrite(redLed, HIGH);
digitalWrite(greenLed, LOW);
tone(buzzer, 1000, 200);
Line_Notify("ไฟใหม้");
delay(7000);
}
else
{
digitalWrite(redLed, LOW);
digitalWrite(greenLed, HIGH);
noTone(buzzer);
}
delay(100);
}
void Line_Notify(String message) {
axTLS::WiFiClientSecure client; // กรณีขึ้น Error ให้ลบ axTLS:: ข้างหน้าทิ้ง
if (!client.connect("notify-api.line.me", 443)) {
Serial.println("connection failed");
return;
}
String req = "";
req += "POST /api/notify HTTP/1.1\r\n";
req += "Host: notify-api.line.me\r\n";
req += "Authorization: Bearer " + String(LINE_TOKEN) + "\r\n";
req += "Cache-Control: no-cache\r\n";
req += "User-Agent: ESP8266\r\n";
req += "Connection: close\r\n";
req += "Content-Type: application/x-www-form-urlencoded\r\n";
req += "Content-Length: " + String(String("message=" + message).length()) + "\r\n";
req += "\r\n";
req += "message=" + message;
// Serial.println(req);
client.print(req);
delay(20);
// Serial.println("-------------");
while(client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
break;
}
//Serial.println(line);
}
// Serial.println("-------------");
}