IoTHubTrigger not work in the cloud but work locally.

Samy 1 Reputation point
2022-12-05T19:26:46.927+00:00

Hi,
The iot trigger works fine when it's executed from my laptop (in Visual studio). I received the messages from my devise. But when the function is deployed in the cloud, the function gets never triggered. The app settings are also set in the Portal. The signalR works in azure and locally. The configuration is correct. I tested it in the previous version without trigger

My appsetting :

    - IotHubConnectionString = Endpoint=sb://ihsuprodparres009dednamespace.servicebus.windows.net/;SharedAccessKeyName=iothubowner;SharedAccessKey=.....;EntityPath=iothub-xxxx  

And my code

 [FunctionName("IoTHubTriggerFunction")]  
        public static async Task Run(  
                [IoTHubTrigger("messages/events", Connection = "IotHubConnectionString")]EventData message,  
                [SignalR(HubName = "flowmeterHubName")] IAsyncCollector<SignalRMessage> signalRMessages,  
                ILogger log)  
        {  
                dynamic data = new  
                {  
                    Temperature = 1,  
                    Humidity = 10,  
                    Pression = 11,  
                    FlowWater = 12,  
                    Device = "Test",  
                };  
      
                await signalRMessages.AddAsync(new SignalRMessage  
                {  
                    Target = "target",  
                    Arguments = new[] { JsonSerializer.Serialize(data) }  
                });  
      
            }            
     }  


    
  
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,264 questions
Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,116 questions
{count} votes

3 answers

Sort by: Most helpful
  1. LeelaRajeshSayana-MSFT 13,456 Reputation points
    2022-12-07T16:53:37.463+00:00

    Hi @Samy ,

    Greetings! I have created an IoTHubTrigger function using Visual Studio and noticed a similar behavior when the function is deployed to cloud. And as you suspected, the cloud deployment missed the configuration for the connection string set to the Event Hub end point.

    You can view this configuration issue when you click on Diagnose and solve problems setting under your Azure function on the portal. Refer the below image.

    268250-screenshot-27.png

    To resolve this, click on Configuration under Settings section and add a new Application Setting to add the connection string parameter. Refer the below image

    268240-screenshot-28.png

    Please note that you would have to name the application setting parameter as IotHubConnectionString in your case.

    Once you add the parameter and save the settings, you can see the IoTHubTrigger function on the cloud gets triggered and you the graph reflects the metrics in a bit. Refer the below image.

    268341-screenshot-29.png

    Please let me know if you face any issues in implementing the steps suggested or need further clarification.

    ----------

    Kindly accept answer or upvote if the response is helpful so that it would benefit other community members facing the same issue. I highly appreciate your contribution to the community.


  2. Samy 1 Reputation point
    2022-12-11T08:57:25.643+00:00

    269294-image.png


  3. Sander van de Velde 28,386 Reputation points MVP
    2022-12-12T00:12:49.49+00:00

    Hello @Samy ,

    You try to ingest telemetry from an IoT Hub into an Azure Function using the Azure IoT Hub trigger.

    There are a number of things that could be in your way.

    First, it is recommended to use a separate consumer group for each (single) Azure resource that is consuming messages from the IoT Hub event-hub-compatible endpoint.

    Here, I created one function using the newly created 'fa' consumer group:

    [FunctionName("SendIngestedIoTHubMessagesToAdtEnvironment")]  
    public static async Task Run(  
        [IoTHubTrigger("messages/events", ConsumerGroup = "fa", Connection = "IoTHubEventHubString")] EventData message,   
        ILogger log)  
    

    Note: I personally try to avoid using the $default consumer group. standard tooling like VS Code, the IoT explorer, Azure Stream Analytics, and Azure Data Explorer all start with that $default so if not changed, a 'collision' can occur:

    269418-image.png

    Next to that, create a separate route for the default eventhub-compatible-endpoint once you start adding other routes:

    269433-image.png

    Why?

    That endpoint will change its purpose once you add one other endpoint. It will turn into an fallback solution:

    Create an endpoint, and then add a route (you can add up to 100 routes from each IoT hub). Since routing is based on a matching query, a message can be sent to multiple endpoints. Messages that don’t match a query are automatically sent to messages/events if you’ve enabled the fallback route. When you create new endpoints and routes, messages stop flowing to the built-in endpoint unless you create a separate route and direct them there. If no routes to the built-in endpoint exist, enabling a fallback route will direct any messages that don't match a route query to that endpoint. Learn more

    ----------

    If the response helped, do "Accept Answer". If it doesn't work, please let us know the progress. All community members with similar issues will benefit by doing so. Your contribution is highly appreciated.