Collects data from a variety of sensors using a Raspberry Pi running a Windows 10 IoT Core UWP app and stores it in an Azure SQL Database.
- Raspberry Pi 2 Model B v1.1
- Keyes KY-036 Metal Touch Sensor
- Keyes K869051 Gas Sensor
- Keyes KY-026 Flame Sensor
- Keyes KY-031 Knock Sensor
- Adafruit BMP280 Barometric Pressure + Temperature Sensor
- Keyes KY-028 Light Sensor
- Windows 10 IoT Core
- Windows UWP App on Pi
- Azure Function
- Azure IoT Hub
- Azure SQL Server and Database
- Create an Azure IoT Hub and device. Copy the device connection string and save or copy for a later step.
- Create a shared access policy or use one of the existing ones such as iothubowner. Compile the shared access policy and the endpoint url into a string like the following and save or copy for a later step.
Endpoint=sb://iothub-ns-kristinott-86295-658bfe8745.servicebus.windows.net/;SharedAccessKeyName=sensorappkeyname;SharedAccessKey=superlongkey1234567901234567890=
- Fork the repo SensorAppAzureFunction
and update the function.json file variable
path
with your IoT Hub Event Hub Compatible name. - Create an Azure SQL Server and Database. In the Firewall settings, add your client IP. Copy or save the database ADO.NET connection string. Replace
Server=tcp:
withdata source=
and fill in the username and password so that the string goes fromtoServer=tcp:senserapp.database.windows.net,1433;Initial Catalog=SensorDB;Persist Security Info=False;User ID={your_username};Password={your_password};MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;
data source=senserapp.database.windows.net;initial catalog=SensorDB;persist security info=True;user id=username;password=p@ssw0rd;MultipleActiveResultSets=True;App=EntityFramework
- Create an Azure Function from the source code from step 3.
- Go into the Application Settings and create the variable named
eventhubconnectionstring
under App settings and paste the endpoint url from step 1 as the value. - Create the variable
SensorReadingContext
under Connection strings and paste the value from step 4. - To check that the setup is working, head over to the Azure Function logs on the SensorReadingtoSQL function.
- Download the code in this repo and open in Visual Studio.
- In the AzureIoTHub.cs page, change the variable
deviceConnectionString
where it says"***DEVICE CONNECTION STRING HERE***"
to the device connection string from the previous section step 1. - Change the run mode in Visual Studio to Remote Machine on ARM
- Update the IP Address of the Remote Connection
- Double click on Properties in the Solution Explorer and update the Debug section
- Run the project on the Remote Machine
- Nuget packages may need to be restored and project may need to be built or rebuilt and few times.
- Create a Service Bus Event Hub.
- Update the Azure Function function.json file for the SensorReadingtoSQL file
path
variable with the value of the event hub name. Push to GitHub and the Azure Function should automatically pull the update. - In the Endpoints blade on the IoT Hub, create an Endpoint on the IoT Hub that has an endpoint type of Service Bus Queue and points to the queue created in step 1. Choose an appropriate name. Copy the endpoint url for a later step. See example below.
- In the Routes blade for the IoT Hub, create a new routing rule with an appropriate name, data source as DeviceMessages, Query string
Alert = "alert!"
, and Endpoint set to the endpoint created in step 3. See example below. - In the Azure Function Application Settings, update the value for
eventhubconnectionstring
under App settings with the url copied in step 3. - (Optional) Setup a Logic App to send an email (or use another service) to notify when an "alert" message enters the event hub.
*Note, to change the routing, update the AzureIoTHub.cs file where it says msg.Properties.Add("Alert", "alert!");
with the property and message you would like to query on or add additional properties. Update the Azure IoT Hub route query.