Building an IoT and Azure demo - Part 11: Using AMQP from the Raspberry Pi

 (Jump to part 1)

There are two ways to send data to an Azure Event Hub: HTTPS and AMQP.  Advanced Message Queuing Protocol is an international standard protocol that sends the data in binary format and is very efficient.  So far, my demo code used HTTPS to send the telemetry data.  Why not use AMQP?  Well, I would need using the Azure Service Bus API library that supports AMQP but it is not compatible with PCL and Universal Windows Apps.  Stuck?  Of course not, thanks to Twitter!

Paolo Patierno, a Microsoft MVP on Windows Embedded & IoT, saw one of my tweets and asked me if I used Azure SB Lite:

Paolo wrote Azure SB Lite to facilitate sending messages using AMQP to Azure Event Hub on the .NET Micro Framework, .NET Compact Framework, Mono (on Linux) and WinRT,  It's a wrapper on AMQP.Net Lite, a lightweight AMQP 1.0 client library.  Sweet!  

As Paolo suggests, let's try using it in my Xamarin.Forms project.  Well, it didn't worked because of an issue with System.Net.Sockets in AMQP.Net Lite.  Clearly, it's not PCL compatible.  Bummer but how about my Universal Windows App running on the Pi?

I added the Azure SB Lite package, and started adding code to support both HTTPS and AMQP by adding a Toggle Switch on the UI so that I can select either protocols during the demo.

Install-Package AzureSBLite

Sending data using HTTPS requires the use of a shared access signature while AMQP requires the use of a connection string.  Retrieving it is easy, simply select the event hub in the Azure portal and locate the Connection Information button at the bottom of the screen.

 

 This will display the connection strings.  I grabbed the one for my SendRule.

And added a new method to my Web API controller so I can return the connection string to the client from the Web server instead of hardcoding it:

[Route("api/connectionstring/{id}")]
public string GetConnectionString(string id)
{
	if (!IsDeviceActive(id))
		throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Unauthorized));

	string connectionString =
		"Endpoint=sb://gb-iotdemo-ns.servicebus.windows.net/;SharedAccessKeyName=SendRule;SharedAccessKey=hEllOsASKeyU=";
	return connectionString;
}

I modified the client code a little bit to handle both protocols.  Here's the modified portion of the buttonStart_Click event initializing the Azure SB Lite objects:

private MessagingFactory _factory;
private EventHubClient _client;
private EventHubSender _sender;

if (AmqpSwitch.IsOn)
{
	_connectionString = await _svc.GetConnectionString(_id);
	TextBlockSas.Text = _connectionString;

	ServiceBusConnectionStringBuilder builder = new ServiceBusConnectionStringBuilder(_connectionString);
	builder.TransportType = TransportType.Amqp;
	_factory = MessagingFactory.CreateFromConnectionString(_connectionString);
	_client = _factory.CreateEventHubClient("iotdemo");
	_sender = _client.CreateSender(_id);
}
else
{
	_sas = await _svc.GetSas(_id);
	TextBlockSas.Text = _sas;
}

Sending the data only takes a couple of lines.  Note however that the Send method is not asynchronous.

public void EventHubSendToPublisher(Telemetry telemetry)
{
	if (_sender == null) return;

	var payloadJson = JsonConvert.SerializeObject(telemetry);

	EventData data = new EventData(Encoding.UTF8.GetBytes(payloadJson));
	data.Properties["time"] = DateTime.UtcNow;

        _sender.Send(data);
}

 Voilà!  Telemetry sent to the Azure Event Hub using the AMQP protocol thanks to Paolo Patierno's Azure SB Lite library.  Thanks Paolo!

blog comments powered by Disqus

Page List

Month List