-
-
Save arvind-india/bca47394b1923787317d80a0cd8abde8 to your computer and use it in GitHub Desktop.
Simple MQTT publish and subscribe using Paho embedded C/C++ Client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cp ../../src/MQTTClient.c . | |
sed -e 's/""/"MQTTLinux.h"/g' ../../src/MQTTClient.h > MQTTClient.h | |
gcc sub.c -I ../../src -I ../../src/linux -I ../../../MQTTPacket/src MQTTClient.c ../../src/linux/MQTTLinux.c ../../../MQTTPacket/src/MQTTFormat.c ../../../MQTTPacket/src/MQTTPacket.c ../../../MQTTPacket/src/MQTTDeserializePublish.c ../../../MQTTPacket/src/MQTTConnectClient.c ../../../MQTTPacket/src/MQTTSubscribeClient.c ../../../MQTTPacket/src/MQTTSerializePublish.c -o sub ../../../MQTTPacket/src/MQTTConnectServer.c ../../../MQTTPacket/src/MQTTSubscribeServer.c ../../../MQTTPacket/src/MQTTUnsubscribeServer.c ../../../MQTTPacket/src/MQTTUnsubscribeClient.c | |
gcc pub.c -I ../../src -I ../../src/linux -I ../../../MQTTPacket/src MQTTClient.c ../../src/linux/MQTTLinux.c ../../../MQTTPacket/src/MQTTFormat.c ../../../MQTTPacket/src/MQTTPacket.c ../../../MQTTPacket/src/MQTTDeserializePublish.c ../../../MQTTPacket/src/MQTTConnectClient.c ../../../MQTTPacket/src/MQTTSubscribeClient.c ../../../MQTTPacket/src/MQTTSerializePublish.c -o pub ../../../MQTTPacket/src/MQTTConnectServer.c ../../../MQTTPacket/src/MQTTSubscribeServer.c ../../../MQTTPacket/src/MQTTUnsubscribeServer.c ../../../MQTTPacket/src/MQTTUnsubscribeClient.c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
g++ pub.cpp -I ../../src/ -I ../../src/linux -I ../../../MQTTPacket/src ../../../MQTTPacket/src/MQTTPacket.c ../../../MQTTPacket/src/MQTTDeserializePublish.c ../../../MQTTPacket/src/MQTTConnectClient.c ../../../MQTTPacket/src/MQTTSubscribeClient.c ../../../MQTTPacket/src/MQTTSerializePublish.c ../../../MQTTPacket/src/MQTTUnsubscribeClient.c -o pub | |
g++ sub.cpp -I ../../src/ -I ../../src/linux -I ../../../MQTTPacket/src ../../../MQTTPacket/src/MQTTPacket.c ../../../MQTTPacket/src/MQTTDeserializePublish.c ../../../MQTTPacket/src/MQTTConnectClient.c ../../../MQTTPacket/src/MQTTSubscribeClient.c ../../../MQTTPacket/src/MQTTSerializePublish.c ../../../MQTTPacket/src/MQTTUnsubscribeClient.c -o sub |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include "MQTTClient.h" | |
#include <stdio.h> | |
#include <signal.h> | |
#include <memory.h> | |
#include <sys/time.h> | |
volatile int toStop = 0; | |
void usage(char** argv) | |
{ | |
printf("Usage: %s topic channel_token\n", argv[0]); | |
exit(-1); | |
} | |
void cfinish(int sig) | |
{ | |
signal(SIGINT, NULL); | |
toStop = 1; | |
} | |
int main(int argc, char** argv) | |
{ | |
int rc = 0; | |
unsigned char buf[100]; | |
unsigned char readbuf[100]; | |
if (argc < 3) | |
usage(argv); | |
char* host = "mqtt.beebotte.com"; | |
int port = 1883; | |
char* topic = argv[1]; | |
char* clientid = "myid"; | |
char* username = argv[2]; | |
Network n; | |
Client c; | |
signal(SIGINT, cfinish); | |
signal(SIGTERM, cfinish); | |
NewNetwork(&n); | |
ConnectNetwork(&n, host, port); | |
MQTTClient(&c, &n, 1000, buf, 100, readbuf, 100); | |
MQTTPacket_connectData data = MQTTPacket_connectData_initializer; | |
data.willFlag = 0; | |
data.MQTTVersion = 3; | |
data.clientID.cstring = clientid; | |
data.username.cstring = username; | |
//data.password.cstring = opts.password; | |
data.keepAliveInterval = 10; | |
data.cleansession = 1; | |
printf("Connecting to %s %d\n", host, port); | |
rc = MQTTConnect(&c, &data); | |
printf("Connected %d\n", rc); | |
printf("Publishing to %s\n", topic); | |
char payload[100]; | |
sprintf(payload, "Hello World - youppi!"); | |
MQTTMessage pubmsg; | |
pubmsg.payload = payload; | |
pubmsg.payloadlen = strlen(payload) + 1; | |
pubmsg.qos = QOS0; | |
pubmsg.retained = 0; | |
rc = MQTTPublish (&c, topic, &pubmsg); | |
while (!toStop) | |
{ | |
MQTTYield(&c, 1000); | |
rc = MQTTPublish (&c, topic, &pubmsg); | |
} | |
printf("Stopping\n"); | |
MQTTDisconnect(&c); | |
n.disconnect(&n); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "MQTTClient.h" | |
#include "linux.cpp" | |
void usage(char** argv) | |
{ | |
printf("Usage: %s topic channel_token\n", argv[0]); | |
exit(-1); | |
} | |
int main(int argc, char* argv[]) | |
{ | |
IPStack ipstack = IPStack(); | |
float version = 0.3; | |
if (argc < 3) | |
usage(argv); | |
printf("Version is %f\n", version); | |
MQTT::Client<IPStack, Countdown> client = MQTT::Client<IPStack, Countdown>(ipstack); | |
const char* hostname = "mqtt.beebotte.com"; | |
int port = 1883; | |
const char* topic = argv[1]; | |
printf("Connecting to %s:%d\n", hostname, port); | |
int rc = ipstack.connect(hostname, port); | |
if (rc != 0) | |
printf("rc from TCP connect is %d\n", rc); | |
printf("MQTT connecting\n"); | |
MQTTPacket_connectData data = MQTTPacket_connectData_initializer; | |
data.MQTTVersion = 3; | |
data.clientID.cstring = (char*)"myid"; | |
data.username.cstring = argv[2]; | |
rc = client.connect(data); | |
if (rc != 0) | |
printf("rc from MQTT connect is %d\n", rc); | |
printf("MQTT connected\n"); | |
MQTT::Message message; | |
char buf[100]; | |
sprintf(buf, "Hello World!"); | |
message.qos = MQTT::QOS0; | |
message.retained = false; | |
message.dup = false; | |
message.payload = (void*)buf; | |
message.payloadlen = strlen(buf)+1; | |
rc = client.publish(topic, message); | |
if (rc != 0) | |
printf("Error %d from sending QoS 2 message\n", rc); | |
rc = client.disconnect(); | |
if (rc != 0) | |
printf("rc from disconnect was %d\n", rc); | |
ipstack.disconnect(); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include "MQTTClient.h" | |
#include <stdio.h> | |
#include <signal.h> | |
#include <memory.h> | |
#include <sys/time.h> | |
volatile int toStop = 0; | |
void usage(char** argv) | |
{ | |
printf("Usage: %s topic channel_token\n", argv[0]); | |
exit(-1); | |
} | |
void cfinish(int sig) | |
{ | |
signal(SIGINT, NULL); | |
toStop = 1; | |
} | |
void messageArrived(MessageData* md) | |
{ | |
MQTTMessage* message = md->message; | |
printf("%.*s\t", md->topicName->lenstring.len, md->topicName->lenstring.data); | |
printf("%.*s\n", (int)message->payloadlen, (char*)message->payload); | |
} | |
int main(int argc, char** argv) | |
{ | |
int rc = 0; | |
unsigned char buf[100]; | |
unsigned char readbuf[100]; | |
if (argc < 3) | |
usage(argv); | |
char* host = "mqtt.beebotte.com"; | |
int port = 1883; | |
char* topic = argv[1]; | |
char* clientid = "myid"; | |
char* username = argv[2]; | |
Network n; | |
Client c; | |
signal(SIGINT, cfinish); | |
signal(SIGTERM, cfinish); | |
NewNetwork(&n); | |
ConnectNetwork(&n, host, port); | |
MQTTClient(&c, &n, 1000, buf, 100, readbuf, 100); | |
MQTTPacket_connectData data = MQTTPacket_connectData_initializer; | |
data.willFlag = 0; | |
data.MQTTVersion = 3; | |
data.clientID.cstring = clientid; | |
data.username.cstring = username; | |
//data.password.cstring = opts.password; | |
data.keepAliveInterval = 10; | |
data.cleansession = 1; | |
printf("Connecting to %s %d\n", host, port); | |
rc = MQTTConnect(&c, &data); | |
printf("Connected %d\n", rc); | |
printf("Subscribing to %s\n", topic); | |
rc = MQTTSubscribe(&c, topic, QOS1, messageArrived); | |
printf("Subscribed %d\n", rc); | |
while (!toStop) | |
{ | |
MQTTYield(&c, 1000); | |
} | |
printf("Stopping\n"); | |
MQTTDisconnect(&c); | |
n.disconnect(&n); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "MQTTClient.h" | |
#include "linux.cpp" | |
int arrivedcount = 0; | |
void usage(char** argv) | |
{ | |
printf("Usage: %s topic channel_token\n", argv[0]); | |
exit(-1); | |
} | |
void messageArrived(MQTT::MessageData& md) | |
{ | |
MQTT::Message &message = md.message; | |
printf("Message %d arrived: qos %d, retained %d, dup %d, packetid %d\n", | |
++arrivedcount, message.qos, message.retained, message.dup, message.id); | |
printf("Payload %.*s\n", message.payloadlen, (char*)message.payload); | |
} | |
int main(int argc, char* argv[]) | |
{ | |
IPStack ipstack = IPStack(); | |
float version = 0.3; | |
if (argc < 3) | |
usage(argv); | |
printf("Version is %f\n", version); | |
MQTT::Client<IPStack, Countdown> client = MQTT::Client<IPStack, Countdown>(ipstack); | |
const char* hostname = "mqtt.beebotte.com"; | |
int port = 1883; | |
printf("Connecting to %s:%d\n", hostname, port); | |
int rc = ipstack.connect(hostname, port); | |
if (rc != 0) | |
printf("rc from TCP connect is %d\n", rc); | |
printf("MQTT connecting\n"); | |
const char* topic = argv[1]; | |
MQTTPacket_connectData data = MQTTPacket_connectData_initializer; | |
data.username.cstring = (char*)argv[2]; | |
data.MQTTVersion = 3; | |
data.clientID.cstring = (char*)"myid"; | |
rc = client.connect(data); | |
if (rc != 0) | |
printf("rc from MQTT connect is %d\n", rc); | |
printf("MQTT connected\n"); | |
rc = client.subscribe(topic, MQTT::QOS1, messageArrived); | |
if (rc != 0) | |
printf("rc from MQTT subscribe is %d\n", rc); | |
while (1) { | |
client.yield(5000); | |
} | |
rc = client.unsubscribe(topic); | |
if (rc != 0) | |
printf("rc from unsubscribe was %d\n", rc); | |
rc = client.disconnect(); | |
if (rc != 0) | |
printf("rc from disconnect was %d\n", rc); | |
ipstack.disconnect(); | |
printf("Finishing with %d messages received\n", arrivedcount); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment