Mqttclient

Overview

The main difference between mqttclient and simplemqttclient is that this client program can now print two additional types of JSON-string as messages into mosquitto. These two types are: power-and-heat sensor and volt-and-amp sensor.

Also to make data-parsing easier on the GridDB-Sink connector side of the program, additional fields have been added to the message:

  • id: This represents what would be a sensor-id tag that uniquely identifies a device
  • type: This represents what kind of data the device records.
Determining message format(mqttclient.c)
if(strcmp(argv[1],"light") == 0){
	sprintf(type,"light"); // Set type
	sprintf(idString, "A_%d",id); // Set id-string
	sprintf (text, "{ id: %s, type: %s, light: %f, sound : %f }",idString,type,metric1,metric2); 
		// Create message as a JSON-string
} 
else if(strcmp(argv[1],"volts") == 0) {
	sprintf(type,"volts"); // Set type
	sprintf(idString, "B_%d",id); // Set id-string
	sprintf(text, "{ id: %s, type: %s , volts : %f, amps : %f }",idString,type,metric1, metric2);
	// Create message as a JSON-string
} 
else if(strcmp(argv[1],"watts") == 0) {
	sprintf(type,"watts"); // Set type
	sprintf(idString, "C_%d",id); // Set id-string
	sprintf(text, "{ id: %s, type: %s, watts : %f, heat : %f }",idString,type,metric1, metric2);
	// Create message as a JSON-string
} 

Once the message has been created, the message will go through the same process as mentioned in simplemqttclient.c

Source Code

Complete source code used for the GridDB Kafka and MQTT Application can be downloaded from the following:

Download: griddb-kafka-mqtt-application.tar.gz