Using a Raspberry Pi Zero W with Azure
Setting up a Raspberry Pi Zero W and a rundown of Azure services you can use to send and store data from it (or any other IoT device)
--
Step 1: Setting up a Raspberry Pi Zero W
The reason we’re using a Raspberry Pi Zero W here is because it is cheap, easy to find, has a wireless adapter built in and can run a full-blown OS (as opposed to most micro-controllers). Furthermore, the Raspberry Pi community is huge, meaning there is usually support available if you run into issues.
Setting up a Raspberry Pi Zero W to communicate with Azure involves: flashing an OS onto it (we will use Raspbian Lite), enabling SSH on it and connecting it to WiFi.
The video below shows how to setup the Pi:
Note: if you are connecting the Pi Zero via USB on a Windows machine, then you will need to download Bonjour from the Apple website.
You should now be connected to WiFi and have internet access on the Pi. To test this:
- SSH into the Raspberry Pi Zero W.
- Run a curl command on the Pi’s terminal, e.g.:
curl “https://www.google.com”
If the output looks like a HTML file (as shown above), then voila! You are ready to send data to the cloud!
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.
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!