Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdbool.h>
- #include <stdint.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <inttypes.h>
- #include <netinet/in.h>
- #include <string.h>
- #include <sys/types.h>
- #include <MQTTClient.h>
- #define ADDRESS "tcp://localhost:1883"
- #define QOS0 0
- #define QOS1 1
- #define QOS2 2
- #define TIMEOUT 10000L
- volatile MQTTClient_deliveryToken deliveredtoken;
- void rtfm(char **argv) {
- printf("Usage: %s hamster_client_id {<Option>} <param1> \n", argv[0]);
- printf("Function: Hamster instrumentation device software\n");
- printf("Optionen:\n");
- printf(" -p {<port>} - port of the mqtt server (default (no tls): 1883\n");
- printf(" -s {<IP address>} - IP address to run the server on (default: 127.0.0.1\n");
- printf(" -v - Connect with Certificate based SSL/TLS Support to the MQTT server \n");
- printf(" -V - Connect with Certificate based client authentification to the MQTT server \n");
- printf(" -h - This help \n");
- }
- 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);
- }
- void pubnewmsg(MQTTClient* client, char* clientid, char* topic, char* payload) {
- MQTTClient_message pubmsg = MQTTClient_message_initializer;
- MQTTClient_deliveryToken token;
- pubmsg.payload = payload;
- pubmsg.payloadlen = strlen(payload);
- pubmsg.qos = QOS0;
- pubmsg.retained = 0;
- MQTTClient_publishMessage(client, topic, &pubmsg, &token);
- printf(
- "Waiting for up to %d seconds for publication of %s\n"
- "on topic %s for client with ClientID: %s\n",
- (int)(TIMEOUT / 1000), "PAYLOAD", topic, clientid);
- MQTTClient_waitForCompletion(client, token, TIMEOUT);
- printf("Message with delivery token %d delivered\n", token);
- }
- int main(int argc, char **argv) {
- unsigned int port = 1883;
- char *clientid;
- char topic[256];
- if (argc < 2) {
- rtfm(argv);
- exit(0);
- }
- // parse args
- clientid = argv[1];
- for (int i = 1; i < argc; i++) {
- if (strcmp("-p", argv[i]) == 0) {
- char *end;
- port = strtoul(argv[i + 1], &end, 0);
- if (argv[i + 1] == end) {
- printf("%s: Not a number: %s\n", argv[0], argv[i + 1]);
- exit(1);
- }
- } else if (strcmp("-s", argv[i]) == 0) {
- // todo
- } else if (strcmp("-h", argv[i]) == 0) {
- rtfm(argv);
- exit(0);
- } else if (strcmp("-v", argv[i]) == 0) {
- // todo
- } else if (strcmp("-V", argv[i]) == 0) {
- }
- }
- if (port) {
- }
- MQTTClient client;
- MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_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 = "hamster";
- conn_opts.password = "";
- MQTTClient_setCallbacks(client, NULL, connlost, msgarrvd, delivered);
- if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS) {
- printf("Failed to connect, return code %d\n", rc);
- exit(EXIT_FAILURE);
- }
- MQTTClient_subscribe(client, "/pension/livestock", QOS0);
- MQTTClient_subscribe(client, "/pension/room/A", QOS0);
- sprintf(topic, "/pension/hamster/%s/wheels%c", clientid, '\0');
- printf("%s\n", topic);
- MQTTClient_subscribe(client, topic, QOS0);
- sprintf(topic, "/pension/hamster/%s/state%c", clientid, '\0');
- printf("%s\n", topic);
- MQTTClient_subscribe(client, topic, QOS0);
- sprintf(topic, "/pension/hamster/%s/position%c", clientid, '\0');
- printf("%s\n", topic);
- MQTTClient_subscribe(client, topic, QOS0);
- sprintf(topic, "/pension/hamster/%s/fondle%c", clientid, '\0');
- printf("%s\n", topic);
- MQTTClient_subscribe(client, topic, QOS0);
- sprintf(topic, "/pension/hamster/%s/punish%c", clientid, '\0');
- printf("%s\n", topic);
- MQTTClient_subscribe(client, topic, QOS0);
- do {
- ch = getchar();
- if (ch == 'l' || ch == 'L') {
- pubnewmsg(client, clientid, "/pension/livestock", "payload");
- }
- if (ch == 's' || ch == 'S') {
- sprintf(topic, "/pension/hamster/%s/state%c", clientid, '\0');
- pubnewmsg(client, clientid, topic, "payload");
- }
- } while (ch != 'Q' && ch != 'q');
- MQTTClient_disconnect(client, 10000);
- MQTTClient_destroy(&client);
- return rc;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement