Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,222 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I am trying to connect with MQTT with DigiCert G2 .crt certificate from https://learn.microsoft.com/en-us/azure/security/fundamentals/azure-ca-details?tabs=root-and-subordinate-cas-list#root-certificate-authorities, I am getting this error.
Disconnected from MQTT, error: Optional(Error Domain=MGCDAsyncSocketErrorDomain Code=8 "Error in SSLSetCertificate" UserInfo={NSLocalizedDescription=Error in SSLSetCertificate})
pod 'CocoaMQTT', :modular_headers => true pod 'MqttCocoaAsyncSocket', :modular_headers => true
Anything which I am doing wrong here?
import Foundation
import CocoaMQTT
class MQTTManager: CocoaMQTTDelegate {
func mqttDidDisconnect(_ mqtt: CocoaMQTT, withError err: (any Error)?) {
print("Disconnected from MQTT, error: \(String(describing: err))\n")
}
var mqtt: CocoaMQTT!
func mqtt(_ mqtt: CocoaMQTT, didPublishAck id: UInt16) {
print("Published message with ID: \(id)") }
func mqtt(_ mqtt: CocoaMQTT, didSubscribeTopics success: NSDictionary, failed: [String]) {
print("Subscribed to topics: \(success)") }
func mqtt(_ mqtt: CocoaMQTT, didUnsubscribeTopics topics: [String]) {
print("Unsubscribed from topics: \(topics)")
}
func mqttDidPing(_ mqtt: CocoaMQTT) {
print("Pinged!")}
func mqttDidReceivePong(_ mqtt: CocoaMQTT) {
print("Ponged!") }
func mqttDidDisconnect(_ mqtt: CocoaMQTT, withError err: (Error?)?) {
print("Disconnected from MQTT, error: \(String(describing: err))")
}
func mqtt(_ mqtt: CocoaMQTT, didConnectAck ack: CocoaMQTTConnAck) {
if ack == .accept {
print("Connected to the MQTT broker!")
// Subscribe to a topic only after a successful connection
subscribeToTopic(topic: "your-topic")
} else {
print("Failed to connect to MQTT")
}
}
func mqtt(_ mqtt: CocoaMQTT, didPublishMessage message: CocoaMQTTMessage, id: UInt16) {
print("Published message: \(message.string ?? "") with ID: \(id)")
}
func mqtt(_ mqtt: CocoaMQTT, didReceiveMessage message: CocoaMQTTMessage, id: UInt16) {
if let messageString = message.string {
print("Received message on topic \(message.topic): \(messageString)")
}
}
func connectMQTT(sasToken: String, assignedHub: String, registrationId: String,uuid: String, certificate: String, otp: String,deviceKey: String, modelId: String) {
mqtt = CocoaMQTT(clientID: registrationId, host: assignedHub, port: 8883)
print("Attempting to connect to MQTT broker... with client ID:\(String(describing: mqtt)) & \(mqtt.clientID)")
mqtt.delegate = self
mqtt.username = "\(assignedHub)/\(registrationId)/?api-version=2021-04-12";
mqtt.password = sasToken;
let certPath = Bundle.main.path(forResource: "DigiCertGlobalRootG2", ofType: "crt")!
let certData = try? Data(contentsOf: URL(fileURLWithPath: certPath))
let sslSettings: [String: NSObject] = [
kCFStreamSSLPeerName as String: assignedHub as NSObject,
kCFStreamSSLCertificates as String: [certificate] as NSObject
]
mqtt.allowUntrustCACertificate = true
mqtt.sslSettings=sslSettings
mqtt.connect()
}
}