📜 ⬆️ ⬇️

Raspberry and Telegram: prerequisites for creating a smart home



One Italian shop groped for new ways to use the Telegram (and earlier WhatsApps) by installing it on the Raspberry Pi single-board miniature computer. As it turns out, the messenger can be used for remote communication with its own technology. Below is a translation of articles ( 1 , 2 ) from the Instructables.com site. If there are clarifications on the translation, write about it in the comments.

I. Telegram on Raspberry Pi
')
Step One: Installation

You will need: Raspberry Pi B or B +, with the latest version of Raspbian, or an 8GB Class 10 MIcroSD card with the same pre-installed Raspbian.

First upgrade the software packages:

sudo apt-get update 

 sudo apt-get upgrade 

Install the libraries: readline or libedit, openssl and (if you want to use the configuration) libconfig and liblua. If it is more convenient for you without them, put down --disable-libconfig and --disable-liblua respectively.

 sudo apt-get install libreadline-dev libconfig-dev libssl-dev lua5.2 liblua5.2-dev libevent-dev make 

Clone the GitHub archive

 git clone --recursive https://github.com/vysheng/tg.git && cd tg 

 ./configure 

 make 

The execution of this command will take some time, after which the installation will be completed.

Upd: run the messenger and set up an account by entering a phone number and (if) a nickname. Cm.

Step two: automatically send messages

To automatically send a message, create a file

 sudo nano /home/pi/tg.sh 

with this content:

 #!/bin/bash to=$1 msg=$2 tgpath=/home/pi/tg cd ${tgpath} (echo "msg $to $msg"; echo "safe_quit") | ${tgpath}/bin/telegram-cli -k tg-server.pub -W 

Save and close it, giving permission for actions:

 sudo chmod -R 0655 /home/pi/tg.sh 

Test it with

 /home/pi/tg.sh Name_lastname "your message" 





To send a photo, create a file

 sudo nano /home/pi/tg_photo.sh 

and put in it

 #!/bin/bash to=$1 msg=$2 tgpath=/home/pi/tg cd ${tgpath} (echo "send_photo $to $msg"; echo "safe_quit") | ${tgpath}/bin/telegram-cli -k tg-server.pub -W 

Save and close it, giving permission:

 sudo chmod -R 0655 /home/pi/tg_photo.sh 

and test with

 /home/pi/tg_photo.sh Name_Lastname /folder/photo.png 


Ii. Remote control of Raspberry Pi through Telegram



Telegram is a very versatile instant messenger in which you can work using one phone number simultaneously on several devices.

In the previous review we saw how to install it, how to send text and media files. We also made sure that Raspberry can be configured to automatically send messages via Telegram.

This time we will ask Raspberry to perform a specific action as a function of the message received: for example, we could send the word “photo” to send Raspberry a photo of us at home, or “lamp” to turn the lamp on, or “open” to open the door garage. Let's start.

Step One: Installation

We start on the basis of the actions described above. To intercept the new incoming message, we create the action.lua file (I omit the description of the Lua language with a link to the official website, as habraysers are obviously familiar with it. - Note lane ):

 sudo nano /home/pi/tg/action.lua 

with the following content:

 function on_msg_receive (msg) if msg.out then return end if (msg.text=='ping') then send_msg (msg.from.print_name, 'pong', ok_cb, false) end end function on_our_id (id) end function on_secret_chat_created (peer) end function on_user_update (user) end function on_chat_update (user) end function on_get_difference_end () end function on_binlog_replay_end () end 

Save and close the file. Now that the incoming message is “ping”, the Telegram responds with the message “pong”.

Go to tg

 cd /home/pi/tg 

and write:

 bin/telegram-cli -k tg-server.pub -W -s action.lua 

Try sending a message. If everything is correct, Telegram responds only to "ping" (not to "PING"), and we should see something like this:





OK, let's try something more amusing.
Install a Raspberry camera (see this tutorial) and create a new folder where photos from this camera will be saved by default.

 sudo mkdir /home/pi/camera 

Create a new “camera.ch” file

 sudo nano /home/pi/camera/camera.sh 

here with this stuffing

 #!/bin/bash raspistill -w 800 -h 600 -o /home/pi/camera/photo.jpg 

Save and close it, allowing action.

 sudo chmod -R 0655 /home/pi/camera/camera.sh 

Edit “action.lua”

 sudo nano /home/pi/tg/action.lua 

and add these lines to the on_msg_receive function:

 if (msg.text=='photo') then os.execute('/home/pi/camera/camera.sh') send_photo (msg.from.print_name, '/home/pi/camera/photo.jpg', ok_cb, false) end 



Step Two: Test





 bin/telegram-cli -k tg-server.pub -W -s action.lua 

Now, if you send the word "photo", Raspberry will respond with a photo.

To specify additional commands, simply modify the “action.lua” file by adding a new “if” block. For example, you can activate a switch or request an alarm status.

Source: https://habr.com/ru/post/249265/


All Articles