This article is shared from Huawei Cloud Community " How to connect iOS devices to Huawei Cloud IoT Platform through MQTT protocol: Flutter and Swift two methods ", author: Zhang Jian.
Preface
In today's era, IoT technology is gradually changing the way we live and work. Huawei Cloud IoTDA service provides developers with an open, stable, and reliable infrastructure to achieve seamless connection and two-way communication between devices and the cloud. This article describes how to use Flutter and Swift two development languages to connect to the Huawei Cloud IoT platform.
Preparation
You need to complete the following two steps
- Registered and activated Huawei Cloud IoTDA service
- Registered a Huawei Cloud IoTDA device and obtained the DeviceId (device ID) and Secret (device key)
Additionally, make sure you are familiar with the basic workings of the MQTT protocol.
Flutter way
Flutter is an open source UI software development toolkit developed by Google for developing cross-platform applications.
Add necessary dependencies
Add dependencies in pubspec.yaml and get the latest commitId from the Github repository.
dependencies:
huaweicloud_iot_device_sdk:
git:
url: https://github.com/Shoothzj/huaweicloud-iot-device-sdk-dart.git
ref: latest commitId
Best practices for dart sdk
- Concurrency: Dart has built-in async and await mechanisms, which makes it different from other languages such as Java or Python. It does not need to provide asynchronous and synchronous methods respectively. And once it comes to network programming, etc., it can only be done asynchronously.
- Exceptions: The SDK can customize an exception type. To throw and parse exceptions, asynchronous scenarios can also simply use this mechanism.
Connect devices and report messages
import 'dart:io';
import 'package:huaweicloud_iot_device_sdk/src/device_client.dart';
void main(List<String> arguments) async {
var deviceClient = DeviceClient(
host: "iot-mqtts.cn-north-4.myhuaweicloud.com",
port: 8883,
deviceId: deviceId,
secret: deviceSecret,
disableHmacSha256Verify: true,
useTls: true,
disableTlsVerify: true);
await deviceClient.connect();
await deviceClient.reportDeviceMessage(content: "ddddd");
}
In the above code, we first imported the necessary libraries, then initialized the **DeviceClient** object and connected to the Huawei Cloud IoTDA service. Finally, we send a simple message to verify that the connection was successful.
Swift way
Swift is Apple’s powerful language for iOS, macOS, watchOS, and tvOS application development.
Add necessary dependencies
Add huaweicloud-iot-device-sdk-swift as a dependency in Package.Swift. You can get the latest commitId from the Github repository.
let package = Package(
name: "YourProject",
platforms: [
.iOS(.v13),
.macOS(.v13),
.tvOS(.v13),
],
dependencies: [
.package(url: "https://github.com/your-username/your-package-name.git", .revision("latest commitId")),
],
targets: [
.target(
name: "YourProject",
dependencies: ["HuaweiCloudIoTDevice"]),
]
)
best practices for swift sdk
- Concurrency: In Swift, async/await is a relatively new feature, which was only introduced in Swift 5.5. Therefore, you will find that many existing Swift SDKs and libraries still rely on the callback pattern to handle asynchronous operations. In the callback pattern, you typically pass a function (closure) to an asynchronous function that calls the function after it completes its operation, rather than returning a value. This allows for non-blocking handling of asynchronous operations, but it can also lead to "callback hell", especially when multiple asynchronous operations need to be concatenated.
- Exceptions: Swift also supports exception handling to manage runtime errors. You can define your own exception types, throw exceptions through throw , and use do-catch statements to catch and handle exceptions.
Connect devices and report messages
let deviceClient = DeviceClient(host: "iot-mqtts.cn-north-4.myhuaweicloud.com", port: 1883, deviceId: "b7cfa256-49df-4d2f-81c3-76697c69c03c_aaaa", secret: "18209205026", disableHmacSha256Verify: true)
let connectResult: Bool = try deviceClient.connect()
wait(for: [XCTestExpectation(description: "wait for connect")], timeout: 10)
try deviceClient.reportDeviceMessage(content: "hahaha")
wait(for: [XCTestExpectation(description: "wait for report message")], timeout: 10)
In the above code, we first imported the necessary libraries, then initialized the **DeviceClient** object and connected to the Huawei Cloud IoTDA service. Finally, we send a simple message to verify that the connection was successful.
Summarize
Through the above steps, you have learned how to use Flutter and Swift to connect iOS devices to the Huawei Cloud IoT platform. Now, you can not only connect devices, but also implement basic messaging functions, laying the foundation for subsequent more complex application development.
Currently, the two basic SDKs are still under development. If you have any needs/questions, you can submit an issue in the project on Github. Thank you.
Click to follow and learn about Huawei Cloud’s new technologies as soon as possible~