XKC-Y25-V ทำให้ส่งค่าไปในไลน์ยังไงครับ


เซ็นเซอร์ดังภาพครับ
ตัวนี้ผมเสียบขา D1ในEsp 8266ให้ส่งค่ามาครับ
แต่ผมไม่รู้ว่าจะเขียนโค้ดยังไงครับ
การทำงานของเซนเซอร์จะทำงานแบบเจอน้ำจะทำงาน ถ้าไม่เจอน้ำจะไม่ทำงานครับ
ผมสามารถใช้ digitalRead 1 ส่งค่า digitalRead 0 ไม่ส่งค่า ได้เลยไหมครับ
หรือต้องใช้โค้ดอื่นครับ

const int sensorPin = D0; // กำหนดขาที่เชื่อมกับ OUT ของ XKC-Y25-V
bool isWaterDetected = false;

void setup() {
  pinMode(sensorPin, INPUT);
  Serial.begin(115200);
}

void loop() {
  isWaterDetected = digitalRead(sensorPin);

  if (isWaterDetected) {
    // ทำงานเมื่อพบน้ำ
    Serial.println("พบน้ำ");
    // ทำอะไรบางอย่างที่คุณต้องการ เช่น ส่งข้อมูลหรือควบคุมอุปกรณ์อื่น ๆ
  } else {
    // ไม่พบน้ำ
    Serial.println("ไม่พบน้ำ");
    // ทำอะไรบางอย่างที่คุณต้องการเมื่อไม่พบน้ำ
  }

  delay(1000); // หน่วงเวลาเพื่อไม่ให้อ่านข้อมูลตรวจจับน้ำบ่อยเกินไป
}

หากต้องการเพิ่มเงื่อนไขในโค้ดเพื่อส่งข้อความแจ้งเตือนผ่านไลน์เมื่อพบน้ำ สามารถใช้ไลบรารี TridentTD_LineNotify ใน Arduino IDE เพื่อทำงานนี้ได้. ติดตั้งไลบรารีนี้ใน Arduino IDE ก่อน:

  1. เปิด Arduino IDE.
  2. ไปที่ “เครื่องมือ” (Tools) > “Manage Libraries”.
  3. ค้นหา “TridentTD_LineNotify” และติดตั้งไลบรารีนี้.

เมื่อได้ทำการติดตั้งไลบรารีแล้ว คุณสามารถเพิ่มเงื่อนไขในโค้ดของคุณเพื่อส่งข้อความแจ้งเตือนผ่านไลน์เมื่อพบน้ำได้ดังนี้:

#include <TridentTD_LineNotify.h>

const int sensorPin = D0; // กำหนดขาที่เชื่อมกับ OUT ของ XKC-Y25-V
bool isWaterDetected = false;
const char* LINE_TOKEN = "YOUR_LINE_NOTIFY_TOKEN"; // ใส่ Token ของ Line Notify ที่คุณได้รับมาที่นี่

void setup() {
  pinMode(sensorPin, INPUT);
  Serial.begin(115200);

  // เชื่อมต่อ Line Notify
  LineNotify.setToken(LINE_TOKEN);
}

void loop() {
  isWaterDetected = digitalRead(sensorPin);

  if (isWaterDetected) {
    // ทำงานเมื่อพบน้ำ
    Serial.println("พบน้ำ");

    // ส่งข้อความแจ้งเตือนผ่าน Line Notify
    LineNotify.send("พบน้ำแล้ว!");

    // ทำอะไรบางอย่างที่คุณต้องการ เช่น ควบคุมอุปกรณ์อื่น ๆ
  } else {
    // ไม่พบน้ำ
    Serial.println("ไม่พบน้ำ");
    // ทำอะไรบางอย่างที่คุณต้องการเมื่อไม่พบน้ำ
  }

  delay(1000); // หน่วงเวลาเพื่อไม่ให้อ่านข้อมูลตรวจจับน้ำบ่อยเกินไป
}

ทำงานผ่านแล้วครับ ขอบคุณสำหรับข้อมูลครับ

ตอนนี้มีปัญหาใหม่ครับ ผมเอาไปทำกับblynkด้วยครับ แล้วมันแจ้งเตือนมาที่lineแค่รอบเดียวครับ แต่ไม่ส่งมารอบอื่นๆครับ แต่ในserial monitor ขึ้นข้อความมาเรื่อยครับ แต่ในไลน์ไม่ส่งครับ
ผมทำในblynk.Edgent ครับ ใช้บอร์ดEsp8266
ตรงเซนเซอร์ อยู่ในโค้ดส่วนlineครับ ตรง water1 water2

ตรงBLYNK_WRITE​ไม่มีปัญหาครับ มีแค่lineไม่ส่งข้อความครับ

/*************************************************************
Blynk is a platform with iOS and Android apps to control
ESP32, Arduino, Raspberry Pi and the likes over the Internet.
You can easily build mobile and web interfaces for any
projects by simply dragging and dropping widgets.

Downloads, docs, tutorials: https://www.blynk.io
Sketch generator:           https://examples.blynk.cc
Blynk community:            https://community.blynk.cc
Follow us:                  https://www.fb.com/blynkapp
                            https://twitter.com/blynk_app

Blynk library is licensed under MIT license


Blynk.Edgent implements:

  • Blynk.Inject - Dynamic WiFi credentials provisioning
  • Blynk.Air - Over The Air firmware updates
  • Device state indication using a physical LED
  • Credentials reset using a physical Button
    *************************************************************/

/* Fill in information from your Blynk Template here /
/
Read more: WiFi provisioning - Blynk Documentation */
#define BLYNK_TEMPLATE_ID “TMPL6dlzRjDJ4”
#define BLYNK_TEMPLATE_NAME “pumnum”

#define BLYNK_FIRMWARE_VERSION “0.1.0”

#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG

#define APP_DEBUG

// Uncomment your board, or configure a custom board in Settings.h
//#define USE_SPARKFUN_BLYNK_BOARD
#define USE_NODE_MCU_BOARD
//#define USE_WITTY_CLOUD_BOARD
//#define USE_WEMOS_D1_MINI

#include “BlynkEdgent.h”
#include <TridentTD_LineNotify.h>

#define SSID “@TOT NAYNG35_2.4G”
#define PASSWORD “291704212”
#define LINE_TOKEN “URsRFtzNl9j5wfF8RN3ZUeMW10hhNW8ruhoMg5NL1p2”

const int sensorPin1 = D5; // กำหนดขาที่เชื่อมกับ OUT ของ XKC-Y25-V
int Water1 = 0;
bool isWaterDetected1 = false;

const int sensorPin2 = D6; // กำหนดขาที่เชื่อมกับ OUT ของ XKC-Y25-V
int Water2 = 0;
bool isWaterDetected2 = false;

void setup()
{
pinMode(D1,OUTPUT);
pinMode(D2,OUTPUT);

pinMode(sensorPin1,OUTPUT);
pinMode(sensorPin2,OUTPUT);

digitalWrite(D1, HIGH);
digitalWrite(D2, HIGH);

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.setToken(LINE_TOKEN);
Serial.begin(115200); Serial.println();
Serial.println(LINE.getVersion());

Serial.begin(115200);
BlynkEdgent.begin();
}
void loop() {
BlynkEdgent.run();

{
Water1 = digitalRead(sensorPin1);
if (Water1 ==1){
if (isWaterDetected1 == true) {
// ทำงานเมื่อพบน้ำ
Serial.println(“น้ำล้นออกจากถัง”);
LINE.notify(“น้ำล้นออกจากถัง”);
delay(1000);
isWaterDetected1 = false;
// ทำอะไรบางอย่างที่คุณต้องการ เช่น ส่งข้อมูลหรือควบคุมอุปกรณ์อื่น ๆ
}
}
else {
(Water1 ==0);{
// ไม่พบน้ำ
if (isWaterDetected1 == false) {
Serial.println(“ไม่มีน้ำ”);
LINE.notify(“ไม่พบน้ำ”);
delay(1000);
isWaterDetected1 = true;
// ทำอะไรบางอย่างที่คุณต้องการเมื่อไม่พบน้ำ
}

// delay(1000); // หน่วงเวลาเพื่อไม่ให้อ่านข้อมูลตรวจจับน้ำบ่อยเกินไป
}
}

{
Water2 = digitalRead(sensorPin2);
if (Water2 ==1){
if (isWaterDetected2 == true) {
// ทำงานเมื่อพบน้ำ
Serial.println(“กำลังเติมน้ำ”);
LINE.notify(“ปั้มทำงาน กำลังเติมน้ำ”);
delay(1000);
isWaterDetected2 = false;
// ทำอะไรบางอย่างที่คุณต้องการ เช่น ส่งข้อมูลหรือควบคุมอุปกรณ์อื่น ๆ
}
}
else {
(Water2 ==0);{
// ไม่พบน้ำ
if (isWaterDetected2 == false) {
Serial.println(“ระดับน้ำต่ำ”);
LINE.notify(“ระดับน้ำต่ำ!”);
delay(1000);
isWaterDetected2 = true;
// ทำอะไรบางอย่างที่คุณต้องการเมื่อไม่พบน้ำ
}
}
}
}
}
}

BLYNK_WRITE(V1)
{

if(param.asInt()==1){
digitalWrite(D1, LOW);
Blynk.virtualWrite(V1, 1);
Serial.printf(“Relay1:”);
Serial.println(“ON”);
}
else{
digitalWrite(D1,HIGH) ;
Blynk.virtualWrite(V1, 0);
Serial.printf(“Relay1:”);
Serial.println(“OFF”);

}
delay(100);
}

BLYNK_WRITE(V2)
{

if(param.asInt()==1){
digitalWrite(D2, LOW);
Blynk.virtualWrite(V2, 1);
Serial.printf(“Relay2:”);
Serial.println(“ON”);
}
else{
digitalWrite(D2,HIGH) ;
Blynk.virtualWrite(V2, 0);
Serial.printf(“Relay2:”);
Serial.println(“OFF”);

}
delay(100);
}