Skip to main content
Skip table of contents

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.



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



  3. Then Create.



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

    CODE
    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.

    CODE
    C:\Workspace\btibdev> npm install -g azure-functions-core-tools
  6. Create an Azure Functions project in a subdirectory.

    CODE
    C:\Workspace\btibdev\niagara-testing-cf> func init
    1. Choose the runtime node

    2. Language typescript.

  7. Create a new function.

    CODE
    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.



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

    CODE
    "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

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

    JS
    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.

    CODE
    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.



  13. Click on the function and Get Function URL.



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




Now everything is configured successfully.

Next Step



IoTHubPointTestExt




























JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.