Last Updated on : 2024-06-20 02:25:30download
This topic describes how to integrate a building automation (BA) system with the Tuya Developer Platform.
We provide the following solution to help brand owners connect a BA system to the Tuya Developer Platform by using Tuya IoT Edge Gateway. The architecture is shown as follows:
During communications over the MQTT protocol, the BA gateway works as a client, and Tuya IoT Edge Gateway works as a broker.
Register an account on the Tuya Developer Platform, create a product, and define product functions. For more information, see Create Products.
The following table lists the specified properties of BA sub-systems for your reference.
DP ID | DP name | Identifier | Data transfer type | Data type | DP property |
---|---|---|---|---|---|
1 | Report phase-A voltage, current, and power | phase_a | Report only (read-only) | raw | / |
2 | Report phase-B voltage, current, and power | phase_b | Report only (read-only) | raw | / |
3 | Report phase-C voltage, current, and power | phase_c | Report only (read-only) | raw | / |
4 | Phase-A temperature | phase_a_temp | Report only (read-only) | value |
|
5 | Phase-B temperature | phase_b_temp | Report only (read-only) | value |
|
6 | Phase-C temperature | phase_c_temp | Report only (read-only) | value |
|
7 | Neutral wire current | cur_neutral | Report only (read-only) | value |
|
8 | Uab line voltage | uab_line_voltage | Report only (read-only) | value |
|
9 | Ubc line voltage | ubc_line_voltage | Report only (read-only) | value |
|
10 | Uca line voltage | uca_line_voltage | Report only (read-only) | value |
|
11 | Apparent power | power_apparent | Report only (read-only) | value |
|
12 | Power factor | power_factor | Report only (read-only) | value |
|
13 | Communication status | comm_state | Report only (read-only) | enum | The enumeration values: online and offline . |
15 | On/off status | on_off_state | Send and report (read-write) | enum | The enumeration values.
|
16 | Switch | switch | Send and report (read-write) | bool | Value range: 0 and 1 . |
17 | Actual power | power_current | Report only (read-only) | value |
|
18 | Actual voltage | voltage_current | Report only (read-only) | value |
|
19 | Maximum current | current_max | Send and report (read-write) | value |
|
20 | Actual current | cur_current | Send and report (read-write) | value |
|
21 | Actual temperature | temp_current | Report only (read-only) | value |
|
22 | Undervoltage value | voltage_under | Send and report (read-write) | value |
|
23 | Overvoltage value | voltage_over | Send and report (read-write) | value |
|
24 | Leakage alarm value | leakage_warn_value | Send and report (read-write) | value |
|
25 | Leakage fault value | leakage_fault_value | Send and report (read-write) | value |
|
26 | Leakage current | leakage_current | Send and report (read-write) | value |
|
27 | Leakage protection | leakage_protection | Report only (read-only) | enum | The enumeration values.
|
28 | Enabling switch | switch_enabled | Send and report (read-write) | bool | Value range: 0 and 1 . |
29 | Overcurrent duration | over_current_duration | Send and report (read-write) | value |
|
31 | Device alarm | device_alarm | Report only (read-only) | string | / |
39 | Leakage action value | leakage_action_value | Send and report (read-write) | value |
|
40 | Over-temperature value | over_temp_value | Send and report (read-write) | value |
|
41 | Overvoltage and undervoltage protection time | over_vol_protect_time | Send and report (read-write) | value |
|
42 | Overvoltage and undervoltage protection recovery time | over_vol_recovery_time | Send and report (read-write) | value |
|
43 | Daily electricity consumption data | daily_energy | Send and report (read-write) | raw | / |
44 | On/off times | switch_times | Report only (read-only) | value |
|
45 | Number of times a short circuit happened | short_times | Report only (read-only) | value |
|
46 | Total electricity | electric_total | Send and report (read-write) | value |
|
47 | Daily electricity consumption | electric_daily | Send and report (read-write) | value |
|
48 | Monthly electricity consumption | electric_month | Send and report (read-write) | value |
|
49 | Remaining electricity | residual_electricity | Send and report (read-write) | value |
|
50 | Power limit | power_limit | Send and report (read-write) | value |
|
51 | Electricity consumption alert value | electric_warm_value | Send and report (read-write) | value |
|
52 | Forward total active electricity | forward_energy_total | Report only (read-only) | value |
|
53 | Reactive electricity | energy_reactive | Report only (read-only) | value |
|
58 | Switch status | switch_state | Send and report (read-write) | bool | Value range: 0 and 1 . |
59 | Total active electricity | active_energy_total | Report only (read-only) | value |
|
60 | Bus coupler status | bus_coupler_state | Report only (read-only) | raw | / |
61 | Incoming line status | incoming_line_state | Report only (read-only) | raw | / |
62 | Active power | power_effective | Report only (read-only) | value |
|
63 | Reactive power | power_reactive | Report only (read-only) | value |
|
64 | Frequency | supply_frequency | Report only (read-only) | value |
|
65 | Online status | state | Send and report (read-write) | enum | The enumeration values: online and offline . |
66 | Voltage | battery_voltage | Report only (read-only) | value |
|
67 | Pipe network pressure | pressure_value | Report only (read-only) | value |
|
68 | Running status | status | Send and report (read-write) | enum | The enumeration values: run and stop . |
69 | Monthly electricity consumption data | month_energy | Report only (read-only) | raw | / |
70 | Oil level | oil_level | Report only (read-only) | value |
|
71 | Mode | mode | Send and report (read-write) | enum | The enumeration values: manual , auto , and close . |
72 | Spraying status | spray_state | Report only (read-only) | enum | The enumeration values: opening and closing . |
73 | Status of fireman’s switch | fire_switch_state | Report only (read-only) | enum | The enumeration values: opening and closing . |
74 | Current unbalance | cur_unbalance | Report only (read-only) | value |
|
75 | Line voltage unbalance | l_vol_unbalance | Report only (read-only) | value |
|
76 | Phase voltage unbalance | p_vol_unbalance | Report only (read-only) | value |
|
77 | Leakage status | leakage_state | Report only (read-only) | enum | The enumeration values: normal and alarm . |
78 | Residual current | cur_residual | Send and report (read-write) | value |
|
79 | Phase-N temperature | phase_n_temp | Send and report (read-write) | value |
|
DP ID | DP name | Identifier | Data transfer type | Data type | DP property |
---|---|---|---|---|---|
1 | Status | status | Send and report (read-write) | enum | The enumeration values: run and stop . |
2 | Mode | mode | Send and report (read-write) | enum | The enumeration values: auto and manual . |
3 | Total running time | runtime_total | Report only (read-only) | value |
|
4 | Pressure value | pressure_value | Report only (read-only) | value |
|
5 | Water temperature | water_temp | Report only (read-only) | value |
|
6 | Water flow | water_flow | Report only (read-only) | value |
|
7 | Outdoor temperature | temp_outdoor | Report only (read-only) | value |
|
8 | Outdoor humidity | humidity_outdoor | Report only (read-only) | value |
|
9 | Water flow status | flow_state | Report only (read-only) | enum | The enumeration values: run and stop . |
10 | Valve status | valve_state | Send and report (read-write) | enum | The enumeration values: fully_open and partially_open . |
11 | Fault alarm | fault | Send and report (read-write) | bitmap | The fault values.
|
12 | Water valve feedback | water_valve_return | Report only (read-only) | value |
|
13 | Valve check | valve_check | Send and report (read-write) | bool | Value range: 0 and 1 . |
14 | Bypass valve | bypass_valve_value | Send and report (read-write) | value |
|
15 | Water valve | water_valve_value | Send and report (read-write) | value |
|
16 | Electric butterfly valve switch | butterfly_valve_switch | Send and report (read-write) | bool | Value range: 0 and 1 . |
17 | Differential pressure | pressure_dif | Send and report (read-write) | value |
|
18 | Trip status | trip_state | Report only (read-only) | enum | The enumeration values: normal and device_alarm . |
19 | Instantaneous flow rate | flow_rate_instant_value | Report only (read-only) | value |
|
20 | Total flow rate | flowrate_total | Report only (read-only) | value |
|
21 | Communication status | comm_state | Report only (read-only) | enum | The enumeration values: online and offline . |
22 | Flowing velocity | flow_velocity | Report only (read-only) | value |
|
DP ID | DP name | Identifier | Data transfer type | Data type | DP property |
---|---|---|---|---|---|
1 | Communication status | comm_state | Report only (read-only) | enum | The enumeration values: online and offline . |
2 | Li1 status | li_state_1 | Send and report (read-write) | enum | The enumeration values: opening and closing . |
3 | Li2 status | li_state_2 | Send and report (read-write) | enum | The enumeration values: opening and closing . |
4 | Li3 status | li_state_3 | Send and report (read-write) | enum | The enumeration values: opening and closing . |
5 | Li4 status | li_state_4 | Send and report (read-write) | enum | The enumeration values: opening and closing . |
6 | Li5 status | li_state_5 | Send and report (read-write) | enum | The enumeration values: opening and closing . |
7 | Li6 status | li_state_6 | Send and report (read-write) | enum | The enumeration values: opening and closing . |
8 | Li7 status | li_state_7 | Send and report (read-write) | enum | The enumeration values: opening and closing . |
9 | Li8 status | li_state_8 | Send and report (read-write) | enum | The enumeration values: opening and closing . |
10 | Indoor temperature | temp_indoor | Report only (read-only) | value |
|
11 | Sensor probe | sensor_probe | Report only (read-only) | enum | The enumeration values: normal and device_alarm . |
12 | Indoor humidity | humidity_indoor | Report only (read-only) | value |
|
13 | Liquid depth | liquid_depth | Report only (read-only) | value |
|
14 | Battery voltage | battery_voltage | Report only (read-only) | value |
|
15 | Water detector status | watersensor_state | Report only (read-only) | enum | The enumeration values: alarm and normal . |
16 | Pressure value | pressure_value | Report only (read-only) | value |
|
17 | Inlet water temperature | inlet_temp_1 | Report only (read-only) | value |
|
18 | Contact sensor status | doorcontact_state | Send and report (read-write) | bool | Value range: 0 and 1 . |
19 | Light illuminance | illuminance_value | Report only (read-only) | value |
|
20 | Fault alarm | fault | Report only (read-only) | bitmap | The enumeration values.
|
DP ID | DP name | Identifier | Data transfer type | Data type | DP property |
---|---|---|---|---|---|
1 | Switch status | switch_state | Send and report (read-write) | bool | Value range: 0 and 1 . |
2 | Communication status | comm_state | Report only (read-only) | enum | The enumeration values: online and offline . |
3 | Actual current | cur_current | Report only (read-only) | value |
|
4 | Actual temperature | temp_current | Report only (read-only) | value |
|
5 | Target temperature | target_temp | Report only (read-only) | value |
|
6 | Speed level | fan_level | Send and report (read-write) | enum | The enumeration values: level_1 , level_2 , level_3 , level_4 , and level_5 . |
7 | Switch | switch | Send and report (read-write) | bool | Value range: 0 and 1 . |
8 | Mode | mode | Send and report (read-write) | enum | The enumeration values: auto , colding , hot , air , and dehumidify . |
9 | Vertical swing flap switch | switch_vertical | Send and report (read-write) | bool | Value range: 0 and 1 . |
10 | Horizontal swing flap switch | switch_horizontal | Send and report (read-write) | bool | Value range: 0 and 1 . |
11 | Fault alarm | fault | Report only (read-only) | bitmap | The fault values.
|
12 | Air supply temperature | supply_temp | Report only (read-only) | value |
|
13 | Air supply humidity | humidity_supply | Report only (read-only) | value |
|
14 | Water valve feedback | water_valve_return | Report only (read-only) | value |
|
15 | Status | state | Send and report (read-write) | enum | The enumeration values: auto and manual . |
16 | Primary filter switch | first_filter_switch | Send and report (read-write) | bool | Value range: 0 and 1 . |
17 | Fan filter switch | fan_filter_switch | Send and report (read-write) | bool | Value range: 0 and 1 . |
18 | Water valve | water_valve_value | Send and report (read-write) | value |
|
19 | Humidification valve | hum_valve_value | Send and report (read-write) | value |
|
20 | Fresh air valve switch | freshair_valve_switch | Send and report (read-write) | bool | Value range: 0 and 1 . |
21 | Air exhaust valve | exhaust_valve_value | Send and report (read-write) | value |
|
22 | Instantaneous flow rate | flow_rate_instant_value | Report only (read-only) | value |
|
23 | Total flow rate | flowrate_total | Report only (read-only) | value |
|
24 | Flow rate | flow_velocity | Report only (read-only) | value |
|
25 | Indoor temperature | temp_indoor | Report only (read-only) | value |
|
26 | Indoor humidity | humidity_indoor | Report only (read-only) | value |
|
27 | Inlet water temperature | inlet_temp_1 | Report only (read-only) | value |
|
28 | Outlet water temperature | outlet_temp_1 | Report only (read-only) | value |
|
29 | Inlet water temperature | inlet_temp_f | Report only (read-only) | value |
|
30 | Effluent water temperature | effluent_temp | Report only (read-only) | value |
|
DP ID | DP name | Identifier | Data transfer type | Data type | DP property |
---|---|---|---|---|---|
1 | Switch status | switch_state | Report only (read-only) | bool | Value range: 0 and 1 . |
2 | Running mode | mode | Send and report (read-write) | enum | The enumeration values: manual and auto . |
3 | Fault alarm | fault | Report only (read-only) | bitmap | The fault values.
|
4 | Switch | switch | Send and report (read-write) | bool | Value range: 0 and 1 . |
5 | Battery voltage | battery_voltage | Report only (read-only) | value |
|
6 | Temperature setting | temp_set | Send and report (read-write) | value |
|
7 | CO2 detection value | eco2_set | Report only (read-only) | value |
|
8 | Air blower frequency | frequency_conversion | Send and report (read-write) | value |
|
9 | Frequency feedback of air blower frequency converter | fre_conversion_return | Report only (read-only) | value |
|
10 | Humidification valve | hum_valve_value | Send and report (read-write) | value |
|
11 | Humidification valve feedback | hum_valve_return | Report only (read-only) | value |
|
12 | Water valve | water_valve_value | Send and report (read-write) | value |
|
13 | Water valve feedback | water_valve_return | Report only (read-only) | value |
|
14 | Fresh air valve adjustment | freshair_valve_value | Send and report (read-write) | value |
|
15 | AI fresh air valve feedback | ai_freshair_valve_return | Report only (read-only) | value |
|
16 | Fresh air temperature | temp_fresh | Send and report (read-write) | value |
|
17 | Fresh air humidity | humidity_fresh | Send and report (read-write) | value |
|
18 | Anti-freeze switch detection | antifreeze | Send and report (read-write) | bool | Value range: 0 and 1 . |
19 | Primary filter switch | first_filter_switch | Send and report (read-write) | bool | Value range: 0 and 1 . |
20 | Fan filter switch | fan_filter_switch | Send and report (read-write) | bool | Value range: 0 and 1 . |
21 | Air supply temperature | supply_temp | Send and report (read-write) | value |
|
22 | Air supply humidity | humidity_supply | Send and report (read-write) | value |
|
23 | Indoor carbon dioxide | ico2_ndoor_value | Send and report (read-write) | value |
|
24 | Indoor volatile organic compounds (VOCs) | voc_indoor_value | Send and report (read-write) | value |
|
25 | Oil level | oil_level | Report only (read-only) | value |
|
26 | Remaining oil | oil_remain | Report only (read-only) | value |
|
27 | Total tank capacity | capacity_total | Report only (read-only) | value |
|
28 | Report phase-A voltage, current, and power | phase_a | Report only (read-only) | raw | / |
29 | Report phase-B voltage, current, and power | phase_b | Report only (read-only) | raw | / |
30 | Report phase-C voltage, current, and power | phase_c | Report only (read-only) | raw | / |
31 | Uab line voltage | uab_line_voltage | Report only (read-only) | value |
|
32 | Ubc line voltage | ubc_line_voltage | Report only (read-only) | value |
|
33 | Uca line voltage | uca_line_voltage | Report only (read-only) | value |
|
34 | Single-phase active power | sigle_phase_power | Report only (read-only) | value |
|
35 | Power factor | power_factor | Report only (read-only) | value |
|
36 | Apparent power | power_apparent | Report only (read-only) | value |
|
37 | Total active electricity | active_energy_total | Report only (read-only) | value |
|
38 | Grid power supply frequency | supply_frequency | Report only (read-only) | value |
|
39 | Actual temperature | temp_current | Report only (read-only) | value |
|
40 | Monthly electricity consumption | electric_month | Report only (read-only) | value |
|
41 | Pressure value | pressure_value | Report only (read-only) | value |
|
DP ID | DP name | Identifier | Data transfer type | Data type | DP property |
---|---|---|---|---|---|
1 | Communication status | comm_state | Report only (read-only) | enum | The enumeration values: online and offline . |
2 | Water level setting | water_set | Report only (read-only) | value |
|
3 | Battery voltage | battery_voltage | Report only (read-only) | value |
|
4 | Water detector status | watersensor_state | Report only (read-only) | enum | The enumeration values: water_leak_sensor and normal . |
5 | Water pressure of the system | water_pressure | Report only (read-only) | value |
|
6 | Frequency conversion feedback | fre_conversion_return | Report only (read-only) | value |
|
7 | Switch status | switch_state | Send and report (read-write) | bool | Value range: 0 and 1 . |
8 | Fault alarm | fault | Report only (read-only) | bitmap | The fault values: water_low , water_high , water_low_max , and high_level . |
9 | Mode | mode | Send and report (read-write) | enum | The enumeration values: manual and auto . |
10 | Valve check | valve_check | Send and report (read-write) | bool | Value range: 0 and 1 . |
11 | Frequency conversion | frequency_conversion | Send and report (read-write) | value |
|
12 | Switch | switch | Send and report (read-write) | bool | Value range: 0 and 1 . |
13 | Electric butterfly valve switch | butterfly_valve_switch | Send and report (read-write) | bool | Value range: 0 and 1 . |
14 | Working status | work_state | Report only (read-only) | enum | The enumeration values: run and stop . |
15 | Total running time | runtime_total | Send and report (read-write) | value |
|
16 | Pressure value | pressure_value | Send and report (read-write) | value |
|
17 | Overload trip status of water pump | trip_state | Report only (read-only) | enum | The enumeration values: normal and device_alarm . |
18 | Water capacity | water_capacity | Send and report (read-write) | value |
|
19 | Inlet water temperature | inlet_temp_1 | Report only (read-only) | value |
|
20 | Outlet water temperature | outlet_temp_1 | Report only (read-only) | value |
|
21 | Water flow | water_flow | Report only (read-only) | value |
|
22 | Report phase-A voltage, current, and power | phase_a | Report only (read-only) | raw | voltage indicates the voltage, current indicates the current, power indicates the power, and electricity indicates the electric power.{ “voltage”:“”, // The voltage “current”:“”, // The current “power”:“” // The power “electricity”:“”// The electric power } |
23 | Report phase-B voltage, current, and power | phase_b | Report only (read-only) | raw | voltage indicates the voltage, current indicates the current, power indicates the power, and electricity indicates the electric power.{ “voltage”:“”, // The voltage “current”:“”, // The current “power”:“” // The power “electricity”:“”// The electric power } |
24 | Report phase-C voltage, current, and power | phase_c | Report only (read-only) | raw | voltage indicates the voltage, current indicates the current, power indicates the power, and electricity indicates the electric power.{ “voltage”:“”, // The voltage “current”:“”, // The current “power”:“” // The power “electricity”:“”// The electric power } |
25 | Phase-A temperature | phase_a_temp | Report only (read-only) | value |
|
26 | Phase-B temperature | phase_b_temp | Report only (read-only) | value |
|
27 | Phase-C temperature | phase_c_temp | Report only (read-only) | value |
|
28 | Effective power | power_effective | Report only (read-only) | value |
|
29 | Power factor | power_factor | Report only (read-only) | value |
|
30 | Grid power supply frequency | supply_frequency | Report only (read-only) | value |
|
31 | Total active electricity | active_energy_total | Report only (read-only) | value |
|
32 | Temperature | irac_temperature | Report only (read-only) | value |
|
33 | Noise | noise_value | Report only (read-only) | value |
|
34 | Daily electricity consumption data | daily_energy | Report only (read-only) | raw | / |
35 | Monthly electricity consumption data | month_energy | Report only (read-only) | raw | / |
DP ID | DP name | Identifier | Data transfer type | Data type | DP property |
---|---|---|---|---|---|
1 | Air supply temperature | supply_temp | Report only (read-only) | value |
|
2 | Air supply humidity | humidity_supply | Report only (read-only) | value |
|
3 | Return air temperature | temp_return | Report only (read-only) | value |
|
4 | Return air humidity | humidity_return | Report only (read-only) | value |
|
5 | Air exhaust valve feedback | exhaust_valve_return | Report only (read-only) | value |
|
6 | Water valve feedback | water_valve_return | Report only (read-only) | value |
|
7 | Indoor temperature | temp_indoor | Report only (read-only) | value |
|
8 | Indoor humidity | humidity_indoor | Report only (read-only) | value |
|
9 | Gust speed | windspeed_gust | Report only (read-only) | value |
|
10 | Frequency conversion feedback | fre_conversion_return | Report only (read-only) | value |
|
11 | Switch status | switch_state | Send and report (read-write) | bool | Value range: 0 and 1 . |
12 | Fault alarm | fault | Send and report (read-write) | bitmap | The fault values.
|
13 | Primary filter switch | first_filter_switch | Send and report (read-write) | bool | Value range: 0 and 1 |
14 | Fan filter switch | fan_filter_switch | Send and report (read-write) | bool | Value range: 0 and 1 . |
15 | Bypass valve | bypass_valve_value | Send and report (read-write) | value |
|
16 | Frequency conversion | frequency_conversion | Send and report (read-write) | value |
|
17 | Water valve | water_valve_value | Send and report (read-write) | value |
|
18 | Humidification valve | hum_valve_value | Send and report (read-write) | value |
|
19 | Return air valve adjustment | returnair_valve_value | Send and report (read-write) | value |
|
20 | Switch | switch | Send and report (read-write) | bool | Value range: 0 and 1 . |
21 | Fresh air valve switch | freshair_valve_switch | Send and report (read-write) | bool | Value range: 0 and 1 . |
22 | Air exhaust valve | exhaust_valve_value | Send and report (read-write) | value |
|
23 | Humidification | wet | Send and report (read-write) | bool | Value range: 0 and 1 . |
24 | Status | status | Report only (read-only) | enum | The enumeration values: normal , stop , and device_fault . |
25 | Mode | mode | Send and report (read-write) | enum | The enumeration values: auto and manual . |
26 | Total running time | runtime_total | Report only (read-only) | value |
|
DP ID | DP name | Identifier | Data transfer type | Data type | DP property |
---|---|---|---|---|---|
2 | Air speed | windspeed_gust | Report only (read-only) | value |
|
3 | Wind | wind_power | Report only (read-only) | value |
|
4 | Wind direction | wind_direct | Send and report (read-write) | enum | The enumeration values.
|
5 | Wind direction value | wind_direct_value | Send and report (read-write) | value |
|
6 | Temperature, humidity, EC, and pH value of soil 1 | soil_data_1 | Report only (read-only) | raw | / |
7 | Temperature, humidity, EC, and pH value of soil 2 | soil_data_2 | Report only (read-only) | raw | / |
8 | Temperature, humidity, EC, and pH value of soil 3 | soil_data_3 | Report only (read-only) | raw | / |
9 | Temperature, humidity, EC, and pH value of soil 4 | soil_data_4 | Report only (read-only) | raw | / |
10 | Outdoor humidity | humidity_outdoor | Send and report (read-write) | value |
|
11 | Outdoor temperature | temp_outdoor | Send and report (read-write) | value |
|
12 | Noise | noise_value | Report only (read-only) | value |
|
13 | Atmospheric pressure | atmospheric_pressture | Report only (read-only) | value |
|
14 | Lighting intensity | light_intensity | Report only (read-only) | value |
|
15 | Rainy and snowy status | rain_snow_state | Report only (read-only) | enum | The enumeration values: snowy and normal . |
16 | Ultraviolet index | uv_index | Report only (read-only) | value |
|
17 | Total radiation | radiation_total | Report only (read-only) | value |
|
18 | Photosynthetically active radiation | radiation_pho | Report only (read-only) | value |
|
19 | Cumulative rainfall | rain_total | Report only (read-only) | value |
|
20 | Actual rainfall | rain_current | Report only (read-only) | value |
|
21 | Instantaneous rainfall | rain_instant | Report only (read-only) | value |
|
22 | 1-hour rainfall | rain_1h | Report only (read-only) | value |
|
23 | O3 detection value | o3_value | Report only (read-only) | value |
|
24 | CO detection value | co_value | Report only (read-only) | value |
|
25 | SO2 detection value | so2_value | Report only (read-only) | value |
|
26 | O2 detection value | o2_value | Report only (read-only) | value |
|
27 | H2S detection value | h2s_value | Report only (read-only) | value |
|
28 | Evaporation amount | evap_total | Report only (read-only) | value |
|
29 | PM2.5 detection value | pm25_value | Report only (read-only) | value |
|
30 | PM10 | pm10 | Report only (read-only) | value |
|
31 | Nitrogen | nitrogen_value | Report only (read-only) | value |
|
32 | Phosphorus | phosphorus_value | Report only (read-only) | value |
|
33 | Potassium | potassium_value | Report only (read-only) | value |
|
34 | Negative oxygen ion | air_anion_value | Report only (read-only) | value |
|
35 | NH3 detection value | nh3_value | Report only (read-only) | value |
|
36 | VOC detection value | voc_value | Report only (read-only) | value |
|
37 | Power status setting | relay_status | Send and report (read-write) | enum | The enumeration values: on and off . |
38 | Status | state | Send and report (read-write) | enum | The enumeration values: run and stop . |
39 | Residual chlorine detection value | ppm_value | Send and report (read-write) | value |
|
40 | pH Value | ph | Send and report (read-write) | value |
|
41 | Water quality | water_quality | Report only (read-only) | enum | The enumeration values: normal and alarm . |
42 | Turbidity | turbidity_value | Report only (read-only) | value |
|
43 | Actual temperature | temp_current | Report only (read-only) | value |
|
44 | Fault alarm | fault | Report only (read-only) | bitmap | The fault values: fault , sensor_fault , and amb_temp_fault . |
45 | Temperature | irac_temperature | Send and report (read-write) | value |
|
46 | Humidity | humidity | Report only (read-only) | value |
|
47 | Air quality | air_quality_value | Report only (read-only) | value |
|
49 | Battery capacity | battery_percentage | Report only (read-only) | value |
|
50 | NO2 detection value | no2_value | Report only (read-only) | value |
|
51 | CO2 detection value | co2_value | Report only (read-only) | value |
|
52 | Remaining height | height | Report only (read-only) | value |
|
DP ID | DP name | Identifier | Data transfer type | Data type | DP property |
---|---|---|---|---|---|
1 | Communication status | comm_state | Report only (read-only) | enum | The enumeration values: online and offline . |
2 | Switch | switch | Send and report (read-write) | bool | Value range: 0 and 1 . |
3 | Group control switch | group_control_switch | Send and report (read-write) | string | / |
4 | Group control light | group_control_light | Send and report (read-write) | string | / |
5 | Group | group | Send and report (read-write) | value |
|
6 | Monitoring status reporting | monitor_state_report | Send and report (read-write) | string | / |
7 | Light switch | lamp_switch | Send and report (read-write) | bool | Value range: 0 and 1 . |
8 | Brightness value | bright_value | Send and report (read-write) | value |
|
9 | Actual current | cur_current | Report only (read-only) | value |
|
10 | Actual voltage | voltage_current | Report only (read-only) | value |
|
11 | Actual power | power_current | Report only (read-only) | value |
|
DP ID | DP name | Identifier | Data transfer type | Data type | DP property |
---|---|---|---|---|---|
1 | Door status | door_state | Report only (read-only) | enum | The enumeration values: openning and closing . |
2 | Upstream and downstream status | vertical_state | Report only (read-only) | enum | The enumeration values.
|
3 | Running status | status | Report only (read-only) | enum | The enumeration values: run and stop . |
4 | Actual floor | floor_current_data | Report only (read-only) | raw | / |
5 | Human presence status | have_none_state | Report only (read-only) | enum | The enumeration values.
|
6 | Elevator call | call_elevator | Send and report (read-write) | string | / |
7 | Movement speed | speed_realtime | Send and report (read-write) | value |
|
8 | Top temperature | top_temp | Report only (read-only) | value |
|
9 | Passive infrared (PIR) sensor status | pir_state | Report only (read-only) | enum | The enumeration value: pir . |
10 | Fault alarm | fault | Report only (read-only) | bitmap | The fault values.
|
11 | Communication status | comm_state | Report only (read-only) | enum | The enumeration values: online and offline . |
12 | Actual current | cur_current | Report only (read-only) | value |
|
13 | Report phase-A voltage, current, and power | phase_a | Report only (read-only) | raw | / |
14 | Report phase-B voltage, current, and power | phase_b | Report only (read-only) | raw | / |
15 | Report phase-C voltage, current, and power | phase_c | Report only (read-only) | raw | / |
16 | Uab line voltage | uab_line_voltage | Report only (read-only) | value |
|
17 | Ubc line voltage | ubc_line_voltage | Report only (read-only) | value |
|
18 | Uca line voltage | uca_line_voltage | Report only (read-only) | value |
|
19 | Actual voltage | voltage_current | Report only (read-only) | value |
|
20 | Effective power | power_effective | Report only (read-only) | value |
|
21 | Reactive power | power_reactive | Report only (read-only) | value |
|
22 | Apparent power | power_apparent | Report only (read-only) | value |
|
23 | Forward total active electricity | forward_energy_total | Report only (read-only) | value |
|
24 | Water detector status | watersensor_state | Report only (read-only) | enum | The enumeration value: water_leak_sensor . |
25 | Indoor temperature | temp_indoor | Send and report (read-write) | value |
|
26 | Indoor humidity | humidity_indoor | Send and report (read-write) | value |
|
27 | Unlock and lock status sensor | sensor_probe | Report only (read-only) | enum | The enumeration values: openning and closing . |
28 | Laser ranging distance | target_dis_closest | Report only (read-only) | value |
|
29 | Sensor mode | sensor_choose | Send and report (read-write) | enum | The enumeration values: device_alarm and normal . |
30 | Total occurrences | times_total | Report only (read-only) | value |
|
31 | Sensor status | sensor_state | Report only (read-only) | enum | The enumeration values: device_alarm and normal . |
32 | Reference running height | height | Send and report (read-write) | value |
|
33 | Daily electricity consumption data | daily_energy | Report only (read-only) | raw | / |
34 | Monthly electricity consumption data | month_energy | Report only (read-only) | raw | / |
35 | Battery voltage | battery_voltage | Report only (read-only) | value |
|
36 | Total distance | distance_total | Report only (read-only) | value |
|
37 | Actual load | load_current | Send and report (read-write) | value |
|
38 | Overload status | overweight_state | Report only (read-only) | enum | The enumeration values.
|
39 | Parallel status | parallel_state | Send and report (read-write) | enum | The enumeration value: normal . |
40 | Machine room temperature | com_room_temp | Send and report (read-write) | value |
|
41 | Floor of getting stuck | stuck_layer | Report only (read-only) | value |
|
42 | Open times | open_times | Report only (read-only) | value |
|
43 | Maintenance status | maintenance_state | Report only (read-only) | enum | The enumeration value.
|
44 | Gate locking status | gate_lock_state | Report only (read-only) | enum | The enumeration values: opening and closing . |
45 | Alarm bell status | alarm_bell_state | Report only (read-only) | enum | The enumeration values: alarm and normal . |
DP ID | DP name | Identifier | Data transfer type | Data type | DP property |
---|---|---|---|---|---|
1 | Inlet water temperature | inlet_temp_1 | Send and report (read-write) | value |
|
2 | Outlet water temperature | outlet_temp_1 | Send and report (read-write) | value |
|
3 | Water flow | water_flow | Report only (read-only) | value |
|
4 | Switch | switch | Send and report (read-write) | bool | Value range: 0 and 1 . |
5 | Fault alarm | fault | Report only (read-only) | bitmap | The fault values.
|
6 | Usage of shared cold water | cold_stall_userdata | Report only (read-only) | value |
|
7 | Sharing type of cold water | cold_stall_type | Send and report (read-write) | enum | The enumeration values.
|
8 | Usage of shared hot water | hot_stall_userdata | Report only (read-only) | value |
|
9 | Sharing type of hot water | hot_stall_type | Send and report (read-write) | enum | The enumeration values.
|
10 | Valve openness | valve_open_degree | Send and report (read-write) | value |
|
11 | Valve status | valve_state | Send and report (read-write) | enum | The enumeration values.
|
12 | Total monthly water consumption | monthly_water_total | Report only (read-only) | value |
|
13 | Total daily water consumption | daily_water_total | Report only (read-only) | value |
|
14 | First reading of a day | d_begin_water_total | Report only (read-only) | value |
|
15 | Last reading of a day | d_end_water_total | Report only (read-only) | value |
|
16 | First reading time of a day | d_begin_time | Report only (read-only) | string | / |
17 | Last reading time of a day | d_end_time | Report only (read-only) | string | / |
18 | First reading of a month | m_begin_water_total | Report only (read-only) | value |
|
19 | Last reading of a month | m_end_water_total | Report only (read-only) | value |
|
20 | First reading time of a month | m_begin_time | Send and report (read-write) | string | / |
21 | Last reading time of a month | m_end_time | Send and report (read-write) | string | / |
An MQTT message consists of a fixed header, variable header, and payload.
For more information about the format of fixed headers and variable headers, see MQTT Specification. The format of the payload is defined by Tuya as follows.
The MQTT syntax and API details shall conform to MQTT Specification.
Common MQTT message types include CONNECT
, SUBSCRIBE
, and PUBLISH
.
CONNECT
: A client requests a connection to a server. It is the main parameter of a payload. For more information, see Connection authentication.SUBSCRIBE
: A client requests to subscribe to a specified topic. For more information about the topic name
in a payload, see the specified topic whose subscriber is a device in the Topic definition.PUBLISH
: The platform publishes messages.
Topic name
in a variable header refers to a specified topic whose publisher is a device during reporting to the Tuya Developer Platform. For more information, see Topic definition.Limitations
The device connects to and communicates with Tuya IoT Edge Gateway. The following figure shows how the communications between the device, Tuya IoT Edge Gateway, and Tuya Developer Platform work.
The device reports data to Tuya IoT Edge Gateway
The device connects to and communicates with Tuya IoT Edge Gateway. The device can report data to the platform in the following ways.
The application server sends commands to the device.
After the connection is established, Tuya IoT Edge Gateway can send commands to the device as follows.
After the device connects to Tuya IoT Edge Gateway over the MQTT protocol, the platform communicates with the device through topics. The following table lists the preset topics:
Type | Topic | Publisher | Subscriber | Purpose |
---|---|---|---|---|
Device sync topic | gateway_id/{gateway_id}/devices/sync | Device | Platform | Activate devices. |
Device sync topic | gateway_id/{gateway_id}/devices/sync/response | Platform | Device | The platform returns an activation response. |
Device command topic | gateway_id/{gateway_id}/commands | Platform | Device | The platform sends commands to the device. |
Device command topic | gateway_id/{gateway_id}/commands/response | Device | Platform | The device returns a command response. |
Device property topic | gateway_id/{gateway_id}/properties/report | Device | Platform | Report device properties. |
Device property topic | gateway_id/{gateway_id}/properties/report/response | Platform | Device | The platform returns a response to property reporting. |
Device online/offline topic | gateway_id/{gateway_id}/devices/status | Device | Platform | Report device online and offline events. |
{gateway_id} is used to identify the target device of the topic route. When the device subscribes to the topic or sends messages to the topic, this value needs to be replaced with the gateway ID used when the device establishes an MQTT connection with the platform.
The MQTT protocol specifies the quality of service (QoS) to ensure the reliability of message delivery in different network environments. The MQTT protocol has designed three QoS levels.
The higher the QoS level, the more complex the process, and the greater the consumption of system resources. Applications can choose the appropriate QoS level based on the network scenarios and business requirements.
We recommend that you set the QoS level to 1
for message publishing and subscription during third-party device integration.
On the Tuya Developer Platform, devices support the connect
message interface of the MQTT protocol. After the authentication is passed, the MQTT connection between the device and the platform is established.
Parameter | Required | Type | Description |
---|---|---|---|
broker | Yes | String | The IP address of Tuya IoT Edge Gateway, to be provided by Tuya. |
port | Yes | String | The TCP port number is 58090 and the SSL/TLS port number is 21883 . |
clientId | Yes | String | The unique ID of a specified third-party gateway. |
username | Yes | String | The username to identify a third-party manufacturer, to be provided by Tuya. |
password | Yes | String | The value of a password is generated using the MD5 encryption algorithm. For more information, see password generation rules. |
SSL/TLS is a one-way authentication mode.
Concatenate the clientId and username variables, and then encrypt the concatenated variables with the MD5 encryption algorithm.
The following sample shows how to generate a connection password for Go.
func generatePassword() string {
var (
clientId string
username string
password string
)
// clientId username Assign values first
password = md5V(clientId + username)
return password
}
// MD5 encryption algorithm function
func md5V(str string) string {
h := md5.New()
h.Write([]byte(str))
return hex.EncodeToString(h.Sum(nil))
}
The following table lists the return codes when a device establishes a connection to the platform over the native MQTT protocol.
Return code | Identifier | Description | Reason |
---|---|---|---|
0x00 | connection accepted | The connection is successful. | The connection is successful. |
0x01 | unacceptable protocol version | The request is rejected because the protocol version is incorrect. | The server does not support the MQTT protocol version, based on which the client sends the request. |
0x02 | identifier rejected | The request is rejected because the client identifier is invalid. | The clientId is not in the required format or the heartbeat interval does not meet the platform requirements. |
0x03 | server unavailable | The request is rejected because the server is unavailable. | The platform service is unavailable. |
0x04 | bad user name or password | The request is rejected because the username or password is incorrect. | The username or password is incorrect. |
0x05 | not authorized | The request is rejected because the client is not authorized. | The client is not authorized to establish a connection. |
It is used for device synchronization. After the device gateway is connected to the MQTT server of Tuya IoT Edge Gateway, send the device synchronization command. Then, Tuya IoT Edge Gateway will add or update devices based on the reported device data.
Sub-devices must be synchronized before subsequent operations. Otherwise, the system will return an error message to indicate that devices cannot be found.
Topic
Uplink: gateway_id/{gateway_id}/devices/sync
Downlink: gateway_id/{gateway_id}/devices/sync/response
Field | Required | Type | Description |
---|---|---|---|
t | Yes | Integer | The Unix timestamp. |
request_id | Yes | String | The identifier of a specified request. We recommend that the request identifier be generated by using the UUID algorithm. |
param | Yes | Object | The object. |
param.cid | Yes | String | The ID of a specified third-party device, to be defined by the device. It can be a unique readable identifier of the device, such as device SN or IMEI. |
param.product_id | Yes | String | The ID of a specified product. |
param.vendor_code | Optional | String | The code of a specified manufacturer. |
param.comm_type | Optional | String | The communication mode between a device and a gateway. Enter MQTT . |
param.device_ip | Optional | String | The IP address of a specified device. |
param.mac_address | Optional | String | The MAC address of a specified device. |
param.device_desc | Optional | String | The description of a specified device. |
param.install_location | Optional | String | The installation location of a specified device. |
param.device_server_id | Optional | String | The ID of a specified device server. |
param.product_sub_type | Optional | String | The sub-type of a specified product. |
Field | Required | Type | Description |
---|---|---|---|
t | Yes | Integer | The Unix timestamp. |
request_id | Yes | String | The unique identifier of a request. |
param | Yes | Object | The object. |
param.cid | Yes | String | The CID of a specified device. |
param.msg | Yes | String | The error message. See Global error codes. |
param.code | Yes | Integer | The execution result of a specified command. If the result is 0 , it indicates success. All other values indicate failure. |
Topic: gateway_id/{gateway_id}/devices/sync
Data format:
{
"t":1627280***,
"request_id":"cd0fd3c3-bd15-42f6-8bf9-d2***",
"param":{
"cid":"92dda538fc***",
"product_id":"sie4***",// Enter the PID of a specified product
"vendor_code":"",// The code of a specified device manufacturer
"comm_type":"",
"device_ip":"",
"mac_address":"",
"device_name":"",
"device_desc":"",
"install_location":"",
"device_server_id":"",
"product_sub_type":""
}
}
Topic: gateway_id/{gateway_id}/devices/sync/response
Data format:
{
"t":1627280***,
"request_id":"cd0fd3c3-bd15-42f6-8bf9-d23***",
"param":{
"cid":"92dda538fc***",
"msg":"success",
"code":0
}
}
Send a device control command from the platform to a specified smart device. After the command is sent, the device needs to return the execution result to the platform in time. If the device does not respond, the platform will consider the command execution timed out.
Topic
Downlink: gateway_id/{gateway_id}/commands
Uplink: gateway_id/{gateway_id}/commands/response
Field | Required | Type | Description |
---|---|---|---|
t | Yes | Integer | The Unix timestamp. |
cid | Yes | String | The ID of a specified third-party device, to be defined by the device. It can be a unique readable identifier of the device, such as device SN or IMEI. |
request_id | Yes | String | The identifier of a specified request. We recommend that the request identifier be generated by using the UUID algorithm. |
param | Yes | Object | The object. |
param.id | Yes | String | The ID of a specified data point. |
param.value | Yes | Not fixed | The execution parameters of device commands. |
Respond the command in JSON format.
Field | Required | Type | Description |
---|---|---|---|
t | Yes | Integer | The Unix timestamp. |
cid | Yes | String | The ID of a specified third-party device, to be defined by the device. It can be a unique readable identifier of the device, such as device SN or IMEI. |
request_id | Yes | String | The unique identifier of a specified request. We recommend that the request identifier be generated with the UUID algorithm. |
param | Yes | Object | The object. |
param.msg | Yes | String | The execution result. Enter success to indicate a successful execution. |
param.code | Yes | Integer | The execution result of a specified command. If the result is 0 , it indicates success. All other values indicate failure. |
Topic: gateway_id/{gateway_id}/commands
Data format:
{
"t":1627280***,
"cid": "92dda538fc2e636fd4b0***",
"request_id":"cd0fd3c3-bd15-42f6-8bf9-d23***",
"param": {
"id":"1",
"value": "1"
}
}
Topic: gateway_id/{gateway_id}/commands/response
Data format:
{
"t":1627280***,
"cid":"92dda538fc2e636fd****",
"request_id":"cd0fd3c3-bd15-42f6-8bf9-d23***",
"param": {
"msg": "success",
"code":0
}
}
The device reports property data to the platform in the format defined in the product model. After receiving the reported message, the platform will return the execution result to the device.
Topic
Uplink: gateway_id/{gateway_id}/properties/report
Downlink: gateway_id/{gateway_id}/properties/report/response
Field | Required | Type | Description |
---|---|---|---|
t | Yes | Integer | The UTC timestamp when a device connects to the platform. |
request_id | Yes | String | The unique identifier of a specified request. We recommend that the request identifier be generated with the UUID algorithm. |
param | Yes | Object | The object. |
param.cid | Yes | String | The ID of a specified third-party device, to be defined by the device. It can be a unique readable identifier of the device, such as device SN or IMEI. |
param.product_id | Yes | String | product_id : the ID of your product. |
param.id | Yes | String | The ID of a specified data point. |
param.value | Yes | Refer to the DP type | The property value of a specified device. |
Field | Required | Type | Description |
---|---|---|---|
t | Yes | Integer | The Unix timestamp. |
request_id | Yes | String | The unique identifier of a request. |
param | Yes | Object | The object. |
param.cid | Yes | String | The ID of a specified third-party device, to be defined by the device. It can be a unique readable identifier of the device, such as device SN or IMEI. |
param.msg | Yes | String | The error message. See Global error codes. |
param.code | Yes | Integer | The execution result of a specified command. If the result is 0 , it indicates success. All other values indicate failure. |
gateway_id/{gateway_id}/properties/report
{
"t":1627280***,
"request_id":"cd0fd3c3-bd15-42f6-8bf9-d23***",
"param":{
"cid":"ee1880aca***",
"product_id":"r2fz6uv6a***",
"id":"1",
"value":"run"
}
}
Topic: gateway_id/{gateway_id}/properties/report/response
Data format:
{
"t":1627280***,
"request_id":"cd0fd3c3-bd15-42f6-8bf9-d23***",
"param":{
"cid":"92dda538***",
"msg":"success",
"code":0
}
}
The device gateway needs to regularly synchronize the device’s online and offline status to the Tuya IoT Edge Gateway. We recommend that the status of the sub-devices be reported at least once every minute.
Topic
Uplink: gateway_id/{gateway_id}/devices/status
Field | Required | Type | Description |
---|---|---|---|
t | Yes | Integer | The UTC timestamp when a device connects to the platform. |
request_id | Yes | String | The unique identifier of a specified request. We recommend that the request identifier be generated with the UUID algorithm. |
param | Yes | Object | The array of objects. |
param.cid | Yes | String | The ID of a specified third-party device, to be defined by the device. It can be a unique readable identifier of the device, such as device SN or IMEI. |
param.status | Yes | Bool | The device status. true means online and false means offline. |
Topic: gateway_id/{gateway_id}/devices/status
Data format:
{
"t":1627280***,
"request_id":"cd0fd3c3-bd15-42f6-8bf9-d23***",
"param": [{
"cid": "92dda538fc2e6***",
"status":true
},
{
"cid": "da538fc2eb***",
"status":false
}
]
}
To help you make API requests, we provide development samples for Java, PHP, and Go, and demonstrate basic features such as service connection, verification, encryption and decryption of sensitive information, and message subscription. Development libraries for more languages will be available in the near future.
Add the dependency definitions shown below to the Maven POM file.
<dependencies>
<dependency>
<groupId>org.eclipse.paho</groupId>
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>
// Message publishing
public class Publish {
public static void main(String[] args) {
String clientId = ""; // The gatewayID that identifies the third-party manufacturer
String topic = ""; // The topic
String content = ""; // The content
int qos = 1; // The QoS
String broker = ""; // The IP address of the server
String userName = ""; // The username, to be provided by Tuya
String password = ""; // The password. See password generation rules.
// The memory
MemoryPersistence persistence = new MemoryPersistence();
try {
// Create a client
MqttClient sampleClient = new MqttClient(broker, clientId, persistence);
// Create link parameters
MqttConnectOptions connOpts = new MqttConnectOptions();
// Remember state on reboot and reconnection
connOpts.setCleanSession(false);
// Set a username used for connection
connOpts.setUserName(userName);
connOpts.setPassword(password.toCharArray());
// Establish a connection
sampleClient.connect(connOpts);
// Create a message
MqttMessage message = new MqttMessage(content.getBytes());
// Set the message QoS
message.setQos(qos);
// Publish the message
sampleClient.publish(topic, message);
// Close a connection
sampleClient.disconnect();
// Close the client
sampleClient.close();
} catch (MqttException me) {
System.out.println("reason " + me.getReasonCode());
System.out.println("msg " + me.getMessage());
System.out.println("loc " + me.getLocalizedMessage());
System.out.println("cause " + me.getCause());
System.out.println("excep " + me);
me.printStackTrace();
}
}
}
// Message subscription
public class Subscribe {
public static void main(String[] args) throws MqttException {
String clientId = ""; // The gatewayID that identifies the third-party manufacturer
String host = ""; // The IP address of the server
String topic = "gateway/in/" + clientId; // The topic
int qos = 1; // The QoS
String userName = ""; // The username, to be provided by Tuya
String password = ""; // The password. See password generation rules.
try {
// In MemoryPersistence, set where to save the clientid. The clientid is saved in the memory by default.
MqttClient client = new MqttClient(host, clientid, new MemoryPersistence());
// Set the MQTT connection
MqttConnectOptions options = new MqttConnectOptions();
// Set whether to clear the session. `false` means that the server will keep the connection record of the client, and `true` means that a new identity is used every time the client connects to the server.
options.setCleanSession(true);
// Set a username used for connection
options.setUserName(userName);
// Set a password used for connection
options.setPassword(passWord.toCharArray());
// Set a timeout value in seconds
options.setConnectionTimeout(10);
// Set a session heartbeat interval in seconds
options.setKeepAliveInterval(20);
// Set a callback function
client.setCallback(new MqttCallback() {
public void connectionLost(Throwable cause) {
}
public void messageArrived(String topic, MqttMessage message) throws Exception {
// Message processing
System.out.println("message content:"+new String(message.getPayload()));
}
public void deliveryComplete(IMqttDeliveryToken token) {
}
});
client.connect(options);
// Subscribe to the message
client.subscribe(topic, qos);
} catch (Exception e) {
e.printStackTrace();
}
}
}
package main
import (
"fmt"
mqtt "github.com/eclipse/paho.mqtt.golang"
"log"
"os"
"time"
)
const broker = "" // The IP address of the server. Contact Tuya's administrator.
const username = "" // The username, to be provided by Tuya.
const password = "" // The password. See password generation rules.
const clientID = "" // The gatewayID, the gateway ID of the third-party manufacturer.
const topic = "" // The topic
const port = "21883" // The port number of the server
// Message subscription topic = "gateway/in/" + clientID
// Message publishing topic = "gateway/out/" + clientID
var f mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) {
fmt.Printf("TOPIC: %s\n", msg.Topic())
fmt.Printf("MSG: %s\n", msg.Payload())
}
func main() {
mqtt.DEBUG = log.New(os.Stdout, "", 0)
mqtt.ERROR = log.New(os.Stdout, "", 0)
opts := mqtt.NewClientOptions().AddBroker(fmt.Sprintf("tcp://%s:%s", broker, port)).SetClientID(clientID).
SetUsername(username).SetPassword(password)
opts.SetKeepAlive(60 * time.Second)
// Set a message callback function
opts.SetDefaultPublishHandler(f)
opts.SetPingTimeout(1 * time.Second)
c := mqtt.NewClient(opts)
if token := c.Connect(); token.Wait() && token.Error() != nil {
panic(token.Error())
}
// Subscribe to the topic
if token := c.Subscribe(topic, 0, nil); token.Wait() && token.Error() != nil {
fmt.Println(token.Error())
os.Exit(1)
}
// Publish the message
token := c.Publish(topic, 0, false, "Hello World")
token.Wait()
time.Sleep(6 * time.Second)
// Unsubscribe from the topic
if token := c.Unsubscribe(topic); token.Wait() && token.Error() != nil {
fmt.Println(token.Error())
os.Exit(1)
}
// Close a connection
c.Disconnect(250)
time.Sleep(1 * time.Second)
}
Send a message:
<?php
require(""); // Import mqtt class file.
$server = ''; // The IP address of the server. Contact Tuya's administrator.
$port = 21883; // The port number of the server.
$username = ''; // The username, to be provided by Tuya.
$password = ''; // The password. See password generation rules.
$client_id = '' // The gatewayID that identifies the third-party manufacturer.
$mqtt = new Bluerhinos\phpMQTT($server, $port, $client_id); // Instantiate the MQTT class
if ($mqtt->connect(true, NULL, $username, $password)) // Establish a connection
{
$topic = 'gateway/out/'.$client_id; // The topic
$key = '' // The encryption key, which is the password used for connection authentication
$msg = ''; // The message in the above-mentioned format
// qos = 0: Send only once, regardless of whether it is received or not
// qos = 1: Keep sending a message if no result is returned, and a duplicate message might be received
// qos = 2: Make sure the message must be received and not repeated
$mqtt->publish($topic, $msg, 0);// Send the data
$mqtt->close(); // Close a connection
}
else
{
echo "Time out!\n";
}
Subscribe to a message
<?php
require(""); // Import mqtt class file.
$server = ''; // The IP address of the server. Contact Tuya's administrator.
$port = 21883; // The port number of the server.
$username = ''; // The username, to be provided by Tuya.
$password = ''; // The password. See password generation rules.
$client_id = '' // The gatewayID that identifies the third-party manufacturer.
$mqtt = new Bluerhinos\phpMQTT($server, $port, $client_id);
$mqtt->debug = true;
if(!$mqtt->connect(true, NULL, $username, $password))
{
echo "Connection failed!\n";
exit(1);
}
$topic = 'gateway/in/'.$client_id; // The topic
// Subscription list
$topics = [
$topic => ['qos' => 0, 'function' => 'procmsg'],
];
$mqtt->subscribe($topics, 0);
while ($mqtt->proc()){}
$mqtt->close();
function procmsg($topic, $msg) // Message processing function
{
$key = ''
echo $msg;
}
If errors occur when you make API requests, custom error messages will be returned. This section describes the global error codes.
Error code | Error message | Description |
---|---|---|
400 | tedge error: uplink specific error message | The uplink error to be defined by the uplink service. |
500 | system error, please contact the admin | A system error occurs while processing your request. Contact your administrator. |
1001 | request time is invalid | The request time is invalid. |
1100 | params is empty | The parameter is empty. |
1101 | params range invalid | The parameter range is invalid. |
1102 | params is null | The parameter is null. |
1103 | params type is incorrect | The parameter type is incorrect. |
1104 | request pid is invalid | The PID is invalid. |
1105 | id does not exist in pid | The data point ID does not exist in the PID. |
2001 | device is offline | The device is offline. |
2002 | command or value not support | The command or value is not supported. |
2003 | device not exist | The device does not exist. |
Is this page helpful?
YesFeedbackIs this page helpful?
YesFeedback