Economy solution for the Internet of Things. Azure IoT Hub + Azure functions


    One of the most expensive services in Azure’s standard IoT solution is Stream Analytic. In order to get around this expensive service, more suitable for developing Enterprise solutions, you can take advantage of the capabilities of Azure Functions.

    How I can create an IoT hub and connect Arduino to it, I wrote earlier . Now, let's reduce the cost of the solution. Replace Stream Analytic with Azure Functions.
    Under the cut you will find the How-To manual.

    First, let's go to the endpoints of our IoT hub and take from the endpoint called Events a name compatible with the event hub.



    Copy and save. It will be needed later.
    Now create a new function



    The placement plan will be more economical to choose the “Consumption Plan” if the number of calls to your function is not particularly large. Costs 20 cents per million function launches.

    Next, we create a user-defined function of the type IoT Hub (Event Hub).



    I chose C # as the language, but you can choose another language that is closer to you.



    In the Event Hub name line, enter the value that we copied from the IoT Hub.
    By clicking on the “new” we set the connection value.



    The following code will be automatically generated as a template function code:

    using System;
    public static void Run(string myIoTHubMessage, TraceWriter log)
    {
        log.Info($"C# IoT Hub trigger function processed a message: {myIoTHubMessage}");
    }

    Run the function and enable the device from the previous article. If everything is configured correctly, then in the log window we get the following log:
    2017-12-17T16: 20: 40.486 Function started (Id = 63b0dbda-1624-4e2c-b381-47442e69b853)
    2017-12-17T16: 20: 40.486 C # IoT Hub trigger function processed a message: {"deviceId": "ArduinoAzureTwin" , "Iotdata": 581}
    2017-12-17T16: 20: 40.486 Function completed (Success, Id = 63b0dbda-1624-4e2c-b381-47442e69b853, Duration = 0ms)



    The function works, but we need to save the data to the database. As a database, I use the SQL Server database. You can use any other type of database. Let's go into the existing database or create a new one. In the menu item "Connection strings" take the line ADO.NET.



    Now in our function we go to the "Application Settings".



    We're not going to store our connection string in C # code? We save it in the connection lines of the application, not forgetting to change the username and password.



    Now it remains to change our function so that it can write data from JSON to the database. A fairly standard version of the code looks like this:

    #r "System.Configuration"
    #r "System.Data"
    #r "Newtonsoft.Json"
    using System;
    using System.Configuration;
    using System.Data.SqlClient;
    using System.Threading.Tasks;
    using System.Net;
    using Newtonsoft.Json;
    public static async Task Run(string myIoTHubMessage, TraceWriter log)
    {
    log.Info($"Message: {myIoTHubMessage}");
    var e = JsonConvert.DeserializeObject(myIoTHubMessage);
     var str = ConfigurationManager.ConnectionStrings["SQLServerDB_connection"].ConnectionString;
        using (SqlConnection conn = new SqlConnection(str))
        {
            conn.Open();
            var text = "INSERT INTO [dbo].[SensorData] (DeviceName, SensorValue) Values (@deviceId, @iotdata);";
            using (SqlCommand cmd = new SqlCommand(text, conn))
            {
                cmd.Parameters.AddWithValue("@iotdata", e.iotdata);
                cmd.Parameters.AddWithValue("@deviceId", e.deviceId);
                var result = await cmd.ExecuteNonQueryAsync();
                log.Info($"Inserted: {result.ToString()}");
            }
        }
    }
    public class EventData
    {
        public string deviceId { get; set; }
        public int iotdata { get; set; }
    }

    As a result, we quickly got a cloud-based IoT solution that takes data from the device and stores it in a cloud database. Additionally, you can create a free Azure Web App application for data visualization.

    The price of the solution is slightly more than the cost of the database. That is, if you use a 2 Gb database, you get a little more than 5 dollars a month. Nothing prevents us from using some other cloud database or even some local database located on your server.

    Also popular now: