My first AMQPNetLite program

After having some problems with my Netduino 3 wifi Azure Event Hub client code (which are most probably due to the issues discussed here) I decided to have a look at AMQPNetLite which had been suggested by Paolo Patierno in a response to one of my posts in the Netduino forums.

I usually create a “minimalist” project so I can figure out how a new library works without an domain specific code getting in the way. Overall, my first experience was pretty positive, the code compiled first time, ran second time and worked third time.

The objective for my first AMQPNetLite application running on my Netduino 3 Wifi was to connect to my home wifi, wait for an IP address then upload an event to an Azure EventHub

private const int amqpPortNumber = 5671;
private const string sbNamespace = "ServiceBus Namespace";
private const string keyName = "SaS Key name";
private const string keyValue = "SaS Key value";
private const string eventHub = "EventHub name";

...

// Wait for Network address if DHCP
NetworkInterface networkInterface = NetworkInterface.GetAllNetworkInterfaces()[0];
if (networkInterface.IsDhcpEnabled)
{
   Debug.Print(" Waiting for IP address ");

   while (NetworkInterface.GetAllNetworkInterfaces()[0].IPAddress == IPAddress.Any.ToString())
   {
      Debug.Print(".");
   }
}

// Display network config for debugging
Debug.Print("Network configuration");
Debug.Print(" Network interface type: " + networkInterface.NetworkInterfaceType.ToString());
Debug.Print(" MAC Address: " + BytesToHexString(networkInterface.PhysicalAddress));
Debug.Print(" DHCP enabled: " + networkInterface.IsDhcpEnabled.ToString());
Debug.Print(" Dynamic DNS enabled: " + networkInterface.IsDynamicDnsEnabled.ToString());
Debug.Print(" IP Address: " + networkInterface.IPAddress.ToString());
Debug.Print(" Subnet Mask: " + networkInterface.SubnetMask.ToString());
Debug.Print(" Gateway: " + networkInterface.GatewayAddress.ToString());

foreach (string dnsAddress in networkInterface.DnsAddresses)
{
   Debug.Print(" DNS Server: " + dnsAddress.ToString());
}

string deviceId = BytesToHexString(networkInterface.PhysicalAddress);
Debug.Print("DeviceId " + deviceId.ToString());

Then I constructed the AMQP address for the event hub, started an AMQP session and sent the message. I tried sending a message with a JSON payload and also using the “type safe” application properties.

try
{
   Address address = new Address(sbNamespace, amqpPortNumber, keyName, keyValue);
   Connection connection = new Connection(address);

   Session session = new Session(connection);

   SenderLink sender = new SenderLink(session, "send-link", eventHub);

   string messageBody = @"{""DeviceId"":""" + deviceId + @""",""Time"":""" + DateTime.Now.ToString("yy-MM-dd hh:mm:ss") + @"""}";

   Message message = new Message()
   {
      //BodySection = new Data() { Binary = Encoding.UTF8.GetBytes(messageBody)},
      ApplicationProperties = new Amqp.Framing.ApplicationProperties(),
   };

   message.ApplicationProperties["Time"] = DateTime.Now;
   message.ApplicationProperties["DeviceId"] = deviceId;

   sender.Send(message);

   sender.Close();
   session.Close();
   connection.Close();
}
catch (Exception ex)
{
   Debug.Print("ERROR: Publish failed with error: " + ex.Message);
}

For my scenario I was pleasantly surprised how easy it was to get working.

AMQP has a non TLS option (only for non sensitive data) and if this is supported I could use a device like a Netduino 3 Ethernet which don’t have baked in TLS support.

11 thoughts on “My first AMQPNetLite program

  1. Pingback: My first AzureSBLite program | devMobile's blog

    • Hi,

      Works well with Azure ServiceBus queues & event hubs. I used it (+AzureSBLite) in my recent MS Ignite NZ presentation running on .NetMF Netduino 3 Wifi & Windows 10 IoT Core RPI devices

      For AMQPNetLite I use the GitHub source rather than the nuget package as it is a little bit more current.

      Have some new posts about an AMQPNetLite based field gateway coming.

      @KiwiBryn

  2. Hi

    I’ve just started using Netduino N3 Wifi, trying to connect to Azure IOT hub.
    Is your sample code above still working? I tried it and it fails with a SSL authentication error. I even tried it by setting the Connection.DisableServerCertValidation = true. No luck…
    I scoured the internet and came across old posts (2015) discussing the Netduino SSL problem and it looks like it still not solved.
    There is a mention of a SSL issue that still needs to be fixed in Wilderness Labs TinyCLR Github repository.

    I know this is an old post, but your help shall be much appreciated.

    Thanks and regards.

      • Thank @KiwiBryn.

        Its really sad because the Netduino N3 Wifi is otherwise quite a nice device.
        Did you inform Wilderness about your hunch? From reading their issue log it looks like they are simply waiting for outside help. This might just get them going again?
        Looks like the gateway is the way to go for now. Thanks a lot for the link.
        Regards

        Heinrich

  3. Hi

    I’ll give them a nudge again….

    Have a look at AdaFruit.IO (https://io.adafruit.com/) it’s free for hobbyists USD10 a month for “professional” works pretty well.

    If your application is “hobbyist” I have an AdaFruit.IO Netduino3Wifi client (https://blog.devmobile.co.nz/2018/01/03/adafruit-io-basic-netduino-http-client/) and nRF24L01 FieldGateway in pipeline just need to do blog post.

    Sensors around my house (https://io.adafruit.com/BrynHLewis/dashboards/home-environment)

    @KiwiBryn

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s