(App Ver.) Power-saving Open/Close Detection Device (Linked with Slack) Using obniz Board 1Y with Sleep Feature
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.
- 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
How to make
Connect the obniz and the magnet switch as shown in the following table.
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)
Setting up Slack
You must create a Slack account in advance.
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
Enter the required information in the “Create a Slack App” dialog that appears.
- 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 Webhooks. We’ll use it later in the chapter, “Install your App on your device.”
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.
Make your App!
Make an app to run the program. You can distribute apps you created and installed and run on each device(Please see App – obniz Docs for more details.)
If you want to run the app first without creating it yourself, install and run the following apps that have already been created.
For installation instructions, see Install an App and the next section, “Install your App on your Device.”
Go to the obniz console and click on “App Development” → “Create WebApp.”
Choose “Empty” from the “obniz Board/1Y Templates” and type the name of your app in the window that comes up. Then press “Create New App.”
Then, scroll down the screen a bit and edit the app settings.
In this case, we want to use the sleep feature of obniz Board 1Y to run the program in the cloud every time the device comes online, so we check the “Run on a device become Online” box.
Also, when installing and running the app, set “Settings at Install” as shown below so that you can set an individual Webhook URL of Slack when installing and running the app.
Scroll up a bit to the top and click on the “Edit Program” to enter the program editing screen.
After you get to that screen, paste the program in the 省電力開閉検知 link in the “Program” section below and save it. Once saved, the application is complete.
Install your App on your device
Install the app you have created on your device. Go to the obniz console and click on “Install.”
Then, select the obniz Board 1Y that you would like to run the program. Also, enter the Webhook URL of Slack you have kept a copy of.
The URL you entered is used as
Obniz.App.configs().SlackWebhookURL in the beginning part of the program.
const config = Obniz.App.configs(); const SLACK_WEBHOOK_URL = config.SlackWebhookURL;
Finally, just press “install.”
The app automatically runs in the cloud once the device becomes online due to the magnetic sensor’s reaction, so it will run automatically when you plug it in.
Please see the program of 省電力開閉検知 that have already been created.
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) Cloud Run of App is capped at 150 times/day, so keep that in mind.
You can check if Cloud Run is running properly in your console. After your device is online, if the app status is green in the “Devices” section as shown below, you’ve succeeded.