Assistance Required in Connecting Robot to IoT Central with Specific Project Settings

ALAN PINTOR 0 Reputation points
2023-08-15T14:44:25.81+00:00

Dear IoT Team,

I am encountering challenges while attempting to integrate a robot with IoT Central. This is a critical issue as it pertains to one of many units that require connection.

Project Configuration Details:

  • Android OS Version: 7.1
  • Android Studio Version: Arctic Fox
  • Target SDK Version: 27
  • Dependency: implementation 'com.microsoft.azure.sdk.iot:iot-device-client:1.29.2'

The challenges faced are as follows:

  • Utilizing the Android sample from Azure IoT SDK Java was unsuccessful, owing to incompatibility with my project version.
  • Adaptation of the sample code to my project resulted in compatibility issues.
  • Attempts to implement chatGPT were met with errors:
    • Logs from the Android app reflect a successful connection, but the device in IoT does not register as connected.
    • An error was encountered when sending telemetries: //V/IoTest: Telemetry sent with status: ERROR

Would you be able to provide a sample compatible with my project configuration, or advise me on appropriate solutions?

Enclosed code snippet:

import android.util.Log;
import com.microsoft.azure.sdk.iot.device.*;
import com.microsoft.azure.sdk.iot.device.transport.IotHubConnectionStatus;
import org.json.JSONObject;

public class AzureIoTHelper {
    private DeviceClient deviceClient;

    public AzureIoTHelper(String connectionString) {
        try {
            deviceClient = new DeviceClient(connectionString, IotHubClientProtocol.HTTPS);
            deviceClient.registerConnectionStatusChangeCallback(new IotHubConnectionStatusChangeCallback() {
                @Override
                public void execute(IotHubConnectionStatus status, IotHubConnectionStatusChangeReason statusChangeReason, Throwable throwable, Object callbackContext) {
                    Log.v("IoTest", "Connection status changed to: " + status);
                }
            }, null);
        } catch (Exception e) {
            Log.v("IoTest", "Error initializing Azure IoT Central Helper: " + e.getMessage());
        }
    }

    public void iotConnect() {
        try {
            deviceClient.open();
            Log.v("IoTest", "Connected to Azure IoT Central.");
        } catch (Exception e) {
            Log.v("IoTest", "Error connecting to Azure IoT Central: " + e.getMessage());
        }
    }

    public void sendTelemetries(JSONObject telemetryData) {
        try {
            Message message = new Message(telemetryData.toString());
            deviceClient.sendEventAsync(message, new EventCallback(), null);
            Log.v("IoTest", "Telemetry sent: " + telemetryData.toString());
        } catch (Exception e) {
            Log.v("IoTest", "sendTelemetries - Exception: " + e.getMessage() + ", cause: " + e.getCause());
            e.printStackTrace();
            //V/IoTest: Telemetry sent with status: ERROR
        }
    }

    private static class EventCallback implements IotHubEventCallback {
        @Override
        public void execute(IotHubStatusCode responseStatus, Object callbackContext) {
            Log.v("IoTest", "Telemetry sent with status: " + responseStatus.name());
        }
    }
}

Best Regards,

Alan

Azure IoT Central
Azure IoT Central
An Azure hosted internet of things (IoT) application platform.
344 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Sander van de Velde 28,546 Reputation points MVP
    2023-08-15T16:12:45.55+00:00

    Hello @ALAN PINTOR ,

    you are connecting to an Azure IoT Central device.

    this device needs to be registered there. Then you are able to see the connection information like this:

    User's image

    In the code you shared, I do not see the input of the Scope ID, etc.

    What I see is a connection string. This is perfect for an Azure IoT Hub, a PaaS solution used by Azure IoT Central in the underlying logic. The IoT Hub is not publically accessible from the outside, though.

    To give a bit more context, the credentials seen above are related to the Azure Device Provisioning service, also a PaaS service, and part of the Azure IoT Central underlying logic.

    Via the DPS service, the IoT Hub service is selected for device-to-cloud communication.

    So you need to find a DPS SDK (and code example) using DPS access.


    If the response helped, do "Accept Answer". If it doesn't work, please let us know the progress. All community members with similar issues will benefit by doing so. Your contribution is highly appreciated.

    0 comments No comments