ทำเรื่องแจ้งเตือนแก๊สlpgรั่วไหลผ่านไลน์ค่ะ ตอนนี้รันโค้ดแล้วบอกค่าแก๊สได้แต่ไลน์ไม่แจ้งเตือนค่ะ ลำโพงดังตลอดเวลา ใช้บอร์ดesp8266
ขอบคุณล่วงหน้าค่ะ
int buzzer = D0;
int smokeA0 = A0;
#include <WiFi.h>
#include <ESP8266WiFi.h>
#include <TridentTD_LineNotify.h>
#include <WiFiClientSecure.h>
void Line_Notify(String message) ;
#define WIFI_SSID "P"//ชื่อไวไฟของเรา
#define WIFI_PASSWORD "pims051147"//รหัสผ่านไวไฟของเรา
#define LINE_TOKEN "03X7zwoo3AyvAzt5ed77kVjsTaRs4apCC6qvXntvVD4"//ไลน์ TOKEN ของเรา
// Your threshold value
int sensorThres = 300;
void setup() {
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(300);
}
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)
{
tone(buzzer, 1000, 200);
Line_Notify("warning");
delay(7000);
}
else
{
noTone(buzzer);
}
delay(100);
}
void Line_Notify(String message) {
::WiFiClientSecure client; // กรณีขึ้น Error ให้ลบ axTLS:: ข้างหน้าทิ้ง
if (!client.connect("notify-api.line.me", 443)) {
Serial.println("connection failed");
return;
}
else
{
}
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("-------------");
}
#include <ESP8266WiFi.h>
#define LINE_TOKEN "MJm6ssTnjKy0Iu8P776SpgZwNJnfZ06BJQmGuwg0q0Y"
String message = "Gas Warning"; //ข้อความที่จะส่งไปยงั Line
void LINE_Notify(String message) ;
const char* SSID = "P";
const char* password = "pims051147";
int i=0,a=0;
int sensorPin = A0;
int sensorValue ;
int buzzer = D0 ;
void setup () {
WiFi.mode(WIFI_STA);
pinMode(buzzer, OUTPUT);
Serial.begin(9600);
delay(10);
Serial.println();
Serial.println();
Serial.print("Connecting to "); //แสดงข้อความ “Connecting to”
Serial.println(SSID);
WiFi.begin(SSID, password);
while (WiFi.status() != WL_CONNECTED) //รอจนกว่าจะเชื่อมต่อสา เร็จ
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected"); //แสดงขอ้ความเชื่อมต่อสำเร็จ
Serial.println("IP address: ");
Serial.println(WiFi.localIP()); //แสดงหมายเลข IP ของESP8266(DHCP)
}
void loop() {
sensorValue = analogRead(sensorPin);
Serial.print("sensor=");
Serial.println(sensorValue);
delay(500); //ถ้าค่าแก๊สถึง500 ppm
if(sensorValue>=500){
digitalWrite(buzzer , HIGH) ;
Line_Notify(message);
a=1;
}
else {
digitalWrite(buzzer , LOW) ;
Line_Notify(message);
}
i=1;
}
void Line_Notify(String message) { //ส่งขอ้ความ LINE ACCESS TOKEN
WiFiClientSecure client;
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 access token ") + "\r\n" ;
req += "Cache-Control: no-cache\r\n"; req += "User-Agent: ESP8266\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;
}
}
}
PUYIOT
(อิทธิรงค์ ทองย้อย (ปุ๋ย))
4
ลองดูเป็นแนวทางครับ จัด Code ใหม่ ใช้ Library TridentTD LineNotify นะครับ Code จะสั้นลง
#include <ESP8266WiFi.h>
#include <TridentTD_LineNotify.h>
String LineText;
String string0 = "Gas Warning";
#define SSID "P" // บรรทัดที่ 11 ให้ใส่ ชื่อ Wifi ที่จะเชื่อมต่อ
#define PASSWORD "pims051147" // บรรทัดที่ 12 ใส่ รหัส Wifi
#define LINE_TOKEN "MJm6ssTnjKy0Iu8P776SpgZwNJnfZ06BJQmGuwg0q0Y"
int i = 0, a = 0;
int sensorPin = A0;
int sensorValue ;
int buzzer = D0 ;
void setup () {
pinMode(buzzer, OUTPUT);
Serial.begin(115200); //ถ้าต้องการดูค่าใน Serial monitor ให้เลือก Bound Rate 115200
WiFi.mode(WIFI_STA);
WiFi.begin(SSID, PASSWORD);
Serial.printf("WiFi connecting to %s\n", SSID);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(400);
}
Serial.printf("\nWiFi connected\nIP : ");
Serial.println(WiFi.localIP());
// กำหนด Line Token
LINE.setToken(LINE_TOKEN);
}
void loop() {
sensorValue = analogRead(sensorPin);
Serial.print("sensor=");
Serial.println(sensorValue);
delay(500); //ถ้าค่าแก๊สถึง500 ppm
if (sensorValue >= 500) {
digitalWrite(buzzer , HIGH) ;
LineText = "\n" + string0 ;
LINE.notify(LineText);
a = 1;
}
else {
digitalWrite(buzzer , LOW) ;
LineText = "\n" + string0 ;
LINE.notify(LineText);
}
i = 1;
}
TridentTD_Linenotify.rar (448.1 KB)