In this lab you will write a Node.js application that runs on a hub (your development machine) and collects data from a development board and sends it up to your Azure IoT Hub.
Setup the App Dependencies
Since you will be using Node.js for this lab you can take advantage of the dependency management capabilities that Node.js and NPM provide. You need to let your application know that it has a dependency on the following NPM packages - Azure IoT Device, Johnny-Five and Particle-IO. In Node.js this is done with a package.json file. This file provides some basic meta-data about the application, including any dependencies on packages that can be retrieved using NPM (according to npmjs.com today, NPM stands for Narrating Prophetic Monks…not Node Package Manager like you may have thought).
Create a file in your development directory named package.json (or edit the one your created previously)
Add the following code to the package.json file (select the tab for the type of board you are using)
Next you will create the application code to gather temperature, humidity, and barometer data and send it to the cloud.
Write the Code
The Weather app will run on your gateway (your development machine for now) and communicate between your Thing and your Azure IoT Hub. Create another file in the same directory named weather.js.
The first thing you need to do is define the objects you will be working with in the application.
five - A Johnny-Five framework object,
Shield - The Weather Shield plugin object
device - The Azure IoT device object
Protocol - A definition of the messaging protocol that will be used.
client - An Azure IoT object to manage the connection of the client to the cloud.
message - An object to manage the fomatting of the message to be sent.
board - An object to represent the physical prototyping board.
Add the following code to the weather.js file (select the tab for the type of board you are using).
Now that the objects are created, you can get to the meat of the application. Johnny-Five provides a board ‘ready’ event that makes a callback when the board is on, initialized and ready for action. Inside the anonymous callback function is where your application code executes (this function is invoked when the board is ready for use).
Johnny-Five provides a collection of objects that represent the board, the pins on the board, and various types of sensors and devices that could be connected to the board. The Shield plug-in that you specified earlier is a software representation of the physical SparkFun Weather Shield that abstracts the Johnny-Five Temperature and Barometer classes that represent the HTU21D humidity sensor and the MPL3115A2 barometric pressure sensor respectively. When you create an instance of the Shield class you will specify the variant of the Shield class – either ARDUINO or PHOTON.
In the following code you will invoke the board.on() function which establishes a callback function that is invoked when the board is on, initialized and ready. All of the operational code for the board will be in the board.on() function (helper functions may exist outside the scope on the board.on() function). Within the board.on() function you will create an object reference to the weather shield. Similar to the board object, the object you create to reference the shield will have an on() function that establishes a callback that exposes the data read from the sensors on the shield.
Add the following code to weather.js after the previous code (the code is the same regardless of which prototyping board you are using).
In this code you do a number of things:
board.on() - This function triggers the board to invoke the anonymous callback function as soon as the board is on and ready. All of the application code for the device is written inside this callback function.
client.open(callback) - This function opens the connection to Azure IoT Hub and invokes the callback when it gets a response. In this case, the callback is an anonymous function.
Define the weather object. This is a instance of the wrapper around the temperature and barometer on the weather shield (represented by the Shield object) connected to the board. When you instantiate the object, you can specify a frequency to collect the data from the sensors. Many sensors are capable of collecting data in fraction of a second intervals. You may not want to collect data and send it to your Azure IoT Hub that frequently. The freq property defines (in milliseconds) how often to raise an event to report the data from the sensor. In this example you are establishing the callback at a frequency of once per second for the weather object.
message is the object that represents the data you are sending to Azure IoT Hub. This is a JSON formatted message.
When client.sendEvent() is invoked, the JSON message is sent to Azure IoT Hub. For now, nothing happens with the message once it is received in your IoT Hub because you haven’t set up anything that will capture the message and do something with it (we will get to that soon). By default the messages have a one-day retention time.
Run the App
When you run the application it will execute on your computer, and thanks to Johnny-Five, it will connect with your board and work directly with it. Basically, your computer is acting as a gateway - or hub - and communicating with the board as one of potentially many devices (or spokes). If you continue on past the intro labs, in a future lab you will deploy the Node.js application to another device (like a Raspberry Pi) which will act as the gateway and connect to multiple spoke devices.
Open a terminal window (Mac OS X) or command prompt (Windows)
Execute the following commands (replace c:\Development\IoTLabs with the path that leads to your labs folder):
After the board initializes you will see messages printing out once per second. This is the message payload that is being sent to Azure IoT Hub.
Monitor the Messages Being Received by Azure IoT Hub
If you downloaded the Device Explorer utility for Windows or the iothub-explorer command-line utility in the previous lab, you can monitor the messages being received by Azure IoT Hub Explorer.
Click Monitor to begin monitoring messages as they come into your Azure IoT Hub.
From a command prompt execute the following command, replacing [connection-string] with your iothubowner connection string (from the previous lab) and [device-id] with the Azure IoT Hub id for this device.
When you want to quite the application, press CTRL + C twice to exit the program without closing the window (you may also have to press Enter).
PARTICLE PHOTON USERS: After stopping the application press the Reset button on the Photon to prepare it for the next run.
Conclusion & Next Steps
In this lab you learned how to write a Node.js + Johnny-Five application that collects environment telemetry and sends it to Azure IoT Hub. In the next lab you will setup some Azure services to store and visualize the data.