|
@ -1,7 +1,10 @@ |
|
|
|
|
|
// |
|
|
|
|
|
// Created by zwsd |
|
|
|
|
|
// |
|
|
|
|
|
#include "MQTTClient.h" |
|
|
#include "stdlib.h" |
|
|
#include "stdlib.h" |
|
|
#include "string.h" |
|
|
#include "string.h" |
|
|
#include "unistd.h" |
|
|
#include "unistd.h" |
|
|
#include "MQTTClient.h" |
|
|
|
|
|
|
|
|
|
|
|
#define ADDRESS "mqtt://iot-06z00dtdbnukzsk.mqtt.iothub.aliyuncs.com:1883" |
|
|
#define ADDRESS "mqtt://iot-06z00dtdbnukzsk.mqtt.iothub.aliyuncs.com:1883" |
|
|
#define USERNAME "mh_2&iekhmHv4wIC" |
|
|
#define USERNAME "mh_2&iekhmHv4wIC" |
|
@ -11,8 +14,7 @@ |
|
|
#define TOPIC "/sys/iekhmHv4wIC/mh_2/thing/event/property/post" |
|
|
#define TOPIC "/sys/iekhmHv4wIC/mh_2/thing/event/property/post" |
|
|
#define TIMEOUT 10000L |
|
|
#define TIMEOUT 10000L |
|
|
|
|
|
|
|
|
void publish(MQTTClient client, char *topic, char *payload) |
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
void publish(MQTTClient client, char *topic, char *payload) { |
|
|
MQTTClient_message message = MQTTClient_message_initializer; |
|
|
MQTTClient_message message = MQTTClient_message_initializer; |
|
|
message.payload = payload; |
|
|
message.payload = payload; |
|
|
message.payloadlen = strlen(payload); |
|
|
message.payloadlen = strlen(payload); |
|
@ -24,8 +26,7 @@ void publish(MQTTClient client, char *topic, char *payload) |
|
|
printf("Send `%s` to topic `%s` \n", payload, TOPIC); |
|
|
printf("Send `%s` to topic `%s` \n", payload, TOPIC); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
int on_message(void *context, char *topicName, int topicLen, MQTTClient_message *message) |
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
int on_message(void *context, char *topicName, int topicLen, MQTTClient_message *message) { |
|
|
char *payload = message->payload; |
|
|
char *payload = message->payload; |
|
|
printf("Received `%s` from `%s` topic \n", payload, topicName); |
|
|
printf("Received `%s` from `%s` topic \n", payload, topicName); |
|
|
MQTTClient_freeMessage(&message); |
|
|
MQTTClient_freeMessage(&message); |
|
@ -33,8 +34,7 @@ int on_message(void *context, char *topicName, int topicLen, MQTTClient_message |
|
|
return 1; |
|
|
return 1; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) |
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) { |
|
|
int rc; |
|
|
int rc; |
|
|
MQTTClient client; |
|
|
MQTTClient client; |
|
|
|
|
|
|
|
@ -43,20 +43,16 @@ int main(int argc, char *argv[]) |
|
|
conn_opts.username = USERNAME; |
|
|
conn_opts.username = USERNAME; |
|
|
conn_opts.password = PASSWORD; |
|
|
conn_opts.password = PASSWORD; |
|
|
MQTTClient_setCallbacks(client, NULL, NULL, on_message, NULL); |
|
|
MQTTClient_setCallbacks(client, NULL, NULL, on_message, NULL); |
|
|
if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS) |
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS) { |
|
|
printf("Failed to connect, return code %d\n", rc); |
|
|
printf("Failed to connect, return code %d\n", rc); |
|
|
exit(-1); |
|
|
exit(-1); |
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
} else { |
|
|
printf("Connected to MQTT Broker!\n"); |
|
|
printf("Connected to MQTT Broker!\n"); |
|
|
} |
|
|
} |
|
|
// subscribe topic |
|
|
// subscribe topic |
|
|
MQTTClient_subscribe(client, TOPIC, QOS); |
|
|
MQTTClient_subscribe(client, TOPIC, QOS); |
|
|
char payload[23]; |
|
|
char payload[23]; |
|
|
for (int i = 0; i < 100; i += 1) |
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
for (int i = 0; i < 100; i += 1) { |
|
|
// publish message to broker |
|
|
// publish message to broker |
|
|
snprintf(payload, 23, "{\"RunningState\": \"%d\"}", i); |
|
|
snprintf(payload, 23, "{\"RunningState\": \"%d\"}", i); |
|
|
publish(client, TOPIC, payload); |
|
|
publish(client, TOPIC, payload); |
|
|