W MQTTClient
W MQTTClient
Version 1
Library/Design Limitation:
Since that library had been developed “especially for M2M nodes”, many features of MQTT
is not implemented.
- Message length is limited by 127 chars
- Only “Fire & Forget” type message had been implemented
- Only the functions which is necessary to send “data” from Node to server (i.e. connect
Message, send Message, disconnect Message) had been implemented
Library Functions
MQTTConnect Message
This function, prepares a MQTT message string which can be used for “Connect to MQTT
Server” using existing tcp/ip connectivity. This function expects a message buffer to be
placed with required information for MQTT Server connection. This function will populate
the given string with necessary data (i.e. Req Type, Protocol Name etc). Since, connect
request message mostly contains fixed information (except client ID), supplying only
ClientID is sufficient for this function.
Syntax:
void mqtt_connect_message(uint8_t * mqtt_message, char * client_id)
example:
mqtt_connect_message(mqttMessage, clientId);
where, mqTTMessage is the string that is going to be send to MQTT Server via tcp
where
byte mqtt_message [127*]; // prepared MQTT formatted message to be send to server
char client_id [20*] // arbitrary client id to be used at MQTT Connect message
Please notice that maximum MQTT message size is 127byte in wMQTT Client !
* sizes are given as example. Please use array size which is suitable for your application
MQTTPublish Message
Prepares a Publish MQTT Message (populates the string which is passed as parameter to
function) which can be used to publish a MQTT Message to specified topic (ie. Parameter
“topic”) and given content (ie. Parameter passed as “message) .
Syntax:
void mqtt_publish_message(uint8_t * mqtt_message, char * topic, char * message)
example:
mqtt_publish_message(mqttMessage, topic, message);
where
byte mqttMessage[127*]; // prepared MQTT formatted message to be send to server
char topic[10*] // the name of the topic where message will be published
char message[80*] // the message content to be published at specified topic
* sizes are given as example. Please use array size which is suitable for your application
Please notice that maximum MQTT message size is 127byte in wMQTT Client !
MQTTDisconnect Message
Prepare a Disconnect Message (populates the string which is passed as paramteret to function)
which will be used to disconnect from MQTT Server
Syntax:
void mqtt_disconnect_message(uint8_t * mqtt_message)
example:
mqtt_disconnect_message (mqttMessage);
where
byte mqttMessage[127*]; // prepared MQTT formatted message to be send to server
* size are given as example. Please use array size which is suitable for your application
Please notice that maximum MQTT message size is 127byte in wMQTT Client !
Example Source Code (for Arduino 1.0 or above)
/*
Very, very primitive. MQTT Test program to send data to a MQTT Server
#include <mqtt.h>
/* #include <SoftwareSerial.h>
#include "TinyGPS.h"
TinyGPS gps;
*/
int i = 1;
char atCommand[50];
byte mqttMessage[127];
int mqttMessageLength = 0;
int sentCount = 0;
void setup() {
pinMode(13, OUTPUT);
pinMode(14, OUTPUT); // GSM
digitalWrite(0, HIGH);
Serial.begin(9600);
Serial1.begin(19200);
// gpss.begin(9600);
Serial.println("Hello");
}
void loop(){
if (gprsReady == true){
Serial.println("GPRS Ready");
// Change the IP and Topic.
/* The arguments here are:
clientID, IP, Port, Topic, Message
*/
sendMQTTMessage("nodeTest", "test.mosquitto.org", "1883", "MFNodeTopic", "A Test data from WeightlessMQTT Client");
}
delay(10000);
while (str[0] != 0) {
if ((str[0] > '9') || (str[0] < '0'))
return d;
d *= 10;
d += str[0] - '0';
str++;
}
return d;
}
void readline() {
/*char c;
boolean isGPRSReady(){
Serial1.println("AT+CGATT?");
index = 0;
while (Serial1.available()){
data1 = (char)Serial1.read();
Serial.write(data1);
gprsStr[index++] = data1;
}
Serial.println("Check OK");
Serial.print("gprs str = ");
Serial.println(gprsStr);
if (gprsStr.indexOf("+CGATT: 1") > -1){
Serial.println("GPRS OK");
return true;
}
else {
Serial.println("GPRS NOT OK");
return false;
}
}
void sendMQTTMessage(char* clientId, char* brokerUrl, char* brokerPort, char* topic, char* message){
Serial1.println("AT"); // Sends AT command to wake up cell phone
Serial.println("AT");
delay(1000); // Wait a second
digitalWrite(13, HIGH);
Serial1.println("AT+CIICR");
Serial.println("AT+CIICR");
delay(2000);
Serial1.println("AT+CIFSR");
Serial.println("AT+CIFSR");
delay(2000);
strcpy(atCommand, "AT+CIPSTART=\"TCP\",\"");
strcat(atCommand, brokerUrl);
strcat(atCommand, "\",\"");
strcat(atCommand, brokerPort);
strcat(atCommand, "\"");
Serial1.println(atCommand);
Serial.println(atCommand);
// Serial.println("AT+CIPSTART=\"TCP\",\"mqttdashboard.com\",\"1883\"");
delay(2000);
Serial1.println("AT+CIPSEND");
Serial.println("AT+CIPSEND");
delay(2000);
mqttMessageLength = 16 + strlen(clientId);
Serial.println(mqttMessageLength);
mqtt_connect_message(mqttMessage, clientId);
Serial1.println("AT+CIPSEND");
Serial.println("AT+CIPSEND");
delay(2000);
Serial1.println("AT+CIPCLOSE");
Serial.println("AT+CIPCLOSE");
delay(2000);
}
Credits:
Initial “language neutral” design by Reha Yurdakul, [email protected]
Arduino Implementation by Ates Yurdakul, [email protected]
Final implementation by Omer Sever, [email protected]