Skip to content

Latest commit

 

History

History
 
 

3

Folders and files

NameName
Last commit message
Last commit date
 
 

Connect Your Raspberry Pi to the Remote Monitoring Pre-configured Solution

In this lab, you will configure your Raspberry Pi to connect to the IoT solution that you created earlier. You will create a small application on your Raspberry Pi (python script) to send a D2C (Device to Cloud) message to the IoT Hub and also create another application to receive a C2D (Cloud to Device) message that will be displayed on the Sense HAT or the Sense HAT emulator.

  1. Configure the Raspberry Pi to send messages to the IoT Hub.

  2. Copy the Python code from this HOL to a file. Save the file as SenseHat_IoTHub_Http_Lab.py and open it with a text editor such as Notepad.

  3. Alternatively you can download the file directly to your Raspberry Pi using: git clone https://github.com/khilscher/IoTHubPiHackathon.git and edit the SenseHat_IoTHub_Http_Lab.py using a text editor such as Nano.

  4. Comment/uncomment the import statements that correspond to whether you are using a Sense HAT or the Sense Hat emulator. Below the import statements, you will see:

    #from sense_hat import SenseHat #if using a physical SenseHat attached to Raspi
    from sense_emu import SenseHat #if using a Sense HAT Emulator (Linux only)

    • if using the Sense HAT, uncomment the from sense_hat statement and comment the from sense_emu statement
    • if using the Sense HAT emulator, comment the from sense_hat and uncomment the from sense_emu statement
  5. Next, you will provide the information required to connect the Raspberry Pi to the IoT pre-configured solution: - Update the file with the primary key connection string. Look for connectionString = and paste in the IoT Hub "Connection string - primary key" you noted earlier. (Azure Portal -> IoT Hub -> iothubowner -> Connection string-primary key) - Search for deviceId = and paste in the Device ID you created earlier.

  6. Copy SenseHat_IoTHub_Http_Lab.py to your Raspberry Pi using PuTTY. - If you installed PuTTY using the default settings, the PuTTY environment variables should be set in your PATH already. Otherwise, the pscp executable will be in your PuTTY directory.

Open up a command prompt and enter the following command to copy the python script to your Raspberry Pi. If you didn't change the username/password, it should be pi/raspberry

pscp SenseHat_IoTHub_Http_Lab.py <userid>@<server ip or server name>:/<$path>/SenseHat_IoTHub_Http_Lab.py

(for the <$path>, use /home/pi)

  1. Log into the Raspberry Pi using PuTTY.
  2. Verify that the file was transfered by listing the directory: ls -l

ls -l

  1. Start sending messages by invoking the script in Python

    pi@raspberrypi:~ $ python SenseHat_IoTHub_Http_Lab.py
    
  2. Connect your Device Explorer to the IoT Hub and check to see if messages are arriving at the IoT Hub: - On your laptop, open Device Explorer. Under the Configuration tab, copy and paste the IoT Hub Connection String obtained earlier and click "Update". This connects the Device Explorer app to the IoT Hub that you created.

- Click the Data tab, select your device from the Device ID list, click the "enable" checkbox beside the *Consumer Group* field, enter "deviceexplorer" in the "Consumer Group" text box, and click "Monitor". If you see messages arriving then Congratulations, your Raspberry Pi is now sending data to Azure IoT Hub. 

  <p align="center">
     <img src="/images/DeviceExplorer-ReceiveData.jpg" width="50%" height="50%" /> 
  </p>
  1. Referring to the Sense Hat API, update the code to send other telemetry to IoT Hub from the Sense HAT.

  2. Update the SenseHat_IoTHub_Http_Lab.py code to send multiple telemetry data points (e.g. Yaw, Pitch, Roll, or Temperature, Pressure, Humidity) in a single JSON-formatted message to IoT Hub. See [sample_payload.json] (https://github.com/khilscher/IoTHubPiHackathon/blob/master/sample_payload.json).

  3. Go to the remote monitoring pre-configured solution dashboard and take a look at the new telemetry data points that are being plotted on the Telemetry History chart.

  4. Update SenseHat_IoTHub_Http_Lab.py to display the HTTP response code from the IoT Hub message onto the Sense HAT LED display.

  5. To send messages from IoT Hub back to your Raspberry Pi:

  6. On your laptop, open Device Explorer, click the Messages to Device tab, select your device from the Device ID list, type in a message into the Message textbox and click Send.

Send Message

  1. On your Sense HAT, you should see the message appear on the display. (if you are using the Sense HAT emulator, you will need to VNC to your Raspberry Pi and open the Sense HAT Emulator application: Menu -> Programming -> Sense HAT Emulator)

Sense HAT Message Display

Congratulations! You just connected your Raspberry Pi to the IoT hub and created an application which demonstrated the two-way messaging capabilities.

Hands-On assignment.

Alter the Python code to send the following items to the IoT Hub in JSON format:

  • Pitch
  • Yaw
  • Roll
  • Humidity
  • Temperature

The correct script can be found here. If you copy this script, remember to put in your IoT Hub connection string, and the Device Id.

Back to Main HOL Instructions