Advertisement
pleasedontcode

WiFi Bot rev_01

Aug 2nd, 2024
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: WiFi Bot
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-08-02 23:07:41
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Send messages to telegram bot */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /****** DEFINITION OF LIBRARIES *****/
  24. #include <EasyButton.h> // https://github.com/evert-arias/EasyButton
  25. #include <TelegramBotClient.h> // https://github.com/schlingensiepen/TelegramBotClient
  26. #include <WiFi101.h> // Include WiFi library for Arduino
  27.  
  28. /****** FUNCTION PROTOTYPES *****/
  29. void setup(void);
  30. void loop(void);
  31.  
  32. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  33. const uint8_t Pir_PushButton_PIN_D2 = 2;
  34. const uint8_t Ant_PushButton_PIN_D3 = 3;
  35.  
  36. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  37. // Create instances of EasyButton for each push button
  38. EasyButton pirButton(Pir_PushButton_PIN_D2); // Instance for PIR push button
  39. EasyButton antButton(Ant_PushButton_PIN_D3); // Instance for ANT push button
  40.  
  41. // WiFi connection credentials
  42. const char* ssid = "digitalisierung";
  43. const char* password = "cloudification";
  44.  
  45. // Telegram Bot secure token (replace with your token)
  46. const String botToken = "YOUR BOT TOKEN";
  47.  
  48. // Instantiate the ssl client used to communicate with Telegram's web API
  49. WiFiSSLClient sslPollClient;
  50.  
  51. // Instantiate the TelegramBotClient with the secure token and ssl client
  52. TelegramBotClient client(botToken, sslPollClient); // Correctly initialized TelegramBotClient
  53.  
  54. // Function called on receiving a message
  55. void onReceive(TelegramProcessError tbcErr, JwcProcessError jwcErr, Message* msg) {
  56.     Serial.println("onReceive");
  57.     Serial.print(F("tbcErr: ")); Serial.println((int)tbcErr);
  58.     Serial.print(F("jwcErr: ")); Serial.println((int)jwcErr);
  59.     Serial.print(F("ChatId: ")); Serial.println(msg->ChatId);
  60.     Serial.print(F("Text: ")); Serial.println(msg->Text);
  61.     client.postMessage(msg->ChatId, msg->Text); // Echo the received message back
  62. }
  63.  
  64. // Function called if an error occurs
  65. void onError(TelegramProcessError tbcErr, JwcProcessError jwcErr) {
  66.     Serial.print(F("onError: tbcErr: ")); Serial.println((int)tbcErr);
  67.     Serial.print(F("jwcErr: ")); Serial.println((int)jwcErr);
  68. }
  69.  
  70. // Function to connect to WiFi
  71. void setupWiFi() {
  72.     Serial.print(F("Connecting to network: "));
  73.     Serial.println(ssid);
  74.     WiFi.begin(ssid, password);
  75.     while (WiFi.status() != WL_CONNECTED) {
  76.         delay(500);
  77.     }
  78.     Serial.print(F("Connected, IP: "));
  79.     Serial.println(WiFi.localIP());
  80. }
  81.  
  82. void setup(void) {
  83.     Serial.begin(115200);
  84.     setupWiFi();
  85.    
  86.     // Initialize the EasyButton instances
  87.     pirButton.begin();
  88.     antButton.begin();
  89.    
  90.     // Add callback functions for button presses
  91.     pirButton.onPressed([]() {
  92.         Serial.println("PIR Button pressed");
  93.         client.postMessage("YOUR_CHAT_ID", "PIR Button was pressed!"); // Send message to Telegram
  94.     });
  95.    
  96.     antButton.onPressed([]() {
  97.         Serial.println("ANT Button pressed");
  98.         client.postMessage("YOUR_CHAT_ID", "ANT Button was pressed!"); // Send message to Telegram
  99.     });
  100.    
  101.     // Set the functions implemented above as callback functions for the Telegram client
  102.     client.begin(onReceive, onError);
  103. }
  104.  
  105. void loop(void) {
  106.     // Continuously read the status of the buttons
  107.     pirButton.read();
  108.     antButton.read();
  109.    
  110.     // To process receiving data this method has to be called each main loop()
  111.     client.loop();
  112. }
  113.  
  114. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement