CCCPaste

"WiFi ServoBot" rev_01

/********* 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 ServoBot"
	- Source Code NOT compiled for: ESP32 DevKit V1
	- Source Code created on: 2024-06-14 20:43:46

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* comunicare con bot Telegram   Appena avvio tutto */
	/* con /start mi esce  un menu con l’intestazione che */
	/* poi me la modifico io e la lista di comandi: */
	/* /start manuale  /stop manuale /conferma /stat */
	/* Start manuale fa uscire un men */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include   // Include the WiFi library for ESP32
#include   // Include the UniversalTelegramBot library
#include   // Include the WiFiClientSecure library for HTTPS
#include   // Include the Deneyap Servo library

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void updateOutputs(void);
void handleNewMessages(int numNewMessages);

/***** DEFINITION OF PWM OUTPUT PINS *****/
const uint8_t Ndnd_Servomotor_PWMSignal_PIN_D4 = 4;

/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
/***** used to store raw data *****/
uint8_t Ndnd_Servomotor_PWMSignal_PIN_D4_rawData = 0;

/***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
/***** used to store data after characteristic curve transformation *****/
float Ndnd_Servomotor_PWMSignal_PIN_D4_phyData = 0.0;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
Servo myservo;  // Initialize the Servo object

// WiFi credentials
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";

// Telegram BOT Token (Get from BotFather)
const char* botToken = "YOUR_BOT_TOKEN";

// Initialize Telegram BOT
WiFiClientSecure client;
UniversalTelegramBot bot(botToken, client);

// Timer variables
unsigned long lastTimeBotRan;
const int botRequestDelay = 1000;  // Delay between checking for new messages

void setup(void)
{
  // put your setup code here, to run once:
  Serial.begin(115200);

  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");

  // Set the servo motor pin as output
  pinMode(Ndnd_Servomotor_PWMSignal_PIN_D4, OUTPUT);
  myservo.attach(Ndnd_Servomotor_PWMSignal_PIN_D4);  // Attach the servo to pin D4

  // Initialize the last time the bot ran
  lastTimeBotRan = millis();
}

void loop(void)
{
  // put your main code here, to run repeatedly:
  if (millis() - lastTimeBotRan > botRequestDelay) {
    int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
    handleNewMessages(numNewMessages);
    lastTimeBotRan = millis();
  }

  updateOutputs();  // Refresh output data
}

void updateOutputs()
{
  myservo.write(60);  // Set the servo motor to 60 degrees
}

void handleNewMessages(int numNewMessages) {
  for (int i = 0; i < numNewMessages; i++) {
    String chat_id = String(bot.messages[i].chat_id);
    String text = bot.messages[i].text;

    if (text == "/start") {
      String welcome = "Welcome to the Servo Control Bot.\n";
      welcome += "/start - Show this message\n";
      welcome += "/start manuale - Start manual control\n";
      welcome += "/stop manuale - Stop manual control\n";
      welcome += "/conferma - Confirm action\n";
      welcome += "/stat - Show status\n";
      bot.sendMessage(chat_id, welcome, "");
    }
    // Add more commands here as needed
  }
}