LooUQ provides a handful of device clients to make it easy to connect to the LooUQ iotQi cloud and allow you to focus on your project's core features. For .NET based devices, we have a demonstration .NET console application that utilizes our iotQi-PCL client. You can use it for small Windows computers; or if your are more familiar with .NET and C#, you can use it to learn how iotQi devices operate.
Setting Up Your Development Environment
We assume you are using Visual Studio for your .NET development efforts. LooUQ has built the .NET version of our client successfully with VS2015 and VS2017, at LooUQ we have converted to using VS2017 as our standard IDE.
The demo is a standard Windows console application. The best way to get the client\sample from GitHub is to clone to your Git desktop or download a ZIP. This will include the demo and the PCL client source code. The PCL client will be available as a NuGet package in the near future.
The repository is found here: https://github.com/LooUQ/iotQi-dotNET
Creating Your Project
Once you have downloaded the project, you can start setting it up for your intended use.
The .NET client is a combined client library and example code. In the near future we intend to release an additional .NET client with separate library and example sections; this version of the client will utilize a NuGet package for the library components of the iotQi client code.
Getting connected to your network is the first step to getting connected to the LooUQ cloud. The .NET client does not perform the local network connect task (this is a client duty for the development boards like the Arduino).
Getting connected to the LooUQ cloud involves changes to App.config file settings as shown below. The only required change is the setup of the device connection string, which will look similar to the example below. You obtain the connection string from the device profile in the Setup application (https://setup.loouq.com).
<configuration> <appSettings> <!-- This setting determines how frequenty the client generates a telemetry sample (aka event) --> <add key="TelemetrySampleTimeInSeconds" value="20" /> <!-- This is the +/- variation for the simulated wind values --> <add key="WindSampleValueWindow" value="5" /> <!-- this sets the high-wind alert warning threshold as a percent, a 0.10 value would alert when above 90% of the value window --> <add key="AlertThreshold" value="0.10" /> <!-- paste the DeviceConnectionString you grabbed from Setup.LooUQ.com below --> <add key="DeviceConnectionString" value="HostName=iothub-a-prod-loouq.azure-devices.net;DeviceId=a6eb6624-9edb-422b-ba7c-113bb3dc6fc0;SharedAccessKey=io7VHGSi4jpK2e/YcJOT4A+g5lMl7nFjWbwKPtyI4H1="/> </appSettings>
Exploring the iotQi Client
As discussed in the iotQi Device Basics guide, there are 3 basic functions for a iotQi to implement. You can implement only one, two or all three.
All iotQi Device Client Can Perform These 3 Activities
- Generate telemetry (data) samples at a specified interval or at event triggered times
- Generate alerts based on local device conditions (different workflow in the iotQi cloud)
- Respond to commands received from the iotQi cloud (commands may or may not return values)
Each of these functions are implemented in a separate library classes found in the iotQi project folder. There is no need to change the library files, but you can review these files for a better understanding of how things work if you like.
The two sample files that you would change to make the sample do something other that generate wind are...
- SampleCommands.cs - This class implements your custom commands.
- WindTelemetryProcessor - This class implement the period sample logic (simulations) and alerts.
The .NET client uses reflection to locate user commands to advertise to the LooUQ cloud. You can have one or more command classes, they are registered in the program.cs file as shown in the code segment below. This example registers two different user defined command classes.
Note: command methods must be defined as public to be visible to the LooUQ cloud processes.
// this registers each public method in the referenced class as user WebAPI commands (user requires device command authority) CommandRegistry.RegisterCommandClass(typeof(SampleCommands)); CommandRegistry.RegisterCommandClass(typeof(SampleWeatherApiCommands));
The WindTelemetryProcessor source file contains one of our two command classes (see above). But, the primary purpose of this source file is the generation of telemetry events and alert actions.
The telemetry generation pattern for the .NET client uses a asynchronous endless loop that is started by code in the program.cs file. The loop is implemented in the GenerateWindTelemetryMessagesAsync() method as follows...
- Start loop: while (true)
- Generate sample (simulation)
- Package it into JSON
- Create a cloud message (event)
- Determine if alert (high wind)
- If alert, create a cloud message (alert)
- Asynchronously wait until next telemetry sample time