ต้องการส่งข้อความไป Line

คือผมทำโปรเจคอยู่โปรดจคหนึ่งครับ
วัตถุประสงค์ของโปรเจคคือ ควบคุมการเปิดไฟผ่านระบบอินเตอร์เน็ตด้วยแอพ Blynk
เบื้องต้นผมสามารถควบคุมการปิดเปิดได้แล้วครับแต่ติดตรงที่


คือผมต้องการให้กดปุ๋มที่ควบคุม GP0 และก็ส่งไปที่ขา Virtual V1 ด้วยอ่าครับ
ผมไม่รู้จะเขียนโปรแกรมยังไงให้มันส่งไปที่ Virtual V1 ยังไงอ่าครับ รบกวนด้วยครับ

2 Likes

จริงๆ ควรเขียนแบบนี้ครับ ปุ่มที่ตั้งเป็น gp0 ให้เปลี่ยนเป็น v1 แทนจากนั้นให้เพิ่มโค้ต ลงไปสั่ง Loop V1 ให้ on gp0 ครับ

แบบนี้ครับ

BLYNK_WRITE(V1) 
{
   if (param.asInt()==1) { // เมื่อ V1 มีค่าเป็น 1

        digitalWrite(0, 1); //Relay1 on

        }
   }else{
        digitalWrite(0, 0); //Relay 1 off

   }
 
}
3 Likes
#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

String LINE_TOKEN = "9999999999999999999999999999999";

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "9999999999999999999999999";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "99999999999999999999";
char pass[] = "99999999999999999";

BLYNK_WRITE(V1)
{
  int pinValue = param.asInt();
  if (pinValue == 1){
    String txt = "Relay 1 on (" + String (millis()) + ")";
    Line_Notify(LINE_TOKEN, txt);
  }
}

void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
}

void loop()
{
  Blynk.run();
}

void Line_Notify(String LINE_Token, String message) {

  String msg = String ("message=") + message;

  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 += "Content-Type: application/x-www-form-urlencoded\r\n";
  req += "Authorization: Bearer " + String(LINE_Token) + "\r\n";
  req += "Content-Length: " + String(msg.length()) + "\r\n";
  req += "\r\n";
  req +=  msg;

  client.print(req);

  unsigned long timeout = millis();
  while (client.available() == 0) {
    if (millis() - timeout > 5000) {
      Serial.println(">>> Client Timeout !");
      client.stop();
      return;
    }
  }

  // Read all the lines of the reply from server and print them to Serial
  while (client.available()) {
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }

  Serial.println();
  Serial.println("closing connection");
}

โค้ดที่ผมใช้ทำอยู่อ่าครับ

2 Likes


มัน Error ครับ

BLYNK_WRITE(V1) 
{
   if (param.asInt()==1) { // เมื่อ V1 มีค่าเป็น 1

        digitalWrite(0, 1); //Relay1 on
   }else{
        digitalWrite(0, 0); //Relay 1 off

   }
 
}

แก้ใหม่ครับ

1 Likes


ต้องแก้ไขตรงไหนครับ

ใช้บอร์ดอะไรครับ ลง lib ของ esp8622 ยัง

ลงแล้วครับ ผมยังใช้ทดลองเปิดปิดไฟอยู่เลยครับ

copy error มาดูทีครับ

Arduino: 1.8.8 (Windows 10), Board: "NodeMCU 1.0 (ESP-12E Module), 80 MHz, Flash, 4M (1M SPIFFS), v2 Lower Memory, Disabled, None, Only Sketch, 115200"

In file included from C:\Users\ต้อม\Documents\Arduino\libraries\blynk-library-master\src/Blynk/BlynkApi.h:18:0,

                 from C:\Users\ต้อม\Documents\Arduino\libraries\blynk-library-master\src/BlynkApiArduino.h:14,

                 from C:\Users\ต้อม\Documents\Arduino\libraries\blynk-library-master\src/BlynkSimpleEsp8266.h:24,

                 from C:\Users\เธ•เน?เธญเธก\Desktop\Off.on.4CH\Off.on.4CH.ino:4:

C:\Users\เธ•เน?เธญเธก\Desktop\Off.on.4CH\Off.on.4CH.ino: In function 'void BlynkWidgetWrite1(BlynkReq&, const BlynkParam&)':

C:\Users\ต้อม\Documents\Arduino\libraries\blynk-library-master\src/Blynk/BlynkHandlers.h:152:10: error: redefinition of 'void BlynkWidgetWrite1(BlynkReq&, const BlynkParam&)'

     void BlynkWidgetWrite ## pin (BlynkReq BLYNK_UNUSED &request, const BlynkParam BLYNK_UNUSED &param)

          ^

C:\Users\ต้อม\Documents\Arduino\libraries\blynk-library-master\src/Blynk/BlynkHandlers.h:160:31: note: in expansion of macro 'BLYNK_WRITE_2'

 #define BLYNK_WRITE(pin)      BLYNK_WRITE_2(pin)

                               ^

C:\Users\เธ•เน?เธญเธก\Desktop\Off.on.4CH\Off.on.4CH.ino:26:1: note: in expansion of macro 'BLYNK_WRITE'

C:\Users\ต้อม\Documents\Arduino\libraries\blynk-library-master\src/Blynk/BlynkHandlers.h:152:10: error: 'void BlynkWidgetWrite1(BlynkReq&, const BlynkParam&)' previously defined here

     void BlynkWidgetWrite ## pin (BlynkReq BLYNK_UNUSED &request, const BlynkParam BLYNK_UNUSED &param)

          ^

C:\Users\ต้อม\Documents\Arduino\libraries\blynk-library-master\src/Blynk/BlynkHandlers.h:160:31: note: in expansion of macro 'BLYNK_WRITE_2'

 #define BLYNK_WRITE(pin)      BLYNK_WRITE_2(pin)

                               ^

C:\Users\เธ•เน?เธญเธก\Desktop\Off.on.4CH\Off.on.4CH.ino:17:1: note: in expansion of macro 'BLYNK_WRITE'

exit status 1
Error compiling for board NodeMCU 1.0 (ESP-12E Module).

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
1 Likes

ตัวนี้ลงยังครับ

ยังครับ พึ่งลงเสร็จแล้วคอมพายก็ไม่ผ่านครับ

แคปหน้า Tools มาดูหน่อยครับ

นี่ครับ


แต่ผมลองแก้ตรง BLYNK_WRITE ให้เลขมันไม่เหมือนกันอ่าครับ มันคอมพายผ่านเฉย

ได้แล้วหรอ

ใช่ครับ แต่มันจะคนละวงกันกับ เปิดปิดไฟอ่าครับ

1 Likes

อ่อมันผิดตรงนี้

ผมก็ดูไม่ทั่ว 555

555 แล้วผมต้องเพิ่มโค้ดที่เป็นชุดส่งไป Line ไปอยู่ในวง V1 ยังไงครับ

เอาไปรวมกันได้เลยครับ

BLYNK_WRITE(V1) 
{
   if (param.asInt()==1) { // เมื่อ V1 มีค่าเป็น 1

        digitalWrite(0, 1); //Relay1 on
        int pinValue = param.asInt();
           if (pinValue == 1){
       String txt = "Relay 1 on (" + String (millis()) + ")";
       Line_Notify(LINE_TOKEN, txt);
            }
   }else{

        digitalWrite(0, 0); //Relay 1 off

   } 
}