Raspberry PiからMQTTでjsonデータをBeaverIoTに送信しIntegrationで受け取る方法

以下の構成で実現できます。

Raspberry Pi → MQTT Publish → Beaver IoT内蔵MQTT Broker → MQTT Device Integrated → Entity化 → Dashboard / Workflowで利用

Beaver IoTは、MQTT Brokerとして動作し、Milesightデバイスまたはサードパーティ製MQTTデバイスからJSON形式のレポートを受け取れます。MQTT Device Integratedでは、アップリンクTopicを指定し、JSONキーをEntityにマッピングするDevice Templateを作成します。


1. Beaver IoT側の設定

1-1. MQTT接続情報を確認

Beaver IoTの画面で以下を確認します。

IntegrationMQTT Device Integrated+Add または設定画面

Beaver IoTの内蔵MQTT Brokerは、標準ではMQTT TCPポート 1883 を使用します。Docker構成でも1883がMQTT接続用ポートとして定義されています。

例:

MQTT Broker Host: BeaverIoTを起動しているPC / Raspberry Pi / サーバーのIP
Port: 1883
Username / Password: Beaver IoTのCredential設定に従う

外部クライアント接続用の認証情報は、Beaver IoTの SettingCredential設定できます。


2. Device Templateを作成

IntegrationMQTT Device IntegratedDevice template management+Add

入力例

Device template name:
raspi_sensor_template

Device topic:
Beaver IoT画面で生成・指定されるTopicを使用
例:/raspi/sensor/uplink

注意点として、Beaver IoTのMQTT Device Integratedでは、Device Topicは / から始める必要があります。また、Beaver IoT側で固定されるTopic前半部分は変更せず、コピーして使用する必要があります。


3. Raspberry Piから送るJSON例

まずは単純なJSONにします。

{
  "device_id": "raspi001",
  "device_name": "Raspberry Pi Sensor 001",
  "temperature": 26.8,
  "humidity": 55.2,
  "co2": 780,
  "occupied": true,
  "time": "2026-05-11 10:30:00"
}

device_id は必須にしておくことを推奨します。Beaver IoT側で、このキーを is_device_id: true にすると、同じ device_id のデータを同じデバイスとして扱いやすくなります。公式例でも device_idis_device_id: true として定義しています。


4. Device Template定義例

Beaver IoTのDevice Entity Definitionに、以下のようなYAML形式で定義します。

definition:
  input:
    type: object
    properties:
      - key: device_id
        type: string
        required: true
        is_device_id: true
        entity_mapping: device_id

      - key: device_name
        type: string
        is_device_name: true

      - key: temperature
        type: double
        entity_mapping: temperature

      - key: humidity
        type: double
        entity_mapping: humidity

      - key: co2
        type: long
        entity_mapping: co2

      - key: occupied
        type: boolean
        entity_mapping: occupied

      - key: time
        type: string
        entity_mapping: time

  output:
    type: object
    properties: []

initial_entities:
  - identifier: device_id
    name: Device ID
    type: property
    access_mod: R
    value_type: string

  - identifier: temperature
    name: Temperature
    type: property
    access_mod: R
    value_type: double
    attributes:
      unit: °C
      fraction_digits: 1

  - identifier: humidity
    name: Humidity
    type: property
    access_mod: R
    value_type: double
    attributes:
      unit: '%'
      fraction_digits: 1

  - identifier: co2
    name: CO2
    type: property
    access_mod: R
    value_type: long
    attributes:
      unit: ppm

  - identifier: occupied
    name: Occupied
    type: property
    access_mod: R
    value_type: boolean

  - identifier: time
    name: Time
    type: property
    access_mod: R
    value_type: string

Beaver IoTのDevice Templateでは、JSONデータ型として objectlongdoublebooleanstring が使用できます。entity_mapping でJSONキーをEntity識別子へ紐付けます。


5. Raspberry Pi側のPython送信例

インストール

pip install paho-mqtt

Pythonコード例

import json
import time
import socket
from datetime import datetime

import paho.mqtt.client as mqtt


BEAVER_HOST = "192.168.1.100"   # Beaver IoTサーバーのIPに変更
BEAVER_PORT = 1883

MQTT_USERNAME = "your_username"  # Beaver IoT Credentialに合わせる
MQTT_PASSWORD = "your_password"

TOPIC = "/raspi/sensor/uplink"   # Beaver IoTで指定したDevice Topicに合わせる

DEVICE_ID = "raspi001"
DEVICE_NAME = "Raspberry Pi Sensor 001"


def build_payload():
    # 実際にはここをDHT22、BME280、CO2センサー、GPIO状態などに置き換えます
    return {
        "device_id": DEVICE_ID,
        "device_name": DEVICE_NAME,
        "temperature": 26.8,
        "humidity": 55.2,
        "co2": 780,
        "occupied": True,
        "time": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    }


def main():
    client = mqtt.Client(client_id=f"{DEVICE_ID}-{socket.gethostname()}")
    client.username_pw_set(MQTT_USERNAME, MQTT_PASSWORD)

    client.connect(BEAVER_HOST, BEAVER_PORT, keepalive=60)
    client.loop_start()

    try:
        while True:
            payload = build_payload()
            message = json.dumps(payload, ensure_ascii=False)

            result = client.publish(
                TOPIC,
                payload=message,
                qos=0,
                retain=False
            )

            result.wait_for_publish()
            print(f"Published: {message}")

            time.sleep(60)

    except KeyboardInterrupt:
        print("Stopped")

    finally:
        client.loop_stop()
        client.disconnect()


if __name__ == "__main__":
    main()

6. 動作確認

Raspberry Piから送信後、Beaver IoT側で以下を確認します。

Device
  → raspi001 が自動作成される

Entity
  → temperature
  → humidity
  → co2
  → occupied
  → time

公式ドキュメントでは、MQTT Device Integratedの設定後、デバイスがBeaver IoTへ接続されると、Beaver IoTがデバイスと対応するEntityを自動作成すると説明されています。


7. トラブル時の確認ポイント

まず確認すべき点は以下です。

1. Raspberry PiからBeaver IoTサーバーの1883番ポートへ到達できるか
2. MQTT Username / PasswordがBeaver IoTのCredentialと一致しているか
3. Publish先TopicがDevice TemplateのTopicと完全一致しているか
4. JSONのキー名がDevice Templateのkeyと一致しているか
5. device_idが含まれているか
6. 数値型が文字列になっていないか

特に多いミスは、以下です。

{
  "temperature": "26.8"
}

この場合、temperature は文字列です。Device Templateで double と定義している場合は、次のように数値で送信します。

{
  "temperature": 26.8
}

 

関連記事

BeaverIoT FAQ

ソリューション / IoT サポート


    Milesight製品

    ウェーブクレスト株式会社が運営するMilesight製品特設サイトです

    お知らせ

    1. 2025-4-3

      ピープル・センシング 駆動型スマートビルディング

      People Sensing Insights を通じてビルインテリジェンスに革命をもたらします。…
    2. 2023-7-21

      LoRaWANの説明: 理論から実践へのガイド

      この包括的なビデオでは、LoRaWANを深く掘り下げ、その仕組み、利点、アプリケーションについて説明…
    3. 2023-4-6

      センシング・インサイト Milesight リブランディングのお知らせ

      https://youtu.be/r40DK40DjIY …

    居住者の健康を確保

    ページ上部へ戻る