Webhook to Azure Function to Data Table

I have been playing around with Azure IoT and various other web services but getting rather frustrated with all the setup and costs. All I need is table data logging for remote sensors. No fancy integrations, management, or visuals.

The Azure function seems like a perfect fit for this since it can trigger on HTTP POST and write directly to the database. AND the first 1,000,000 executions are free.

So, has successful done this chain. Webhook -> Azure Function -> SQL database (preferred) or blob. and if so would they mind sharing the Function code, any code base is fine (#NET, node.js). Starting from example is always much easier.

OK Boys and Girls i got this working finally.

VERY roughly.
Step 1. Setup Azure Function app to trigger on HTTP and output to HTTP and Azure Table storage.
Step 2. Write small function to decode JSON and store in Azure data table
Step 3. Setup particle web hook pointing to Azure function app

Key parts are that the JSON from particle MUST match the receiving class. There might be a better way but this works for now.

So now i have data logging for pennies a month!

Function code:

#r "Newtonsoft.Json"

using System;
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;

public class FixData
{
    public string fixType { get; set; }
    public string unix { get; set; }
    public string lat { get; set; }
    public string lng { get; set; }
    public string accuracy { get; set; }
    public string temp { get; set; }
    public string battV { get; set; }
    public string solarV { get; set; }
}

public class RootObject
{
    public string eventName { get; set; }
    public FixData fixData { get; set; }
    public string coreid { get; set; }
    public string published_at { get; set; }
    public bool noDefaults { get; set; }
}

public class TableData //this gets written to table
{
    public string PartitionKey { get; set; } //Device ID
    public string RowKey { get; set; }  //UNIX time
    public string fixType { get; set; }
    public string lat { get; set; }
    public string lng { get; set; }
    public string accuracy { get; set; }
    public string temp { get; set; }
    public string battV { get; set; }
    public string solarV { get; set; }
}

public static async Task<object> Run(HttpRequestMessage req, ILogger log, IAsyncCollector<TableData> LuminousCdata)  
{  
    log.LogInformation("C# HTTP trigger function processed a request."); 

    //Partition JSON
    string jsonContent= await req.Content.ReadAsStringAsync();  
    RootObject mydataPoint= JsonConvert.DeserializeObject<RootObject>(jsonContent);  

    //copy data to new structure
    var newTableRow = new TableData();
    newTableRow.PartitionKey = mydataPoint.coreid;  
    newTableRow.RowKey = mydataPoint.fixData.unix;
    newTableRow.fixType = mydataPoint.fixData.fixType;
    newTableRow.lat = mydataPoint.fixData.lat;
    newTableRow.lng = mydataPoint.fixData.lng;
    newTableRow.accuracy = mydataPoint.fixData.accuracy;
    newTableRow.temp = mydataPoint.fixData.temp;
    newTableRow.battV = mydataPoint.fixData.battV;
    newTableRow.solarV = mydataPoint.fixData.solarV;

    if (mydataPoint.eventName != "geolocate"){
        return new BadRequestObjectResult("Not a geolocate");
    }else{
        await LuminousCdata.AddAsync(newTableRow); //write to Azure Table
        return (ActionResult)new OkObjectResult($"Received data from {mydataPoint.coreid}");
    }
}
1 Like

Just get a free sandbox with www.Losant.com

I’ve been using mine for years and there is not much you can’t do for zero expense as long as you keep your connected devices below 10.

I tried Ubidots & Azure and like Losant the best personally.

Losant is pretty nifty but it starts at $100/month and climbs substantially after year 1. Business licenses start at $30K/yr. I’m developing for a small scale product with a simple web interface I’ve already written so those prebuilt type solutions are not economic to me. I like to control my own data.

Besides, I thought I would offer an alternative. Functions + Tables is probably the cheapest scale-able data logger I have seen yet.

Yea, I was just saying it’s free up to 10 devices.

If your building bigger than that it starts costing ya.

What did the $100 plan give you with Losant?