Functions Description
SetCredentials(mqtt_username, mqtt_password)
This function allows the user to set the username and password designated for that topic or MQTT URL.
setCredentials('iot_1234', '123456')
The above function call has an example username and password. Using this function, you can set the designated username and password for your asset inside a project. The username and password can be seen under the settings tab inside a project.
DebugMode(condition)
This function is used to enable or disable all the print statement present inside the code; it accepts only Boolean variables (true/false)
debugMode(true)
The above function call passes true as an argument, which will enable all the debug statements (print statements) in the code. If passing false as an argument the debug statements (print statements) will be disabled and only the normal code function as usual.
SetTopic(mqtt_topic)
This function assigns mqtt topic with the function argument value.
setTopic('a/IIOT_demo_12345')
The above function calls a specific topic, this will assign the topic to the local variable inside the library.
AddParameter(key,value)
This function appends the data to be sent via mqtt in a key,value pair.
addParameter('temperature', 32.21)
The above function is used to append data in the key value pair format onto a dictionary variable. This variable will be later used inside the publish function to send the appended data.
On_message(client,userdata,msg)
This function is a callback function, and this executes soon after a message is received via MQTT. It processes the information only for the designated mqtt subscribe topic, and not any topic in which the information is received..
self.mqclient.on_message = self.on_message
The above statement will be included inside the initMqtt() function. This is considered as a callback function and will get executed soon after it receives any message on the subscribe topic. This function displays the message and the topic on which the message has been received.
On_disconnect(client,userdata,rc)
This function is a callback function and executes soon after the connection is terminated with the MQTT broker. This function sets the connected_flag to false, every time it disconnects from MQTT.
self.mqclient.on_disconnect = self.on_disconnect
The above statement will be included inside the initMqtt() function. This is considered as a callback function and will be executed soon after the MQTT client is disconnected due to various factors. This function will toggle the connected_flag to false and stop the mqtt loop.
On_connect(client,userdata,flags,rc)
This function executes when it is connected to the mqtt server, then this function sets the connected_flag to true and subscribes to the required mqtt topic and stores the timestamp of the last received data onto the variable LastReceiveTime.
self.mqclient.on_connect = self.on_connect
The above statement will be included inside the initMqtt()function. This is considered as a callback function and will be executed soon after the MQTT client is connected to the server. This function will toggle the connected_flag to true, and established connection with mqtt server, also starts the mqtt loop function.
InitMqtt()
This function is used to initialize mqtt, it sets the username and password as per the information given by user. It also initializes callback functions like on_connect, on_message, on_disconnect.
initMqtt()
This function has no argument required but has significant importance in establishing a connection with MQTT. This is the function where an object is created for the mqtt client, and also initializes all the required callback function mentioned above. Several other functions of this function are to set username and password given by user and establish a connection with the mqtt server then start the mqtt loop.
Publish()
This function publishes the set message to the set mqtt server, port, username, and password.
publish()
This function has no arguments. This function will first check for connected_flag, only then it starts to send message to the designated mqtt server on a specific topic.The data will be taken from the dictionary variable to which that data has been appended in the addParameter() function, Once the data is sent, it will clear the variable and wait for any other new parameters to be added. Returns none.
Debug_statements(message)
This function is used to print messages as per users input to the argument.
debug_statements('Connection established with MQTT Broker')
The above function call passes an argument of a message to be printed in terminal.
GetLastPublishTime()
To get last, publish time this function can be used.
getLastPublishTime()
This function doesn't need any input arguments thus, this function doesn’t have any input arguments.
GetLastDisconnectTime()
To get the last disconnect time of the device from the MQTT broker this function is used.
getLastDisconnectTime()
This function doesn't require any input arguments thus, this function doesn’t have any input arguments, with this function we can publish last publish time.
GetTopic()
To get the topic of the project use this command.
getTopic()
The function returns the topic of the project which is a string. There is no need for any input arguments.
GetMacId()
This function returns Media Access Control (MAC Address) of the device.
getMacId()
The command returns with string of MAC ID of the project. There is no need for any input arguments thus, this function doesn’t have any input arguments.
GetCredentials()
This function returns the username and password.
getCredentials()
There is no need for any input arguments. The function returns the credentials of the project in string format.
GetLastReceiveTime()
To get the last received time of device from the MQTT broker this function is used.
getLastReceiveTime()
The function returns last received time and there no need of any input arguments.
GetToken()
This function returns token/password.
getToken()
This function doesn't require any input arguments. The command returns the password for the mqtt broker in string format.
GetConfigs()
This function returns configuration data of username, password, and topic.
getConfigs()
The function returns in Json format. This function doesn't require any input arguments thus, this function doesn’t have any input arguments.
GetProjectName()
This function returns the project name.
getProjectName()
This function doesn't require any input arguments. The command returns the project name in string.
GetProjectId()
This function returns the project ID.
getProjectId()
The function returns project ID in string. This function doesn't require any input arguments.