You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Client subscribing in on_connect,but messages sent on config not received #2033
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()
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
Activity
[-]Client not subscribing in on_connect[/-][+]Client subscribing in on_connect,but messages sent on config not received [/+]ishitar commentedon Mar 7, 2019
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.
ishitar commentedon Mar 8, 2019
Figured out. Wrote client.subscribe instead of unused_client.subscribe in the on_connect() callback.