290 likes | 1.38k Views
MQTT. Documentation and Downloads. See: http://www.eclipse.org/paho/ Libraries for a variety of languages including: C++ Java JavaScript Python Go. General Flow. Create a client instance Connect to a broker using one of the connect*() functions
E N D
Documentation and Downloads • See: http://www.eclipse.org/paho/ • Libraries for a variety of languages including: • C++ • Java • JavaScript • Python • Go
General Flow • Create a client instance • Connect to a broker using one of the connect*() functions • Call one of the loop*() functions to maintain network traffic flow with the broker • Use subscribe() to subscribe to a topic and receive messages • Use publish() to publish messages to the broker • Use disconnect() to disconnect from the broker • Callbacks are called to allow the application to process events as necessary
IoT-2 Details • Two types of clients • Devices • Send messages • Receive commands • Applications • Receive messages • Send commands
Device Protocol DEVICE_TYPE = 'v' DEVICE_ORG = 'edg1f' IOT_SERVER = DEVICE_ORG + '.messaging.internetofthings.ibmcloud.com' IOT_PORT = 1883 deviceId = ‘LUCAS77’ device_username = "use-token-auth" device_password = ‘NML?jTHhmSGLlr58!u’ device_clientid = "d:" + DEVICE_ORG + ":"+DEVICE_TYPE+":" + deviceId; deviceClient = mqtt.Client(device_clientid, True) deviceClient.username_pw_set(device_username, device_password) # Publish string ############### telemetryTopic='iot-2/evt/telemetry/fmt/json'
Device Message Handlers # mqtt client event handlers ############################ defdevice_on_publish(client, userdata, mid): print "device_on_publish has fired" print 'device_on_publish: Message ID: ' +str(mid) + ' has been published successfully' defdevice_on_connect(client, userdata, flags, rc): print 'device_on_connect has fired' print 'device_on_connect '+mqtt.connack_string(rc)+ '[message ID: '+str(rc)+']' if rc == 0: print 'Device Connection Success!' client.on_publish=device_on_publish print '---- .on_publish: ' + str(client.on_publish) defdevice_on_disconnect(client, userdata, rc): print "device_on_disconnect has fired" print 'device_on_disconnect: ' + mqtt.connack_string(rc) deviceClient.on_disconnect=device_on_disconnect deviceClient.on_connect=device_on_connect
Device Connect and Publish # Connect the device client res = self.mqttclient.connect(IOT_SERVER, IOT_PORT) self.mqttclient.loop_start() # Publish a message to MQTT row=self.fileReader.next() data = self.cvt2json(row) response = self.mqttclient.publish(telemetryTopic,data)
Application Protocol app_username ='a:edg1f:prizvzry3k' app_password='?OHZ6zc4K74)ReawyT' app_client_id = 'a:edg1f:tedstestapp1011' appClient = mqtt.Client(app_client_id, True) appClient.username_pw_set(app_username, app_password) #Subscribe propertyTopic='iot-2/type/v/id/+/evt/telemetry/fmt/json’
App Message Handlers def app_on_message(client, userdata, message): print "app_on_message has fired" print 'app_on_message received an TOPIC: '+message.topic +' MESSAGE: ' + str(message.payload) #This is where we would call a processing routine to actually handle the message def app_on_connect(client, userdata, flags, rc): print 'app_on_connect has fired' print 'app_on_connect '+mqtt.connack_string(rc)+ '[message ID: '+str(rc)+']' if rc == 0: #client.on_message = app_on_message print '---- .message: ' + str(client.on_message) resp=client.subscribe(propertyTopic) print 'app_on-connect ' print 'app_on_connect.subscribe: ' + mqtt.error_string(resp[0]) print ’---- Message ID: ['+ str(resp[1])+']' def app_on_disconnect(client, userdata, rc): print "app_on_disconnect has fired" print 'app_on_disconnect: ' + mqtt.connack_string(rc) appClient.on_connect=app_on_connect appClient.on_disconnect=app_on_disconnect appClient.on_message=app_on_message
App Connect & Loop self.mqttc.connect(IOT_SERVER, IOT_PORT) mqttc.loop_start()
Tools Etc • Handy IoT test tool • http://mqtt-helper.mybluemix.net • IoT Cloud Dashboard • https://internetofthings.ibmcloud.com/dashboard • PAHO Getting started page • http://www.eclipse.org/paho/#getting-started • IoT documentation page (RESTful API) • https://developer.ibm.com/iot/recipes/api-documentation/ • NOT real time – historical data access only