Vertical button with arduino WEMOS

Pushing a button with a servo motor via Telegram
4
13
0
91
updated July 28, 2024

Description

PDF

Materials needed:

  • Wemos d1 mini
  • Servo motor (ebay)

Others:

The code:

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

// Wifi network station credentials
#define WIFI_SSID "WIFINAME"
#define WIFI_PASSWORD "WifiPassword"
// Telegram BOT Token (Get from Botfather)
#define BOT_TOKEN "BotToken"
#define BOT_ID "BotID"

const unsigned long BOT_MTBS = 1000; 
Servo servoMotor;

X509List cert(TELEGRAM_CERTIFICATE_ROOT);
WiFiClientSecure secured_client;
UniversalTelegramBot bot(BOT_TOKEN, secured_client);
unsigned long bot_lasttime;

void handleNewMessages(int numNewMessages)
{
 for (int i = 0; i < numNewMessages; i++)
 {

   if(bot.messages[i].text == "/start" && bot.messages[i].chat_id == BOT_ID){
     bot.sendMessage(bot.messages[i].chat_id, "Bones!", "");
     servoMotor.write(0);
   } 
   if(bot.messages[i].text == "/on" && bot.messages[i].chat_id == BOT_ID){
     servoMotor.write(0);
     servoMotor.write(180);
     delay(1000);
     servoMotor.write(90);
     
     bot.sendMessage(bot.messages[i].chat_id, "Pressed ON!", "");
   }
   if(bot.messages[i].text == "/hardreset" && bot.messages[i].chat_id == BOT_ID){
     servoMotor.write(90);
     servoMotor.write(180);
     delay(10000);
     servoMotor.write(90);
     
     bot.sendMessage(bot.messages[i].chat_id, "Pressed 10s!", "");
   }
   
 }
}

void setup()
{
 Serial.begin(115200);
 Serial.println();
 
 servoMotor.attach(2);//Declaram es servo a des pin 2

 // attempt to connect to Wifi network:
 Serial.print("Connecting to Wifi SSID ");
 Serial.print(WIFI_SSID);
 WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
 secured_client.setTrustAnchors(&cert); // Add root certificate for api.telegram.org
 
 while (WiFi.status() != WL_CONNECTED)
 {
   Serial.print(".");
   delay(500);
 }
 Serial.print("\nWiFi connected. IP address: ");
 Serial.println(WiFi.localIP());

 Serial.print("Retrieving time: ");
 configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP
 time_t now = time(nullptr);
 while (now < 24 * 3600)
 {
   Serial.print(".");
   delay(100);
   now = time(nullptr);
 }
 Serial.println(now);
}

void loop()
{
 if (millis() - bot_lasttime > BOT_MTBS)
 {
   int numNewMessages = bot.getUpdates(bot.last_message_received + 1);

   while (numNewMessages)
   {
     Serial.println("got response");
     handleNewMessages(numNewMessages);
     numNewMessages = bot.getUpdates(bot.last_message_received + 1);
   }

   bot_lasttime = millis();
 }
}

Tags



Model origin

The author marked this model as their own original creation.

License