#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include “shmem.h”
#include “cJSON.h”
#include “MQTTClient.h”
typedef struct data
{
char name[32];
int num;
float avegrade;
} data;
#define ADDRESS “tcp://localhost:1883”
#define CLIENTID “123”
#define TOPIC “up”
#define PAYLOAD “Hello World!”
#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 = 0;
int *value;
data *value1;
char *payloadptr;
struct shm_param para;
if (shm_init(¶, "shm", 128) < 0)
{
printf("shm init err\n");
return -1;
}
value = shm_getaddr(¶);
if (value == NULL)
{
printf("get addr err\n");
return -1;
}
payloadptr = message->payload;
cJSON *root = cJSON_Parse(payloadptr);
cJSON *item = cJSON_GetObjectItem(root, "data");
*value = cJSON_GetArraySize(item);
value1 = (data *)(value+1);
for (int j = 0; j < cJSON_GetArraySize(item); j++)
{
cJSON *array = cJSON_GetArrayItem(item, j);
cJSON *child_item = cJSON_GetObjectItem(array, "name");
strcpy(value1->name, child_item->valuestring);
printf("%s\n",value1->name);
child_item = cJSON_GetObjectItem(array, "num");
value1->num = child_item->valueint;
printf("%d\n",value1->num);
child_item = cJSON_GetObjectItem(array, "avegrade");
value1->avegrade = child_item->valuedouble;
printf("%.2f\n",value1->avegrade);
value1++;
}
cJSON_Delete(root);
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 const *argv[])
{
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;
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);
}
printf("Subscribing to topic %s\nfor client %s using QoS%d\n\n"
"Press Q<Enter> to quit\n\n",
TOPIC, CLIENTID, QOS);
MQTTClient_subscribe(client, TOPIC, QOS);
do
{
ch = getchar();
} while (ch != 'Q' && ch != 'q');
MQTTClient_unsubscribe(client, TOPIC);
MQTTClient_disconnect(client, 10000);
MQTTClient_destroy(&client);
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include “shmem.h”
#include “cJSON.h”
#include “MQTTClient.h”
#define ADDRESS “tcp://localhost:1883”
#define CLIENTID “ExampleClientSub”
#define TOPIC “down”
#define PAYLOAD “Hello World!”
#define QOS 1
#define TIMEOUT 10000L
typedef struct data
{
char name[32];
int num;
float avegrade;
} data;
int main(int argc, char const *argv[])
{
MQTTClient client;
MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
MQTTClient_message pubmsg = MQTTClient_message_initializer;
MQTTClient_deliveryToken token;
int rc;
int len;
struct shm_param para;
data *st;
int *p;
//初始化
if (shm_init(¶, "shm", 128) < 0)
{
printf("shm init err\n");
return -1;
}
//获取共享内存的地址
p = shm_getaddr(¶);
if (st == NULL)
{
printf("get addr err\n");
return -1;
}
len = *p;
MQTTClient_create(&client, ADDRESS, CLIENTID,
MQTTCLIENT_PERSISTENCE_NONE, NULL);
conn_opts.keepAliveInterval = 20;
conn_opts.cleansession = 1;
if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
{
printf("Failed to connect, return code %d\n", rc);
exit(EXIT_FAILURE);
}
while (1)
{
st = (data *)(p + 1);
cJSON *root = cJSON_CreateObject();
cJSON *array = cJSON_CreateArray();
cJSON_AddItemToObject(root, "data", array);
for (int j = 0; j < len; j++)
{
cJSON *item = cJSON_CreateObject();
cJSON_AddItemToArray(array, item);
cJSON *child_item = cJSON_CreateString(st->name);
cJSON_AddItemToObject(item, "name", child_item);
child_item = cJSON_CreateNumber(st->num);
cJSON_AddItemToObject(item, "num", child_item);
child_item = cJSON_CreateNumber(st->avegrade);
cJSON_AddItemToObject(item, "avegrade", child_item);
st++;
}
char *pp = cJSON_Print(root);
pubmsg.payload = pp;
pubmsg.payloadlen = (int)strlen(pp);
pubmsg.qos = QOS;
pubmsg.retained = 0;
MQTTClient_publishMessage(client, TOPIC, &pubmsg, &token);
free(pp);
cJSON_Delete(root);
sleep(1);
}
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);
rc = MQTTClient_waitForCompletion(client, token, TIMEOUT);
printf("Message with delivery token %d delivered\n", token);
MQTTClient_disconnect(client, 10000);
MQTTClient_destroy(&client);
return 0;
}
gcc MQTTClient_subscribe.c -lpaho-mqtt3c -lm