Initializing Your Python Application

You must initialize your application and specify the connection configuration file to use when the application connects.

When you create your application, you can change the default connection configuration file name and location, as well as the log level for your application.

The default name of the configuration file is app_id-connect.cfg, and the default configuration directory is the directory from which you run the application (the current directory).

The default log level is DEBUG.

The application and the thing definition that corresponds to your client application must exist in Device Cloud.

To enable auto-registration, the thing definition must be associated with the application. Otherwise, you must create the thing manually on the Management Portal before you run your application.

For more information about creating applications and thing definitions, see the following:

  • Device Cloud Management Portal User's Guide: Thing Definitions
  • Device Cloud Management Portal User's Guide: Applications
  1. Create your client application.

    For the app_id parameter, specify a name for your application.

    client = iot.Client("MyApp")
  2. (Optional) Specify the directory name and file name of your configuration file.
    client.config.config_dir = config_dir
    client.config.config_file = config_file
    
  3. Apply the configuration changes.
    client.initialize()
  4. (Optional) Specify the log level.

    Valid log levels are CRITICAL, DEBUG, ERROR, INFO, LOG, WARNING, and ALL.

    client.log_level("INFO")
  5. Connect the application to Device Cloud.

    The timeout parameter specifies the maximum time in seconds to wait for a successful connection. The default value is zero.

    client.connect(5)

You are ready to define the other elements of your application, including data and methods.

When your application terminates, call the disconnect function to disconnect your application from Device Cloud.