How do I establish an connection to Azure IoT Hub? Say a connection like MQTT/AMQP using Python.

+1 vote
Apr 13, 2018 in Azure by subhani
• 180 points
494 views

2 answers to this question.

+1 vote
Best answer

Azure IoT Hub comes with 3 SDK's:

  1. Service SDK
  2. Device SDK
  3. Edge SDK

We can Establish Connection to Azure IoT Hub with MQTT,AMQP,HTTPS Protocols.

Sample Code:

#import iothub_client
from iothub_client import *

#confirmation_callback of the message
def send_confirmation_callback(message, result, userContext):
    print "Confirmation[%d] received for message with result = %s" % (userContext, result)

#recieve_message_callback
def receive_message_callback(message, counter):
    buffer = message.get_bytearray()
    size = len(buffer)
    print "Received Message"
    print "    Data: <<<%s>>> & Size=%d" % (buffer[:size], size)
    return IoTHubMessageDispositionResult.ACCEPTED

#ConnectionString and Protocol here we have given MQTT you can give HTTPS/AMQP insted of MQTT
#Example: iotHubClient = IoTHubClient(connectionString, IoTHubTransportProvider.AMQP)

iotHubClient = IoTHubClient(connectionString, IoTHubTransportProvider.MQTT)

iotHubClient.set_message_callback(receive_message_callback, 0)

#sends messages Asynchronously to IoT Hub instance
iotHubClient.send_event_async(message, send_confirmation_callback, 0)
answered Apr 13, 2018 by null_void
• 3,220 points

selected Aug 16, 2018 by Priyaj
0 votes
You can consult the following link:-
https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-protocols
This deals with the entire process in brief
answered Aug 16, 2018 by Priyaj
• 56,160 points

Related Questions In Azure

0 votes
1 answer

How do I upload a picture to Windows Azure Website?

Check out this example: @model List<string> ...READ MORE

answered Jun 27 in Azure by Perry
• 17,010 points
33 views
0 votes
2 answers

How can I download a .vhd image to my local machine from azure and upload the same to a different azure account?

From the Windows Azure Portal you can ...READ MORE

answered Aug 20, 2018 in Azure by Priyaj
• 56,160 points
1,157 views
0 votes
2 answers

How can I add database connection string to Azure Functions?

The best way to do this is ...READ MORE

answered Aug 17, 2018 in Azure by Priyaj
• 56,160 points
2,424 views
0 votes
1 answer

How to add an App Settings to existing Azure Web Application using Azure Power Shell?

The example is for slot-specific settings, if ...READ MORE

answered Jun 13, 2018 in Azure by club_seesharp
• 3,450 points
152 views
0 votes
2 answers

How to process a file using Azure Function?

You don't really need a http trigger ...READ MORE

answered Jun 18, 2018 in Azure by club_seesharp
• 3,450 points
1,103 views
0 votes
2 answers
0 votes
1 answer

How do point my domain name to an Azure Website?

It is possible to have a CNAME directed ...READ MORE

answered Apr 12 in Azure by Prerna
• 1,940 points
19 views
0 votes
1 answer

How do disconnect to Azure SQL database if I can't access it?

Based on info  from Microsoft, I think ...READ MORE

answered Apr 26 in Azure by Prerna
• 1,940 points
52 views
0 votes
1 answer

How can I copy SQL Azure database to a local development server?

There are certain ways to do this: Using SSIS ...READ MORE

answered Jun 26, 2018 in Azure by null_void
• 3,220 points
41 views
0 votes
4 answers

How can I rename a SQL Azure database?

This command serves the purpose ALTER DATABASE [oldname] ...READ MORE

answered Nov 30, 2018 in Azure by Abhinav
304 views