Setup the azure cloud function for testing

To be able to use the point test extension you need to create an Azure cloud function. To do so follow these steps:

  1. On the Azure portal. New → Compute → Function App.

    image2020-5-26_9-12-31.png

  2. Chose or create a resource groupe, give your app a name, select node js as runtime, then hit Review + Create.

    image2020-5-26_9-14-44.png

  3. Then Create.

    image2020-5-26_9-17-1.png

  4. Open to the cmd on your desktop. and make sure that you have nodejs 10 installed by tiping this command:

    C:\Workspace\btibdev> node --version
    v10.0.0
    


    if you getting command not found error, download the nodejs 10 from this link https://nodejs.org/en/download/releases/


  5. Install the azure functions tools.

    C:\Workspace\btibdev> npm install -g azure-functions-core-tools
    


  6. Create an Azure Functions project in a subdirectory.

    C:\Workspace\btibdev\niagara-testing-cf> func init
    


    1. Choose the runtime node

    2. Language typescript.

  7. Create a new function.

    C:\Workspace\btibdev\niagara-testing-cf> func new --template "Http Trigger" --name NiagaraTestCommands
    


    1. Choose Http Trigger.

    2. Give your function a name.

  8. Open the project in your favorite editor you should see these files.

    image2020-5-26_10-22-6.png

  9. Open the package.json and add iothub dependencies.

    "dependencies": {
        "azure-iot-common": "^1.12.2",
        "azure-iothub": "^1.12.2"
      },
      "devDependencies": {
        "@azure/functions": "^1.0.2-beta2",
        "@types/node": "^14.0.5",
        "typescript": "^3.3.3"
    }
    

    Then run

    npm install
    


  10. Open index.ts and add this implementation. and add your iothub connection string

    JavaScript
    import { AzureFunction, Context, HttpRequest } from "@azure/functions";
    
    
    import { Client } from "azure-iothub";
    
    import { Message } from "azure-iot-common";
    
    let connectionString ="<add your iothub connection string>";
    
    let client = Client.fromConnectionString(connectionString);
    
    const httpTrigger: AzureFunction = async function (
      context: Context,
      req: HttpRequest
    ): Promise<void> {
      if (req.body && req.body.deviceId) {
        let message = new Message(JSON.stringify(req.body));
        let clientResponse = await client.send(req.body.deviceId, message);
        console.log("resp", JSON.stringify(clientResponse));
        context.res = {
          // status: 200, /* Defaults to 200 */
          body: JSON.stringify(clientResponse),
        };
      } else {
        context.res = {
          status: 400,
          body: "Please pass a command",
        };
      }
    };
    
    export default httpTrigger;
    


  11. Publish the function.

    C:\Workspace\btibdev\niagara-testing-cf> npm run build:production
    
    
    C:\Workspace\btibdev\niagara-testing-cf> func azure functionapp publish niagara-testing --force
    

    if you get this error: Unable to connect to Azure. Make sure you have the `az` CLI or `Az.Accounts` PowerShell module installed and logged in and try again

    Just install azure cli tools https://docs.microsoft.com/en-us/cli/azure/install-azure-cli-windows?view=azure-cli-latest

    and type az login.


  12. Now your function is deployed.

    image2020-5-26_10-52-36.png


  13. Click on the function and Get Function URL.

    image2020-5-26_10-54-33.png


  14. Copy this url and pasting on the point test extension. Don't forget to add https:// before the link.

    image2020-5-26_10-59-30.png



Now everything is configured successfully.

Next Step



IoTHubPointTestExt