言語切り替え

郵便物が届いたらLINEにお知らせ(スリープ編)

Notify LINE when your mail arrives (sleep version)

Make things

Last time, we introduce this article (Notify LINE when your mail arrives!).

This time, we’ll use obniz Board 1Y to update the power-saving mail notification system using sleep.

Notify LINE when the mail arrives

Change it a bit from the previous system and create a system with the following flow.

  1. A tilt sensor is attached to the slot of the mailbox and detects the tilt.
  2. When the mail is placed in the mailbox, the mail slot tilts to detect that the mail has been delivered.
  3. LINE Notify’s API will be used to notify LINE when it detects that mail has arrived.
  4. You’ll be happy to know your mail has arrived at hand!

Materials

How to make

Hardware Connection

Connect a tilt sensor to an obniz Board like the table and the image below.

obniz tilt sensor
0 Signal
+ Vcc
GND

Software

LINE Notify

Please refer to “Notify LINE when your mail arrives!” because it has the same steps.

Don’t forget to note token. You cannot get token if you leave the page.

program

Write the program that notifies through LINE Notify if the mailbox slot is tilted.

  • Replace the LINE_NOTIFY_TOKEN_HERE (line6) with the noted token.
  • Replace the OBNIZ_ID_HERE (line24) with your obniz ID.

Then, install packages of request and obniz from npm.

Program

The program is written in Node.js.

const fs = require(‘fs’),
request = require(‘request’),
Obniz = require(‘obniz’);

const LINE_NOTIFY_URL = ‘https://notify-api.line.me/api/notify’;
const TOKEN = ‘LINE_NOTIFY_HERE’;

const MESSAGE = “郵便物が投函されました。”;
const HEADERS = {
‘Content-Type’: ‘application/x-www-form-urlencoded’,
‘Authorization’: ‘Bearer ‘ + TOKEN
};

const OPTIONS = {
url: LINE_NOTIFY_URL,
method: ‘POST’,
headers: HEADERS,
json: true,
form: { message: MESSAGE }
}

let obniz = new Obniz(‘OBNIZ_ID_HERE’);
let isFirstCall = true;

obniz.onconnect = async () => {

if(isFirstCall){
isFirstCall = !isFirstCall;
}else{
await request(OPTIONS, async(error, response, body) => {
console.log(body);
if(error){
console.log(error);
}
});

obniz.sleepIoTrigger(false);

}

At the obniz.sleepIoTrigger(false) (line40), enters the sleep state, which is released when the voltage of obniz’s pin 0 goes from HIGH to LOW (see Sleep | obniz.js for details).

The tilt sensor used this time is the mechanism that detects the tilt by contacting the built-in ball when it tilts. When this ball makes contact, pin 0 of obniz is set to LOW, and sleep is released.

Each time the sleep mode is released, a message will be sent to LINE as soon as the mail has arrived from the mailbox.

How to use

Connect the power to the obniz Board and place it behind the mailbox’s drop-off point. At this time, pay attention to the direction of installation so that the sensor responds to the tilt of the lid when the mail is thrown in.

When you run the program, you’ll be notified by LINE Notify every time your mail arrives.

With the sleep feature added to obniz Board 1Y, you can save power and make your system last longer.
There are a lot of benefits, such as reducing the frequency of battery replacement and not having to be aware of the location of the outlet due to power savings. I hope you’ll take advantage of it.