The following sample will walk you through building an IoT application to post a tweet to Twitter.
If you have any questions or issues feel free to create them on this repo following the provided templates.
The following walkthrough will guide you in building an application that can process IoT messages and have them take an action like sending a tweet.
The sample assumes the following are already installed and prepared:
The following are optional:
The first step we need to do is create the IoT Hub. Azure IoT Hub allows you to manage, configure, and monitor IoT devices.
If you do not have a teXXmo button, you are welcome to use the community managed simulator which will send a "device to cloud" to your IoT hub. To use visit this link and simulate a button press by pressing the "Submit" button after adding the device connection string from the previous section (Device details page).
NOTE: This site is community managed. Only use for simple tests with non-production IoT Hubs
If using the teXXmo button to connect with Azure IoT Hub, follow the instructions to connect to the button access point (hold down button for a few seconds until blinking yellow, then connect to the broadcast access point). Go to the device configuration page in a web browser (http://192.168.4.1) and configure the IoT Hub with your Device Id, Key, and Hub hostname. The IoT Hub Hostname can be found on the Overview section of IoT Hub.
If you do not have a button and don't want to use the community button simulator, I recommend using the SDK directly in your own Visual Studio project. Instructions can be found here
Now that you have an IoT Hub and a connected device, we need to write a simple Azure Function to process the data the devices are generating.
IoTHubConnectionString
as the name of the environment variable.messages/events
and click OKIoT Hub communicates with Azure Functions triggers via the events endpoint. First we need to get the events endpoint connection string.
Endpoint=sb://myendpoint.net/;SharedAccessKeyName=123=
and my compatible name was myiothub
my Connection String would be Endpoint=sb://myendpoint.net/;SharedAccessKeyName=123=;EntityPath=myiothub
If you recall from the project creation of the Azure Function, you set an environment variable called IoTHubConnectionString
as the connection string setting. In order to set this environment variable in your local development, the easiest method is to set it as a value in the local.settings.json
file. This can store and create environment variables for local development.
Values
for the IoTHubConnectionString
. For example (NOTE: don't use this connection string, use yours)
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"AzureWebJobsDashboard": "UseDevelopmentStorage=true",
"IoTHubConnectionString": "Endpoint=sb://myendpoint.net/;SharedAccessKeyName=123=;EntityPath=myiothub"
}
}
Now let's test to make sure everything is wired up correctly.
Finally, we will add some simple logic to call an Azure Logic App and post a tweet.
NOTE: You are more than welcome to use any action you want to perform on an IoT event
I just built something awesome with Azure IoT! Try it yourself here: http://aka.ms/azureiotdemo
Now that we have the workflow created, we simply need to call it from our Azure Function.
.cs
file (default named Function1.cs
) to edit the code for your function.log.Info($"..")
line:
await client.PostAsync("https://prod-07..yourLogicAppURL..", null);
NOTE: replace the URL with the unique URL of your workflow
async
which should change the method signature to:
public static async System.Threading.Tasks.Task RunAsync([IoTHubTrigger("messages/events", Connection = "IoTHubConnectionString")]EventData message, TraceWriter log)
Feel free to continue to add logic to the function or logic app as desired.
Run the function again and test out with the button. It should call the logic app and post a tweet!
When everything is working as expected, right-click the Azure Function project and select Publish. You can then set this project to run in your Azure Subscription. Since this entire solution is serverless, you won't pay for the function or logic app unless they are actually executed.
Be sure to edit the application settings of the function after publishing to make sure the environment in Azure is correct. This includes saying Yes to the prompt to upgrade the runtime version to beta
(the v2 setting we specified for our Function on create), and adding an IoTConnectionString Application Setting and pasting in the connection string you have in the local.settings.json
file.
IoTHubConnectionString
and paste in the value from your local.settings.json
connection string.This is just a simple IoT backend leveraging IoT Hub and Azure serverless. You could continue to build more features to this solution as needed.