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
2,124 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
• 58,100 points

Related Questions In Azure

0 votes
1 answer
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 28, 2019 in Azure by Perry
• 17,100 points
1,614 views
0 votes
1 answer

How to import an image from Docker Hub to a private registry in Azure?

Hi@akhtar, You can import any image from Docker ...READ MORE

answered Nov 13, 2020 in Azure by MD
• 95,460 points
1,566 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
• 58,100 points
13,938 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
• 58,100 points
13,799 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
1,394 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
7,235 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
1,143 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
2,256 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP