GithubHelp home page GithubHelp logo

Comments (16)

zhaozhenhong5 avatar zhaozhenhong5 commented on May 30, 2024 2

@fbeltrao @gerfen Thank you for your help!

from iotedge-lorawan-starterkit.

gerfen avatar gerfen commented on May 30, 2024 1

Here's a link to instructions for creating an Azure function project in VS code: https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-first-function-vs-code.

You will need to install the Azure Webjobs SDK: https://docs.microsoft.com/en-us/azure/app-service/webjobs-sdk-get-started

And to make monitoring you IoT Hub easier, you'll want to install the Azure IoT Hub tools for VS code: https://blogs.msdn.microsoft.com/iotdev/2019/01/08/introducing-azure-iot-tools-for-visual-studio-code/

After you have these tools installed, it will be a trivial exercise to create the Azure function.

from iotedge-lorawan-starterkit.

gerfen avatar gerfen commented on May 30, 2024

You'll need to create an Azure function which consumes an EventHubTrigger on a custom consumer group. Visit the "Built-in endpoints" page for your IotHub and copy the "Event Hub-compatible endpoint". You'll need this for the "Connection" parameter of the EventHubTrigger. While you are on the "Built-in endpoints" page, create a new consumer group and record the name for the "ConsumerGroup" parameter.

image

Your function will look something like this:

       [FunctionName("ForwardLoraData")]
       public static async Task RunAsync([EventHubTrigger("%HubName%", Connection = 
      "RouterConnection", ConsumerGroup = "%ConsumerGroupForwardLoraData%")]EventData 
       eventData, ILogger log, ExecutionContext context)
       {
            var configuration = BuildConfiguration(context);
            var bodyBytes = eventData.Body.Array;
            var deviceId = GetDeviceId(eventData);
            var payload = GetPayload(bodyBytes);
            var requestUri = configuration[ConfigurationKeys.WebApiUrl];
            var httpClient = CreateHttpClient();

            var measurement = new LoraMeasurement
            {
                DeviceId = deviceId,
                Json = payload,
            };
            var message = await httpClient.PostAsJsonAsync(requestUri, measurement);

             log.LogInformation(message.IsSuccessStatusCode
                ? $"ForwardLoraData: request sent successfully"
                : $"ForwardLoraData: request not sent successfully - {message.ReasonPhrase}");
        }

        private static HttpClient CreateHttpClient()
        {
            var httpClient = new HttpClient();
            httpClient.DefaultRequestHeaders.Accept.Clear();
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            return httpClient;
        }

        private static string GetPayload(byte[] body)
        {
            var json = Encoding.UTF8.GetString(body);
            return json;
        }

        private static string GetDeviceId(EventData message)
        {
            return message.SystemProperties["iothub-connection-device-id"].ToString();
        }

        priate static IConfigurationRoot BuildConfiguration(ExecutionContext context)
        {
            return new ConfigurationBuilder()
                .SetBasePath(context.FunctionAppDirectory)
                .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
                .AddEnvironmentVariables()
                .Build();
        }
    }

from iotedge-lorawan-starterkit.

zhaozhenhong5 avatar zhaozhenhong5 commented on May 30, 2024

Thanks for your reply. Can I add the function code you write above to my EventHubTrigger in the Azure function? And what parameter shall I add? I want to post the gateway edge device data to my application(it has a URL address for being posted,how I add this URL to the code?)
And I add your code to my EventHubTrigger, save and run, but it shows many error.

from iotedge-lorawan-starterkit.

gerfen avatar gerfen commented on May 30, 2024

Create an Azure functions project in VS Code or Visual Studio.

For local testing, add a file called local.settings.json to your function project. Add the following:

{
  "IsEncrypted": false,
  "Values": {
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",   
      RouterConnection": "<your iot hub connection string>",
     "WebApiUrl": "<your url>",
    "ConsumerGroupForwardLoraData": "<your consumer group>",
    "HubName": "lorawan-data",
  }
}

After you have tested your function locally,deploy it to Azure and set the same variuables except for "FUNCTIONS_WORKER_RUNTIME" in the application settings for functions project.

from iotedge-lorawan-starterkit.

zhaozhenhong5 avatar zhaozhenhong5 commented on May 30, 2024

Sorry, I tried to do this with VS Code, but failed. I can't create an EventHubTrigger in the VS Code. One reason is that I am not familiar with the tool.
So, can you make this for me with VS Code and send to github?I think it's a easy thing to you.
Thanks very much!

from iotedge-lorawan-starterkit.

zhaozhenhong5 avatar zhaozhenhong5 commented on May 30, 2024

Sorry again. I tried again but still fail to build. Another reason maybe I am not familiar with the C#, if python will do this job, it maybe good to me.
I found that the VS Code haven't the "EventHubTrigger" to creat ,so I used Visual Studio to do this job.And here is my code code,the code report many error in Visual Studio,can you find the problem?
I need this funtion because I need the Azure-lorawan for my product. I must solve the problem as soon as possible.
Thank you.

from iotedge-lorawan-starterkit.

fbeltrao avatar fbeltrao commented on May 30, 2024

You need to install the Azure Function Extensions.

In a new Azure Function project, enter this in the terminal:
func extensions install --package Microsoft.Azure.WebJobs.Extensions.EventHubs --version 3.0.1

This will add the package into the .csproj file.

Create new function using the Azure Function cli func new in VsCode terminal:

$ func new
Select a template:
1. QueueTrigger
2. HttpTrigger
3. BlobTrigger
4. TimerTrigger
5. DurableFunctionsOrchestration
6. SendGrid
7. EventHubTrigger
8. ServiceBusQueueTrigger
9. ServiceBusTopicTrigger
10. EventGridTrigger
11. CosmosDBTrigger
12. IotHubTrigger
Choose option:

IMPORTANT: Remove the Azure connection strings from you example file! Also, generate new keys for your IoT Hub.

from iotedge-lorawan-starterkit.

zhaozhenhong5 avatar zhaozhenhong5 commented on May 30, 2024

@gerfen Hello, in your sample code the "priate static IConfigurationRoot BuildConfiguration(ExecutionContext context)" shows error (error CS1585:Member modifier 'static' must precede the member type and name)
Do you know why?

from iotedge-lorawan-starterkit.

gerfen avatar gerfen commented on May 30, 2024

Most likely, you need to install Microsoft.Extensions.Configuration nuget package. From the command line or the Terminal windows in vs code editor execute dotnet add package Microsoft.Extensions.Configuration. You'll most likely need to do the same for Microsoft.Extensions.Logging.

FWIW, here are all of the using statements you'll need at the top of your function:

using Microsoft.Azure.EventHubs;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

And for completeness here is the code for the LoraMeasurement class.

    public class LoraMeasurement
    {
        public string DeviceId { get; set; }
        public string GatewayId { get; set; }
        public string Json { get; set; }
    }

Feel free to modify as you see fit.

from iotedge-lorawan-starterkit.

zhaozhenhong5 avatar zhaozhenhong5 commented on May 30, 2024

I added the using and LoraMeasurement,but it show the same error of "static IConfigurationRoot".
So I see the Configuration is to add the "local.settings.json" to the code,is it? Can I delete it and directly write the parameter to the code. Below is my whole code,it shows no error,but in the end of degug shows "the terminal process terminated with exit code:1" ,and I can deploy it to the azure.

`using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;
using System.Net.Http;
using Microsoft.Azure.EventHubs;
using System.Text;
using System.Net.Http.Headers;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace test4
{
public static class test4
{
[FunctionName("test4")]
// public static async Task RunAsync([EventHubTrigger("%HubName%", Connection =
// "RouterConnection", ConsumerGroup = "%ConsumerGroupForwardLoraData%")]EventData
// eventData, ILogger log, ExecutionContext context)
public static async Task RunAsync([EventHubTrigger("todaairhub", Connection =
"Endpoint=sb://iothub-ns-todaairhub-1150195-4c04d9cda7.servicebus.windows.net/;SharedAccessKeyName=iothubowner;SharedAccessKey=Q8ZO/chXyDJL+UlA7lTXxL5Ilri/alZIUc2jONc5i+Q=;EntityPath=todaairhub", ConsumerGroup = "loradata")]EventData
eventData, ILogger log, ExecutionContext context)
{
//var configuration = BuildConfiguration(context);
var bodyBytes = eventData.Body.Array;
var deviceId = GetDeviceId(eventData);
var payload = GetPayload(bodyBytes);
//var requestUri = configuration[ConfigurationKeys.WebApiUrl];
var requestUri = "http://192.168.0.111:9999"; // or the URL you want to send data to
var httpClient = CreateHttpClient();

        var measurement = new LoraMeasurement
        {
            DeviceId = deviceId,
            Json = payload,
        };
        var message = await httpClient.PostAsJsonAsync(requestUri, measurement);

         log.LogInformation(message.IsSuccessStatusCode
            ? $"ForwardLoraData: request sent successfully"
            : $"ForwardLoraData: request not sent successfully - {message.ReasonPhrase}");
    }

    public class LoraMeasurement
    {
        public string DeviceId { get; set; }
        public string GatewayId { get; set; }
        public string Json { get; set; }
    }
    private static HttpClient CreateHttpClient()
    {
        var httpClient = new HttpClient();
        httpClient.DefaultRequestHeaders.Accept.Clear();
        httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        return httpClient;
    }

    private static string GetPayload(byte[] body)
    {
        var json = Encoding.UTF8.GetString(body);
        return json;
    }

    private static string GetDeviceId(EventData message)
    {
        return message.SystemProperties["todaair55"].ToString();
    }     

    // priate static IConfigurationRoot BuildConfiguration(ExecutionContext context)
    // {
    //     return new ConfigurationBuilder()
    //         .SetBasePath(context.FunctionAppDirectory)
    //         .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
    //         .AddEnvironmentVariables()
    //         .Build();
    // }
}

}
`

from iotedge-lorawan-starterkit.

gerfen avatar gerfen commented on May 30, 2024

Sure, you can hard code whatever you like. You'll also need to hard code the parameters in the EventHubTrigger attribute.

 [FunctionName("ForwardLoraData")]
       public static async Task RunAsync([EventHubTrigger("%HubName%", Connection = 
      "RouterConnection", ConsumerGroup = "%ConsumerGroupForwardLoraData%")]EventData 
       eventData, ILogger log, ExecutionContext context)

to

 [FunctionName("ForwardLoraData")]
       public static async Task RunAsync([EventHubTrigger("<your hub nam>", Connection = 
      "<your connection string>", ConsumerGroup = "<your consumer group>")]EventData 
       eventData, ILogger log, ExecutionContext context)

from iotedge-lorawan-starterkit.

zhaozhenhong5 avatar zhaozhenhong5 commented on May 30, 2024

So, can you find it something wrong in my code? It seems can't work well. Is the "application/json" need to be added? I changed "iothub-connection-device-id" to my gateway edge device name,right?

from iotedge-lorawan-starterkit.

gerfen avatar gerfen commented on May 30, 2024

You do not want to change that. Are you able to set a break point and execute the code? Is the break point hit? I don't have enough information to help you further at this point.

from iotedge-lorawan-starterkit.

fbeltrao avatar fbeltrao commented on May 30, 2024

I have created a sample app based on the good example given by @gerfen here: https://github.com/fbeltrao/Samples/tree/master/iothubforwarder

You need to change the local.settings.json file to:

  • add a storage connection string (or use the development)
  • add the iot hub event hub connection string

In the TelemetryToApp.cs file change:

  • The consumer group used
  • The destination URL

from iotedge-lorawan-starterkit.

zhaozhenhong5 avatar zhaozhenhong5 commented on May 30, 2024

I am testing this funtion today and find two problems as below:
1、It can working with VS Code, but deployed to the azure Funtion App, it can't. Is it need something to be set when deployed?
2、I found the IoT Hub messages posted to my application endpoint appear occur the delay,the delay I can see in the working VS Code. Just like the delay occur before when the devices messages sent to azure, this issue solved by fbeltrao before.

from iotedge-lorawan-starterkit.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.