1
votes

I am trying to use Microsoft Azure IOT Hub to demonstrate connecting my device to IOT Hub and send messages using mqtt protocol. I have an account and created device and SAS with TTL as required. I am using java client SDK given by microsoft azure to connect but my connection is not authorized. I have followed all the steps as given in the below URL: https://azure.microsoft.com/en-us/documentation/articles/iot-hub-mqtt-support/ Please help me in this regards. My logs are below:

Logs:

MqttIotHubConnection.open() connectionOptions=
============== Connection options ==============
CleanSession : false
SocketFactory : null
MqttVersion : 4
KeepAliveInterval : 20
ConTimeout : 30
UserName : PCSIOTHub.azure-devices.net/MQ...
SSLProperties : null
WillDestination : null


Exception in thread "main" java.io.IOException: Error initializing MQTT connection:Not authorized to connect
at com.microsoft.azure.iothub.transport.mqtt.MqttIotHubConnection.open(MqttIotHubConnection.java:142)
at com.microsoft.azure.iothub.transport.mqtt.MqttTransport.open(MqttTransport.java:83)
at com.microsoft.azure.iothub.DeviceClient.open(DeviceClient.java:163)
at samples.com.microsoft.azure.iothub.SendReceive.main(SendReceive.java:210)
2

2 Answers

1
votes

If you're using the Microsoft Java client for IoT Hub, you don't need to create the SAS token yourself, the SDK does it for you.

If you follow the example here, https://azure.microsoft.com/en-us/documentation/articles/iot-hub-java-java-getstarted/#create-a-simulated-device-app, all you need to do is replace:

private static IotHubClientProtocol protocol = IotHubClientProtocol.AMQPS;

with:

private static IotHubClientProtocol protocol = IotHubClientProtocol.MQTT;

and you will have a basic Java client that can communicate with IoT Hub over MQTT.

1
votes

As @DominicBetts said, you don't need to create the SAS token when using the IoTHub SDK for Java. More explaination for the security of IoTHub, please see the offical document Azure IoT Hub developer guide.

As reference as sample, you can refer to the example on GitHub to know how to send messages in the different protocols for Java.