test.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * @Author: jiejie
  3. * @Github: https://github.com/jiejieTop
  4. * @Date: 2020-04-18 12:37:34
  5. * @LastEditTime: 2020-06-08 20:32:33
  6. * @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
  7. */
  8. #include <stdio.h>
  9. #include <unistd.h>
  10. #include <fcntl.h>
  11. #include <stdlib.h>
  12. #include <pthread.h>
  13. #include "mqttclient.h"
  14. extern const char *test_ca_get();
  15. static void interceptor_handler(void* client, message_data_t* msg)
  16. {
  17. (void) client;
  18. MQTT_LOG_I("-----------------------------------------------------------------------------------");
  19. MQTT_LOG_I("%s:%d %s()...\ntopic: %s\nmessage:%s", __FILE__, __LINE__, __FUNCTION__, msg->topic_name, (char*)msg->message->payload);
  20. MQTT_LOG_I("-----------------------------------------------------------------------------------");
  21. }
  22. void *mqtt_publish_thread(void *arg)
  23. {
  24. mqtt_client_t *client = (mqtt_client_t *)arg;
  25. char buf[100] = { 0 };
  26. mqtt_message_t msg;
  27. memset(&msg, 0, sizeof(msg));
  28. sprintf(buf, "welcome to mqttclient, this is a publish test...");
  29. msg.qos = 0;
  30. msg.payload = (void *) buf;
  31. while(1) {
  32. sprintf(buf, "welcome to mqttclient, this is a publish test, a rand number: %d ...", random_number());
  33. mqtt_publish(client, "topic1", &msg);
  34. sleep(4);
  35. }
  36. }
  37. int main(void)
  38. {
  39. int res;
  40. pthread_t thread1;
  41. mqtt_client_t *client = NULL;
  42. printf("\nwelcome to mqttclient test...\n");
  43. mqtt_log_init();
  44. client = mqtt_lease();
  45. mqtt_set_port(client, "6002");
  46. mqtt_set_host(client, "183.230.40.39");
  47. mqtt_set_client_id(client, "599908192");
  48. mqtt_set_user_name(client, "348547");
  49. mqtt_set_password(client, "mqttclienttest1");
  50. mqtt_set_clean_session(client, 1);
  51. mqtt_connect(client);
  52. mqtt_subscribe(client, "topic1", QOS0, NULL);
  53. mqtt_set_interceptor_handler(client, interceptor_handler); // set interceptor handler
  54. res = pthread_create(&thread1, NULL, mqtt_publish_thread, client);
  55. if(res != 0) {
  56. MQTT_LOG_E("create mqtt publish thread fail");
  57. exit(res);
  58. }
  59. while (1) {
  60. sleep(100);
  61. }
  62. }