Ekasari Amaliadirakit community
Published

Switch Servo using Telegram - B24

This IoT project demonstrates a simple way to control a servo motor as a switch using the Telegram messaging app.

BeginnerFull instructions provided6 hours141
Switch Servo using Telegram - B24

Things used in this project

Hardware components

NodeMCU ESP8266 Lolin
×1
SG90 Micro-servo motor
SG90 Micro-servo motor
×2
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×1
Male/Female Jumper Wires
Male/Female Jumper Wires
(Optional)
×1

Software apps and online services

Arduino IDE
Arduino IDE
circuito.io
circuito.io

Story

Read more

Custom parts and enclosures

Telegram Screnshoot

Schematics

Circuit Diagram using Circuit.io

Code

Source Code

Arduino
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <Servo.h>

// WiFi credentials
const char* ssid = "TP-Link_37F6";
const char* password = "67566808";

// Telegram Bot Token (Get from BotFather)
#define BOTtoken "7345613144:AAHZsJkii5Etv2fT-JoiUW44EDDYbK_kNFg"

// Initialize Wi-Fi client
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);

Servo myservo;  // Create servo object

const int servoPin = D4;  // Define servo pin

unsigned long lastTimeBotRan;
const long botRequestDelay = 1000;  // Delay between bot requests

void setup() {
  Serial.begin(115200);

  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  Serial.println("Connecting to WiFi...");
  
  unsigned long startAttemptTime = millis();
  
  // Attempt to connect to Wi-Fi for 30 seconds
  while (WiFi.status() != WL_CONNECTED && millis() - startAttemptTime < 30000) {
    delay(1000);
    Serial.print(".");
  }
  
  if(WiFi.status() != WL_CONNECTED) {
    Serial.println("Failed to connect to WiFi.");
    // Add additional error handling here (e.g., restart the ESP8266 or enter deep sleep)
    return;
  }

  Serial.println("Connected to WiFi");

  client.setInsecure();  // This is to skip SSL certification validation

  // Attach servo to pin
  myservo.attach(servoPin);
  myservo.write(90);  // Initialize servo to neutral position

  lastTimeBotRan = 0;
}

void handleNewMessages(int numNewMessages) {
  Serial.print("handleNewMessages: ");
  Serial.println(numNewMessages);

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

    Serial.println("Received message: " + text);

    if (text == "/start") {
      String welcome = "Welcome! Use /left, /right, or /center to control the servo motor.";
      bot.sendMessage(chat_id, welcome, "");
    }

    if (text == "/left") {
      myservo.write(0);  // Move servo to 0 degrees
      bot.sendMessage(chat_id, "Servo moved to left position", "");
    }

    if (text == "/right") {
      myservo.write(180);  // Move servo to 180 degrees
      bot.sendMessage(chat_id, "Servo moved to right position", "");
    }

    if (text == "/center") {
      myservo.write(90);  // Move servo to 90 degrees (center)
      bot.sendMessage(chat_id, "Servo moved to center position", "");
    }
  }
}

void loop() {
  if (WiFi.status() != WL_CONNECTED) {
    Serial.println("WiFi not connected!");
    delay(1000);  // Wait for a second before trying again
    return;
  }

  if (millis() - lastTimeBotRan > botRequestDelay) {
    int numNewMessages = bot.getUpdates(bot.last_message_received + 1);

    while (numNewMessages) {
      handleNewMessages(numNewMessages);
      numNewMessages = bot.getUpdates(bot.last_message_received + 1);
    }
    lastTimeBotRan = millis();
  }
}

Credits

Ekasari Amalia
1 project • 0 followers
Contact
dirakit community
23 projects • 109 followers
Indonesia IoT Community by Informatics UIN Sunan Kalijaga Yogyakarta
Contact

Comments

Please log in or sign up to comment.