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.
-
Configure the Raspberry Pi to send messages to the IoT Hub.
-
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. -
Alternatively you can download the file directly to your Raspberry Pi using:
git clone https://github.com/khilscher/IoTHubPiHackathon.git
and edit theSenseHat_IoTHub_Http_Lab.py
using a text editor such as Nano. -
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
-
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 fordeviceId =
and paste in the Device ID you created earlier. -
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)
- Log into the Raspberry Pi using PuTTY.
- Verify that the file was transfered by listing the directory:
ls -l
-
Start sending messages by invoking the script in Python
pi@raspberrypi:~ $ python SenseHat_IoTHub_Http_Lab.py
-
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>
-
Referring to the Sense Hat API, update the code to send other telemetry to IoT Hub from the Sense HAT.
-
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). -
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.
-
Update
SenseHat_IoTHub_Http_Lab.py
to display the HTTP response code from the IoT Hub message onto the Sense HAT LED display. -
To send messages from IoT Hub back to your Raspberry Pi:
-
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.
- 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)
Congratulations! You just connected your Raspberry Pi to the IoT hub and created an application which demonstrated the two-way messaging capabilities.
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.