We will use the AMQP library to implement using a AMQP protocol directly with IoT Hub:
- Create a C# console application and add a class amqpClient.
- Open the Add new NuGet packages and search for AMQPNetLite:
A Nuget package installer for AMQP
- Declare the global variables:
string IoThubURI = "IoTHubCookBook.azure-devices.net";
int port = 5671;
- We will create a SendEvent method which accepts the deviceId parameter.
- 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);
- Connect AMQP session to start sending telemetry data:
session = new Session(connection);
SenderLink senderevent = new SenderLink(session, "senderevent", to);
- 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"
};
- Convert the data into JSON:
var json = JsonConvert.SerializeObject(telemetryDataPoint);
- Now, publish the message:
var messageValue = Encoding.UTF8.GetBytes(json.ToString());
var telemetryMessage = new Message()
{
BodySection = new Data() { Binary = messageValue }
};
senderevent.Send(telemetryMessage);
- Read messages from the IoT Hub.
- We will use the device explorer to read the messages published:
Device explorer tool reading window
- Using the device explorer only, we will go to Message to Device tab and send Message:
Device explorer sending a cloud to device message
- 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
- 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);
}
- Let's connect the AMQP client with the IoT Hub:
ReceiverLink receiveCommand = new ReceiverLink(session, "receiveCommand", entity);
- 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();
- 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();