Control Arduino Using Telegram Bot Without Any Extra Hardware

About: iOS Developer | Amateur NodeJS Developer | Arduino Lover

Intro: Control Arduino Using Telegram Bot Without Any Extra Hardware

There is a wide range of things you can do with Arduino, but did you ever think about control your Arduino using a Telegram bot?

WHAT YOU WILL NEED:

  • Arduino UNO
  • Node.js installed on your PC
  • Some controllable device (We use Arduino's on-board LED at pin 13 here, but feel free to ask about anything you want to do in comments)

Step 1: Install Node.js on Your PC

Node.js is a JavaScript runtime, but don,t panic if you don't have a programming background. I prepared what you need and you can download them. In this project, we will use Node.js for:

  1. Create Telegram bot
  2. Control Arduino


INSTALL NODE.JS:

Go to official Node.js download page and download latest installer package based on your OS.

*Remember latest version number as mentioned on top of the download page, we'll use it in the next step.

After downloading the package and installing it, you should check to see if Node.js installed completely or not.


CHECK TO SEE IF PACKAGE INSTALLED COMPLETELY:

If you are on windows, open CMD and if you are on macOS open terminal and type:

node -v

Now you should see the version that you visited on Node.js downloads page, printed here.

Otherwise I put some tutorial below to try again:




Step 2: Arduino Part

In this step the only thing you need to do is upload a built-in Arduino IDE firmware on your Arduino Uno.

Important: You need to update your Arduino IDE to latest released version by Arduino.


Import firmata library:

Top Menu >> File >> Examples >> Firmata >> StandardFirmata


Verify and upload code on Arduino:

1. Top Menu >> Sketch >> Verify/Compile
2. Top Menu >> Sketch >> Upload</p></li></ol>

All done.



Step 3: Get Telegram Bot Token

Open Telegram and search for @BotFather or click here to open this bot on telegram.

  1. Type /newbot and hit enter
  2. Choose a name for your bot
  3. Choose a username for your bot. It must end in 'bot'
  4. Now bot sends you some info including your API access token. Write it down, we,ll use this token in next steps

Step 4: Create a Node.js Project

CREATE A NEW NODE.JS PROJECT

For create Node.js project and install modules we need to use terminal, thus if you are on Windows, use CMD and in case of using macOS use Terminal for all below steps.

1. Create a folder somewhere for project

2. CD(Change Directory) to recently created folder. For example if you named that folder 'TelegramBot' and put it on desktop, write this in terminal and hit enter:

cd Desktop/TelegramBot

3. For create Node.js project enter following command:

npm init

4. Answer some questions including name, description, license and etc. if you don't know what to answer, just hit enter. In this case the word between parentheses will be used as default value.

5.Now you should see 'package.json' file created in your folder and this means that you created Node.js project successfully.

Step 5: Coding

We need write some codes here. Let's describe some used libraries:

  1. Node Telegram Bot API to handle telegram bot API requests.
  2. Johnny-Five Platform to connect to Arduino.
All you need to know about these libraries can be found in their linked websites. we just use them in our code but the full documentation of them is on their blogs.

I use Microsoft Visual Studio Code for code editing. but you can use any other you like.

1. Open code editing software and create a file named 'index.js' (or anything you entered in entry point part of Node.js project creation step) in related directory.

2. Write code:

var five = require("johnny-five");
let TelegramBot = require('node-telegram-bot-api'); const token = '############################################'; const bot = new TelegramBot(token, { polling: true }); var board = new five.Board(); const answerCallbacks = {};

bot.on("message", function (msg) { const callback = answerCallbacks[msg.chat.id]; if (callback) { delete answerCallbacks[msg.chat.id]; return callback(msg); } });

board.on("ready", function () {

var led = new five.Led(13);

bot.on('message', (msg) => { const chatId = msg.chat.id; const text = msg.text; if (text == '/start') { start(chatId, led); } }); });

function start(chatId, led) { bot.sendMessage(chatId, "Arduino Control Panel", getKeyboardOptions());

bot.on("callback_query", (callbackQuery) => { const msg = callbackQuery.message; bot.answerCallbackQuery(callbackQuery.id) .then(() => { const data = callbackQuery.data; if (data == 'turnon') { led.on(); } else if (data == 'turnoff') { led.off(); } }) }); }

function getKeyboardOptions() { const options = { "reply_markup": { resize_keyboard: true, "inline_keyboard": [ [ { text: "Turn On", callback_data: "turnon", } ], [ { text: "Turn Off", callback_data: "turnoff", }

] ] } }

return options; }

3. Replace token property's value with one you wrote down in bot creation step

4. Save the code

5. Open terminal and enter following command:

npm i --save johnny-five node-telegram-bot-api

6. After installing modules, while your Arduino is connected to USB port, in terminal enter following command:

node index.js

7. You should see something like:

1534514872949 Available /dev/cu.usbmodem1411  
1534514872957 Connected /dev/cu.usbmodem1411
1534514876660 Repl Initialized<br></em>>></p>

8. Open telegram and search for your bot's username(or open it from BotFather) and enter this command:

/start

9. You should see a control panel that can control built-in Arduino LED with 'Turn on' and 'Turn off' commands, if you are lucky enough ;)

Share

    Recommendations

    • Metalworking Contest

      Metalworking Contest
    • Fix It! Contest

      Fix It! Contest
    • Audio Contest 2018

      Audio Contest 2018

    2 Discussions