fork download
  1. #define BLYNK_TEMPLATE_ID "TMPL6bdCSejvW"
  2. #define BLYNK_TEMPLATE_NAME "Slider controlCopy"
  3. #define BLYNK_AUTH_TOKEN "ieUHeQmnPVcEXyTvw74w3wFzw1mnWdvR"
  4.  
  5. #include <ESP8266WiFi.h>
  6. #include <BlynkSimpleEsp8266.h>
  7. #include <WiFiClientSecure.h>
  8. #include <UniversalTelegramBot.h>
  9. #include <ArduinoJson.h>
  10.  
  11. // --- ข้อมูลการเชื่อมต่อ ---
  12. char ssid[] = "KOKOAROI";
  13. char pass[] = "79487948";
  14. String botToken = "8543769803:AAF1kvQN4U3sK8R1yb4dlSvsdCO4eLDg8iU";
  15. String chatID = "6996909193";
  16.  
  17. // --- การตั้งค่าขา Pin ---
  18. const int motorPin = D0; // ขาสั่งมอเตอร์ (แนะนำต่อผ่าน Relay)
  19. const int switchPin = D1; // ขาสวิตช์นับรอบ
  20.  
  21. // --- ตัวแปรควบคุมการทำงาน ---
  22. int targetCycles = 0;
  23. int currentCount = 0;
  24. bool isProcessing = false;
  25. bool lastSwitchState = HIGH;
  26. unsigned long lastDebounceTime = 0;
  27. unsigned long debounceDelay = 50; // หน่วงเวลาป้องกันสัญญาณรบกวน (ms)
  28.  
  29. WiFiClientSecure client;
  30. UniversalTelegramBot bot(botToken, client);
  31.  
  32. // ฟังก์ชันรับค่าจาก Slider บนแอป Blynk (ตั้งค่าเป็น Virtual Pin V1)
  33. BLYNK_WRITE(V1) {
  34. targetCycles = param.asInt(); // รับค่าจำนวนรอบที่ต้องการจาก Slider
  35.  
  36. if (targetCycles > 0 && !isProcessing) {
  37. currentCount = 0;
  38. isProcessing = true;
  39. digitalWrite(motorPin, HIGH); // เริ่มเดินมอเตอร์
  40.  
  41. // แจ้งเตือนเริ่มต้น
  42. Blynk.virtualWrite(V2, currentCount); // เคลียร์เลขหน้าจอ Blynk (V2)
  43. bot.sendMessage(chatID, "🚀 เริ่มทำงาน! เป้าหมาย: " + String(targetCycles) + " รอบ", "");
  44. }
  45. }
  46.  
  47. void setup() {
  48. Serial.begin(115200);
  49.  
  50. pinMode(motorPin, OUTPUT);
  51. pinMode(switchPin, INPUT_PULLUP); // ใช้ Pull-up ภายใน (Switch ต่อขา D1 กับ GND)
  52. digitalWrite(motorPin, LOW);
  53.  
  54. client.setInsecure(); // สำหรับ ESP8266 เพื่อให้ส่ง Telegram ได้โดยไม่ต้องเช็ค Cert
  55.  
  56. Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  57. Serial.println("System Ready!");
  58. }
  59.  
  60. void loop() {
  61. Blynk.run();
  62.  
  63. if (isProcessing) {
  64. int reading = digitalRead(switchPin);
  65.  
  66. // ตรวจจับจังหวะการกดสวิตช์ (State Change Detection)
  67. if (reading != lastSwitchState) {
  68. lastDebounceTime = millis();
  69. }
  70.  
  71. if ((millis() - lastDebounceTime) > debounceDelay) {
  72. if (reading == LOW && lastSwitchState == HIGH) { // สวิตช์ถูกกด (GND)
  73. currentCount++;
  74. Serial.println("Count: " + String(currentCount));
  75.  
  76. // 1. อัปเดตตัวเลขบนแอป Blynk (V2)
  77. Blynk.virtualWrite(V2, currentCount);
  78.  
  79. // 2. ส่งข้อความแจ้งเตือนผ่าน Telegram ทุกรอบ (ถ้าไม่ต้องการให้ลบบรรทัดนี้ออก)
  80. bot.sendMessage(chatID, "✅ นับแล้ว: " + String(currentCount) + "/" + String(targetCycles), "");
  81.  
  82. // ตรวจสอบว่าครบตามเป้าหมายหรือยัง
  83. if (currentCount >= targetCycles) {
  84. stopMachine();
  85. }
  86. }
  87. }
  88. lastSwitchState = reading;
  89. }
  90. }
  91.  
  92. void stopMachine() {
  93. isProcessing = false;
  94. digitalWrite(motorPin, LOW); // หยุดมอเตอร์
  95.  
  96. // แจ้งเตือนเมื่อจบงาน
  97. bot.sendMessage(chatID, "🏁 ทำงานเสร็จสิ้นครบ " + String(currentCount) + " รอบแล้วครับ!", "");
  98. Serial.println("Work Finished");
  99. targetCycles = 0;
  100. }
Success #stdin #stdout 0.02s 25588KB
stdin
Standard input is empty
stdout
#define BLYNK_TEMPLATE_ID "TMPL6bdCSejvW"
#define BLYNK_TEMPLATE_NAME "Slider controlCopy"
#define BLYNK_AUTH_TOKEN "ieUHeQmnPVcEXyTvw74w3wFzw1mnWdvR"

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>

// --- ข้อมูลการเชื่อมต่อ ---
char ssid[] = "KOKOAROI";
char pass[] = "79487948";
String botToken = "8543769803:AAF1kvQN4U3sK8R1yb4dlSvsdCO4eLDg8iU";
String chatID = "6996909193";

// --- การตั้งค่าขา Pin ---
const int motorPin = D0;       // ขาสั่งมอเตอร์ (แนะนำต่อผ่าน Relay)
const int switchPin = D1;      // ขาสวิตช์นับรอบ

// --- ตัวแปรควบคุมการทำงาน ---
int targetCycles = 0;          
int currentCount = 0;          
bool isProcessing = false;     
bool lastSwitchState = HIGH;   
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50; // หน่วงเวลาป้องกันสัญญาณรบกวน (ms)

WiFiClientSecure client;
UniversalTelegramBot bot(botToken, client);

// ฟังก์ชันรับค่าจาก Slider บนแอป Blynk (ตั้งค่าเป็น Virtual Pin V1)
BLYNK_WRITE(V1) {
  targetCycles = param.asInt(); // รับค่าจำนวนรอบที่ต้องการจาก Slider
  
  if (targetCycles > 0 && !isProcessing) {
    currentCount = 0;
    isProcessing = true;
    digitalWrite(motorPin, HIGH); // เริ่มเดินมอเตอร์
    
    // แจ้งเตือนเริ่มต้น
    Blynk.virtualWrite(V2, currentCount); // เคลียร์เลขหน้าจอ Blynk (V2)
    bot.sendMessage(chatID, "🚀 เริ่มทำงาน! เป้าหมาย: " + String(targetCycles) + " รอบ", "");
  }
}

void setup() {
  Serial.begin(115200);
  
  pinMode(motorPin, OUTPUT);
  pinMode(switchPin, INPUT_PULLUP); // ใช้ Pull-up ภายใน (Switch ต่อขา D1 กับ GND)
  digitalWrite(motorPin, LOW);

  client.setInsecure(); // สำหรับ ESP8266 เพื่อให้ส่ง Telegram ได้โดยไม่ต้องเช็ค Cert
  
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  Serial.println("System Ready!");
}

void loop() {
  Blynk.run();

  if (isProcessing) {
    int reading = digitalRead(switchPin);

    // ตรวจจับจังหวะการกดสวิตช์ (State Change Detection)
    if (reading != lastSwitchState) {
      lastDebounceTime = millis();
    }

    if ((millis() - lastDebounceTime) > debounceDelay) {
      if (reading == LOW && lastSwitchState == HIGH) { // สวิตช์ถูกกด (GND)
        currentCount++;
        Serial.println("Count: " + String(currentCount));
        
        // 1. อัปเดตตัวเลขบนแอป Blynk (V2)
        Blynk.virtualWrite(V2, currentCount);
        
        // 2. ส่งข้อความแจ้งเตือนผ่าน Telegram ทุกรอบ (ถ้าไม่ต้องการให้ลบบรรทัดนี้ออก)
        bot.sendMessage(chatID, "✅ นับแล้ว: " + String(currentCount) + "/" + String(targetCycles), "");

        // ตรวจสอบว่าครบตามเป้าหมายหรือยัง
        if (currentCount >= targetCycles) {
          stopMachine();
        }
      }
    }
    lastSwitchState = reading;
  }
}

void stopMachine() {
  isProcessing = false;
  digitalWrite(motorPin, LOW); // หยุดมอเตอร์
  
  // แจ้งเตือนเมื่อจบงาน
  bot.sendMessage(chatID, "🏁 ทำงานเสร็จสิ้นครบ " + String(currentCount) + " รอบแล้วครับ!", "");
  Serial.println("Work Finished");
  targetCycles = 0;
}