obniz developer team
Published

Let's Get Notified with Messenger When Parcels are Delivered

Don't you want to be notified of parcel delivery with messenger app? Let's make things around you smart!

BeginnerFull instructions provided499
Let's Get Notified with Messenger When Parcels are Delivered

Things used in this project

Hardware components

obniz
Cambrian Robotics obniz
×1
infrared distance sensor
×1
Power Supply
×1

Software apps and online services

LINE

Story

Read more

Code

Untitled file

JavaScript
const fs = require('fs'),
      request = require('request'),
      Obniz = require('obniz'),
      { createCanvas } = require('canvas');

const LINE_NOTIFY_URL = 'https://notify-api.line.me/api/notify';
const TOKEN = 'LINE_NOTIFY_TOKEN_HERE';

const POSTED_THOREHOLD = 2.0;
const MESSAGE = "Your package is delievered!";

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');

obniz.onconnect = async () => {
  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<arr.length-1; i++){
    await prevChanges.push((arr[i+1]-arr[i])/300 * 10);
  }
  
  let denoNum = prevChanges.length;
  let total = await prevChanges.reduce((sum, data) => {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;
}

Credits

obniz developer team

obniz developer team

74 projects • 26 followers
Development board "obniz" is controlled via the internet.

Comments