Exemple #1
0
 def connect(self, transport_type, connection_string, cert):
     print("connecting using " + transport_type)
     auth_provider = auth.from_connection_string(connection_string)
     if "GatewayHostName" in connection_string:
         auth_provider.ca_cert = cert
     self.client = IoTHubDeviceClient.from_authentication_provider(
         auth_provider, transport_type)
     self.client.connect()
Exemple #2
0
# license information.
# --------------------------------------------------------------------------

import os
import time
import uuid
from azure.iot.device import IoTHubDeviceClient, Message
from azure.iot.device import auth

# The connection string for a device should never be stored in code. For the sake of simplicity we're using an environment variable here.
conn_str = os.getenv("IOTHUB_DEVICE_CONNECTION_STRING")
# The "Authentication Provider" is the object in charge of creating authentication "tokens" for the device client.
auth_provider = auth.from_connection_string(conn_str)
# For now, the SDK only supports MQTT as a protocol. the client object is used to interact with your Azure IoT hub.
# It needs an Authentication Provider to secure the communication with the hub, using either tokens or x509 certificates
device_client = IoTHubDeviceClient.from_authentication_provider(
    auth_provider, "mqtt")

# Connect the client.
device_client.connect()

# send 5 messages with a 1 second pause between each message
for i in range(1, 6):
    print("sending message #" + str(i))
    msg = Message("test wind speed " + str(i))
    msg.message_id = uuid.uuid4()
    msg.correlation_id = "correlation-1234"
    msg.custom_properties["tornado-warning"] = "yes"
    device_client.send_event(msg)
    time.sleep(1)

# send only string messages