Search code examples
azure-functionsazure-iot-hub

Azure Functions as IoT Hub device


Is it possible to use Azure Functions to simulate a device connected to an Azure IoT Hub? (So far I have only found samples of Azure Functions that process data received by the hub.)

More precisely, the Azure Functions should perform the following two tasks, as if it were a device:

  1. Periodically send telemetry data to the IoT Hub.

    • The obvious solution would be to have a Function with a timer trigger, and in the code instantiate an IoT client to send the data. Is that right?
  2. Get triggered on device twin property change, and do something with the new device twin properties.

    • Is there a trigger for this kind of event?

Any code snippets or pointers to samples would be helpful!


Solution

  • Yes, it can be done using the following functions:

    1. Use the REST API for send a telemetry data
    2. Use the Azure IoT Hub message routing for Device Twin Change Events to the built-in endpoint for consuming by IoTHubTrigger function. I do recommend to create for this function own consumer group in the Azure IoT Hub.