0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Arduino × AWSサーバーレスで作るIoT温度モニタリングシステム(仮)【第5回】

Last updated at Posted at 2025-04-20

自作アプリでBluetooth接続&温湿度データ受信できてるかな?

はじめに

「Arduino × AWSサーバーレスで作るIoT温度モニタリングシステム(仮)」の第5回目の報告です!

今回は、自作した Android アプリを使って、前回(第4回)で作成した Arduino + DHT22 のセンサーシステムに Bluetooth 経由で接続し、リアルタイムで温湿度データを受信できるようにしました!

ちょっと急ぎ気味だったので操作UIはまだ粗い部分もありますが…
Bluetooth 機器のスキャン → 接続 → データ受信 → 切断まで、基本的なフローはバッチリ動いてます✨

この記事は「Arduino × AWSサーバーレスで作るIoT温度モニタリングシステム」シリーズの一部です。シリーズの全体リンクは記事の末尾にまとめてあります!


やったこと

  • 自作 Android アプリで Bluetooth 機器スキャンを実装
  • HC-06 モジュールとの接続確立
  • 受信したセンサーデータを画面に表示(JSON形式)
  • ログ表示エリアで動作ログを確認
  • 操作ボタンで接続/切断・POST動作を制御(未完成)

アプリUIと動作の様子

📱 UI構成(スクショ)

アプリ画面1を見る

Bluetoothアプリ画面1

アプリ画面2を見る

Bluetoothアプリ画面2

実装したコードの抜粋(MainActivity.java)

今回のアプリでは、Bluetooth機器への接続やデータの受信処理を自前で実装しています。
以下はその中でも、接続とデータ受信に関わるメイン部分のコードです。

private void connectToBluetoothDevice(BluetoothDevice device) {
    try {
        bluetoothSocket = device.createRfcommSocketToServiceRecord(HC06_UUID);
        bluetoothSocket.connect();
        textViewA.setText("接続成功!");
        startReceiveData();
    } catch (IOException e) {
        Log.e(TAG, "接続失敗", e);
        textViewA.setText("接続失敗...");
    }
}

private void startReceiveData() {
    executorService.execute(() -> {
        try (InputStream inputStream = bluetoothSocket.getInputStream()) {
            byte[] buffer = new byte[1024];
            int bytes;
            while (isReceivingData) {
                bytes = inputStream.read(buffer);
                String data = new String(buffer, 0, bytes, StandardCharsets.UTF_8);
                runOnUiThread(() -> {
                    textViewB.setText(data);
                    Log.d(TAG, "UI更新");
                });
            }
        } catch (IOException e) {
            Log.e(TAG, "データ受信エラー", e);
        }
    });
}

※このコードは一部抜粋です。完成版は次回(第6回)でGitHubにて公開予定です!


🎥 動作動画

🎥 動画はこちら:

👉 YouTubeで直接見る場合はこちら


次にやりたいこと

次の段階では、受信したセンサーデータをそのまま
自作 Android アプリ → AWS API Gateway → Lambda(Python)→ DynamoDB
という流れでクラウドへPOSTできるようにしたいと思います!

いよいよクラウド連携の最終章…がんばるよっ!


おわりに

今回でようやく、Arduinoからのセンサーデータを ワイヤレスで自作アプリに表示 できるようになりました!
最初に夢見た「センサー × 自作アプリ」の形が、だんだん現実になってきてて…ちょっと感動してますw

次はいよいよ、AWSとの連携を完結させに行くよ〜〜っ!


シリーズ記事一覧

※今後の更新もお楽しみに!


つづく…(第6回へ)📡🌡️📲

0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?