Notify LINE when your mail arrives!

Make things

Let’s make a system that notifies on LINE if you get mails or packages.

Related Article.:IoT x mail notification

The flow of the system is as follows.

    1. A distance sensor mounted on the ceiling of the mailbox constantly measures the distance to the floor of the mailbox.
    2. When the mail is placed in the mailbox, the distance between them is reduced so that it can detect that the mail has been delivered.
    3. When it detects that your mail has arrived, it will tap the LINE Notify API and notify LINE.
    4. You’ll be happy to know your mail has arrived from your smartphone!

(This implementation also reacts when you take out the mail because of the distance changes, but we assume that you run this program to receive the notification when you are not in front of the mailbox.)


How to make

Hardware Connection

Connect the infrared distance sensor  GP2Y0A21YK0F to an obniz Board like the table and the image below by referring to the infrared distance sensor GP2Y0A21YK0F Library.

obniz infrared distance sensor  GP2Y0A21YK0F
0 Vcc
2 signal

obniz Boardと赤外線センサー配線図


The program measures the distance between the floor and the ceiling of the mailbox every 300ms. Notify on LINE Notify if the amount of change goes over the threshold.

Go to the LINE Notify page and log in, then open My Page (LINE account required).

Scroll down to My Page and click on “Issue Access Token (for developers)”.

Enter the token name you want to display when you get the notification. And select “receive notifications as 1:1 from LINE Notify” (this time, the token name is posted).

When you click on “Publish“, a token will be displayed as shown below. Save it not to lose it. If you leave this page, you will not be able to check your token.

Replace the LINE_NOTIFY_TOKEN_HERE (code.js line45) with the noted token.

Lastly, don’t forget to friend your LINE Notify account in the LINE app.


The code is written in Node.js.

const fs = require(‘fs’), request = require(‘request’), Obniz = require(‘obniz’), { createCanvas } = require(‘canvas’); const LINE_NOTIFY_URL = ‘’; const TOKEN = ‘LINE_NOTIFY_TOKEN_HERE’; const POSTED_THOREHOLD = 2.0; 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 distanceVals = []; let passCount = 8; let passFlag = false; let obniz = new Obniz(‘OBNIZ_ID_HERE’); let connected = await obniz.connectWait({timeout:10}); if(connected){ obniz.display.clear(); const canvas = createCanvas(128, 64); const ctx = canvas.getContext(‘2d’); ctx.fillStyle = “white”; ctx.font = “20px Avenir”; let disSensor = obniz.wired(“GP2Y0A21YK0F”, {vcc:0, gnd:1, signal:2}); let rawDistance = 0; let changeDiffAbs = 0; rawDistance = await disSensor.getWait(); await obniz.wait(500); rawDistance = await disSensor.getWait(); await arrangeArray(distanceVals, rawDistance); obniz.repeat(async () => { rawDistance = await disSensor.getWait(); changeDiffAbs = await calChangeDiffAbs(distanceVals, rawDistance); await console.log(changeDiffAbs); if(passFlag){ await passCount–; if(passCount <= 0){ passCount = 8; passFlag = false; obniz.display.clear(); } return; } if(changeDiffAbs < POSTED_THOREHOLD || Number.isNaN(changeDiffAbs)){ return; } await request(OPTIONS, async(error, response, body) => { console.log(body); ctx.fillText(‘郵便物が’, 0, 26); ctx.fillText(‘届いています’,0,56); obniz.display.draw(ctx); passFlag = true; if(error){ console.log(error); } }); }, 300); } async function arrangeArray(arr, val){ const ARR_LIMIT_NUM = 20; await arr.push(val); if(arr.length > ARR_LIMIT_NUM){ await arr.shift(); } } async function calPrevChangeAve(arr){ let prevChanges = []; for(let i=0; i {return sum + data}, 0); return total / denoNum; } async function calChangeDiffAbs(arr, val){ let prevVal = arr[arr.length-1]; let currentChange = (prevVal – val)/300 * 10; let prevChangeAve = await calPrevChangeAve(arr); let diff = await Math.abs(prevChangeAve – currentChange); await arrangeArray(arr, val); return diff; }

How to use

When you run the program, you’ll be notified by LINE Notify every time your mail arrives. (If it doesn’t respond well, try adjusting the value of POSTED_THOREHOLD in line 9.)

in the end

Thus, by using obniz Board and LINE Notify, you can easily send notifications to LINE based on the value of the sensor. I hope you’ll take advantage of it.