5.7 KiB
Connect your IoT device to di cloud - Virtual IoT Hardware and Raspberry Pi
For dis part of di lesson, you go connect your virtual IoT device or Raspberry Pi to your IoT Hub, so e fit dey send telemetry and receive commands.
Connect your device to IoT Hub
Di next step na to connect your device to IoT Hub.
Task - connect to IoT Hub
-
Open di
soil-moisture-sensorfolder for VS Code. Make sure say di virtual environment dey run for di terminal if you dey use virtual IoT device. -
Install some extra Pip packages:
pip3 install azure-iot-deviceazure-iot-devicena library wey go help you communicate with your IoT Hub. -
Add di imports wey dey below to di top of di
app.pyfile, under di imports wey dey already:from azure.iot.device import IoTHubDeviceClient, Message, MethodResponseDis code go import di SDK wey go help you communicate with your IoT Hub.
-
Remove di
import paho.mqtt.client as mqttline because we no need dis library again. Remove all di MQTT code including di topic names, all di code wey dey usemqtt_clientand dihandle_command. Make sure say you keep diwhile True:loop, just delete dimqtt_client.publishline wey dey inside dis loop. -
Add di code wey dey below under di import statements:
connection_string = "<connection string>"Replace
<connection string>with di connection string wey you collect for di device earlier for dis lesson.💁 Dis no be di best way to do am. Connection strings no suppose dey inside source code because e fit enter source code control and anybody fit see am. We dey do am like dis for now to make am simple. Normally, you suppose use something like environment variable and tool like
python-dotenv. You go learn more about dis one for di next lesson. -
Under dis code, add di code wey dey below to create device client object wey fit communicate with IoT Hub, and connect am:
device_client = IoTHubDeviceClient.create_from_connection_string(connection_string) print('Connecting') device_client.connect() print('Connected') -
Run dis code. You go see say your device don connect.
pi@raspberrypi:~/soil-moisture-sensor $ python3 app.py Connecting Connected Soil moisture: 379
Send telemetry
Now wey your device don connect, you fit dey send telemetry go IoT Hub instead of di MQTT broker.
Task - send telemetry
-
Add di code wey dey below inside di
while Trueloop, just before di sleep:message = Message(json.dumps({ 'soil_moisture': soil_moisture })) device_client.send_message(message)Dis code go create IoT Hub
Messagewey get di soil moisture reading as JSON string, then e go send am go IoT Hub as device to cloud message.
Handle commands
Your device need to handle command wey di server code go send to control di relay. Dis one dey sent as direct method request.
Task - handle a direct method request
-
Add di code wey dey below before di
while Trueloop:def handle_method_request(request): print("Direct method received - ", request.name) if request.name == "relay_on": relay.on() elif request.name == "relay_off": relay.off()Dis one dey define method,
handle_method_request, wey go run when direct method dey called by IoT Hub. Each direct method get name, and dis code dey expect method wey dem callrelay_onto turn di relay on, andrelay_offto turn di relay off.💁 You fit also do dis one for one direct method request, by passing di state wey you want for di relay inside payload wey fit dey passed with di method request and dey available from di
requestobject. -
Direct methods need response to show di calling code say dem don handle am. Add di code wey dey below for di end of di
handle_method_requestfunction to create response for di request:method_response = MethodResponse.create_from_method_request(request, 200) device_client.send_method_response(method_response)Dis code dey send response to di direct method request with HTTP status code of 200, and e go send am back to IoT Hub.
-
Add di code wey dey below under dis function definition:
device_client.on_method_request_received = handle_method_requestDis code dey tell di IoT Hub client to call di
handle_method_requestfunction when direct method dey called.
💁 You fit find dis code for di code/pi or code/virtual-device folder.
😀 Your soil moisture sensor program don connect to your IoT Hub!
Disclaimer:
Dis dokyument don use AI transleto service Co-op Translator do di translation. Even as we dey try make am accurate, abeg sabi say machine translation fit get mistake or no dey correct well. Di original dokyument wey dey for im native language na di main source wey you go trust. For important mata, e good make professional human transleto check am. We no go fit take blame for any misunderstanding or wrong interpretation wey fit happen because you use dis translation.