Stefan
Stefan

Reputation: 585

Azure Functions - IoTHubTrigger But It will not trigger

I create a azure function with IoT Hub trigger. As an example I use this Azure Functions - how to set up IoTHubTrigger for my IoTHub messages?

Function1.cs

using IoTHubTrigger = Microsoft.Azure.WebJobs.ServiceBus.EventHubTriggerAttribute;

using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.ServiceBus.Messaging;
using System.Text;
using System.Net.Http;

namespace LogTheIoTHubMessage
{
    public static class Function1
    {
        private static HttpClient client = new HttpClient();

        [FunctionName("Function1")]
        public static void Run([IoTHubTrigger("messages/events", Connection = "ConnectionString")]EventData message, TraceWriter log)
        {
            log.Info($"C# IoT Hub trigger function processed a message: {Encoding.UTF8.GetString(message.GetBytes())}");
        }
    }
}

local.settings.json

{
    "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "AzureWebJobsDashboard": "UseDevelopmentStorage=true",
    "ConnectionString": "HostName=AAA.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=YYYYYY"
  }
}

But when I test the function it will start but the trigger will not trigger. For testing I use

C:\Program Files\mosquitto>mosquitto_pub -d -h AAA.azure-devices.net -i TestRaspberryPi -u "AAA.azure-devices.net/TestRaspberryPi" -P "SharedAccessSignature sr=YYY" -m "noch ein test" -t "devices/TestRaspberryPi/messages/events/readpipe/" --cafile "c:\Projects\azureiot.pem" -p 8883 -V mqttv311

Client TestRaspberryPi sending CONNECT Client TestRaspberryPi received CONNACK (0) Client TestRaspberryPi sending PUBLISH (d0, q0, r0, m1, 'devices/TestRaspberryPi/messages/events/readpipe/', ... (13 bytes)) Client TestRaspberryPi sending DISCONNECT

Upvotes: 1

Views: 1409

Answers (2)

silent
silent

Reputation: 16138

Your Function looks all good, just your connection string is the wrong one. You need the connection string from the event hub endpoint. It should look like this:

Endpoint=sb://iothub-ns-xxxxxxx.servicebus.windows.net/;SharedAccessKeyName=iothubowner;SharedAccessKey=*******;EntityPath=abc

See here for a similar example: https://github.com/sebader/iotedge-end2end/blob/master/CloudFunctions/IotHubMessageProcessor.cs

Upvotes: 1

Vladislav
Vladislav

Reputation: 2982

The first parameter of the EventHubTriggerAttribute is eventHubName, whereas you are passing in an endpoint name.

You have to use the "Event Hub-compatible name" of your endpoint: enter image description here

The "Event Hub-compatible endpoint" should be used as a connection string.

By the way, it is recommended to use a dedicated consumer group for your trigger.

Hope this helps.

Upvotes: 1

Related Questions