Step 2: Sending data to Azure

Now that the Raspberry Pi is setup with Raspbian and connected to the internet, we can collect data and send it to Azure.

There are a number of different Azure services that can be used, some of them are described below.

Azure IoT Hub

Azure IoT hub is used to provide bidirectional communication between IoT devices and Azure.

IoT Hub supports a number of protocols to send data, including:

HTTPS

AMQP

MQTT

MQTT and AMQP over WebSockets

Using IoT hub is advantageous if you have a large number of IoT devices and want to easily send messages back to the device, allow file upload from the device and track each device’s status. The Microsoft Docs tutorial on sending telemetry with Python is extremely useful.

Logic Apps

If you require a simple, flexible solution that requires little to no knowledge of coding, and provides an easy method to connect Azure services together; Azure Logic Apps is the ideal solution. With Azure Logic Apps, you can create a HTTP trigger and use HTTP POST or GET requests to send data to Azure. This data can then easily be stored on a database, such as Azure SQL, Azure Table Storage or Cosmos DB.

Below is an example of a Logic App that reads data send through parameters in a HTTP GET request and inserts it into an SQL database.

(Left) The HTTP trigger expanded, (Right) SQL insert row action expanded.

The HTTP GET trigger action takes some values as parameters on a relative path (i.e. the device’s name, temperature, latitude and longitude) and passes them to an SQL insert action which puts them into our database. We can then send a HTTP 200 response, along with a message to let the device know that the request succeeded.

A similar approach can be used to insert data into Cosmos DB containers or Table Storage tables. Moreover, intermediary actions can be added to process the data before putting it into the database.

Azure Functions

A similar approach to Logic apps can be used with Azure Functions. With Azure Functions you can run code in a serverless environment, which scales up or down with demand. Functions can be created in number of languages, such as: C#, JavaScript, Java, Python, F# and PowerShell.

To send data from an IoT device to an Azure Function, we can create a function with a HTTP Trigger. We can then send a POST or GET request to the function, which will process the data and insert it into a database if need be.

An example of a HTTP Trigger function is given in Microsoft Docs. You receive the URL for the function when it is deployed to Azure.

Making a HTTP Request with Python

Once you have created a Logic App or Azure Function, you can send data to it with a HTTP request to the URL. You can send HTTP GET/POST request in Python the following way:

Summary

A Raspberry Pi Zero W is an accessible way to start with IoT, and Azure empowers you to start collecting, processing and analysing your data.

There are a number of Azure services that can be used in your IoT project. Each have their own advantages. The ones mentioned above are not the only services, but their serverless functionality allows you to make full use of the cloud.

Be sure to play around with these services, along with others to build your projects and share your learnings!

Useful links:

Contact: