#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; }
Standard input is empty
#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;
}