I'm the type of person who brings lunch to the office, but it's a pain to wash. The process of going home and washing the lunch box is very painful. So, I want to wash it in the sink of the company. However, there are quite a few people in the company who think the same way, and they have to wait for a vacancy in the daytime or the sink. This can be a hassle, and there was a story that I would like you to be notified when it becomes available. And I think this is rarely common, but I heard that an unused Raspberry PI (hereinafter referred to as Raspberry Pi) was dropped in the company and I thought "I think I can notify if there is a person with this?" This is the background.
It notifies you when there are people in the sink for a certain period of time, and when there are no people for a certain period of time, it notifies you that you are gone. Use Slack as the notification destination.
I need a sensor and a jumper wire, but I have everything at Marutsu Akihabara Main Store. We also sell the Raspberry Pi itself.
Or I think that there is also a self-made version such as a plastic version.
Follow the link below to connect the motion sensor and Raspberry Pi and copy and run the program. [Raspberry Pi] How to use and use the self-made motion sensor -CHASUKE \ .com
If it works without any problem, the connection is OK.
The motion sensor detects a human and reacts immediately, so it notifies you when a person passes by. So, I will notify you when I stay for a certain period of time. In addition, there were many cases where it was detected that there were no people even if there were people, so if there are no people for N seconds, it is judged that the sink has been used. That is the following code.
sink_observer.py
from datetime import datetime
import RPi.GPIO as GPIO
import json
import logging
import requests
import sys
import time
GPIO_PIN = 18
SLACK_SETTINGS = {
'url' : '{URL of Incoming Webhook}',
'botname' : 'who's eyes are those',
'icon' : ':eyes',
'start_using': 'There are people in the sink.',
'finished' : 'It seems that you have finished using it.'
}
is_using = False
formatter = '%(levelname)s \t%(asctime)s\t%(message)s'
logging.basicConfig(filename='sink_observer.log', level=logging.INFO, format=formatter)
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
def push_to_slack(status):
datas = {
'username' : SLACK_SETTINGS['botname'],
'icon_emoji' : SLACK_SETTINGS['icon'],
'text' : SLACK_SETTINGS[status],
'contentType': 'application/json'
}
payload = json.dumps(datas)
requests.post(SLACK_SETTINGS['url'], payload)
def start_using():
global is_using
if is_using:
return
is_using = True
push_to_slack('start_using')
logger.info('start_using')
def finished():
global is_using
if not is_using:
return
is_using = False
push_to_slack('finished')
logger.info('finished')
def main(sleeptime, interval, retry):
GPIO.setmode(GPIO.BCM)
GPIO.setup(GPIO_PIN, GPIO.IN)
detect_cnt = 0
no_detect_cnt = 0
try:
logger.info('End:Ctrl+C')
while True:
if GPIO.input(GPIO_PIN) == GPIO.HIGH:
detect_cnt += 1
#Log for operation check
logger.info('detect\tdetect_cnt:{}'.format(str(detect_cnt)))
if detect_cnt <= 2: # HACK:Actually, it is easier to adjust if you can also specify here
time.sleep(interval)
continue
detect_cnt = 0
no_detect_cnt = 0
start_using()
time.sleep(sleeptime)
else:
no_detect_cnt += 1
logger.info('no detect\tdetect_cnt:{}'.format(str(detect_cnt)))
if no_detect_cnt <= retry:
time.sleep(interval)
continue
detect_cnt = 0
no_detect_cnt = 0
finished()
time.sleep(interval)
except KeyboardInterrupt:
logger.info('In the process of termination...')
finally:
GPIO.cleanup()
logger.info('GPIO clean completed')
if __name__ == '__main__':
sleeptime = 10
interval = 3
retry = 10
#For parameter adjustment
if len(sys.argv) > 4:
sleeptime = int(sys.argv[1])
interval = int(sys.argv[2])
retry = int(sys.argv[3])
main(sleeptime, interval, retry)
SSH to the Raspberry Pi and do the following: You only need to install pip for the first time.
$ pip3 install requests
$ nohup python3 sink_observer.py &
Since it does nohup
and&
, it keeps working even if the SSH connection is disconnected. If you want to quit, you can look up the process number with ps aux | grep sink_observer.py
and quit withkill {process number}
.
I thought it was naked, so I processed the plastic box that contained the Raspberry Pi. I drew a marker appropriately where the GPIO / USB / power supply / LAN cable was passed, made a hole with a pin vise, and then trimmed the edge with a design knife. The box I processed this time was made of polypropylene-based plastic, and it broke when I wore it sideways, so it was important to make a hole with a pin vise. After that, make a hole in a suitable place in the case, pass a wire, and fix the motion sensor, and you're done. It turned out to be something like this. The note paper is in the foreground because the motion sensor has a viewing angle of 100 °, which limits the field of view to prevent false positives (a complete first aid measure).
I made the following because it makes a false positive too badly.
As a result of doing this, the false positive rate dropped sharply.
It's just a hypothesis. From the conclusion, it is the theory that the sensor responded to visible light. The specific content of false positives
It was that. The characteristic of the sensor is an infrared sensor that uses a pyroelectric element. The pyroelectric element itself can detect infrared rays and light (visible light) in a rough interpretation. The theory is that the human body emits infrared rays, so it can be used as a motion sensor. However, it does not detect only infrared rays, but also reacts to ** visible light, that is, the light we see **. The Raspberry Pi and Wi-Fi dongle clearly emitted visible light, so detecting this would result in a false positive. The improvements made were made with the aim of limiting the 100 ° viewing angle of the sensor and giving it directivity, while keeping the sensor away from the light emitted by the Raspberry Pi and the light emitted by the Wi-Fi dongle. is. I made a large-scale one with plastic boards and plastic sticks, but if I can do the same thing, I think it is not necessary to make it so far. This eliminates false positives caused by some people passing in front of the sink and false positives caused by detecting Raspberry Pi / Wi-Fi dongle light at night. After doing this, the false positive rate seems to be less than 1%.
~~ It will be falsely detected. It's unavoidable because there is a sink in a busy place ... However, it is functioning properly, so if you devise an algorithm or adjust the parameters, it will be at a practical level. Now you don't have to wait for the sink or go back to your seat because you're using it ... I hope!
Recommended Posts