This sample shows how to send data from a light sensor (photoresistor) to an Azure IoT Hub (and to Azure storage) using a Node.js UWP app.
If you don’t have an Azure IoT hub, go to the ‘Create and IoT hub’ section in this link for instructions to set one up. It’s free to try out!
For this sample you will only need to get the connection string (see below) for your IoT Hub. You’ll get that from your Azure Portal
Start Visual Studio 2017 and create a new project (File | New Project…). In the
New Project dialog, navigate to
Basic Node.js Johnny-Five Application (Universal Windows) template (shown below), enter a name for your project, then press OK.
Wait for the Johnny-Five package and its dependencies to complete downloading. This will be indicated by the message below in the npm output window.
Right-click on the npm node in the Solution Explorer (shown below) and select Update npm Packages. This step will run npm dedupe and update serialport (a Johnny-Five dependency) with a version that works with Node.js UWP.
Right-click on the npm node again and select Install New npm Packages(s). When the dialog (shown below) is displayed, search for
azure-iothub, select the latest version found, then click on the Install Package button.
Repeat this step for
Replace the code in app.js with the code shown below. Every 10 seconds, the code will send the value of the photoresistor to your hub. Be sure to assign connectionString and deviceConnectionString with a valid strings.
Connect your Arduino and Raspberry Pi 2 or 3 with the USB cable. If your Raspberry Pi 2 or 3 is connected to a monitor, you should see the device getting recognized as shown in the image below (the name of the device may be “Arduino Uno” instead of “USB Serial Device”):
Use the instructions here to set up the connection between your photoresistor and the Arduino. The final setup should look something like this:
Go to the Project menu and select ‘<Your project name> Properties’ (You could also right-click on the project node in solution explorer to access Properties).
Enter the IP Address in the Remote Machine text box. Since you’re building for Raspberry Pi 2 or 3, select
ARM in the dropdown menu.
Now we’re ready to deploy the app to the Raspberry Pi 2 or 3. Simply press F5 (or select Debug | Start Debugging) to start the app.
You can then view the data sent in real-time with iothub-explorer.
npm install -g iothub-explorer@latest
iothub-explorer monitor-events myRPi2 --login <Your IoT Hub connection string> . After running the command you should then see your data being received:
You can also send the light sensor data to Azure storage. If you don’t have an account, go to this link for instructions to set one up. It’s free to try out! When you have a storage account set up, follow the instructions below:
Right-click on the npm node in Solution Explorer and select Install New npm Packages(s). When the dialog (shown below) is displayed, search for
azure, select the latest version found, then click on the Install Package button.
Replace the code in app.js with the code shown below. Every 10 seconds, the code will send the value of the photoresistor to the table specified. Be sure to enter valid values for accountName and accountKey. You may also change tableName.
Press F5 to deploy and start the app.
You can then view the data being sent to Azure storage with Power BI (download here). In Power BI, click on the Get Data button, select ‘Microsoft Azure Table Storage’ as your source, then follow the steps to connect. Once connected you can select your table and view the light sensor data that has been sent from your Raspberry Pi 2 or 3.