# Wifiモジュールの使い方 ## 使用機材 - [Espr Developer](https://www.switch-science.com/catalog/2500/) - PC ## 使用サービス - Google Cloud Platform - IoT Core / PubSub - Cloud Functions - Firestore ## Flow ESP32 →[MQTT]→ Cloud Iot Core → Cloud Pub/Sub → Cloud Function → Stackdriver Logging ## Setup 0. Arduino Boardのsetup https://msr-r.net/arduino-ide-esp8266/ https://poche.xsrv.jp/iot-gym/2018/02/28/how-to-install-usb-driver-to-mac/ 2. gcloudコマンドのインストール GCPのプロジェクトを作成し、その後gcloud initを実行し設定。 https://cloud.google.com/sdk/install?hl=ja https://cloud.google.com/sdk/docs/initializing 2. firebaseコマンドをインストール GCPのプロジェクトをもとにFirebaseプロジェクトを作成。 https://firebase.google.com/docs/cli/?hl=ja 終了したら、以下のコマンドを入力 ```bash= firabase login firebase init ``` 以下のファイルができているか確認 ``` functions/src/index.ts functions/package.json ``` 3. IoT Coreを有効化 https://console.cloud.google.com/iot/ 4. コマンドからIoT Coreを設定 ```bash=terminal # 設定値をあからじめ環境変数にて設定する export REGISTRY_ID=test_registry # レジストリIDを入力(名前みたいなものでプロジェクトに適した任意の名前を設定して下さい) export CLOUD_REGION=asia-east1 export GCLOUD_PROJECT=$(gcloud config list project --format "value(core.project)") # pubsubのtopicを作成 gcloud pubsub topics create device-logs # iot coreのレジストリを作成 gcloud iot registries create $REGISTRY_ID --region=$CLOUD_REGION --event-notification-config=subfolder="",topic=device-logs # cloud functionsのデプロイ cd functions npm install firebase use $GCLOUD_PROJECT firebase deploy --only functions # device用のフォルダ作成(鍵を作成して置くだけなのでどこでもよいです) cd ../ mkdir sample-device cd sample-device # デバイスの認証に使用する公開鍵、秘密鍵を生成 openssl ecparam -genkey -name prime256v1 -noout -out ec_private.pem openssl ec -in ec_private.pem -pubout -out ec_public.pem # iot coreのデバイスを作成(公開鍵を登録している) sample-deviceの箇所は任意のデバイス名に書き換え可能です gcloud iot devices create sample-device --region $CLOUD_REGION --registry $REGISTRY_ID --public-key path=./ec_public.pem,type=ES256 # 下記コマンドで秘密キーを抽出してpriv箇所をスケッチに記載する。 openssl ec -in ec_private.pem -noout -text ``` 5. Espr Developer Source files - [From Google](https://github.com/GoogleCloudPlatform/google-cloud-iot-arduino) 今回は[こちら](https://github.com/GoogleCloudPlatform/google-cloud-iot-arduino/tree/master/examples/Esp8266-lwmqtt)を利用しました 6. ciotc_config.hを設定 ```c++ // WIFI const char *ssid = "xxx"; const char *password = "xxx"; // Cloud IoT Coreの各種設定 const char *project_id = "GCPのプロジェクトIDを記述"; // echo $GCLOUD_PROJECTで確認できます const char *location = "asia-east1"; const char *registry_id = "test_registry"; // IoT CoreのレジストリーIDを記述 const char *device_id = "sample-device"; // IoT CoreのデバイスIDを記述 // Cloud IoT Coreのデバイスごとに設定する秘密キーを設定します。先頭が"00:"ではじまる始まる場合はその"00:"を消して合計32個のHEX値ペアになるように(32byte)。 const char *private_key_str = "xx:xx:xx..." "xx:xx:xx..." "xx:xx:xx"; ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up