LoginSignup
1
2

More than 5 years have passed since last update.

Firebaseを使ってNefry BTとPythonでデータのやり取り

Last updated at Posted at 2018-10-04

はじめに

ちょっと前ですが、Nefry BTでハードウェアをつくり、そのハードウェアをトリガーにしてPCのwebカメラで画像認識(openCV)をする、といったものをつくりました。
そのとき、システムのイメージとしては下記の画像のような感じです。

Nefry BTを使うと簡単にwifiを通してデータのやり取りができるので、firebaseにデータを渡しpythonにデータを下ろすというイメージです。
図1.png

Nefry BT側のプログラム

写真のようなボタンが押されたら、1というデータをFirebaseに渡すというプログラムになります。

P_20180901_133106_vHDR_On.jpg


#include <Nefry.h>
#include <NefryFireBase.h>
#define SW_PIN A0
//URLに書き換えること
#define FIREBASE_HOST "自分のfirebaseのURL"

NefryFireBase firebase;

void setup()
{
  firebase.begin(FIREBASE_HOST);
  pinMode(SW_PIN, INPUT_PULLUP);
  Nefry.enableSW(); 
}

void loop()
{
  if(digitalRead(SW_PIN) == HIGH) {
      Serial.print("ボタンが押されていない");  
      DataElement elem = DataElement();
      elem.setValue("SW", "0");
      firebase.write("Nefry", &elem);//FireBaseのデータを書き込む
      Nefry.ndelay(1000);
  }else{
      Serial.print("ボタンが押された");  
      DataElement elem = DataElement();
      elem.setValue("SW", "1");
      firebase.write("Nefry", &elem);//FireBaseのデータを書き込む
      Nefry.ndelay(1000);

  }
}

Pythonのプログラム


from firebase import firebase

firebase = firebase.FirebaseApplication("https://自分のfirebaseのURL/", None)
result = firebase.get('/Nefry', None)

#jsonをパースする
response = result["SW"]
print(response)

#もしif文の中身がうまくいかないときは,型をチェックしてみること
if response == "1":
    print("ボタンが押された")
else:
    print("ボタンが押されていない")

まとめ

firebaseでNefry BTとpythonの間でデータがやり取りできると、画像認識などモノづくりの幅が広がるかもしれませんね。

1
2
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
1
2