#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
const char *ssid = "Wokwi-GUEST";
const char *password = "";
const char *telegramBotToken = "YOUR-TELEGRAM-BOT-TOKEN";
const char *telegramChatID = "YOUR-TELEGRAM-CHAT-ID";
const int proximitySensorPin = 2; // Replace with your actual pin number
const int lcdColumns = 16;
const int lcdRows = 2;
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows); // Replace with your I2C address
unsigned long lastStrokeTime = 0;
unsigned long lastRegularCheckTime = 0;
void setup() {
Serial.begin(115200);
lcd.begin(lcdColumns, lcdRows);
lcd.print("Stroke Counter");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
}
void loop() {
unsigned long currentTime = millis();
// Read proximity sensor and update stroke count
int strokeCount = readProximitySensor();
// Display stroke count on LCD
lcd.setCursor(0, 1);
lcd.print("Strokes: " + String(strokeCount));
// Check stroke regularity (every 2 minutes)
if (currentTime - lastRegularCheckTime >= 120000) {
checkStrokeRegularity(strokeCount);
lastRegularCheckTime = currentTime;
}
// Check breakdown (no stroke in 5 minutes)
if (currentTime - lastStrokeTime >= 300000) {
sendBreakdownMessage(strokeCount);
lastStrokeTime = currentTime;
}
}
int readProximitySensor() {
// Implement logic to read from the proximity sensor
// Update stroke count accordingly
// Replace the following line with your actual sensor reading logic
return analogRead(proximitySensorPin);
}
void checkStrokeRegularity(int strokeCount) {
// Implement logic to check stroke regularity (every 2 minutes)
// Compare stroke count with expected regular strokes within the time frame
int expectedStrokes = 1; // Adjust as needed
if (strokeCount >= expectedStrokes) {
// Regular strokes occurred
Serial.println("Strokes are regular.");
} else {
// Irregular strokes
Serial.println("Strokes are irregular.");
// Add code to send a message to Telegram about irregular strokes
sendTelegramMessage("Irregular strokes detected!");
}
}
void sendBreakdownMessage(int strokeCount) {
// Implement logic to send a breakdown message if no strokes in 5 minutes
Serial.println("Breakdown detected!");
// Add code to send a message to Telegram about the breakdown
sendTelegramMessage("Machine breakdown! Strokes: " + String(strokeCount));
}
void sendTelegramMessage(String message) {
// Implement logic to send a message to Telegram using the Bot API
WiFiClientSecure client;
if (client.connect("api.telegram.org", 443)) {
String url = "/bot" + String(telegramBotToken) + "/sendMessage?chat_id=" + String(telegramChatID) + "&text=" + message;
client.println("GET " + url + " HTTP/1.1");
client.println("Host: api.telegram.org");
client.println("User-Agent: ESP32");
client.println("Connection: close");
client.println();
delay(1000); // Allow time for the message to be sent
}
client.stop();
}