2
0

PipedreamでobnizやLINEを使う実験 #iotlt #pipedream #obniz

Last updated at Posted at 2023-08-24

PipedreamはiPaaSなのにNode.jsやPythonやGolangのコードが書けて、npmモジュールも使えるのでobnizを使ってIoTやれるかを実験してみます。

先に結論っぽい話

(メモ書きしたあとに追記してます)

https://speakerdeck.com/n0bisuke2/tuiniipaasdeobnizgadong-kukamo-number-iotlt-number-pipedream-number-obniz
スクリーンショット 2023-08-24 20.47.00.png

IoTLTで話をしてきた↑が一旦現状の結論ぽい感じです。

ベースのaxiosサンプル

axiosを使うコードがサンプルにあるのでこちらをベースに実験してみます。

import axios from "axios"

export default defineComponent({
  async run({ steps, $ }) {
    const { data } = await axios({
      method: "GET",
      url: "https://pokeapi.co/api/v2/pokemon/charizard",
    })
    return data.species
  },
})

↓こんな感じぽいですね

//何かモジュール読み込み

export default defineComponent({
  async run({ steps, $ }) {
    
    //ワークフローが実行された時の処理
    
  },
})

run()の中に入れてみる。

import Obniz from 'obniz'

export default defineComponent({

  async run({ steps, $ }) {  	
  	const obniz = new Obniz('xxxx-xxxx'); //newした時に接続しにいくのでrun内に

  	obniz.onconnect = async function () {
  	 obniz.display.clear();
     obniz.display.print('Hello World!!!!!!!!!!');
     console.log('hello~');
    };
    
  },
})

とりあえず一瞬動いた

run()の外に置いてみる

import Obniz from 'obniz'

const obniz = new Obniz('xxxx-xxxx');
obniz.onconnect = async function () {
  obniz.display.clear();
  obniz.display.print('Hello World!!!!!!!!!!');
  console.log('hello~');
};
    
export default defineComponent({

  async run({ steps, $ }) {
  	//何か処理    
  },
})

おぉ、これ動くんですね。結構驚きです。

ずっと動いてくれそう。。。?

ただこちらは停止されるタイミングがイマイチ分からず、Pipedreamの管理画面でワークフローを停止してもアーカイブしても止まってくれず、少ししてから止まりました。

どっちも動いたがクレジット問題あるかも

とりあえずどちらも動きました。

  • Runの中
    • 一瞬だけ起動してくれるので
  • Runの外
    • ずっと動いてくれて待機してくれそう
    • 100クレジット/Dayの制限的に無料枠死んじゃうかも...?

obnizが止まらない...?

停止しただけでは止まらずにアーカイブまでしないとNode.jsのプロセスが停止しませんでした。
裏でバッチ処理があってそのうち止まるとかあるかもですが、再起動させたい時はアーカイブが必要みたいですね。うーむ、トリッキーな使い方してるこちらが悪いですが面倒...

スクリーンショット 2023-08-22 14.03.58.png

LINE Botと組み合わせる

別記事でLINEのプッシュメッセージを試してみてました。

こちらの記事内容と合体させていきます。

import Obniz from 'obniz'
import line from '@line/bot-sdk'

const config = {
    channelSecret: '',
    channelAccessToken: ''
};

const client = new line.Client(config);

const sendMsg = async (msg) => {

    const messages = [{
        type: 'text',
        text: msg
    }];

    try {
        const res = await client.broadcast(messages);
        console.log(res);        
    } catch (error) {
        console.log(`エラー: ${error.statusMessage}`);
        console.log(error.originalError.response.data);
    }
}

export default defineComponent({
  async run({ steps, $ }) {
  
    //メッセージが来た時に起動する
    const obniz = new Obniz('xxxx-xxxx');
    
    obniz.onconnect = async function () {
        const tempsens = obniz.wired('LM60', { gnd: 0, output: 1, vcc: 2 });
        const temp = await tempsens.getWait();
        console.log(temp);
        sendMsg(`部屋の温度は${temp}です。`);
        const led = obniz.wired("LED", {anode:4, cathode:5});
        led.on();
        await obniz.wait(1000); // led ON for 1sec.
        led.off();
        obniz.closeWait();

        return steps;
    }; 
  }
})
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0