Create a Web Bot Server using NodeMCU devkit v1.0 board via Telegram

in #utopian-io8 years ago (edited)

What Will I Learn?

  • You will learn how to create a simple Bot server using NodeMCU devkit v1.0 board.
  • You will learn how to configure a Bot to match a reply to a specific chat of a user from Telegram using NodeMCU devkit v1.0 board.
  • You will learn how to link your NodeMCU devkit v1.0 board to Telegram by using the Internet.

Requirements


Materials Needed

  • NodeMCU devkit v1.0
  • USB Cable (Type A to Mini B)
  • Computer


Software Applications

  • Open-Source Arduino Software (IDE) / Arduino IDE
  • Telegram Messenger App for Android or you can use the desktop version of Telegram.

Difficulty

  • Intermediate


Description:


Image References: 1, 2, 3

An Internet Bot, otherwise called web robot, WWW robot or essentially bot, is a product application that runs computerized errands (contents) over the Internet. Typically, bots perform undertakings that are both basic and fundamentally dreary, at a considerably higher rate than would be exhausting for a human alone. The biggest utilization of bots is in web crawling (web crawler), in which a computerized content gets, investigates and records data from web servers at ordinarily the speed of a human. The greater part of all web movement is comprised of bots. In this tutorial, we will use the IoT platform of the NodeMCU devkit v1.0 board by creating a server for our chat Bot in Telegram. This server will determine the anticipated response of our Bot in a specific command or chat of a user that chats with our Bot.
References

For further research about NodeMCU devkit v1.0. Here is its official website

Tutorial Contents



"Create a Web Bot Server using NodeMCU devkit v1.0 board via Telegram"


Step 1:
"Downloading the ESP8266 Boards
with its Necessary Files and the ESP8266TelegramBot library"




Open Arduino IDE then, check if you have the ESP8266 library from your installed libraries . This is necessary to configure the NodeMCU to run on Arduino IDE.

Note: Make sure that you have already downloaded and installed the necessary files to run the NodeMCU devkit v1.0. If you are still starting to use ESP8266 boards in Arduino IDE and you haven't install or download them, kindly click here. And follow the first step of my tutorial there.

To make this tutorial, we need to install the ESP8266TelegramBot library. So, go to Sketch tab and click on Include libraries > Manage libraries. And search for the ESP8266TelegramBot library and install it.


Actual Photo
Step 2:
"Talking to BotFather in Telegram"

Open your Telegram Messenger app and click on the search button. Search for the BotFather and start the conversation. Ask for a new bot by replying /newbot. You have to pick a unique name and username for your bot. When you are done, the BotFather will give you a token that you will use to access the HTTP API to connect to your Bot.


Actual Photo


Actual Photo

Step 3:

"Programming your Board"

When you have finished downloading and installing the necessary files to run ESP8266 boards like the NodeMCU devkit v1.0 board, it would be required to close the Arduino IDE then, open it after a minute. It is good practice to do, so as to allow the new libraries to be loaded for setup when you open the Arduino IDE again, in some instances to avoid errors.

After, go to Tools tab and change the board to NodeMCU 1.0 (ESP-12E Module).

Then, click on File> New, to create a new sketch in Arduino IDE.


Actual Photo

On your new sketch in Arduino IDE, input this program:

#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <ESP8266TelegramBOT.h>
const char *ssid = "HUAWEI Y5 2017";  //Change this to your WiFi SSID
const char *password = "12345678";  // Change this to your WiFi password


//Static IP details IPAddress ip(192, 168, 43, 155); IPAddress gateway(192, 168, 43, 1); IPAddress subnet(255, 255, 255, 0); IPAddress dns(192, 168, 43, 1);

// Initialize Telegram BOT #define BOTtoken "487302481:AAH5A3uGw8aI7fNA7UDrGeVgTkwdgBKh6JE" //token of Japh_ChatBot #define BOTname "Japh" #define BOTusername "Japh_ChatBot" TelegramBOT bot(BOTtoken, BOTname, BOTusername); int BOT_MEANTIME = 1000; //mean time between scan messages long BOT_LASTTIME; //last time messages' scan has been done bool Start = false;

void Bot_ExecMessages() { String A = "/start"; String B = "/ledon"; String C = "/ledoff"; for (int i = 1; i < bot.message[0][0].toInt() + 1; i++) { bot.message[i][5]=bot.message[i][5].substring(1,bot.message[i][5].length()); Serial.println(String(bot.message[0][0])); if (String(bot.message[i][5])== B) { digitalWrite(LED_BUILTIN, LOW); // turn the LED on (HIGH is the voltage level) bot.sendMessage(bot.message[i][4], "Led is ON", ""); } if (String(bot.message[i][5]) == C) { digitalWrite(LED_BUILTIN, HIGH); // turn the LED off (LOW is the voltage level) bot.sendMessage(bot.message[i][4], "Led is OFF", ""); } if (String(bot.message[i][5]) == A) { Serial.println("Welcome string equal"); String wellcome = "Wellcome from Japh, your personal Bot on NodeMCU devkit v1.0 board. My master is @japh. A handsome Utopian.io Contributor"; String wellcome1 = "/ledon : to switch the Led ON"; String wellcome2 = "/ledoff : to switch the Led OFF"; bot.sendMessage(bot.message[i][4], wellcome, ""); bot.sendMessage(bot.message[i][4], wellcome1, ""); bot.sendMessage(bot.message[i][4], wellcome2, ""); Start = true; } } bot.message[0][0] = ""; // All messages have been replied - reset new messages }

void setup() { Serial.begin(115200); delay(3000); // attempt to connect to Wifi network: Serial.print("Connecting Wifi: "); Serial.println(ssid); //Static IP Setup Info Here... WiFi.config(ip,gateway,subnet,dns); WiFi.begin(ssid, password); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); // Print the IP address Serial.println(WiFi.localIP()); bot.begin(); // launch Bot functionalities pinMode(LED_BUILTIN, OUTPUT); // initialize digital pin 2 as an output. }

void loop() { if (millis() > BOT_LASTTIME + BOT_MEANTIME ) { bot.getUpdates(bot.message[0][1]); // launch API GetUpdates up to xxx message Bot_ExecMessages(); // reply to message with Echo BOT_LASTTIME = millis(); } }

The program basically scans for updates on the messages that are being sent to your Bot by streaming the new messages on Telegram thru HTTP API. This is the use of the token that we had from BotFather in Telegram.
On the code #define BOTtoken, we input the token our Bot to access the HTTP API. Other credentials for the verification of our Bot are included in #define BOTname and #define BOTusername. On the function Bot_ExecMessages(), the scan of message for our Bot is done every second as defined by the code int BOT_MEANTIME = 1000; above it.
For the Bot to have a unique reply to a specific command by a user from Telegram, we used string comparisons. We compared the message for the bots harnessed thru the code bot.message[i][5] and the string variables A,B,C. As an example; when a user initiates a chat with the Bot by a command of /start on Telegram, it is compared to the string variable “A” that we have declared. If two strings are equal, then the condition will be true and thus initializing the bot.sendMessage(); to send replies as written on the program.
This program utilize the built-in LED of the NodeMCU devkit v1.0 board for the /ledon and /ledoff.
In order for the chat server to have wireless connection to the clients, the board must connect to a wireless access point or to a hotspot. The SSID and the password of the access point it would connect is defined by the code const char* MY_WIFI_SSID = "HUAWEI Y5 2017"; and const char* MY_WIF_PASSWORD = "12345678";. This should be changed based on the configuration of the wireless access point, you are using.

Now, connect your NodeMCU devkit v1.0 to your computer using the USB cable (Type A to Mini B).


Actual Photo

Go to the device manager then, identify its port number if it is your first time to use this board. Now, go the Arduino IDE and click Tools > Port and look for the port of your NodeMCU devkit v1.0.


Actual Photo

Input the program above properly. And click Verify/Compile when you are done with the program to check for errors.I have already compiled this program and it has no errors based on my screenshot. If compiling your code is unsuccessful, try comparing your program to my program in this tutorial. You may have missed something.


Actual Photo

If your program has no errors after you compiled, click on Upload. If uploading your code is unsuccessful, check if you are using the right port.


Actual Photo

Step 4:

"Testing your System"

Before testing, make sure that you have already turned on the Wi-Fi hotspot which the NodeMCU board connects as specified on the program above.

1.Open your Telegram Messenger App. Search for the name of your Bot on the search tab and start a conversation. When you send /start, the response of the Bot is as shown below.


Actual Photo

2. Keep an eye on the built-in LED of the NodeMCU board and on the Telegram Messenger App, send /ledon. The built-in LED of the NodeMCU must glow and your Bot should reply you with "Led is on".


Actual Photo

3. Now, we will turn off the built-in LED. On the Telegram Messenger App, send /ledoff. The built-in LED must be off and your Bot should reply you with "Led is off."


Actual Photo

Now, we have successfully created a Web Bot Server using NodeMCU devkit v1.0 board via Telegram

Curriculum



Auxiliary Wireless TV Power Remote Control using NodeMCU devkit v1.0
Make Your Own Web Server using NodeMCU devkit v1.0
External GPIO Pin State Monitoring using HTTP Client Request for NodeMCU Devkit v1.0 Boards
Web-controlled Servomotor using NodeMCU devkit v1.0 board
Intruder Alarm System with Automatic Online Updates using NodeMCU devkit v1.0
Online Temperature Monitor using NodeMCU devkit v1.0
Water Overflow Alarm with Online Updates using NodeMCU devkit v1.0
Set up Your Own Wireless Access Point using NodeMCU devkit v1.0
Create a Local Chat Server using NodeMCU devkit v1.0 board via Telnet



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Hey @japh I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Thank you, sir @creon.

Coin Marketplace

STEEM 0.04
TRX 0.33
JST 0.102
BTC 63861.21
ETH 1783.63
USDT 1.00
SBD 0.39