Azure Functions - Create powerfull IoT solutions

Technical Value

Earlier this year Microsoft released a new feature in AzureAzure Functions.

Azure Functions opens the possibility to write serverless code (JavaScript, C#, Python and PHP) which can be triggered by events (or by a timer) in Azure.

Recently we used Azure Functions in a project to send a file upload command to a device connected to an Azure IoT Hub triggered by an http call.

After creating a function app in Azure Portal we were able to choose between a lot of examples to get started. We chose the HTTP Trigger C# example. Azure Functions Examples We changed the query parameter to ask for a device id, so that we could invoke this function with the URL provided by Azure Functions + our Device ID.

  1.  // parse query parameter
  2.  string deviceId = req.GetQueryNameValuePairs()
  3.  .FirstOrDefault(q => string.Compare(q.Key, "deviceId", true) == 0)
  4.  .Value;
  6.  // Get request body
  7.  dynamic data = await req.Content.ReadAsAsync<object>();
  9.  // Set name to query string or body data
  10.  deviceId = deviceId ?? data?.deviceId;
  1. Function URL: https://[yourfunctionapp].azurewebsites.net/api/[yourfunctionname]?code=[yourcode]?deviceid=[yourdeviceid]

Then we needed a C# method to send a command to the IoT Hub which delegates it to the corresponding device.

  1. private async static Task SendCommand(string deviceId, TraceWriter log)
  2. {
  3.  var connectionString =
  4.  "HostName=yourhostname;SharedAccessKeyName=yourkeyname;SharedAccessKey=yourkey";
  6.  var serviceClient = ServiceClient.
  7.  CreateFromConnectionString(connectionString);
  9.  var commandMessage = new Message(Encoding.ASCII.GetBytes("{\"Name\":\"UploadLastFile\"}"));
  11.  await serviceClient.SendAsync(deviceId, commandMessage);
  13.  log.Info($"Sent FileUpload command to device: {deviceId}");
  14. }

To do it easy like this we needed to add some dependencies (Azure IoT Hub Service SDK) to our function. Luckily we were able to use Nuget in Azure Functions.

The trick is to add a new file project.json (klick on “View Files” bottom left of the code window to see and add your function’s files) and add your dependencies like this: Function App Project JSON To test the code immediately Azure Functions provides a “Run” Button and a “Log” window for debug information. Azure Function Debug It is also possible to use Visual Studio Online (Open Azure Function App Settings -> go to App Service Settings -> Tools -> App Service Editor) to write your code with features like IntelliSense.

So if you need a fast way to develop a small solution to process data inside Azure - Functions is a great tool to this.

If you have any questions - feel free to ask.

Have Fun !  

Neuen Kommentar schreiben

Der Inhalt dieses Feldes wird nicht öffentlich zugänglich angezeigt.


  • Keine HTML-Tags erlaubt.
  • HTML - Zeilenumbrüche und Absätze werden automatisch erzeugt.
  • Web page addresses and email addresses turn into links automatically.
Teilen auf

Newsletter Anmeldung

Abonnieren Sie unseren Newsletter!
Lassen Sie sich regelmäßig über alle Neuigkeiten rundum ORAYLIS und die BI- & Big-Data-Branche informieren.

Jetzt anmelden