[PYTHON] The story that the guard was confined when the laboratory was converted to IoT

This article is the 20th day of Retty Advent Calendar 2019. Yesterday, Mr. Hirano's "Retty Data Analysis Team-Review of the Second Year of Launch-Data Platform / Data Democratization / Statistical Model- / 190000) ”.

Nice to meet you. I'm Koda, a 20-year-old engineer who is currently an intern at Retty.

For the time being, I was wondering what to write after registering for participation, but I made my own entry / exit management system (electronic lock) that is operated in the laboratory of the university I attend, so I will talk about that.

Supplement

We received a lot of opinions from Hatena bookmarks. Thank you very much. As some people have pointed out, the reaction on Slack when the problem "I couldn't get out of the room at night" occurred was inappropriate, so I deleted it. I regret that it was not appropriate to take up such contents in the blog. Also, since we have not taken measures in the event of an unexpected situation such as an earthquake or fire, we would like to consider a mechanism that can be unlocked in an emergency.

Background

Immediately after I was assigned to the laboratory, I was asked what to do with the room key.

When it comes to making duplicate keys for the number of people, it costs a lot and it is troublesome to manage the keys for the number of people. When the total number of third-year students reaches about 20, some people will lose it.

Next, I thought about introducing a smart lock like Qrio, but the door of our laboratory was not compatible with the thumb turn "type with doorknob". qrio (Other supported thumb turns are summarized in detail on this page.)

As far as we know, there is no smart lock product for this type, which raises the bar for door IoT considerably.

I had no choice but to make it myself, so I decided to make it. [^ 1] 

Overall composition

The final composition looks like this. I made it as I like with the technology I want to use. slide1

Electronic lock

The electronic lock body is a RaspberryPi3 Model B + with a servo motor, reed switch, and card reader connected. The program that controls the servo motor was written in Python. Two card readers are used, and by attaching them to the inside and outside of the door, the state of in the room or going out is managed. I will explain why I manage it later.

In order to make it ** auto lock **, a reed switch is attached to acquire the open / closed status of the door.

Card key

We registered the student ID card and ICOCA ʻidm` that everyone has in the database and authenticated them as a key. We haven't made it compatible yet, but if you make it compatible, you can unlock it even with a card registered with Apple Pay.

As a secondary benefit, "** You can reduce the risk of losing your key **". This is because if you delete the target data from the database, you will not be able to enter or exit with that card.

The one to detect

It was maddox / dasher that was often used when the Amazon Dash Button hack was popular, but when I looked into the repository for the first time in a while, Nekmo / amazon-dash was mentioned.

All settings can be written in yaml format, and a Docker image is also available.

Room management

The "** in-room display board (whiteboard) **" that is familiar in university laboratories seems to be good by using an electronic lock, so I made it good.

It will be explained in the next function introduction.

Function introduction

I would like to introduce it because it has some functions, not just moving as an "electronic lock".

Web management screen

I made a simple management screen with Flask to register / edit / delete members. Everyone loves the front desk, Bootstrap + jQuery.

By the way, you have to look up ʻidmyourself and then enter it manually. If you useWebUSB` or something, you can connect a Felica reader, read it from there, and register it, but I haven't implemented it yet.

Dashboard function

We have made it possible to see the occupancy status online so that you can see who is from anywhere. A monitor is installed at the entrance with a monitor arm, and another RaspberryPi is used to create a kiosk terminal for display. It's convenient because you don't have to move the magnet on the whiteboard.

Slack integration function

When you press the button on the door, you'll hear this message on Slack's notification channel. notification1 It is linked with Slack's Interactive Components, and by pressing "Unlock", you can unlock without having to go to the entrance to pick up. I will. notification2 By the way, in this case, you will be asked to open the door and come in by yelling "Key is open!" From your seat in a loud voice.

You can stay away by pressing "Ignore". notification3

As shown in the image, a log of "who responded" remains. The message is overwritten, so it's nice to see the Slack notification badge disappear when someone responds.

Where I devised

I devised a little part to connect the unlock button of Slack and RaspberryPi. Specifically, when the Slack button is pressed, Slack sends a POST request to the endpoint you specified, and it's about where to put this endpoint.

It seems quick to set the endpoint directly on the RaspberryPi, but I didn't want to expose the electronic lock to the Internet, so I decided to put MQTT in between. For MQTT and MQTT broker, "First MQTT" was easy to understand, so I will post it.

スクリーンショット 2019-12-16 14.56.02.png First, set up an API to receive requests from Slack, and when you receive a request that "the unlock button has been pressed", publish to the target topic. By subscribing the same topic on the RaspberryPi side, it is possible to detect the occurrence of an event without exposing it to the Internet.

I thought it would take some time to unlock, but is it practical because it unlocks in about 1 second after pressing the Slack button? It was within the range.

One day ...

One day around 20:00, while working at home, I received a visit notification from the call button in the laboratory.

Apparently, the teacher remained on this day, and he went out for a while with the lights on. Most of the visits during this time are guards, and they come when the lights are on. ~~ I will get angry at a later date if there is no one left on. ~~

The moment I received the visit notification, I thought "Ah" and turned off the lights remotely. [^ 2] slack2

After this, the title incident happened.

** When the guard entered the laboratory and checked the room, I just turned off the lights **, and he was panicking ... Furthermore, ** it was locked by auto-lock while checking the room **.

I had a button that can be unlocked from the room for a while, but it didn't seem necessary, so it happened immediately after I removed it.

at the end

Mr. Mamoru, I'm really sorry for making you scared.

[^ 1]: It was NG from the university side to replace the entire doorknob. [^ 2]: Lighting is also IoT. Originally, it is a function that automatically turns off at a fixed time in case you forget to turn it off.

Recommended Posts

The story that the guard was confined when the laboratory was converted to IoT
The story that the version of python 3.7.7 was not adapted to Heroku
The story that the Homebrew environment was blown away when Anaconda was installed
The story that XGBoost was finally installed
A story that I was addicted to when I made SFTP communication with python
A story that was convenient when I tried using the python ip address module
A story that failed when trying to remove the suffix from the string with rstrip
A story that got stuck when trying to upgrade the Python version on GCE
I tried to make the phone ring when it was posted at the IoT post
A story that I was addicted to at np.where
The story that the return value of tape.gradient () was None
The story that Japanese output was confused with Django
The story I was addicted to when I specified nil as a function argument in Go
The story that my pull request was incorporated into Scipy
The story that the new drawing library "HiPlot" was pretty good
The story that the private key is set to 600 with chmod
A story that didn't work when I tried to log in with the Python requests module
A story that I was addicted to calling Lambda from AWS Lambda.
The record I was addicted to when putting MeCab on Heroku
A story that struggled to handle the Python package of PocketSphinx
Normalize the file that converted Excel to csv as it is.
A story that was struggling to loop processing 3 million ID data
When I tried to run Python, it was skipped to the Microsoft Store
A story I was addicted to when inserting from Python to a PostgreSQL table
Asynchronous API that combines API Gateway and Step Functions was the strongest story
The day when basic authentication was added to the service operated by flask
The story that Apache dealt with because it was down at AH00144
A story that suffered from OS differences when trying to implement a dissertation
The story that had nothing to do with partitions when I did disk backup with dd for the first time