หน้านี้ยังไม่พร้อมใช้งานในภาษาท้องถิ่นของคุณ เรากำลังพยายามอย่างหนักเพื่อเพิ่มเวอร์ชันภาษาอื่น ๆ เพิ่มเติม ขอบคุณสำหรับการสนับสนุนเสมอมา

Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive
Help Center/ IoT Device Access/ Getting Started/ Quick Device Access - Property Reporting and Command Receiving/ Using a Virtual Smart Street Light to Communicate with the Platform (Java SDK)

Using a Virtual Smart Street Light to Communicate with the Platform (Java SDK)

Updated on 2024-12-31 GMT+08:00

Overview

This section describes how to connect a device to Huawei Cloud IoTDA through MQTTS/MQTT using Java code, implement southbound data reporting and command delivery using platform APIs, and receive messages subscribed by the northbound server using the application-side sample code. Taking a smart street light as an example, the device reports information such as luminance to IoTDA, and an application receives device data pushed by IoTDA.

Prerequisites

  • You have installed JDK 1.8 or later.
  • You have installed IntelliJ IDEA. If you have not installed IntelliJ IDEA, visit the IntelliJ IDEA official website to download and install it.

Uploading a Product Model

A product model is a JSON file that describes device capabilities. It defines basic device properties and message formats for data reporting and command delivery. Defining a product model is to construct an abstract model of a device in the platform to enable the platform to understand the device function.

Procedure

  1. Access the IoTDA service page and click Access Console.
  2. Choose Products in the navigation pane and click Create Product.

    Figure 1 Creating a product

  3. In the displayed dialog box, set parameters based on your requirements.

    Figure 2 Creating a product - MQTT

  4. Download the model file. For details about the development process, see Developing a Product Model Online.
  5. After the product is created, click the product, and then click Import from Local to upload the downloaded model file. The model file does not need to be decompressed, and the package name cannot contain brackets.

    Figure 3 Uploading a product model - MQTT

Creating a Device

  1. In the navigation pane, choose Devices > All Devices, and click Register Device.

    Figure 4 Registering a device

  2. In the displayed dialog box, configure the parameters by referring to the following figure (select the created product), and click OK. If you do not specify Secret, a secret will be automatically generated by the platform. In this example, the secret is automatically generated.

    Figure 5 Registering a device (test123)

  3. After the device is created, save the device ID and secret, which will be used for device connection.

    Figure 6 Device registered

Importing Sample Code

  1. Download the Java demo.
  2. Open the IDEA developer tool and click Import Project.

  3. Select the Java demo downloaded in 1 and click Next.

  4. Import the sample code.

Establishing a Connection

To connect a device or gateway to the platform, upload the device information to bind the device or gateway to the platform.
  1. Before establishing a connection, modify the following parameters:
    1
    2
    3
    4
    5
    // MQTT connection address of IoTDA
    static String serverIp = "iot-mqtts.cn-north-4.myhuaweicloud.com";
    // Device ID and secret obtained during device registration (Replace them with the actual values.)
    static String deviceId = "yourDeviceID"; // device_id obtained during device registration
    static String secret = "yourSecret";     // secret obtained during device registration
    
    • serverIp indicates the address used by devices to access IoTDA using MQTT. For details about how to obtain the address, see Obtaining Resources.
    • device_id and secret indicate the device ID and secret, which can be obtained after the device is registered.
  2. Run the program. The device is displayed as online on the platform.
    Figure 7 Device list - Device online status

Reporting Properties

A device reports its properties to IoTDA. (The sample code implements scheduled reporting. You can view the data reported by the device in IoTDA by referring to Viewing Reported Data.)

1
2
// Report JSON data. service_id must be the same as that defined in the product model.
String jsonMsg = "{\"services\":[{\"service_id\":\"BasicData\",\"properties\":{\"luminance\":32},\"eventTime\":null}]}";
  • The message body jsonMsg is assembled in JSON format, and service_id must be the same as that defined in the product model. properties indicates a device property.
  • luminance indicates the street light brightness.
  • eventTime indicates the UTC time when the device reports data. If this parameter is not specified, the system time is used by default.

After a device or gateway is connected to the platform, you can call publish(String topic,MqttMessage message) of MqttAsyncClient to report device properties to the platform.

Viewing Reported Data

After the main method is called, you can view the reported device property data on the device details page. For details about the API, see Device Reporting Properties.

Figure 8 Viewing reported data - luminance
NOTE:

If no latest reported data is displayed on the device details page, modify the services and properties in the product model to ensure that the services and properties reported by the device are consistent with those in the product model. If they are inconsistent, the data reported by the device is not available on the historical data page. Alternatively, delete all services on the Basic Information page.

Delivering a Command

  1. In the navigation pane, choose API Explorer.

    Figure 9 Navigation pane -API retrieval and debugging

  2. Locate the row that contains the device command. For details about the delivered parameters, see the figure (consistent with those in the product model). Then, click Debug to send the command.

    • service_id indicates the service ID, for example, BasicData.
    • command_name indicates the command name, for example, lightControl.
    • paras indicates a delivered parameter, for example, {"switch":"ON"}.

    You can view the received commands on the device. (The sample code has implemented the subscription to the command receiving topic.)

Obtaining Data Reported by a Device from the Cloud

The following uses AMQP as an example to describe how to obtain data reported by a device to the cloud.

  1. Obtain the Java AMQP access demo.
  2. Log in to the console, choose Rules > Data Forwarding, and click Create Rule to create a data forwarding rule.

    Figure 10 Data forwarding - Creating a rule

  3. On the Set Forwarding Data page, configure parameters, and click Create Rule.

    Figure 11 Data forwarding - Creating a property reporting rule

    Parameter

    Description

    Rule Name

    Customize a rule name.

    Description

    Describe the rule.

    Data Source

    Select Device property.

    Trigger

    Select Device property reported.

    Resource Space

    Select All resource spaces.

  4. Set the forwarding target. Note that you need to click Preset Access Credential to download the file.

    Figure 12 Creating a forwarding target - to an AMQP push message queue

    Parameter

    Description

    Forwarding Target

    Select AMQP message queue.

    Access Credential

    Click Preset Access Credential and save the downloaded file, which includes access_key and access_code.

    Message Queue

    DefaultQueue is selected by default.

  5. Click Enable Rule.

    Figure 13 Enabling a rule - Forwarding data to AMQP

  6. Modify the parameters in the AMQP sample code obtained in 1.

    • yourAccessKey: access key of the access credential. For details about how to obtain it, see 4.
    • yourAccessCode: access code of the access credential. For details about how to obtain it, see 4.
    • yourAMQPUrl: AMQP domain name. You can log in to the console, choose Overview, and click Access Addresses to obtain the domain name, as shown in the following figure.
      Figure 14 Access information - AMQP access address
    • yourQueue: queue name. Use the default queue DefaultQueue.

  7. AMQP data is received successfully.

Additional Information

For more development guides, see Using IoT Device SDKs for Access and Using MQTT Demos for Access.

เราใช้คุกกี้เพื่อปรับปรุงไซต์และประสบการณ์การใช้ของคุณ การเรียกดูเว็บไซต์ของเราต่อแสดงว่าคุณยอมรับนโยบายคุกกี้ของเรา เรียนรู้เพิ่มเติม

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback