[PYTHON] Raspberry Pi? What's that? Level of hardware Inexperienced college students created a system to grasp if there are people in the office

Introduction

Hello. I am currently working as an intern at an IT company while working as a university student in Nagoya.

I'm such a person, but when I spend my time, I want to come to work on days other than the default work day. After taking a lecture at university, I go to work. On such a day, I come to work around 18:00 and work until there are no employees (the office closes). Yes, at my internship, the employee has the key to the office, and if there is an employee, the office is vacant even on holidays.

One day

The university ended around 19:00 and I decided to go to the office. I was convinced that there would be someone. When I arrive at the office ...

** Closed uuuuuuuuuuuuuuuuuuuuuuuuuu **

On that day, all the employees happened to be back.

I was in trouble ... (I can't tell the employees to stay until late, and I'm sorry to let them know when the last employee left the company ...)

Main subject

That's why I decided to create a system to know if there are people in the office. So, when I was wondering what to do, I found out that I could use Raspberry Pi and ʻArduino. By installing a illuminance sensor` on them and detecting the illuminance in the office, it may be possible to grasp whether or not there are people. I thought.

I don't know anything about the hardware. I want to understand it completely.

Embarrassingly, I was really ignorant about the Raspberry Pi and the ʻArduino`, so I did a lot of research. One of my acquaintances was from a technical college who is familiar with hardware (GANGAN), so I even listened to it on twitter DM.

(Like this / approved for publication) スクリーンショット 2019-12-31 12.38.32.png (GANGAN's blog is here)

And ...

[Raspberry Pi Zero WH](https://www.switch-science.com/catalog/3646/?gclid=CjwKCAiA3abwBRBqEiwAKwICA5CCJgfeovCYDEK3oXFVhl9hso62JA-3iYHto7KK-bbIUvlGd_gWWRoCPZEQAv It was decided to use trade-one.co.jp/product/module/adrszlx/). (Decisive factor: cheap)

I bought the above two items online. I bought the conversion cable in Akihabara (Osu) in Nagoya because it was a big deal.

(Like this) スクリーンショット 2019-12-31 13.12.51.png スクリーンショット 2019-12-31 13.12.43.png スクリーンショット 2019-12-31 13.12.33.png

(I forgot to take a picture, but I also bought 4GB of storage.)

Environment

I want to connect the Raspberry Pi to the display and keyboard and understand it completely. With that in mind, I made trial and error while referring to various sites.

And finally ... スクリーンショット 2019-12-31 13.16.26.png

** Kita! !! !! Great hey hey hey hey! !! !! ** **

Raspberry Pi is now a computer! !!

reference: Until you install the OS on Raspberry Pi Zero WH and start it Introducing Raspbian OS to Raspberry Pi Zero WH


Also, since it would be dull if I connected it to the display or keyboard many times, I did ssh remote access. From now on, I decided to connect to Raspberry Pi from my PC.

reference: Remote connection to Raspberry Pi

Implementation

architecture

I want to detect the illuminance and inform myself of the value in some way. I thought about using LINE API or SLACK API.

So we have this kind of architecture. スクリーンショット 2019-12-31 13.32.21.png

It's simple.

Detect illuminance

The [Zero One Series] I purchased this time (https://bit-trade-one.co.jp/product/module/zeroone01top/) is very convenient and [Sample Code](https: /) for detecting illuminance. /github.com/bit-trade-one/RasPi-Zero-One-Series) has been released.

Refer to the above sample code,

python


#!/usr/bin/env python3
# coding: utf-8

import smbus

bus = smbus.SMBus(1)
bus.write_byte_data(0x13, 0x80, 0xFF)
bus.write_byte_data(0x13, 0x82, 0x00)
bus.write_byte_data(0x13, 0x84, 0x9D)
data = bus.read_i2c_block_data(0x13, 0x85, 4)

luminance = data[0] * 256 + data[1]
proximity = data[2] * 256 + data[3]

Then, the illuminance could be detected.

Approximately

When the light is on, luminance: 3000 When the light is off, luminance: 5

It was like that. In addition, to prevent false detections due to sunlight, we installed a Raspberry Pi in a place not exposed to the sun.

Works with slack

Create a new channel and consider a mechanism in which the bot sends the illuminance as a message. I got the webhook URL by referring to here. All you have to do now is make a post request in the specified format.

Completed program

Holidays are after 10 o'clock On weekdays, we will send a message to slack after 17:00.

#!/usr/bin/env python3
# coding: utf-8

import smbus
import time
import requests, json
import datetime
import jpholiday
WEB_HOOK_URL = "****"
message = u'defalut'

bus = smbus.SMBus(1)
bus.write_byte_data(0x13, 0x80, 0xFF)
bus.write_byte_data(0x13, 0x82, 0x00)
bus.write_byte_data(0x13, 0x84, 0x9D)
time.sleep(0.8)
data = bus.read_i2c_block_data(0x13, 0x85, 4)

luminance = data[0] * 256 + data[1]
proximity = data[2] * 256 + data[3]


today = datetime.date.today()
today_str = str(today).replace('-', '')
current_hour = datetime.datetime.now().hour

def isBizDay(DATE):
 Date = datetime.date(int(DATE[0:4]), int(DATE[4:6]), int(DATE[6:8]))
 if Date.weekday() >= 5 or jpholiday.is_holiday(Date):
  return 0
 else:		
  return 1

def sendMessage():
 if luminance >= 800:
  message = u'There are people in the office now! Let's go home early!'	
 else:
  message = u'Currently there are no people in the office.'

 requests.post(WEB_HOOK_URL, data = json.dumps({
 'text': u'Illuminance is'+str(luminance)+u'is!'+message,  #Notification content
 'username': u'Bakira-Tech-Python-Bot',  #username
 'icon_emoji': u':smile_cat:',  #icon
 'link_names': 1,  #Link names
 }))


if isBizDay(today_str) == 1:
 print("It's a weekday")		
 if current_hour > 17:
  sendMessage()
else: 	
 print("holiday")		
 if current_hour > 10:
  sendMessage()

I was able to send a message like this ↓.

スクリーンショット 2019-12-31 20.02.26.png

Periodic execution of the program

Give read permission to the python file and execute it periodically from the crontab command.

I set it to run the corresponding python file once every 30 minutes.

*/30 * * * * /home/pi/luminance.py

reference: Cronize Python on Raspberry Pi3B. Run the program regularly on Raspberry Pi

in conclusion

With this, I've never been vacant even though I went to the office !! It was a Raspberry Pi level, but I am very happy to be able to implement it somehow. When it was over, it was possible with only a very simple implementation. Not only this time, I would like to use Raspberry Pi to solve more familiar problems. It's convenient !! The best Raspberry Pi !!

If you are a company, why don't you introduce it in this era when work style reform is called for? Lol

that's all.


reference: Measure temperature, humidity and barometric pressure with raspberry pi zero What to do if you get a "Couldn't resolve host" error in curl

Recommended Posts

Raspberry Pi? What's that? Level of hardware Inexperienced college students created a system to grasp if there are people in the office
How to make a Raspberry Pi that speaks the tweets of the specified user
Make a note of what you want to do in the future with Raspberry Pi
Is there a secret to the frequency of pi numbers?
Create a bot that posts the number of people positive for the new coronavirus in Tokyo to Slack
How to compare if the contents of the objects in scipy.sparse.csr_matrix are the same
I created a script to check if English is entered in the specified position of the JSON file in Python.
I made a system with Raspberry Pi that regularly measures the discomfort index of the room and sends a LINE notification if it is a dangerous value
Create a BOT that displays the number of infected people in the new corona
A magic word (?) That may save people who are addicted to building using the Intel compiler of Python + Numpy.