TELEBOT Complete Home Automation
TELEBOT Complete Home Automation
by Karanveer105
In this Instructable I'am going to tell you about how to make a complete "HOME AUTOMATION" package using
some basic Electronic controllers and some Open source software.
The Relays here will help controlling the switches while the IR sensor will be used to control the TV.
Also this package includes a security feature named as "CamShot" which will click photos for you whenever a
signal is send to it.
Components:
Tools:
Cutter
Glue Gun
Knife
Solder Wire
Soldiering Iron
Software:
Raspbian
Node.js
Arduino IDE
Telegram API
Telegram App (Any Device)
Go to App Store (iPhone) or Play Store (Android), Now, you can use Telegram but not yet for the
download and install Telegram on your phone or Raspberry Pi. Telegram reserves a special kind of
Open this link here accounts for machines, called bot accounts. As the
owner of your own Pi, you have to obtain a bot
Here we are using Telegram as a "Chat Bot" to give account for it. So in the next step we will learn how to
instructions to Raspberry Pi based on the control get your own Bot on telegram.
actions we want.
Steps:
3. Setting up Pi:
Please remember that after booting up there might be some situations where the user credentials
are required. Raspberry Pi comes with default username's "login" & "password".
Now when the pi is booted for the first time, a configuration screen called "Setup Options" should
appear.
Don't worry if you have missed this, you can still get it by typing the following command in the
terminal.
sudo raspi-config<br>
Now that the Setup Options windows is up, we will have to set up few things. After completing each
step if it asks to reboot the pi than please do so. After rebooting if you don't get the Setup option
then follow the above step to get the setup option screen.
a.) First thing to do : Click on the first option that is "Expand Files" and Enter. We do this to make
use of full space available on the SD card as a full partition. It will expand the OS to fit in the total
space available in the SD card which can than be used as a memory storage for the pi.
b.) Second thing to do : Select the third option " Enable boot to desktop/scratch" and enter. It will
take you to another window called the "choose boot option" window. Now select the option of "
Desktop Log in as user 'pi' at the graphical desktop and hit the enter button.Once done you will be
taken back to the Setup Option window and if not then click the "ok" button present at the bottom
and it will get back to the previous window.
Once both the steps are done, click the finish button at the bottom and then it should start rebooting
automatically. If not then enter the following command in the terminal to reboot
sudo reboot
After the reboot from the previous stage if everything went right, you will end up on the desktop.
Once you are on the desktop, open the terminal and enter the the following command to update the firmware of the
pi.
sudo rpi-update
Steps:
http://arduino.esp8266.com/stable/package_esp8266com_index.json
4. Click OK to close the preference tab.
5. After completing the above steps go to Tools > Boards and then select the board manager.
6. Navigate to esp8266 by esp8266 community and install the software for Arduino.
7. Once all the above steps are completed we are ready to program our NodeMcu.
To Communicate between Raspberry Pi and Node MCU we need a static IP For each NodeMcu. So Raspberry Pi
can send HTTP Request to Each Module.
For that Purpose we need to configure the Router that every time any of the one Node MCU will connect to it will
assign it a pre defined IP.
Firstly connect the pi-cam with Raspberry pi 0 using the connector cable.
Now if you are using raspberry 3 than connect the pi-cam directly with the connector which comes with it.
But if you are using Raspberry pi 0 than you will be needing a special type of connector.
console.log("BOT ready!");
});
bot.onText(/\/start/, function onPhotoText(msg) {
// From file path
bot.sendMessage(msg.chat.id, 'Your User Id is '+ msg.chat.id +'', opts);
});
function isAuthorized(userid) {
for(i = 0; i < authorized_users.length; i++)
if(authorized_users[i ] == userid) return true;
return false;
}
This Bot Code is Written in node.js, So in your Raspberry Pi You Must Have Installed npm and Node.js.
node App.js
8. So Now Your Bot is working.
9. Now go to Your Phone or Laptop and open telegram app and send Start to the Bot.
10. The if Your Bot is working Properly you will receive your User ID.
11. Now Copy Paste This User ID in the Code.
var authorized_users = [
XXXXXXXXX, //Your User Id Here
XXXXXXXXX,
XXXXXXXXX
];
12. Here Replace XXXX with the User ID You have received.
13. Now Restart the Code via Pressing " CTRL + C " and then Again " node App.js ".
As You can See Every time user sent message to bot via bot.ontext method we detect the message and do the
preferred actions.
if(!isAuthorized(msg.from.id)) return;
request('http://192.168.1.106/r1on', function (error, response, body) {
});
bot.sendMessage(msg.chat.id, 'Switched on Relay 1', opts);
});
Like for the given example we checked that if the message received from User is switch1_on then it will check
wheter this user is authorized or not and then finally do the action by sending HTTP Request to Nod MCU on it's
specific IP Address.
So if You Want to Add Your Own Cases You can add.. :-)
(Note : After Adding the new case in Main Code add the Appropriate action for the same HTTP Request in your
Node MCU Code.)
We had done adding Cases for specific inputs from User. Now As you can to turn the light on or off every time you
So For Your Regular Use You can set a special Keyboard For Your Bot Using Telegram API.
You don't have to do much for this, just change the switched in below code and restart the bot.and the next time
bot will show the keyboard in new order and with new Switches.
<p>const opts = {
reply_markup: JSON.stringify({
keyboard: [
['/switch1_on','/switch1_off'],
['/switch2_on','/switch2_off'],
['/switch3_on','/switch3_off'],
['/switch4_on','/switch4_off'],
['/switch5_on','/switch5_off'],
['/switch6_on','/switch6_off'],
['/switch7_on','/switch7_off'],
['/switch8_on','/switch8_off'],
['/all_switchon','/all_switchoff'],
['/hum','/temp','/getouts'],
['/power','/mute'],
['/ch_0','/ch_1'],
['/ch_2','/ch_3'],
['/ch_4','/ch_5'],
['/ch_6','/ch_7'],
['/ch_8','/ch_9'],
['/tv'],
['/ch_up','/ch_down'],
['/vol_up','/vol_down'],
['/camshot']
]
})
};
As You can see by setting this opt constant you can set the Inline keyboard.
If You don't want to mess up with the code and just want to set some specific shortcuts you can set
it by Using BotFather.
Connections:
The Vin and Vcc of the 8 channel relay board are connected with the positive and negative terminal of the bread
board power supply.
Also to power the NodeMcu the other pair of supplies of power supply is connected with the ground and vcc.
the positive terminal all the relays are connect with D0 to D7 pins and the negative terminal is provided a common
ground.
https://www.youtube.com/watch?v=4k1KOAZIQqk
#include <ESP8266WiFi.h>
WiFiClient client;
WiFiServer server(80);
const char* ssid = "username";
const char* password = "password";
String command =""; // Command received from Android device
void setup()
{
Serial.begin(115200);
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(relay4, OUTPUT);
pinMode(relay5, OUTPUT);
pinMode(relay6, OUTPUT);
pinMode(relay7, OUTPUT);
pinMode(relay8, OUTPUT);
digitalWrite(relay1,LOW);
digitalWrite(relay2,LOW);
digitalWrite(relay3,LOW);
digitalWrite(relay4,LOW);
digitalWrite(relay5,LOW);
digitalWrite(relay6,LOW);
digitalWrite(relay7,LOW);
digitalWrite(relay8,LOW);
connectWiFi();
server.begin();
}
void loop()
{
client = server.available();
if (!client) return;
command = checkClient ();
/* connecting WiFi */
void connectWiFi()
{
Serial.println("Connecting to WIFI");
WiFi.begin(ssid, password);
while ((!(WiFi.status() == WL_CONNECTED)))
{
delay(300);
Serial.print("..");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("NodeMCU Local IP is : ");
Serial.print((WiFi.localIP()));
}
As Earlier we had configured the Raspberry Pi App Code to Send Request to All Node MCU According to User
Input in Telegram App.
Now when that request came to Relay Module Containing Node MCU, So in this code we have to define the GPIO
action to turn it on or off according to Pi's HTTP Request.
1. First we have to download ESP8266 Library and then will upload this code to raspberry pi.
2. Download it from Here : Click Here to Download
3. Now Add this Library to Arduino IDE and then Open New File.
4. Paste the Code from Last Step
Here First You have to change SSID and Password according to your Router's Credentials .
6. This Code Constantly Checks for HTTP Request and pass it to command.
7. Everytime any command received check it using nested if else and do proper action
8. Time to Upload it on the Node MCU.(Don't Forget to Download Board Manager for Node MCU as Mentioned
Before. )
Connections: Module.
The positive terminal of the LED is connected with D3 So by sending particular code to the IR LED we can
pin. control the Appliances. In this example we have
showed that how you can distantly switch on and off
And the negative terminal is connected to the ground your tv and also showed some other common
features like switching over to other channels and
This is IR Emiiter Which Emits the IR Singals which volume options.
are used to Control Appliances which uses IR
#include <IRremoteESP8266.h>
#include <ESP8266WiFi.h>
#include <IRsend.h>
WiFiClient client;
WiFiServer server(80);
const char* ssid = "user";
const char* password = "password";
String command =""; // Command received from Android device
void setup()
{
irsend.begin();
Serial.begin(9600);
connectWiFi();
server.begin();
}
void loop() {
client = server.available();
if (!client) return;
command = checkClient ();
// Check Which Command is received and Do Action According to It.
if (command == "power") irsend.sendSony(0xA90, 12, 2);
else if (command == "s1") irsend.sendSony(0x10, 12, 2);
else if (command == "s2") irsend.sendSony(0x810, 12, 2);
else if (command == "s3") irsend.sendSony(0x410, 12, 2);
else if (command == "s4") irsend.sendSony(0xc10, 12, 2);
else if (command == "s5") irsend.sendSony(0x210, 12, 2);
else if (command == "s6") irsend.sendSony(0xa10, 12, 2);
else if (command == "s7") irsend.sendSony(0x610, 12, 2);
else if (command == "s8") irsend.sendSony(0xe10, 12, 2);
else if (command == "s9") irsend.sendSony(0x110, 12, 2);
else if (command == "s0") irsend.sendSony(0x910, 12, 2);
else if (command == "vup") irsend.sendSony(0x490, 12, 2);
else if (command == "vdown") irsend.sendSony(0xc90, 12, 2);
else if (command == "pup") irsend.sendSony(0x90, 12, 2);
else if (command == "pdown") irsend.sendSony(0x890, 12, 2);
else if (command == "tv") irsend.sendSony(0xA50, 12, 2);
else if (command == "mute") irsend.sendSony(0x290, 12, 2);
sendBackEcho(command); // send command echo back to android device
command = "";
Serial.println(command);
/* connecting WiFi */
void connectWiFi()
{
Serial.println("Connecting to WIFI");
WiFi.begin(ssid, password);
while ((!(WiFi.status() == WL_CONNECTED)))
{
delay(300);
Serial.print("..");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("NodeMCU Local IP is : ");
Serial.print((WiFi.localIP()));
}
This Module Code is also similar to the Relay Module Code. But there is a slight change because in this Module
we are using and IR Sensor. So as the command will be received from Pi it will send appropriate signal to IR
Sensor using Digital Pin.
6. Here Also the Module will listen for HTTP Request from Pi and Do Appropriate action.
7. Like for Power Command it will send 0xA90 to the IR and for s1 it will send 0x10.
8.So if you want to add more function just add more case in if else statement.
9. If You Do Have Sony TV You Don't have to do anything. Just Upload the code and Enjoy :-)
10. But if You have any other TV You Have to Change the Code. Replace irsend.sendSony with your TV
Company name.
11. And you also have to change the IR Codes Placed in the function. ex. 0xA90 For Power in Sony
12. You can find this codes online for any TV. Just Search for "IR Codes Samsung" or any company name.
So its a feature which will help you get pictures of your home when you aren't present at your home.
Actually due to security concerns we hadn't shown images from proper angle but you can change the angle as
your home situation.
Awesome project
Could this be built without implementing the "telegraph" section? I'm interested in a lan / wan
version with a simple web interface on the pi :)
Yes You Can create a simple HTML Page with Js Script Which sends specific HTTP request to
Node MCU on Button Click.
Even I am working on an app to control this so will update this instructable soon..
Great work
Can You Live Stream the Picam on Any Browser or in any App ?
Yes, Currently we are working on it and as a addon we are adding support to control direction of
camera module on 4- axis using servos.
Awesome work
Very nice
Thanks Ashish :)