In this project, we will showcase a use case of sending temperature data from a Particle.io device through Azure IoT Hubs using a pre-built solution from the ConnectTheDots project.
ConnectTheDots.io is an open source project created by Microsoft to help you get tiny devices connected to Microsoft Azure IoT and to implement great IoT solutions taking advantage of Microsoft Azure advanced analytic services such as Azure Stream Analytics and Azure Machine Learning.Getting Started
In order to follow along with this project you will need the following:
- Active Microsoft Azure account
- A device from Particle.io (Core, Photon, or Electron)
We will begin by deploying a copy of the ConnectTheDots project by leveraging a pre-built Azure ARM template. These templates use JSON to describe the pieces of a hosted environment and allow for sharing ready-to-deploy solutions in the cloud.
To begin deployment navigate to:
https://github.com/Azure/connectthedots/tree/master/Azure/ARMTemplate
It is highly recommended to read through this content. Once you are ready, click the "Deploy to Azure" Button.
You should be presented with a screen that looks like the following:
Here is a brief description of the available fields:
- Directory: used to denote the Azure AD directory to deploy to
- Subscription: the subscription to apply the deployment to
- Resource Group: A selection of available resource groups if they exist
- Resource Group Name: The name for the resource group if "Create New" is selected. Otherwise, this will populate with the name of the selected existing Resource Group
- Region: The geographic datacenter location to deploy your project to
- Solution Name: Will become the prefix for the deployed web app (i.e. SolutionName.azurewebsites.net)
- IoT Hub Sku: Denotes the Size of the IoT Hub to create. In the picture above, I have selected the free "F1" offering.
- Admin Name: the name of the Administrator for the deployment
After filling in the available fields, select next to validate and preview the deployment. Once that has completed, select "Deploy" to kick off the official deployment.
Head to http://azure.com to verify your deployment has completed successfully.
As part of the deployment step, an Azure IoT Hub was created. You can find this in the "Resource Group" section by looking for the name used for your Resource Group in the deployment step. We will use information from this service to create the Integration in the next step.
Login to your account at http://particle.io and create a new integration @ https://console.particle.io/integrations/create
Select the "Azure IoT Hub" integration
Since we already have an Azure IoT Hub from the previous step, we can skip to the instructions to "Add a Shared Access Policy".
Upon completion we should have everything we need to proceed. Select "I Have Done All These Things" and fill in the fields with the appropriate information used in the previous "Add a Shared Access Policy" step.
Your event name can technically be set to anything but any code that follows will assume an event name of "ctddemo".
The IoT Hub Name is the name of your IoT Hub in azure.
The Shared Policy Name is set to the value used in the previous "Add A Shared Policy" step.
Setting Device to "Any" allows any devices under your account to use this particular Integration.
Finally, expand the "Advanced Settings" node and add the following JSON inside the text field that appears.
{
"guid": "{{PARTICLE_DEVICE_ID}}",
"organization": "{{organization}}",
"displayname": "{{displayname}}",
"location": "{{location}}",
"measurename": "{{measurename}}",
"unitofmeasure": "{{unitofmeasure}}",
"timecreated": "{{PARTICLE_PUBLISHED_AT}}",
"value": "{{value}}"
}
This JSON will map to values in the code that we will flash to our particle device. and corresponds to a format that is expected by the ConnectTheDots solution.
Setting up the Photon DeviceWe will assume that your Particle device has been claimed and that you know how to program a basic application for the device.
Our sample code will use an off the shelf DHT-11 sensor to capture and send realTemperature and Humidity values to our Azure IoT Hub for display on the ConnectTheDots Web App.
Attaching the DHT 11 Sensor1. Remove the Particle Core from the white Breadboard.
2. Remove the blue DHT11 sensor from its packaging.
3. Inspect the Core until you locate three labels: "3V3' ", "GND", and "D2".
4. Inspect the DHT11 sensor until you locate three labels: "+", "out", and "-".
5. The colors of the wires you receive is random. The important part is to ensure these connections:
- A) (Ground) "GND" to "-"
- B) "D2" to "out"
- C) (Power) "3V3' " to "+"
6, The final assembly should look like the following:
1. Head to https://build.particle.io/build/new and create a new app titled "ConnectTheDots"
2. Add the AdaFruit_DHT library:
- A) Click the "Library" Icon in the lower left (looks like a bookmark ribbon)
- B) Search for "DHT"
- C) Select the library result "ADAFRUIT_DHT"
- D) Select "Include in App"
- E) Select "ConnectTheDots"
- F) Select "Add to this App"
3. If done correctly, the last step should have added the following text to the top of your project code:
// This #include statement was automatically added by the Particle IDE.
#include <Adafruit_DHT.h>
4. Copy and paste the following code into your project, overwriting any existing code:
// This #include statement was automatically added by the Particle IDE.
#include <Adafruit_DHT.h>
#define DHTPIN 2 // what pin we're connected to
// Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
DHT dht(DHTPIN, DHTTYPE);
char Org[] = "ORGANIZATION_NAME";
char Disp[] = "DISPLAY_NAME";
char Locn[] = "LOCATION";
void setup()
{
dht.begin();
delay(10000);
}
void loop()
{
//Delay if using a Free (F1) Azure IoT Hub Instance (8000 request per month) that resets monthly
delay(652000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a
// very slow sensor)
float h = dht.getHumidity();
// Read temperature as Farenheit
float f = dht.getTempFarenheit();
// Read temperature as Celsius
// float t = dht.getTempCelcius();
// Check if any reads failed and exit early (to try again).
// if (isnan(h) || isnan(t) || isnan(f)) {
// Serial.println("Failed to read from DHT sensor!");
// return;
// }
char payload[255];
//Example payload: {"guid":"62X74059-A444-4797-8A7E-526C3EF9D64B","organization":"My Org Name","displayname":"Sensor Name","location":"Sensor Location","measurename":"Temperature","unitofmeasure":"F","timecreated":"1975-09-16T12:00:00Z", "value":74}
snprintf(payload, sizeof(payload), "{ \"organization\":\"%s\",\"displayname\":\"%s\",\"location\":\"%s\",\"measurename\":\"Temperature\",\"unitofmeasure\":\"F\",\"value\": \"%f\" }", Org, Disp, Locn, f);
Particle.publish("ctddemo", payload);
//5 second delay to avoid throttling from Particle
delay(5000);
snprintf(payload, sizeof(payload), "{ \"organization\":\"%s\",\"displayname\":\"%s\",\"location\":\"%s\",\"measurename\":\"Humidity\",\"unitofmeasure\":\"%%\",\"value\": \"%f\" }", Org, Disp, Locn, h);
Particle.publish("ctddemo", payload);
}
The code begins with some customizable variables for identifying the Particle device once connected to the ConnectTheDots IoT Hub (Org, Disp, and Locn).
We then setup the DHT11 sensor in the Setup function.
Next, we enter the loop where we read the Temperature and Humidity, form a JSON payload, and send it to the IoT Hub Integration that we created earlier.
5. Modify the following variables appropriately to your liking:
char Org[] = "ORGANIZATION_NAME";
char Disp[] = "DISPLAY_NAME";
char Locn[] = "LOCATION";
You will note a delay of 652000ms in the main loop. Feel free to modify to your liking. This value corresponds to the highest reporting interval possible if using an the Free F1 Azure IoT Hub instance. This was derived by the following:
- F1 IoT Hub instances allow for 8,000 free requests per month
- For each measurement we make 2 requests (one for temperature, one for humidity)
- Therefore the maximum wait time if using a free account that resets to each month with 8,000 allowable requests is:
- 1 month in minutes / (8000 requests/2 requests per call) = X
- Therefore, X = a delay of 10.95 minutes per loop = 657 seconds
- If we substact the included 5 second delay to account for throttling to the Particle cloud, we get 652 seconds OR 652000ms
6. Select the "Check Mark" in the upper left to verify that your code compiles without error.
7. Once your code is verified, select the "Flash Icon" (looks like a lightning bolt) to flash the code to your device. You should receive a message indicating success if all went well!
It may be useful to monitor events coming into the Particle Service. After programming the Particle Core and running the program above, events should begin arriving. These can be viewed in the Particle.io Console.
To view data from the Particle.io Console:
Login to your Particle account and go to https://console.particle.io/events
You should see your events publish to the event console every time that Particle.Publish() is called in the program from the previous step.
You can also monitor on command line by clicking "See In Terminal" which will reveal a curl command to track events as they come in.
To verify that data is being properly sent to the Azure IoT Hub, you can follow the instructions @ https://docs.particle.io/tutorials/integrations/azure-iot-hub/#confirming-the-data-reaches-azure-iot-hub
Once properly setup, events will look like the following as they arrive on the CLI:
In the "Deploying the Arm Template" step, an Azure Web App was created which plots events as they arrive to the IoT Hub. This web app can be found in the Resource Group that was deployed in this step. The URL to the site will be of the form (SolutionName.azurewebsites.net) i.e. the value you chose for "SolutionName" when deploying.
As part of the ConnectTheDots deployment step, an Azure Stream Analytics job is created. This can be modified to your liking to produce alerts on data as it comes into the IoT Hub.
Locate the Streaming Analytics Job in the Resource Group of your deployment and open the Query:
Here you can modify the existing Query to produce new results:
There are quite a few things that can be accomplished with minor modifications to the ConnectTheDots deployment.
For example, an Azure Storage Container is created which houses a copy of data recently sent to the Azure IoT Hub. It may be possible to use this source as input to a PowerBI report or Azure Machine Learning Experiment.
Let us know your ideas and share any concepts that you create in the comments! The maintainers of ConnectTheDots are happy to accept Pull Requests. You never know, your work may make it into the official project!
Comments