Mo B.
Mo B.

Reputation: 5805

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!

Upvotes: 1

Views: 565

Answers (2)

Matthijs van der Veer
Matthijs van der Veer

Reputation: 4085

Device simulations for Azure IoT Hub are usually long-running processes, hence you won't find many Azure Functions implementations with this purpose. That doesn't mean it's impossible. Like you suggested, a timer trigger could be used to periodically send telemetry to Azure IoT Hub. You can then send one or more messages, close the connection (if using MQTT or AMQP) and the function will stop. You can also use the REST API for this.

Normally, a device will react to changed desired properties events of its Device Twin by keeping a bi-directional connection open (again through MQTT or AMQP). Because your Function isn't always running, you would need to accomplish this in a different way. A solution would be to route Device Twin Change events to an EventHub that your Function is listening to.

Message Routing

Upvotes: 1

Roman Kiss
Roman Kiss

Reputation: 8235

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.

Upvotes: 1

Related Questions