
One of the most expensive services in the standard IoT solution from Azure is Stream Analytic. In order to get around this expensive service, more suitable for the development of Enterprise solutions, you can use the capabilities of Azure Functions.
How can I create an IoT hub and connect the Arduino to it I
wrote earlier . Now, let's cheapen the solution. Replace Stream Analytic with Azure Functions.
Under the cat you will find a manual How-To
First we go to the endpoints of our IoT hub and take from the endpoint called Events, the name compatible with the event hub.

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

The allocation plan will be more economical to choose the “Consumption Plan” if the number of calls to your function is not particularly large. Cost 20 cents per million function launches.
Next, create a custom function like IoT Hub (Event Hub)

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

In the string Event Hub name we enter the value that we copied from the IoT Hub.
Clicking on the "new" set the value of the connection.

The following code will be automatically created as a function template 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 turn on the device from the previous article. If everything is configured correctly, then in the log window we will 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. I use SQL Server as a database. You can use some other type of database. Go to the existing database or create a new one. In the menu item "Connection Strings" take the line ADO.NET.

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

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

Now it remains to change our function so that it can write data from JSON to the database. A pretty standard code version 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<EventData>(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 IoT solution that removes data from the device and saves it to the cloud database. Additionally, you can create a free Azure Web App data visualization application.
The price of the solution is slightly more than the cost of the database. That is, if you use the base size of 2 Gb, 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.