GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Client subscribing in on_connect,but messages sent on config not received · Issue #2033 · GoogleCloudPlatform/python-docs-samples

Client subscribing in on_connect,but messages sent on config not received  #2033

New issue
Copy link
New issue
Copy link
Closed
Closed
Client subscribing in on_connect,but messages sent on config not received #2033
Copy link

Description

@ishitar
ishitar
opened on Mar 7, 2019
Issue body actions

Describe the issue

In on_connect I'm trying to subscribe to the config topic, but it's not happening for some reason. When I do it in the main() in client.loop_forever() it works but then I'm not able to retreive the persisted messages when device re connects.

Basic code is as given below:

def on_connect(unused_client, unused_userdata, unused_flags, rc):
	"""Callback for when a device connects."""
	device_id=unused_userdata
	print('on_connect', error_str(rc))
	mqtt_config_topic = '/devices/{}/config'.format(args.device_id)
	print('Subscribing to {}'.format(mqtt_config_topic))
	client.subscribe(mqtt_config_topic, qos=1)

def main():
	global minimum_backoff_time
	args = parse_command_line_args()
	run_get(args)
	# Create our MQTT client. The client_id is a unique string that identifies
	# this device. For Google Cloud IoT Core, it must be in the format below.
	client = mqtt.Client(client_id=(	'projects/{}/locations/{}/registries/{}/devices/{}'
					.format(args.project_id, args.cloud_region,args.registry_id, args.device_id)), 
                                        clean_session=False)

	# With Google Cloud IoT Core, the username field is ignored, and the
	# password field is used to transmit a JWT to authorize the device.
	client.username_pw_set(
			username='unused',
			password=create_jwt(
					args.project_id, args.private_key_file, args.algorithm))

	# Enable SSL/TLS support.
	client.tls_set(ca_certs=args.ca_certs, tls_version=ssl.PROTOCOL_TLSv1_2)

	client.on_connect = on_connect
	client.on_publish = on_publish
	client.on_message=on_message
	client.on_disconnect = on_disconnect

	client.user_data_set(args.device_id)

	client.connect(args.mqtt_bridge_hostname, args.mqtt_bridge_port)
	time.sleep(5)
        client.loop_forever()



Activity

  1. ishitar
    changed the title [-]Client not subscribing in on_connect[/-] [+]Client subscribing in on_connect,but messages sent on config not received [/+] on Mar 7, 2019
  2. ishitar
    closed this as completedon Mar 7, 2019
  3. ishitar commented on Mar 7, 2019

    @ishitar
    ishitar
    on Mar 7, 2019
    Author
    More actions

    I figured out the error, wrote args.device_id instead of device_id while subscribing.
    Though, now the messages sent in config aren't being received.

  4. ishitar
    reopened this on Mar 7, 2019
  5. ishitar commented on Mar 8, 2019

    @ishitar
    ishitar
    on Mar 8, 2019
    Author
    More actions

    Figured out. Wrote client.subscribe instead of unused_client.subscribe in the on_connect() callback.

  6. ishitar
    closed this as completedon Mar 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.