📜 ⬆️ ⬇️

How to make a Telegram bot that opens the door with an electromagnetic lock using Go lang, Raspberry Pi and a soldering iron

image

By the end of September 60, September was nearing, and I, being a 16-year-old schoolboy, decided to do something interesting and useful with my own hands. In the company in which I am an intern, there is a door with an electromagnetic lock, which is opened with the help of an electronic key fob or the button that is located inside. It was inconvenient to use these key chains, besides, they were often lost. And I was offered to make Telegram bot, which will open this door.

The principle of operation is very simple:
')

For a start, I placed the RPi in the closet where the router and modem are located, and connected it to the Internet. Then assembled the transistor key:


The scheme looks like this:

Key scheme

How was going

How was going

Then I had to write the code:

Code
package main import ( //   "log" "io/ioutil" "path/filepath" "strings" "time" //  yaml  "gopkg.in/yaml.v2" //     rpi "github.com/stianeikeland/go-rpio" //     telegram API "github.com/Syfaro/telegram-bot-api" ) type Config struct { //    Token string `yaml:"token"` //    AllowedChatIds []int `yaml:"allowed_chat_ids"` //      OpenDoorPhrases []string `yaml:"open_door_phrases"` TurnLedOnPhrases []string `yaml:"turn_led_on_phrases"` TurnLedOffPhrases []string `yaml:"turn_led_off_phrases"` } var bot *tgbotapi.BotAPI var config *Config var OpenDoorPhrases []string var TurnLedOnPhrases []string var TurnLedOffPhrases []string var AllowedChatIds []int var doorOpened chan *tgbotapi.Message var ledTurnedOn chan *tgbotapi.Message var ledTurnedOff chan *tgbotapi.Message var doorPin = rpio.Pin(10) var ledPin = rpio.Pin(9) func readConfig() (*Config, error) { var yamlFile []byte var err error filename, _ := filepath.Abs("./config.yml") yamlFile, err = ioutil.ReadFile(filename) if err != nil { return nil, err } var conf Config if err := yaml.Unmarshal(yamlFile, &conf); err != nil { return nil, err } return &conf, err } func main() { var err error //   if config, err = readConfig(); err != nil { panic(err) } //   bot, err = tgbotapi.NewBotAPI(config.Token) if err != nil { log.Panic(err) } //    gpio  rpi if err = rpio.Open(); err != nil { log.Panic(err) } defer rpio.Close() //    output ledPin.Output() doorPin.Output() //     doorOpened = make(chan *tgbotapi.Message) ledTurnedOn = make(chan *tgbotapi.Message) ledTurnedOff = make(chan *tgbotapi.Message) AllowedChatIds = config.AllowedChatIds OpenDoorPhrases = config.OpenDoorPhrases TurnLedOnPhrases = config.TurnLedOnPhrases TurnLedOffPhrases = config.TurnLedOffPhrases log.Printf("Authorized on account %s", bot.Self.UserName) var ucfg tgbotapi.UpdateConfig = tgbotapi.NewUpdate(0) ucfg.Timeout = 60 err = bot.UpdatesChan(ucfg) //   go Listen() ListenUpdates() } func OpenDoor() chan<- *tgbotapi.Message { //   go launchDoor() return (chan<-*tgbotapi.Message)(doorOpened) } func TurnLedOn() chan<- *tgbotapi.Message { //   ledPin.High() return (chan<-*tgbotapi.Message)(ledTurnedOn) } func TurnLedOff() chan<- *tgbotapi.Message { //   ledPin.Low() return (chan<-*tgbotapi.Message)(ledTurnedOff) } //   func launchDoor() { log.Println("door is beeing opened") doorPin.High() ledPin.High() time.Sleep(100*time.Millisecond) doorPin.Low() ledPin.Low() } // ,      func tryToDo(text string, phrases []string) bool { for i:=0; i<len(phrases); i++ { if strings.ToLower(text) == phrases[i] { return true } } return false } // ,      func auth(chatId int) bool { for i:=0; i<len(AllowedChatIds); i++ { if chatId == AllowedChatIds[i] { return true } } return false } //   func send(chatId int, msg string) { log.Println(msg) bot_msg := tgbotapi.NewMessage(chatId, msg) bot.SendMessage(bot_msg) } func Listen() { for { select { case msg := <- doorOpened: reply := msg.From.FirstName + " () " send(msg.Chat.ID, reply) case msg := <- ledTurnedOn: reply := msg.From.FirstName + " () " send(msg.Chat.ID, reply) case msg := <- ledTurnedOff: reply := msg.From.FirstName + " () " send(msg.Chat.ID, reply) } } } func ListenUpdates() { for { select { case update := <-bot.Updates: userName := update.Message.From.UserName chatID := update.Message.Chat.ID text := update.Message.Text //       if !auth(chatID) { reply := "   " log.Println(reply) bot_msg := tgbotapi.NewMessage(chatID, reply) bot.SendMessage(bot_msg) continue } log.Printf("[%s] %d %s", userName, chatID, text) //     , /  if tryToDo(text, OpenDoorPhrases) { OpenDoor() <- &update.Message } if tryToDo(text, TurnLedOnPhrases) { TurnLedOn() <- &update.Message } if tryToDo(text, TurnLedOffPhrases) { TurnLedOff() <- &update.Message } } } } 


As for config.yml; This is just a config:

 token: <secret token> allowed_chat_ids: - <id1> - <id2> open_door_phrases: - open - open the door - open door - door open -   -   -  -  -  -  - -,  - abrete sesamo - -o - o -  - /open turn_led_on_phrases: - led on - test on turn_led_off_phrases: - led off - test off 

It only remained to download the compiled file on the RPi and connect all the wires.

It looks like the finished version of the device
It looks like the finished version of the device

The button to which I connected the control system
The button to which I connected the control system

The door itself
Castle itself

Chat with bot

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


All Articles