Power-saving Open/Close Detection Device (Linked with Slack) Using obniz Board 1Y with Sleep Feature
Contents
Make things
The obniz Board 1Y now has a sleep feature. With this feature, you can make a device that runs for a year on batteries only.
This time, we’ll make a system that utilizes the sleep feature to notify you of the opening and closing doors and refrigerators using a magnetic switch.
When the magnetic switch detects a door opening or closing, obniz wakes up out of sleep. Then, when it wakes up, it sends a message to Slack and then goes to sleep again.
Materials
- obniz Board 1Y
- Grove CT10 (Magnetic Switch) : [SWITCH SCIENCE link]
- obniz Grove connector : [BOOTH link]
- or Grove 4pin connector – jumper pin conversion cable : [千石電商 link]
- Slack account
- Magnet
How to make
Hardware Connection
Connect the obniz and the magnet switch as shown in the following table.
obniz | Grove |
---|---|
0 | SIG |
GND | GND |
+ | VCC |
In this article, we used an obniz Grove connector, so we could easily connect the obniz to Grove’s magnet switch.
[Reference *Without Grove connector] obniz parts library(CT10)
Software
Setting up Slack
You must create a Slack account in advance.
We’re going to set up to send a message to Slack from https://api.slack.com/apps.
1) Create a new App
From the slack API admin screen, click on “Create New App.”
2) Create a Slack App
You’ll see a dialog that says Create a Slack App and you can enter the information you need there.
- App Name: As you like (this time obniz_event)
- Development Slack Workspace: Select the workspace where you want to send the message
When you’re done, click on “Create App“.
The above figure is the screen of the application that you made earlier.
It seems you can make various settings, but click on “Incoming Webhooks” in the center of the screen because we will user Webhooks.
3) Webhooks setting
Go to the Incoming Webhooks page.
Click on the part turned off on the right side of the “Activate Incoming Webhooks” below the title.
If you click on it, the OFF place is switched to ON.
You will then see “Webhooks URLs for Your Workspace.”
Scroll down to the bottom and click on the “Add New Webhook to Workspace.”
4) Channel settings
Select where you want the message to be sent. You can send messages to an individual channel or a group channel.
This time, we set up a channel called sandbox as a posting destination.
5) Copy the Webhook URL
Click “Copy” to copy the URL of the Webhooks. We’ll use it later in the program.
Register your obniz Board!
Go to the obniz console and click on “Devices” → “Add Device.”
When the dialog opens, click on “Add Bought Device.” Follow the instructions on the screen to register.
▼
Let’s write a program.
Put the program in a repository to be used in serverless events. From the console menu, select [Repository] → [Create New].
A dialog will appear, and you can set it as follows
- Type. : WebApp
- Access Level: Private
- Filename: your choice (this time, sleep_sw)
Once configured, click “Create”.
When the program input screen appears, replace it with the following final program.
Fix the following two areas
- Changed “`YOUR Slack Webhooks“` to Webhook URLs obtained by slack.
- Change “`OBNIZ_ID_HERE“` to the ID displayed on the obniz Board.
The content of the message that will be notified to the slack can be set on line 33 (body: JSON.stringify()).
After creating the program, hover your mouse over the top left corner of the screen and click “Save”.
Configuring a Serverless Event
Serverless events are a feature that makes it easy to use obniz without having to prepare the servers that each one of you will need.
From the obniz console, click on “Serverless Events” → “Create New”. Let’s set them in order.
- Name: as you like (this time, SleepSwEvent)
- 1. Event Triggers: obniz Hardware Event
- obniz: ID of obniz to measure the temperature this time
- Event: online
- 2. Application to run
- HTML in Repository: Name of the program you just saved
Once you’re set-up, click “Create”! Now you’re ready to go.
Run
Place the magnet and the magnetic switch close to the object you want to detect the opening and closing. Let it sleep in that state.
When you release the magnet and the magnet switch, the obniz is activated. Once waked up, obniz will send a message to Slack.
The current (November 2020) serverless event is capped at 150 times/day, so keep that in mind.
Program
I could make obniz triggered by the input. You can run it on batteries for a long time while there is no input, so give it a try.
Why don’t you try your hand at using the obniz Board 1Y and create your own!
obniz Board 1Y will be available on December 17, 2019!