# Product To Device Closed Loop

## Page Guide

- **Scenario**: Use this flow when a new user needs to understand how FastBee organizes products, devices, thing models, and device messages.
- **Prerequisites**: The demo environment can be opened, and MQTTX or an equivalent MQTT client is available.
- **Goal**: Create a product, define properties and functions, create a device, report data, and verify command delivery.

## 1. Overall Flow

```mermaid
%%{init: {"flowchart": {"htmlLabels": false}}}%%
flowchart LR
  A["Create product"] --> B["Define thing model"]
  B --> C["Release product"]
  C --> D["Create device"]
  D --> E["Connect with MQTTX"]
  E --> F["Report properties"]
  F --> G["View logs"]
  G --> H["Send command"]
```

## 2. Create A Product

Create a product first. The product determines the device type, protocol, networking mode, and thing-model template used by devices.

![Product management](https://www.fastbee.cn/doc/assets/product-manage.B9AI_8wG.png)

Recommended fields:

| Field | Recommendation |
| --- | --- |
| Product name | Use a business-readable name, such as `Demo Temperature Sensor`. |
| Device type | Choose direct device, gateway, sub-device, video device, or other types according to the scenario. |
| Protocol | Use MQTT for the first verification. |
| Data format | Keep the platform default format unless the project has a custom protocol. |

## 3. Define The Thing Model

Define properties, functions, and events before releasing the product.

![Thing model](https://www.fastbee.cn/doc/assets/object-model.CcYVTgWh.png)

| Type | Purpose | Example |
| --- | --- | --- |
| Property | Device status or telemetry | Temperature, humidity, switch status |
| Function | Platform command delivered to device | Turn on relay, set threshold |
| Event | Important device event | Alarm, fault, offline notice |

## 4. Create A Device

After the product is released, create a device and copy the MQTT credentials.

![Create device](https://www.fastbee.cn/doc/assets/device-add.CCsTmPLq.png)

Pay attention to product ownership, device number, device secret, and status. Device credentials must match the MQTT access document exactly.

## 5. Verify With MQTTX

Use MQTTX to connect to the platform, report a property, and subscribe to command topics.

![MQTTX connection](https://www.fastbee.cn/doc/assets/image-01.xYe8qo4U.png)

![MQTTX reporting](https://www.fastbee.cn/doc/assets/image-02.COivONxi.png)

After reporting data, check the device running status, logs, and latest property values in the console.

## 6. FAQ

### MQTTX connection fails

Check first:

1. Whether the product has been released.
2. Whether the device is disabled.
3. Whether the Client ID follows the `S&deviceNumber&productId&userId` format.
4. Whether the product ID was filled in with the product number; they are not the same field.
5. Whether the username and password come from the same product.
6. When device authorization is enabled for the product, whether the auth code is appended to the password.
7. Whether port `1883` on the server is open.

### Device is online but data is not displayed

Check first:

1. Whether the product ID and device number in the reporting topic are correct.
2. Whether the JSON is in array format.
3. Whether `id` exactly matches the thing-model identifier.
4. Whether `value` matches the thing-model data type.
5. Whether the product has been re-released and whether the device has inherited the latest thing model.

### MQTTX does not receive messages after platform delivery

Check first:

1. Whether MQTTX has subscribed to `/{productId}/{deviceNum}/function/get`.
2. Whether the device stays online.
3. Whether the thing model selected at the delivery entry is a writable function or a deliverable property.
4. Whether permission control on the browser page disables the button.
5. Whether the client subscription exists in the EMQX console.

### Behavior is inconsistent after enabling device shadow

Device shadow caches platform commands when the device is offline. For the first integration test, it is recommended to disable device shadow and enable it only after the online delivery path is stable.

## 7. Development Entry

| Type | Path Or Module | Description |
| --- | --- | --- |
| Frontend product page | `vue3/src/views/iot/product` | Product, thing model, release, and application configuration entry. |
| Frontend device page | `vue3/src/views/iot/device` | Device list, detail, running status, and command delivery. |
| Frontend API | `vue3/src/api/iot` | Request wrappers for products, devices, thing models, and more. |
| Backend product API | `springboot/fastbee-open-api` | `/iot/product`, `/iot/model`, and related APIs. |
| Backend device API | `springboot/fastbee-open-api` | `/iot/device`, device logs, device users, and related APIs. |
| MQTT authentication | `springboot/fastbee-open-api` | `/iot/tool/mqtt/auth`, EMQX webhook authentication entry. |
| Protocol parsing | `springboot/fastbee-protocol` | Device message codec and protocol extension. |
| Data service | `springboot/fastbee-iot-data` | Device running data and time-series data processing. |

## Related Docs

* [10-Minute Demo Environment](https://www.fastbee.cn/doc/en/business/v2/start/demo-quickstart.html)
* [MQTTX Simulate Device Access](https://www.fastbee.cn/doc/en/business/v2/start/mqtt.html)
* [MQTT Device Access](https://www.fastbee.cn/doc/en/business/v2/link/mqtt.html)
* [Platform Topics](https://www.fastbee.cn/doc/en/business/v2/dev/topic.html)
* [Product Management](https://www.fastbee.cn/doc/en/business/v2/manual/product.html)
* [Thing Model](https://www.fastbee.cn/doc/en/business/v2/manual/things.html)
* [Device Management](https://www.fastbee.cn/doc/en/business/v2/manual/device.html)
* [Data Debug](https://www.fastbee.cn/doc/en/business/v2/start/debug.html)
