言語切り替え

Power-saving open/close detection device (linked with slack) using obniz board 1Y with sleep function

Make things

The obniz Board 1Y now has a sleep function. This feature can be used to create a device that can run on batteries for a year.

In this article, we will create a sleep function to acquire information on the opening and closing of doors and refrigerators using a magnetic switch.

Once the magnet switch detects the door opening and closing, the obniz will be released from sleep and activated.
Send a message to Slack and then sleep again.

Materials

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

This time I used an obniz Grove connector so I can easily connect the 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” on the far right side of Your Apps.

 

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 I made earlier.

It seems that various settings can be made, but because I use Webhooks this time, I click “Incoming Webhooks” near the center of the screen.

4) Setting up Webhooks

Navigate to the Incomming Webhooks page.

Under the title “Activate Incoming Webhooks”, click on the part that is turned off to the right.

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 button that says “Add New Webhook to Workspace”.

5) Channel Settings

Select where you want the message to be sent. It can be sent to an individual channel or a group channel.

This time, I set up a channel called sandbox as a posting destination.

6) Copying a Webhook URL

Click “Copy” to copy the URL of the Webhooks you want to use this time.
We’ll use it later in the program.

Register your obniz Board!

Go to the developer console in obniz and click on “Devices” → “Add Device”.

When the dialog opens, click on “Add Official 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
  • File name : 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.

Execution

Place the magnet and the magnet 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, obniz is activated. Once activated, obniz will send a message to Slack.

The current (December 2019) serverless event is capped at 150, 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!