1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
| #define HOST "localhost" #define PORT 1883 #define KEEP_ALIVE 60 #define MSG_MAX_SIZE 512 #define TOPIC_NUM 3
#define QOS 1 #define CLIENT_ID "king654321" #define USERNAME "0voice_mqtt" #define PASSWORD "123456" #define TOPIC_NAME "mtopic"
static bool clean_session = false;
void print_mqtt_message(const struct mosquitto_message *message) { if(message) { printf("topic:%s mid:%d, qos:%d, retain:%d payload:%s \n", \ message->topic, message->mid, message->qos, message->retain, (char *)message->payload); } }
void my_message_callback(struct mosquitto *mosq, void *userdata, const struct mosquitto_message *message) { print_mqtt_message(message); if(message->payloadlen){ char *data=(char *)message->payload; cJSON *pose=cJSON_Parse(data); cJSON *item_x = cJSON_GetObjectItem(pose,"x"); double pose_x=item_x->valuedouble;
cJSON *item_y = cJSON_GetObjectItem(pose,"y"); double pose_y = item_y->valuedouble;
cJSON *item_yaw = cJSON_GetObjectItem(pose,"yaw"); double pose_yaw = item_yaw->valuedouble; printf("pose_x :%lf, pose_y :%lf, pose_yaw :%lf\n\n", pose_x, pose_y, pose_yaw);
}else{ printf("%s (null)\n", message->topic); } fflush(stdout); }
void my_connect_callback(struct mosquitto *mosq, void *userdata, int result) { if(!result){ printf("Connect ok, start sub %s, qos:%d\n", TOPIC_NAME, QOS); mosquitto_subscribe(mosq, NULL, TOPIC_NAME, QOS); }else{ printf( "Connect failed, result:%d\n", result); } }
void my_subscribe_callback(struct mosquitto *mosq, void *userdata, int mid, int qos_count, const int *granted_qos) { int i; printf("Subscribed (mid: %d): %d", mid, granted_qos[0]); for(i = 1; i < qos_count; i++){ printf(", %d", granted_qos[i]); } printf("\n"); }
void my_log_callback(struct mosquitto *mosq, void *userdata, int level, const char *str) { printf("log_callback-> %s\n", str); }
int main() { struct mosquitto *mosq = NULL; mosquitto_lib_init(); mosq = mosquitto_new(CLIENT_ID, clean_session, NULL); if(!mosq){ printf("create client failed..\n"); mosquitto_lib_cleanup(); return 1; } mosquitto_log_callback_set(mosq, my_log_callback);
mosquitto_connect_callback_set(mosq, my_connect_callback); mosquitto_message_callback_set(mosq, my_message_callback); mosquitto_subscribe_callback_set(mosq, my_subscribe_callback);
if(mosquitto_username_pw_set(mosq, USERNAME, PASSWORD) != MOSQ_ERR_SUCCESS) { printf("client username_pw_set failed..\n"); mosquitto_lib_cleanup(); return 1; }
if(mosquitto_connect(mosq, HOST, PORT, KEEP_ALIVE)){ printf("Unable to connect.\n"); return 1; } int loop = mosquitto_loop_start(mosq); if(loop != MOSQ_ERR_SUCCESS) { printf("mosquitto loop error\n"); return 1; }
while(1) { sleep(1); }
mosquitto_destroy(mosq); mosquitto_lib_cleanup();
return 0; }
|