Last active
June 18, 2021 11:54
-
-
Save bachwehbi/c5f06ec086da5d5a6f6c to your computer and use it in GitHub Desktop.
Connect to Beebotte using Paho MQTT C client - with encryption
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
/** | |
* to compile this code | |
* cc -g -o subscriber subscriber.c -lpaho-mqtt3cs -lpthread | |
**/ | |
#include "stdio.h" | |
#include "stdlib.h" | |
#include "string.h" | |
#include "MQTTClient.h" | |
// set protocol to ssl and port number to 8883 to use encryption | |
#define ADDRESS "ssl://mqtt.beebotte.com:8883" | |
#define TOPIC "test/test" | |
#define CLIENTID "my_device_identifier" | |
#define QOS 1 | |
#define TIMEOUT 10000L | |
volatile MQTTClient_deliveryToken deliveredtoken; | |
void delivered(void *context, MQTTClient_deliveryToken dt) | |
{ | |
printf("Message with token value %d delivery confirmed\n", dt); | |
deliveredtoken = dt; | |
} | |
int msgarrvd(void *context, char *topicName, int topicLen, MQTTClient_message *message) | |
{ | |
int i; | |
char* payloadptr; | |
printf("Message arrived\n"); | |
printf(" topic: %s\n", topicName); | |
printf(" message: "); | |
payloadptr = message->payload; | |
for(i=0; i<message->payloadlen; i++) | |
{ | |
putchar(*payloadptr++); | |
} | |
putchar('\n'); | |
MQTTClient_freeMessage(&message); | |
MQTTClient_free(topicName); | |
return 1; | |
} | |
void connlost(void *context, char *cause) | |
{ | |
printf("\nConnection lost\n"); | |
printf(" cause: %s\n", cause); | |
} | |
int main(int argc, char* argv[]) | |
{ | |
MQTTClient client; | |
//Initialize connection options | |
MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer; | |
//Initialize SSL options | |
MQTTClient_SSLOptions sslopts = MQTTClient_SSLOptions_initializer; | |
int rc; | |
int ch; | |
MQTTClient_create(&client, ADDRESS, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL); | |
conn_opts.keepAliveInterval = 20; | |
conn_opts.cleansession = 1; | |
conn_opts.username = "token:YOUR_CHANNEL_TOKEN"; | |
//get the server certificate from beebotte.com/certs/mqtt.beebotte.com.pem | |
sslopts.trustStore="mqtt.beebotte.com.pem"; | |
//enable server certificate validation | |
sslopts.enableServerCertAuth = 1; | |
//set SSL options in connection options | |
conn_opts.ssl = &sslopts; | |
//Set callbacks | |
MQTTClient_setCallbacks(client, NULL, connlost, msgarrvd, delivered); | |
//Connect | |
if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS) | |
{ | |
printf("Failed to connect, return code %d\n", rc); | |
exit(-1); | |
} | |
printf("Subscribing to topic %s\n using QoS%d\n\n" | |
"Press Q<Enter> to quit\n\n", TOPIC, QOS); | |
//Subscribe | |
MQTTClient_subscribe(client, TOPIC, QOS); | |
do | |
{ | |
ch = getchar(); | |
} while(ch!='Q' && ch != 'q'); | |
MQTTClient_disconnect(client, 10000); | |
MQTTClient_destroy(&client); | |
return rc; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment