Advertisement
pleasedontcode

"WiFi ServoBot" rev_01

Jun 14th, 2024
206
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 ServoBot"
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-06-14 20:43:46
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* comunicare con bot Telegram   Appena avvio tutto */
  21.     /* con /start mi esce  un menu con l’intestazione che */
  22.     /* poi me la modifico io e la lista di comandi: */
  23.     /* /start manuale  /stop manuale /conferma /stat */
  24.     /* Start manuale fa uscire un men */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <WiFi.h>  // Include the WiFi library for ESP32
  29. #include <UniversalTelegramBot.h>  // Include the UniversalTelegramBot library
  30. #include <WiFiClientSecure.h>  // Include the WiFiClientSecure library for HTTPS
  31. #include <Deneyap_Servo.h>  // Include the Deneyap Servo library
  32.  
  33. /****** FUNCTION PROTOTYPES *****/
  34. void setup(void);
  35. void loop(void);
  36. void updateOutputs(void);
  37. void handleNewMessages(int numNewMessages);
  38.  
  39. /***** DEFINITION OF PWM OUTPUT PINS *****/
  40. const uint8_t Ndnd_Servomotor_PWMSignal_PIN_D4 = 4;
  41.  
  42. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  43. /***** used to store raw data *****/
  44. uint8_t Ndnd_Servomotor_PWMSignal_PIN_D4_rawData = 0;
  45.  
  46. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  47. /***** used to store data after characteristic curve transformation *****/
  48. float Ndnd_Servomotor_PWMSignal_PIN_D4_phyData = 0.0;
  49.  
  50. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  51. Servo myservo;  // Initialize the Servo object
  52.  
  53. // WiFi credentials
  54. const char* ssid = "YOUR_SSID";
  55. const char* password = "YOUR_PASSWORD";
  56.  
  57. // Telegram BOT Token (Get from BotFather)
  58. const char* botToken = "YOUR_BOT_TOKEN";
  59.  
  60. // Initialize Telegram BOT
  61. WiFiClientSecure client;
  62. UniversalTelegramBot bot(botToken, client);
  63.  
  64. // Timer variables
  65. unsigned long lastTimeBotRan;
  66. const int botRequestDelay = 1000;  // Delay between checking for new messages
  67.  
  68. void setup(void)
  69. {
  70.   // put your setup code here, to run once:
  71.   Serial.begin(115200);
  72.  
  73.   // Connect to Wi-Fi
  74.   WiFi.begin(ssid, password);
  75.   while (WiFi.status() != WL_CONNECTED) {
  76.     delay(1000);
  77.     Serial.println("Connecting to WiFi...");
  78.   }
  79.   Serial.println("Connected to WiFi");
  80.  
  81.   // Set the servo motor pin as output
  82.   pinMode(Ndnd_Servomotor_PWMSignal_PIN_D4, OUTPUT);
  83.   myservo.attach(Ndnd_Servomotor_PWMSignal_PIN_D4);  // Attach the servo to pin D4
  84.  
  85.   // Initialize the last time the bot ran
  86.   lastTimeBotRan = millis();
  87. }
  88.  
  89. void loop(void)
  90. {
  91.   // put your main code here, to run repeatedly:
  92.   if (millis() - lastTimeBotRan > botRequestDelay) {
  93.     int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  94.     handleNewMessages(numNewMessages);
  95.     lastTimeBotRan = millis();
  96.   }
  97.  
  98.   updateOutputs();  // Refresh output data
  99. }
  100.  
  101. void updateOutputs()
  102. {
  103.   myservo.write(60);  // Set the servo motor to 60 degrees
  104. }
  105.  
  106. void handleNewMessages(int numNewMessages) {
  107.   for (int i = 0; i < numNewMessages; i++) {
  108.     String chat_id = String(bot.messages[i].chat_id);
  109.     String text = bot.messages[i].text;
  110.  
  111.     if (text == "/start") {
  112.       String welcome = "Welcome to the Servo Control Bot.\n";
  113.       welcome += "/start - Show this message\n";
  114.       welcome += "/start manuale - Start manual control\n";
  115.       welcome += "/stop manuale - Stop manual control\n";
  116.       welcome += "/conferma - Confirm action\n";
  117.       welcome += "/stat - Show status\n";
  118.       bot.sendMessage(chat_id, welcome, "");
  119.     }
  120.     // Add more commands here as needed
  121.   }
  122. }
  123.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement