
# 快速接入
本章将使用SmokeDetector样例指导您快速体验产品模型的创建、样例的配置与启动、以及SDK基本功能的使用。为了方便体验，提供了一个烟感的产品模型，烟感会上报烟雾值、温度、湿度、烟雾报警，还支持响铃报警命令。 以烟感为例，体验消息上报、属性上报、命令响应等功能。
#### 创建产品
为了方便体验，提供了一个烟感的产品模型，烟感会上报烟雾值、温度、湿度、烟雾报警、还支持响铃报警命令。以烟感为例，体验消息上报、属性上报等功能。
1. 访问[设备接入服务](https://www.huaweicloud.com/product/iothub.html)，单击"管理控制台"进入设备接入控制台，选择您的实例，单击实例卡片进入。
2. 单击左侧导航栏"产品"，单击页面左侧的"创建产品"。 
   图1产品-创建产品   
   ![](https://support.huaweicloud.com/sdkreference-iothub/figure/zh-cn_image_0000002358714305.png "点击放大")
   
   
3. 根据页面提示填写参数，然后单击"确定"完成产品的创建。 
   
   |                                                                     **基本信息**                                                                     ||
   |---|---|
   | 所属资源空间 | 平台自动将新创建的产品归属在默认资源空间下。如需归属在其他资源空间下，下拉选择所属的资源空间。如无对应的资源空间，请先创建[资源空间](https://support.huaweicloud.com/usermanual-iothub/iot_01_0006.html)。 |
   | 产品名称   | 自定义。支持字母、数字、下划线（_）、连字符（-）的字符组合。                                                                                                          |
   | 协议类型   | 选择"MQTT"。                                                                                                                                |
   | 数据格式   | 选择"JSON"。                                                                                                                                |
   | 设备类型选择 | 选择"自定义类型"                                                                                                                                |
   | 设备类型   | 填写"smokeDetector"                                                                                                                        |
   | **高级配置**                                                                                                                                         ||
   | 产品ID   | 不填写                                                                                                                                      |
   | 产品描述   | 请根据实际情况填写。                                                                                                                               |
      
   图2创建产品-MQTT   
   ![](https://support.huaweicloud.com/sdkreference-iothub/figure/zh-cn_image_0000001981306829.png "点击放大")
   
   
 
 #### 上传产品模型
1. 单击下载烟感产品模型[smokeDetector](https://iot-developer.obs.cn-north-4.myhuaweicloud.com:443/smokeDetector.zip)，获取产品模型文件。
2. 找到创建的产品，单击产品进入产品详情页。
3. 选择"基本信息"页签，单击"上传模型文件"，上传[1]获取的产品模型文件。
   
   图3产品-上传产品模型   
   ![](https://support.huaweicloud.com/sdkreference-iothub/figure/zh-cn_image_0000001981312633.png "点击放大")
   
   
 
#### 注册设备
1. 选择左侧导航栏"设备 \> 所有设备"，单击"注册设备"。
2. 根据页面提示信息填写参数，然后单击"确定"。 
   
   | 参数名称   | 说明                                |
   |:---|:---|
   | 所属资源空间 | 确保和创建的产品归属在同一个资源空间。               |
   | 所属产品   | 选择创建的产品。                          |
   | 设备标识码  | 即nodeID，设备唯一物理标识。可自定义，由英文字母和数字组成。 |
   | 设备名称   | 即device_name，可自定义。                |
   | 设备认证类型 | 选择"密钥"。                           |
   | 密钥     | 设备密钥，可自定义。若不填写密钥，物联网平台会自动生成密钥。    |
      
   设备注册成功后保存设备标识码、设备ID、密钥。
   
   
 
#### 设备接入
1. 获取接入地址。可在控制台的"总览 \> 接入信息"中查看。 
   图4接入信息-设备侧MQTT接入地址   
   ![](https://support.huaweicloud.com/sdkreference-iothub/figure/zh-cn_image_0000002379668860.png "点击放大")
   
   
2. 打开文件samples/rule /rule_demo.go，将获取的接入地址、设备ID以及设备密钥填入对应位置。 
   ```
   // 创建一个设备并初始化
   authConfig := &config2.ConnectAuthConfig{
   Id:           "your device id",
   Servers:      "mqtts://{MQTT_ACCESS_ADDRESS}:8883",
   Password:     "your password",
   ServerCaPath: "iotda server ca path",  
   }
   authConfig.RuleEnable = true
   device := device2.NewMqttDevice(authConfig)
   if device == nil {
   glog.Warningf("create mqtt device failed.")
   return
   }
   ```
   ![](https://support.huaweicloud.com/sdkreference-iothub/public_sys-resources/note_3.0-zh-cn.png)
   平台的证书可以从源码中[/samples/resources/root.pem](https://github.com/huaweicloud/huaweicloud-iot-device-sdk-go/blob/main/samples/resource/root.pem)下载。
   
   
3. 编译及运行。运行demo后输出示例如下，如果存在connect result is : true则表示连接成功。 
   图5连接成功   
   ![](https://support.huaweicloud.com/sdkreference-iothub/figure/zh-cn_image_0000002394853209.png "点击放大")
   
   
4. 查看设备运行情况。在控制台的设备详情页面可以查看到设备已在线以及设备上报的物模型数据。 
   图6设备列表-设备在线   
   ![](https://support.huaweicloud.com/sdkreference-iothub/figure/zh-cn_image_0000001950367902.png "点击放大")
   
   
![](https://support.huaweicloud.com/sdkreference-iothub/public_sys-resources/notice_3.0-zh-cn.png)
SDK默认提供断线重连功能，当调用设备初始化后，当由于网络不稳定、网络不可达、平台主动断开连接等，导致连接失败，会在后台自动申请重连。若是客户不需要该机制，请见：[断线重连](https://github.com/huaweicloud/huaweicloud-iot-device-sdk-go/blob/main/README_CN.md#415-断链重连)文档。
#### 消息上报
消息上报是指设备向平台上报消息。
1. ./samples/message/message_demo.go是一个上报消息例子，通过SendMessage方法将消息上报给自定义topic，如果message中未填写topic，消息将会上报给平台的默认topic。
   ```
   """ create device code here """
       //向平台发送消息
       message := model.Message{
   Topic:   "{custom topic}",
   Payload: "first message",
   }
   for {
   sendMsgResult := device.SendMessage(message)
   glog.Infof("send message %v", sendMsgResult)
   time.Sleep(1 * time.Second)
   }
   ```
   
2. 在设备接入控制台，选择"设备 \> 所有设备"-查看设备是否在线。
   图7设备列表-设备在线   
   ![](https://support.huaweicloud.com/sdkreference-iothub/figure/zh-cn_image_0000001950367902.png "点击放大") 
3. 选择对应设备，单击"详情"，在设备详情页面启动设备消息跟踪。
   图8消息跟踪-启动消息跟踪   
   ![](https://support.huaweicloud.com/sdkreference-iothub/figure/zh-cn_image_0000001950378340.png "点击放大") 
4. 查看消息跟踪，确认平台是否收到设备消息。
   ![](https://support.huaweicloud.com/sdkreference-iothub/public_sys-resources/notice_3.0-zh-cn.png)
   消息跟踪会有一定的延时，如果没有看到数据，请等待后刷新。
   图9消息跟踪-查看device_sdk_java消息跟踪   
   ![](https://support.huaweicloud.com/sdkreference-iothub/figure/zh-cn_image_0000001981497769.png "点击放大") 
 
#### 属性上报
属性上报指的是设备将当前属性值上报给平台。
1. ./samples/properties/device_properties.go是一个属性上报/设置的例子，可以通过ReportProperties方法将属性上报给平台。
   ```
   < create device code here ... >
       // 设备上报属性
    props := model.DevicePropertyEntry{
   ServiceId: "smokeDetector",
   EventTime: iot.GetEventTimeStamp(),
   Properties: test_model.DemoProperties{
   Temperature: 28,
   },
    }
    var content []model.DevicePropertyEntry
    content = append(content, props)
    services := model.DeviceProperties{
   Services: content,
    }
    device.ReportProperties(services)
   ```
   
2. 在左侧导航栏中选择"设备 \> 所有设备"，选择注册的设备进行查看，在"设备影子"处可以看到刚刚上报的属性值。
   图10设备影子-temperature   
   ![](https://support.huaweicloud.com/sdkreference-iothub/figure/zh-cn_image_0000002394220836.png "点击放大")
   ![](https://support.huaweicloud.com/sdkreference-iothub/public_sys-resources/note_3.0-zh-cn.png)
   若是没有查看到属性上报值，请查看是否进行了：[上传产品模型]。
   
 
#### 命令下发
设置命令监听器用来接收平台下发的命令，在回调接口里，将对命令进行处理，并上报响应。
1. ./samples/command/platform_command.go是一个处理平台命令下发的例子。使用CommandHandler函数作为命令监听器，即：
   ```
   mqttDevice.Client.AddCommandHandler(func(command model.Command) (bool, interface{}) {
   glog.Infof("I get command from platform")
   glog.Infof("command device id is %s", command.ObjectDeviceId)
   glog.Infof("command name is %s", command.CommandName)
   glog.Infof("command serviceId is %s", command.ServiceId)
   glog.Infof("command params is %v", command.Paras)
   return true, map[string]interface{}{
   "cost_time": 12,
   }
   })
   ```
   
2. 当device收到命令时将自动调用CommandHandler函数，并将函数响应结果返回给平台。
3. 执行main方法，在平台给设备下发命令，查看代码日志如下。
   图11go语言命令下发   
   ![](https://support.huaweicloud.com/sdkreference-iothub/figure/zh-cn_image_0000002361168174.png "点击放大") 
 
