Skip to content

Latest commit

 

History

History
92 lines (68 loc) · 5.12 KB

iot-hub-raspberry-pi-kit-node-lesson4-send-cloud-to-device-messages.md

File metadata and controls

92 lines (68 loc) · 5.12 KB
featureFlags title description services documentationcenter author manager tags keywords ROBOTS redirect_url ms.assetid ms.service ms.devlang ms.topic ms.tgt_pltfrm ms.workload ms.date ms.author
usabilla
Connect Raspberry Pi (Node) to Azure IoT - Lesson 4: Cloud-to-device | Microsoft Docs
The sample application runs on Pi and monitors incoming messages from your IoT hub. A new gulp task sends messages to Pi from your IoT hub to blink the LED.
iot-hub
shizn
timlt
cloud to device, message from cloud
NOINDEX
/azure/iot-hub/iot-hub-raspberry-pi-kit-node-get-started
6ae6539e-1163-4490-8d72-fdf7803e3054
iot-hub
node
article
na
na
3/21/2017
xshi

Run the sample application to receive cloud-to-device messages

In this article, you deploy a sample application on Raspberry Pi 3. The sample application monitors incoming messages from your IoT hub. You also run a gulp task on your computer to send messages to Pi from your IoT hub. When the sample application receives the messages, it blinks the LED. If you have any problems, seek solutions on the troubleshooting page.

What you will do

  • Connect the sample application to your IoT hub.
  • Deploy and run the sample application.
  • Send messages from your IoT hub to Pi to blink the LED.

What you will learn

In this article, you will learn:

  • How to monitor incoming messages from your IoT hub
  • How to send cloud-to-device messages from your IoT hub to Pi.

What you need

Connect the sample application to your IoT hub

  1. Make sure that you're in the repo folder iot-hub-node-raspberrypi-getting-started. Open the sample application in Visual Studio Code by running the following commands:

    cd Lesson4
    code .

    Notice the app.js file in the app subfolder. The app.js file is the key source file that contains the code to monitor incoming messages from the IoT hub. The blinkLED function blinks the LED.

    Repo structure in the sample application

  2. Initialize the configuration file by using the following commands:

    npm install
    gulp init

    If you completed the steps in Create an Azure function app and storage account on this computer, all the configurations are inherited, so you can skip to the task of deploying and running the sample application. If you completed the steps in Create an Azure function app and storage account on a different computer, you need to replace the placeholders in the config-raspberrypi.json file. The config-raspberrypi.json file is in the subfolder of your home folder.

    Contents of the config-raspberrypi.json file

  • Replace [device hostname or IP address] with the IP address of Pi or the host name that you get by running the devdisco list --eth command.
  • Replace [IoT device connection string] with the device connection string that you get by running the az iot device show-connection-string --hub-name {my hub name} --device-id {device id} -g iot-sample {resource group name} command.
  • Replace [IoT hub connection string] with the IoT hub connection string that you get by running the az iot hub show-connection-string --name {my hub name} -g iot-sample {resource group name} command.

Note

Run gulp install-tools as well, if you haven't done it in Lesson 1.

Deploy and run the sample application

Deploy and run the sample application on Pi by running the following command:

gulp deploy && gulp run

The command deploys the sample application to Pi. Then, it runs the application on Pi and a separate task on your host computer to send 20 blink messages to Pi from your IoT hub.

After the sample application runs, it starts listening to messages from your IoT hub. Meanwhile, the gulp task sends several "blink" messages from your IoT hub to Pi. For each blink message that Pi receives, the sample application calls the blinkLED function to blink the LED.

You should see the LED blink every two seconds as the gulp task sends 20 messages from your IoT hub to Pi. The last one is a "stop" message that tells the application to stop running.

Sample application with gulp command and blink messages

Summary

You’ve successfully sent messages from your IoT hub to Pi to blink the LED. The next task is optional: change the on and off behavior of the LED.

Next steps

Change the on and off behavior of the LED