2016年6月10日金曜日

RaspberryPI3でBlynk libraryを使ってみる

RaspberryPI3でBlynk libraryを使ってみる

IoT時代ということでここを参考にRaspberry PI3と温湿度センサーのDHT11を使って温度計測をしてみる。

スマホ側の準備

IFTTTの準備

1.IF by IFTTTをインストールし、アカウントは取得する。

2.IF by IFTTTを起動し、画面右上のすり鉢のようなアイコンをタップ

enter image description here

3.「My Recipes」を開いて「+」(プラス)アイコンをタップ

enter image description here

3.画面下の「Create a New Recipe」をタップ

enter image description here

4.If の右側の「+」をタップした後に、「Maker」を選択。
enter image description here

enter image description here

5.Trigger として 「Receive a web request」を選択し、Event Name に“alert-temp”と入力
※RaspberryPi側から送信する Event Name と同じ名称にする必要あり。

enter image description here  

6.then の右側の「+」をタップした後に、「IF Notifications」を選択。

enter image description here

enter image description here

7.Action として「Send a notification」を選択し、Notification に“異常温度が計測されました。”などと入力し、Continueをタップ。
enter image description here

8.「Finish」をタップすれば Recipe は完成し、My Receipesに登録されます。
enter image description here

9.「+」をタップし、Makerを選択し、
enter image description here

「設定」をタップ。
enter image description here

以下のキーをメモっておく。
enter image description here

Blynkの準備

1.Blynkをインストール
2.プロジェクトの作成
enter image description here

3.プロジェクト名を入れてHARDWARE MODELとして”Raspberry Pi 3B”を選択。
AUTH TOKENはメモしておくこと。

enter image description here

4.「+」をタップし、「Value Display S」を2つ、「History Graph」を 1 つ追加
enter image description here

enter image description here

4.「Value Display S」、「History Graph」はそれぞれ以下のように設定
[1つ目]
名称: “温度”
INPUT: “Virtual”の“V0”
FREQUENCY: “10sec”
enter image description here

[2つ目]
名称: “湿度”
INPUT: “Virtual”の“V1”
FREQUENCY: “10sec”
enter image description here

「History Graph」
上から順に、以下を設定。
“Virtual”の“V0”、“温度”
“Virtual”の“V1”、“湿度”、
SHOW LEGEND を“ON”
enter image description here

こんな感じになったら完成です。
enter image description here

Raspberry PI側の準備

Raspberry Pi 用の C言語で書かれた GPIOアクセスライブラリのWiringPiのインストール

$ git clone git://git.drogon.net/wiringPi
$ cd ./wiringPi
$ ./build

blynk-libraryのインストール

$ git clone https://github.com/blynkkk/blynk-library.git
$ cd blynk-library/linux
$ make clean all target=raspberry

Curlライブラリのインストール

$ sudo apt-get update
$ sudo apt-get install libcurl4-openssl-dev

ソースコードの編集

今回変更及び追加するファイル一覧は以下。
kuro-iotexp1.cpp 追加ソースコード
kuro-iotexp1.h 追加ヘッダファイル
Makefile 変更(パッチ適用)
main.cpp 変更(パッチ適用)

ソースファイルと差分ファイルをダウンロード

$ wget http://driver.cfd.co.jp/cfd-drv/files/kuro-iotexp_kit/kuro-iotexp1.cpp
$ wget http://driver.cfd.co.jp/cfd-drv/files/kuro-iotexp_kit/kuro-iotexp1.h
$ wget http://driver.cfd.co.jp/cfd-drv/files/kuro-iotexp_kit/Makefile.patch
$ wget http://driver.cfd.co.jp/cfd-drv/files/kuro-iotexp_kit/main.cpp.patch

パッチを適用

$ patch -u -b < Makefile.patch
$ patch -u -b < main.cpp.patch

main.cpp を編集

以下の行の xxxxxxxx の箇所を IFT

char url[] = "https://maker.ifttt.com/trigger/alart-temp/with/key/xxxxxxxxxxxxxxxxxxxxxx";

再ビルド

$ make clean all target=raspberry

実行

tokenはBlynkででてきたAUTH TOKENを入れる。

$ sudo ./blynk --token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
[0] Blynk v0.3.7 on Linux
[5001] Connecting to blynk-cloud.com:8442
[5206] Ready (ping: 95ms).
[5692] Trouble detected: http://docs.blynk.cc/#troubleshooting-flood-error
Read DHT11
Temperature = 27 degree C, Humidity = 58 %

となったので動き出したし、Blynkのほうでもデータが取得できた。

しかし、IFTTTへのPushの動検ができていないため、
意図的にIFTTTにpushされる温度にしても、
“Push IFTTT”とでるがスマホ側でPushされない。

試しにhttps://ifttt.com/makerからHow to Trigger Eventsを
クリックし、evnet nameを入れるとちゃんとPushできたし、
Raspberry PIの端末で以下のcurlを実施してもPushできた。
enter image description here

IFTTTへの自動的にPushするのはもう少し別のサンプルを
見ながら理解を深めるとします。

0 件のコメント :

コメントを投稿