/********* Pleasedontcode.com **********
Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- Terms and Conditions:
You have a non-exclusive, revocable, worldwide, royalty-free license
for personal and commercial use. Attribution is optional; modifications
are allowed, but you're responsible for code maintenance. We're not
liable for any loss or damage. For full terms,
please visit pleasedontcode.com/termsandconditions.
- Project: WiFi Bot
- Source Code NOT compiled for: Arduino Uno
- Source Code created on: 2024-08-02 23:07:41
********* Pleasedontcode.com **********/
/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
/* Send messages to telegram bot */
/****** END SYSTEM REQUIREMENTS *****/
/****** DEFINITION OF LIBRARIES *****/
#include // https://github.com/evert-arias/EasyButton
#include // https://github.com/schlingensiepen/TelegramBotClient
#include // Include WiFi library for Arduino
/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t Pir_PushButton_PIN_D2 = 2;
const uint8_t Ant_PushButton_PIN_D3 = 3;
/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
// Create instances of EasyButton for each push button
EasyButton pirButton(Pir_PushButton_PIN_D2); // Instance for PIR push button
EasyButton antButton(Ant_PushButton_PIN_D3); // Instance for ANT push button
// WiFi connection credentials
const char* ssid = "digitalisierung";
const char* password = "cloudification";
// Telegram Bot secure token (replace with your token)
const String botToken = "YOUR BOT TOKEN";
// Instantiate the ssl client used to communicate with Telegram's web API
WiFiSSLClient sslPollClient;
// Instantiate the TelegramBotClient with the secure token and ssl client
TelegramBotClient client(botToken, sslPollClient); // Correctly initialized TelegramBotClient
// Function called on receiving a message
void onReceive(TelegramProcessError tbcErr, JwcProcessError jwcErr, Message* msg) {
Serial.println("onReceive");
Serial.print(F("tbcErr: ")); Serial.println((int)tbcErr);
Serial.print(F("jwcErr: ")); Serial.println((int)jwcErr);
Serial.print(F("ChatId: ")); Serial.println(msg->ChatId);
Serial.print(F("Text: ")); Serial.println(msg->Text);
client.postMessage(msg->ChatId, msg->Text); // Echo the received message back
}
// Function called if an error occurs
void onError(TelegramProcessError tbcErr, JwcProcessError jwcErr) {
Serial.print(F("onError: tbcErr: ")); Serial.println((int)tbcErr);
Serial.print(F("jwcErr: ")); Serial.println((int)jwcErr);
}
// Function to connect to WiFi
void setupWiFi() {
Serial.print(F("Connecting to network: "));
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
Serial.print(F("Connected, IP: "));
Serial.println(WiFi.localIP());
}
void setup(void) {
Serial.begin(115200);
setupWiFi();
// Initialize the EasyButton instances
pirButton.begin();
antButton.begin();
// Add callback functions for button presses
pirButton.onPressed([]() {
Serial.println("PIR Button pressed");
client.postMessage("YOUR_CHAT_ID", "PIR Button was pressed!"); // Send message to Telegram
});
antButton.onPressed([]() {
Serial.println("ANT Button pressed");
client.postMessage("YOUR_CHAT_ID", "ANT Button was pressed!"); // Send message to Telegram
});
// Set the functions implemented above as callback functions for the Telegram client
client.begin(onReceive, onError);
}
void loop(void) {
// Continuously read the status of the buttons
pirButton.read();
antButton.read();
// To process receiving data this method has to be called each main loop()
client.loop();
}
/* END CODE */