Connect a device on Linux and macOS
This topic describes how to use the C Link SDK to connect a device with an audio player on Linux or macOS to Link Speech to enable smart voice broadcasting.
Important notes
The steps in this topic are performed by a regular user. If you encounter an operation that requires administrative permission, run the command with sudo.
Prerequisites
-
You have activated Link Speech. For more information, see Activate Service.
-
You have set up a development environment. For more information, see Environment requirements.
Step 1: Install an audio player
Run the following command for your operating system to install the audio player.
Linux
sudo apt-get install sox
macOS
brew install sox
Step 2: Create project and device
-
Log on to the IoT Platform console. In the left-side navigation pane, choose Value-added Services.
-
On the Value-added Services page, click Go to Service under Link Speech.
NoteIf you have not activated Link Speech, click View Service in the Purchase Value-added Services section. Then, activate Link Speech in the Value-added Service Center. For more information, see Activate Service.
-
In the left-side navigation pane, choose Project Management.
-
On the Project Management page, click Create Project. In the Create Project dialog box, set the parameters and click OK.
-
Click Configure for the project to open its details page. Then, click Device Management > Create Device.
-
You are redirected to the Device Management page of IoT Platform. Set the parameters and click Confirm.
Example DeviceName: device.
In the dialog box, select a Product and optionally enter an Alias. The DeviceName field is optional. If you leave it blank, the system automatically generates a unique identifier.
-
On the Addition Complete page, obtain the device credentials (ProductKey, DeviceName, and DeviceSecret), and then click Done.
Alternatively, you can return to the Link Speech service page to obtain the device credentials. After you reload the page, the newly created device appears on the Device Management page. On the Device List page, find your device and click Credentials in the Actions column. In the dialog box that appears, you can view and copy the device credentials.
Copy the device credentials to a secure location. You will need them for later steps.
Step 3: Build SDK
-
Download linkspeech_posix_demo_v2.zip and extract the
./LinkSDKdirectory. -
Open the
./LinkSDK/demos/link_speech_basic_demo.cfile. Modify the device credentials and the MQTT endpoint, and then save the file.To obtain the MQTT endpoint for your instance, see View instance endpoints.
/* TODO: Replace the following with your device credentials. */ char *product_key = "a1Ch***R5"; char *device_name = "device"; char *device_secret = "e9801e41***a70c88b6d"; /* TODO: Replace the following with the MQTT endpoint for your device instance. */ char *host = "a1Ch*****R5.iot-as-mqtt.cn-shanghai.aliyuncs.com"; -
From the SDK root directory
./LinkSDK, runmaketo build the sample program. -
Run the executable file.
./output/link-speech-basic-demoIf you see output similar to the following, the device is connected to IoT Platform and ready to use Link Speech.
Z:/opt/LinkSDK# ./output/link-speech-basic-demo [1648625170.366][LK-0313] MQTT user calls aiot mqtt_connect api, connect [1648625170.366][LK-032A] mqtt host: xxx.iot-as-mqtt.cn-shanghai.aliyuncs.com [1648625170.366][LK-0317] user name: device&axxx [1648625170.366][LK-0318] password: xxx establish tcp connection with server(host='axxx.iot-as-mqtt.cn-shanghai.aliyuncs.com', port=[443]) success to establish tcp, fd=3 local port: 44730 [1648625170.377][LK-1000] establish mbedtls connection with server(host='axxx.iot-as-mqtt.cn-shanghai.aliyuncs.com', port=[443]) [1648625170.400][LK-1000] success to establish mbedtls connection, (cost 45252 bytes in total, max used 47988 bytes) [1648625170.488][LK-0313] MQTT connect success in 115 ms AIOT_MQTTEVT_CONNECT
Step 4: Smart voice broadcasting
You can now create and push text to the device for voice broadcasting based on your business requirements. For instructions, see Step 2: Create text, Step 3: Push text, and Step 4: Voice broadcast.
For information about the data interaction between the device and Link Speech, see Service calls.