In previous blog post #5 I have shown the Setup New Device page in Mobile App. But unfortunately I found that Azure services are not easily available from Xamarin Forms. There are many Xamarin, the tech that I want to use for cross-platform apps, examples on Azure site but they are all for either .NET console apps or Xamarin Native code. The Xamarin Forms is so appealing that I completely neglected native coding with Xamarin. Hence I chose to work with .NET Console Apps for the present. The following screenshot shows the console app named vs_add_new_device.

Console app - Setup New Device

 

This program is to be run in the cmd window. This asks for the values for the various parameters needed one by one. The format is same as explained earlier.

 

  1. First it will ask for the Device Model #. The 1st letter can be either C, W or B. If it is C then WiFi details will not be asked later on. The 2nd letter can be either M, A or G. Currently only M is allowed which stands for Microsoft Azure services. The last 2 digits indicate the hardware board version.
  2. Next Device Serial # will be asked.
  3. If the Device Model # doesn't start with C, then Wi-Fi Network Name and Wi-Fi Password will be asked. Though the password is shown in clear text, it will not be displayed in the Release mode.
  4. Then Unit of Temperature and Threshold Temperature will be asked. For Unit of Temperature only C or F values are allowed.
  5. Then Mobile Number for Alert Text Messages and E-mail Address for Alert Messages will be asked for. In the Console app, these entries will not be verified by sending confirmation messages. That is skipped for now.

 

Once all the details are entered, the entered values will be displayed and the Azure IoT Hub will be contacted to register the device whose device id is formed by appending the model number and serial number to a fixed set of letters. Once the device id is successfully registered, the Azure IoT Hub will return a device primary key that is to be used by the device to connect to Azure IoT Hub and send sensor data to be stored in cloud.

 

Unfortunately I couldn't lay immediately on code examples to use Bluetooth to send all the required data to the device. Hence I thought of copying the Device Access Key to the mobile app and send it to the device. That is why the access key is displayed here. But that effort also was not successful as I was not able to communicate with the device from phone's Bluetooth. So for the present I hard coded the device access key into the device code.