Description

The principle of operation:

It's done with  Python + Bash + Telegram Bot.


Preparation

For this you will need an RPI3 with the Ubuntu Mate installed. You can get it here: https://ubuntu-mate.org/download/

Installing ALPR library

The easiest way to install

sudo apt-get update && sudo apt-get install -y openalpr openalpr-daemon openalpr-utils libopenalpr-dev

Install ImageMagick

sudo apt-get install imagemagick

Installing dependencies for Telegram bot

sudo apt-get install libreadline-dev libconfig-dev libssl-dev lua5.2 liblua5.2-dev libevent-dev libjansson-dev libpython-dev make 
apt-get update
apt-get -y install python-pip 
pip install pytelegrambotapi 
pip install json

Installing Telegram-cli

Check this link to install and configure telegram-cli in ubuntu.

Relay connection

You can test the relay with shell script, just create a simple sh script and run it:

nano open.sh
chmod +x open.sh
sh open.sh

Paste the code below and run it.

Script code for relay 1:

#!/bin/bash 
echo 23 > /sys/class/gpio/export 
echo out > /sys/class/gpio/gpio23/direction 
echo 0 > /sys/class/gpio/gpio23/value 
ping -c 2 localhost 
echo 23 > /sys/class/gpio/export 
echo out > /sys/class/gpio/gpio23/direction 
echo 1 > /sys/class/gpio/gpio23/value

Script code for relay 2:

#!/bin/bash 
echo 26 > /sys/class/gpio/export 
echo out > /sys/class/gpio/gpio26/direction 
echo 0 > /sys/class/gpio/gpio26/value 
ping -c 2 localhost 
echo 26 > /sys/class/gpio/export 
echo out > /sys/class/gpio/gpio26/direction 
echo 1 > /sys/class/gpio/gpio26/value


Connect relays to gates

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

Telegram photo-send script

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

Create 2 files for the main recognition script:

touch log.txt
touch numbers.txt

In the numbers.txt - add all the known car plates numbers.

Order of the numbers.txt should be as follows:

YZO169

RFS961

RFS123

ILAI123

RFS872

Car plate recognition script

#! /bin/bash 
wget -q http://login:password@ipadress/cgi-bin/snapshot.cgi -O /home/user/Downloads/autocheck/snapshot.cgi 
mv /home/user/Downloads/autocheck/snapshot.cgi /home/user/Downloads/autocheck/snapshot.jpg 
convert /home/user/Downloads/autocheck/snapshot.jpg -gravity Center -crop 45%\! /home/user/Downloads/autocheck/output.jpg 
FILENAME=$(alpr -c eu -d -n 1 /home/user/Downloads/autocheck/output.jpg |awk '{print $2}' | awk 'FNR>=2 && FNR<=4') 
if grep -Fxq "$FILENAME" /home/user/Downloads/autocheck/numbers.txt 
then 
echo CAR FOUND!, opening the gates! | while IFS= read -r line; do echo "$FILENAME, $(date) $line"; done >>/home/user/Downloads/autocheck/log.txt 
sh /home/user/Downloads/autocheck/relay-control/open.sh 2> /dev/null 
sh /home/user/Downloads/autocheck/tg.sh xGates "Car Found! Welcome" > log.out 2> /dev/null 
sh /home/user/Downloads/autocheck/tg.sh xGates  $FILENAME > log.out 2> /dev/null 
sh /home/user/Downloads/autocheck/tgphoto.sh xGates /home/user/Downloads/autocheck/output.jpg > log.out 2> /dev/null 
sleep 20 
sh /home/user/Downloads/autocheck/relay-control/open.sh 2> /dev/null 
elif [ -n "$FILENAME" ];  #if not empty 
then 
echo Unregistered car is at the gates! | while IFS= read -r line; do echo "$FILENAME, $(date) $line"; done >>/home/user/Downloads/autocheck/log.txt 
sh /home/user/Downloads/autocheck/relay-control/open.sh 2> /dev/null 
sh /home/user/Downloads/autocheck/tg.sh xGates "Unregistered car is at the gates!" > log.out 2> /dev/null 
sh /home/user/Downloads/autocheck/tg.sh xGates  $FILENAME > log.out 2> /dev/null 
sh /home/user/Downloads/autocheck/tgphoto.sh xGates /home/user/Downloads/autocheck/output.jpg > log.out 2> /dev/null 
sleep 20 
sh /home/user/Downloads/autocheck/relay-control/open.sh 2> /dev/null 
else 
2> /dev/null 
fi 
sleep 1 
exec bash "$0"  

Telegram bot and script

Open Telegram app, search for @BotFather and start the chat. Send /newbotcommand and follow the instructions. After completing the initial steps, you’ll get your TOKEN

# -*- coding: utf-8 -*- 
import config 
import telebot 
import os 
import time 
import requests 
import subprocess 
import simplejson 
import sys 
from telegram import InlineKeyboardButton, InlineKeyboardMarkup 
from telebot import types 
bot = telebot.TeleBot(config.token) 
@bot.message_handler(content_types=["text"]) 
def any_msg(message): 
   keyboard = types.InlineKeyboardMarkup() 
   callback_button1 = types.InlineKeyboardButton(text="Gate-1", callback_data="open") 
   callback_button2 = types.InlineKeyboardButton(text="Gate-2", callback_data="open2") 
   callback_button3 = types.InlineKeyboardButton(text="Photo", callback_data="photo") 
#    callback_button3 = types.InlineKeyboardButton(text="Add car", callback_data="add") 
   keyboard.add(callback_button1,callback_button2,callback_button3) 
   bot.send_message(message.chat.id, "Welcome to the xGates", reply_markup=keyboard) 
@bot.callback_query_handler(func=lambda call: True) 
def callback_inline(call): 
   if call.message: 
       if call.data == "open": 
           os.system("cat </dev/null |sh /home/user/Downloads/autocheck/relay-control/open.sh 2>/dev/null") 
   if call.message: 
       if call.data == "open2": 
           os.system("cat </dev/null |sh /home/user/Downloads/autocheck/relay-control/open2.sh 2>/dev/null") 
   if call.message: 
       if call.data == "photo": 
           os.system("sh /home/user/Downloads/autocheck/tgphoto.sh xGates /home/user/Downloads/autocheck/output.jpg > log.out 2> /dev/null") 
   if call.message: 
       if call.data == "close": 
           os.system("cat </dev/null |sh /home/user/Downloads/autocheck/relay-control/close.sh 2>/dev/null") 
   if call.message: 
       if call.data == "add": 
           os.system("cat </dev/null |sh /home/user/Downloads/autocheck/relay-control/add.sh 2>/dev/null") 
def run(self): 
       while True: 
           self.updates() 
           time.sleep(1) 
if __name__ == '__main__': 
   bot.polling(none_stop=True) 
# -*- coding: utf-8 -*- 
token = 'TOKEN' 

Run it

Check all the paths in scripts and run.

sh check.sh
python bot.py

*I hope that I did not miss anything.

If you have any questions - do not hesitate to ask!