User Rating: 5 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Active
 

Introductiontelegram doorbell

The purpose of this document is to illustrate how to configure and connect Domoticz/Raspberry controller to get a snapshot on our Telegram app when someone push the doorbell.

When people come to your home and press the doorbell switch, immediately a picture is grabbed from the IPCAM at your house and sent to your Telegram app on smartphone or computer.

Hardware

You must use Raspberry PI board with Domoticz software installed.

You must connect the doorbell pushbutton wires to a domoticz input switch (see next sections).

In the following examples, DomESP1 is a board, develped by us, with some analog/digital inputs, relay/photomos outputs, 1wire and I2C bus, where it's possibile to plug the NodeMCU V3 ESP8266 controller: it supports WiFi connections at 2.4GHz, and can be easily configured by a web browser; it's possible to define the parameters of the Domoticz controller so it will update the status of virtual inputs/outputs/sensors on Domoticz through a WiFi connection. In our case, if the doorbell switch wires arrives in another place than Domoticz/Raspberry room, it's possible to use this board without the need to install other wires to the Raspberry location.

Our products for Domoticz: Creasol DomESP1 and DomBus1

Using DomESP1 board with a doorbell pushbutton already used to ring the door bell

ToDo

Using DomESP1 board with a unconnected doorbell pushbutton

ToDo

Software

The following sections explain how to configure DomESP1 ESP8266 board and Domoticz to run a script that grab/resize a picture and send it by Telegram to your smartphone.
Also, explain how to get snapshot from IP CAM, even from silly cameras without the capture/snapshot feature.

Configure virtual input on Domoticz, associated with the doorbell switch

Connect, with the web browser, to Domoticz, click on Setup -> Hardware menu, then add a virtual device named "ESPEasy somename", and click on Add.

domoticz virtual sensor

 Select Switches panel, click on Manual Light/Switch button, and define the doorbell switch device. Don't care about Type, House and Unit Code! Then click on Add Device button

 domoticz virtual sensor3

Then, in the Switches panel, click on Edit button of the doorbell switch and define the On Action script as shown, then click on Save.

domoticz virtual sensor4

Finally, go to Setup -> Devices and keep note of the IDX for the newly created doorbell switch (42, in this example).

domoticz virtual sensor5

Now, connect to the DomESP1/EasyESP device, define the parameters of the domoticz controller

domesp1 controller

and finally define the parameters of the switch input and IDX of the virtual device on Domoticz.

domesp1 device

How to query ONVIF camera to get the video stream and snapshot URI

Some bad chinese ONVIF ipcams do not have snapshot URI: very bad, but we can generate a snapshot from the video stream, using ffmpeg!!

ONVIF is a standard protocol to query ONVIF ipcams, so it's possible to get ipcam characteristics sending some queries using SOAP system.

A very good tutorial to detect video and snapshot URI of your ONVIF camera can be found at http://me-ol-blog.blogspot.com/2017/07/getting-still-image-urluri-of-ipcam-or.html

How to get a Telegram token and chat_id, needed to send messages/pictures by Telegram

Telegram is a very powerful instant messaging system, similar to whatsapp, but much better because open-source, multi-platform (not only for smartphones....Telegram Desktop works on Linux, Mac, Windows!), and very customizable through its API.

The following instructions can be used to get a photo on Telegram when someone push the door bell button.

Below the step-by-step instructions to create a BOT on Telegram and a channel used to send notifications to you and other people:

  1. obviously, you must have telegram installed on your smartphone, and also Telegram Desktop on your PC/Mac/Linux : you can download it from https://desktop.telegram.org/
  2. search in you contact list for BotFather, select it and start it
  3. type /newbot
  4. type a name for this bot, for example domApi
  5. type a username, ending with "bot", for example domApiUserbot
  6. at this point, BotFather will return you an API key (below an example in bold) that you must store somewhere:
    784324329:EETRNJU3jQEGWQdjNv3llb4bnDSDREGuuuL
  7. Now, using your smartphone, create a new channel (not group: channel!), select a name for it (for example Domoticz), set it public and specify a link for it (must be unique), for example t.me/dom123abc
  8. Add members to this channel: your partner, sons, everyone that should receive domoticz notifications
  9. Enter the channel, and add a new administrator: select the bot that you just created at step 3-6 (search for domApiUserbot or the username typed at step 5)
  10. Add to that channel any other users you want, if you need to send picture/notifications to those users.
  11. Open the following URL from your browser (replace the API key with your, and channellink with your):
    https://api.telegram.org/bot
    784324329:EETRNJU3jQEGWQdjNv3llb4bnDSDREGuuuL/sendMessage?chat_id=@dom123abc&text=test
  12. You'll get an output like the following picture: store somewhere the ID (-1001194779203, in this example): this is the ID for your channel, where domoticz have to send notifications, and you have to write it (with the minus sign, if exist) in the sendsnapshot.sh script below (variable TELEGRAMCHATID).
    telegram channel id
  13. From Telegram on your smartphone, configure the created channel to set channel type Private.
  14. On Domoticz, go to Switches, select the doorbell button input, Edit, and write into the "On Action" text box script://sendsnapshot.sh  : in this way, every time people push the doorbell button, Domoticz will execute sendsnapshot.sh script that will grab a snapshot from the IPCam and send it to Telegram channel, so every user you have put in that channel will receive the photo of people that rang the door bell.

 

Script that gets a snapshot, or generate a snapshot from RTSP media stream, and send it by Telegram

The following script can be called by a scene event, on domoticz, to generate a snapshot jpeg picture and send it by Telegram.

It grabs a snapshot from an IP camera, extract the selected region of picture (to get a zoomed image of the useful area), and send that picture by Telegram.

It needs that ffmpeg, imagemagick and curl are installed in the linux system (debian, rasbian on raspberry, ....), executing the shell commands:

apt-get update; apt-get install imagemagick curl; apt-get install ffmpeg

File sendsnapshot.sh to be placed inside /home/pi/domoticz/scripts

#!/bin/bash
#/home/pi/domoticz/scripts/sendsnapshot.sh script

#telegramToken and chat_id: search on google how to activate a telegram API and bot.
TELEGRAMTEXT='Bell rang!'  #Caption text that will be sent with picture
TELEGRAMCHATID=91674205    #Telegram chatId (to send this picture/caption to: may be the ID associate with a user or a channel: see above
TELEGRAMSCRIPT='/home/pi/domoticz/scripts/telegramSendText.sh'
SNAPURL='http://192.168.1.170:8088/snap.jpg' #snapshot URL: comment with # prefix if camera does not support snapshot
MEDIAURL='192.168.3.201:554/mpeg4cif' #low quality video stream (do not care if SNAPURL is defined) #MEDIAURL='192.168.3.201:554/mpeg4' #high quality stream MEDIAUSER='admin' MEDIAPASS='camerapassword' PICTURE_REGION='500x200+228+40' #extract picture 500x200px starting from offset 228x+40y
PICTURE_SCALE='500' #width of picture that we want to get on Telegram PICTURE2_REGION='1400x600+0+40' #extract wider picture 1400x600px starting from offset 0x+40y. Set to '' if you do not need another picture with different geometry PICTURE2_SCALE='1400' #width of picture that we want to get on Telegram if [ -n "$SNAPURL" ]; then #IPCam supported snapshot curl -s -o /tmp/snapshot.jpg ${SNAPURL} #Get snapshot from IP camera (if supported) else #IPCam does not support snapshot #Use ffmpeg to extract snapshot from media stream (if camera does not support snapshot) ffmpeg -loglevel quiet -y -i rtsp://${MEDIAUSER}:${MEDIAPASS}@${MEDIAURL} -vframes 1 /tmp/snapshot.jpg fi #extract image region convert -extract ${PICTURE_REGION} -scale ${PICTURE_SCALE} /tmp/snapshot.jpg /tmp/snap.jpg #send picture by telegram datetime=`date "+%x %T"` ${TELEGRAMSCRIPT} ${TELEGRAMCHATID} "${datetime} - ${TELEGRAMTEXT}" /tmp/snap.jpg if [ -n "${PICTURE2_REGION}" ]; then convert -extract ${PICTURE2_REGION} -scale ${PICTURE2_SCALE} /tmp/snapshot.jpg /tmp/snap2.jpg #send picture by telegram
${TELEGRAMSCRIPT} ${TELEGRAMCHATID} "${datetime} - ${TELEGRAMTEXT}" /tmp/snap2.jpg fi

File telegramSendText.sh to be placed inside /home/pi/domoticz/scripts

#!/bin/bash
#File /home/pi/domoticz/scripts/telegramSendText.sh
telegramToken='671359246:AAG2v9DpxUpEla1ql0FiuGI1cRaVK7B7fzo'

function printsyntax () {
        echo "Syntax: $0 chatid text [picture]"
        echo "Send text, or text + picture, to telegram chat"
        echo "e.g. $0 123456789 \"Look at the photo....\" snaphost.jpg"
}

chat_id=$1
msg=$2
picture=$3
if [ $# -eq 2 ]; then
        curl -s --data chat_id=${chat_id} --data-urlencode "text=${msg}"  "https://api.telegram.org/bot${telegramToken}/sendMessage" >/dev/null
elif [ $# -eq 3 ]; then
        curl -s -X POST -F chat_id=${chat_id} -F photo="@${picture}" -F caption="${msg}" "https://api.telegram.org/bot${telegramToken}/sendPhoto" 
else
        printsyntax
fi

The scripts above must be own by user pi and have exec permission: cd /home/pi/domoticz/scripts; chown pi *sh; chmod +x *sh

Pin It