I'm a beginner to AzureIot, I've been given access to an azure account which has an IOT hub.
from the azure portal I see a connection string of the form:
HostName=iothub-mycompany.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=_somekey_in_base64_
Now all I need is to simply send a JSON payload to this IOT hub. Any protocol would do.
I was assuming that when I send a message I can see this message with the this device explorer tool. but I'm not sure if I'm doing something wrong or perhaps there is a flaw in my understanding.
I tried examples from blogs, msdn etc In the end I've come up with the code below
It runs, however I have no clue what it's doing and where to look for whether this message was sent.
The Device explorer shows nothing.
I checked the Activity Login portal.azure.com but it seems more like a login activity log.
- Also in the azure portal Under explorers I See Iot Devices. I see the device name I need to target.(I know it exists)
Here is the code, part of .Net4.5.2 ConsoleApplication.
Program.cs
:
class Program
{
static DeviceClient deviceClient;
const string IOT_HUB_CONN_STRING = "connection str in format specified above.";
const string IOT_HUB_DEVICE_LOCATION = "_some_location.";
const string IOT_HUB_DEVICE = "device_id";
static void Main(string[] args)
{
var ret = SendMessageToIoTHubAsync("temperature", 15);
Console.WriteLine("completed:" + ret.IsCompleted);
}
private static async Task SendMessageToIoTHubAsync(string sensorType, int sensorState)
{
deviceClient = DeviceClient.CreateFromConnectionString(IOT_HUB_CONN_STRING, TransportType.Mqtt);//i tried other trsnsports
try
{
var payload = "{" +
"\"deviceId\":\"" + IOT_HUB_DEVICE + "\", " +
"\"location\":\"" + IOT_HUB_DEVICE_LOCATION + "\", " +
"\"sensorType\":\"" + sensorType + "\", " +
"\"sensorState\":" + sensorState + ", " +
"\"localTimestamp\":\"" + DateTime.Now.ToLocalTime() + "\"" +
"}";
var msg = new Message(Encoding.UTF8.GetBytes(payload));
Debug.WriteLine("\t{0}> Sending message: [{1}]", DateTime.Now.ToLocalTime(), payload);
await deviceClient.SendEventAsync(msg);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("!!!! " + ex.Message);
}
}
}
As a side note, my "device" is ClearScada. I will have C# Driver which will get data out of a ClearScada instance, this driver should send that data to the azure iot hub. essentially I just need to figure the sending from C# to Azure iot. Questions:
- Where Can I see my messages.?
- Is my function correct or is there a flaw in my understanding.