How to do it...

We will use the AMQP library to implement using a AMQP protocol directly with IoT Hub:

  1. Create a C# console application and add a class amqpClient.
  2. Open the Add new NuGet packages and search for AMQPNetLite:
A Nuget package installer for AMQP
  1. Declare the global variables:
string IoThubURI = "IoTHubCookBook.azure-devices.net";
int port = 5671;
  1. We will create a SendEvent method which accepts the deviceId parameter.
  1. Initialize the variables required for AMQP connection with IoT Hub:
string to = Fx.Format("/devices/{0}/messages/events", deviceId);
string entity = "/messages/events";
string audience = Fx.Format("{0}/devices/{1}", IoThubURI, deviceId);
string resourceUri = Fx.Format("{0}/devices/{1}", IoThubURI, deviceId);
string sasToken = “”;//get shared signature from Device Explorer
bool cbs = PutCbsToken(connection, IoThubURI, sasToken, audience);
  1. Connect AMQP session to start sending telemetry data:
session = new Session(connection);
SenderLink senderevent = new SenderLink(session, "senderevent", to);
  1. Once the connection is made, we will define our telemetry data point:
var telemetryDataPoint = new
{
deviceId = deviceId,
windSpeed = currentWindSpeed,
highTemp = 72.3,
lowtemp = 11.2,
latitude = "17.5122560",
longitude = "70.7760470"
};
  1. Convert the data into JSON:
var json = JsonConvert.SerializeObject(telemetryDataPoint);      
  1. Now, publish the message:
var messageValue = Encoding.UTF8.GetBytes(json.ToString());
var telemetryMessage = new Message()
{
BodySection = new Data() { Binary = messageValue }
};
senderevent.Send(telemetryMessage);
  1. Read messages from the IoT Hub.
  2. We will use the device explorer to read the messages published:
Device explorer tool reading window
  1. Using the device explorer only, we will go to Message to Device tab and send Message:
Device explorer sending a cloud to device message
  1. On the AMQP client console, we will create a new ReceivedCommand function to receive the messages from the IoT Hub:
AMQP message read at device client
  1. Initialize device connectivity with the IoT Hub and the device will post the messages to our AMQP client:
           string audience = Fx.Format("{0}/messages/servicebound/feedback", IoThubURI);
string resourceUri = Fx.Format("{0}/messages/servicebound/feedback", IoThubURI);
string entity = Fx.Format("/devices/{0}/messages/deviceBound", "myFirstDevice");
string returnString = "";
string sasToken = “”;//get shared signature from Device Explorer
bool cbs = PutCbsToken(connection, IoThubURI, sasToken, audience);
if (cbs)
{
session = new Session(connection);
}
  1. Let's connect the AMQP client with the IoT Hub:
ReceiverLink receiveCommand = new ReceiverLink(session, "receiveCommand", entity);
  1. Read the message and write to the console line:
received = receiveCommand.Receive();
if (received != null)
{
receiveCommand.Accept(received);
returnString = Encoding.UTF8.GetString(received.GetBody<byte[]>());
Console.WriteLine(returnString);
// process the Command at Device
// Write your code here
}
receiveCommand.Close();
  1. Now, from your main method of the console application, simply create the object of the amqpClient class and call the methods:
amqpClient clsamqpClient = new amqpClient();
clsamqpClient.SendEvent("myFirstDevice");
Thread receiverThread = new Thread(clsamqpClient.ReceiveCommand);
receiverThread.Start();
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.140.198.173