You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
IoT-For-Beginners/translations/pcm/2-farm/lessons/6-keep-your-plant-secure/single-board-computer-x509.md

3.7 KiB

Use di X.509 certificate for your device code - 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 wit di X.509 certificate.

Connect your device to IoT Hub

Di next step na to connect your device to IoT Hub wit di X.509 certificates.

Task - connect to IoT Hub

  1. Copy di key and certificate files go di folder wey get your IoT device code. If you dey use Raspberry Pi through VS Code Remote SSH and you create di keys for your PC or Mac, you fit drag and drop di files enter di explorer for VS Code to copy dem.

  2. Open di app.py file

  3. To connect wit X.509 certificate, you go need di host name of di IoT Hub, and di X.509 certificate. Start by creating one variable wey go hold di host name by adding dis code before you create di device client:

    host_name = "<host_name>"
    

    Replace <host_name> wit di host name of your IoT Hub. You fit find am for di HostName section inside di connection_string. E go be di name of your IoT Hub, wey go end wit .azure-devices.net

  4. Under dis one, declare one variable wey go hold di device ID:

    device_id = "soil-moisture-sensor-x509"
    
  5. You go need one instance of di X509 class wey go hold di X.509 files. Add X509 to di list of classes wey you dey import from di azure.iot.device module:

    from azure.iot.device import IoTHubDeviceClient, Message, MethodResponse, X509
    
  6. Create one X509 class instance wey go use your certificate and key files by adding dis code under di host_name declaration:

    x509 = X509("./soil-moisture-sensor-x509-cert.pem", "./soil-moisture-sensor-x509-key.pem")
    

    Dis one go create di X509 class using di soil-moisture-sensor-x509-cert.pem and soil-moisture-sensor-x509-key.pem files wey you don create before.

  7. Replace di line of code wey dey create di device_client from connection string, wit dis one:

    device_client = IoTHubDeviceClient.create_from_x509_certificate(x509, host_name, device_id)
    

    Dis one go connect using di X.509 certificate instead of connection string.

  8. Delete di line wey get connection_string variable.

  9. Run your code. Dey monitor di messages wey di device dey send go IoT Hub, and dey send direct method requests like before. You go see say di device don connect and e dey send soil moisture readings, plus e dey receive direct method requests.

💁 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 wit X.509 certificate!


Disclaimer:
Dis dokyument don use AI transle-shon service Co-op Translator do di transle-shon. Even as we dey try make am accurate, abeg make you sabi say transle-shon wey machine do fit get mistake or no dey correct well. Di original dokyument for di language wey dem take write am first na di one wey you go take as di correct source. For important mata, e good make you use professional human transle-shon. We no go fit take blame for any misunderstanding or wrong interpretation wey fit happen because you use dis transle-shon.