You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5. #include <mosquitto.h>
  6. #include <time.h>
  7. #include <unistd.h>
  8. #include <string.h>
  9. #include "hardware_info.h"
  10. #define MQTT_SERVER_IP "127.0.0.1"
  11. #define MQTT_SERVER_PORT 1883
  12. #define MQTT_SERVER_USER "iops"
  13. #define MQTT_SERVER_PASSWORD "12345678"
  14. #define MQTT_THIS_CLIEN_NAME "n3150-sensors"
  15. #define MQTT_PUB_TOPIC "server-status"
  16. // 连接回调函数,当连接成功时会进入这里,可以在这里进行连接成功之后的操作,比如连接之后的信息同步
  17. void my_connect_callback(struct mosquitto *mosq, void *obj, int rc)
  18. {
  19. }
  20. // 断开连接回调函数,在断开连接之后进入
  21. void my_disconnect_callback(struct mosquitto *mosq, void *obj, int result)
  22. {
  23. printf("%d:\n", __LINE__);
  24. }
  25. void my_message_callback(struct mosquitto *mosq, void *obj, const struct mosquitto_message *msg)
  26. {
  27. time_t t;
  28. struct tm *lt;
  29. time(&t);
  30. lt = localtime(&t);
  31. printf("%d.%d.%d %d:%d:%d ", lt->tm_year + 1900, lt->tm_mon, lt->tm_mday, lt->tm_hour, lt->tm_min, lt->tm_sec);
  32. printf("%d:topic(%s)->%s\n", __LINE__, (char *)msg->topic, (char *)msg->payload);
  33. }
  34. // 订阅消息回调
  35. void my_subscribe_callback(struct mosquitto *mosq, void *obj, int mid, int qos_count, const int *granted_qos)
  36. {
  37. #if 0
  38. int i;
  39. time_t t;
  40. struct tm *lt;
  41. time(&t);
  42. lt = localtime(&t);
  43. printf("%d: %s\n", __LINE__, (char *)obj);
  44. printf("%d: mid=%d\n", __LINE__, mid);
  45. printf("%d: qos_count=%d\n", __LINE__, qos_count);
  46. for (i = 0; i < qos_count; i++)
  47. {
  48. printf("%d: granted_qos[%d]=%d\n", __LINE__, i, granted_qos[i]);
  49. }
  50. #endif
  51. }
  52. int main(int argc, char *argv[])
  53. {
  54. /* 不配置流缓冲区,PRINT_LOG会立即写入文件 */
  55. setbuf(stdout, NULL);
  56. setbuf(stderr, NULL);
  57. struct mosquitto *m_hMqtt;
  58. char content[1024];
  59. hardware_info_init();
  60. //初始化lib库函数
  61. mosquitto_lib_init();
  62. // 定义一个客户端名为subtest的发布端。客户端表示订阅端的唯一性
  63. m_hMqtt = mosquitto_new(MQTT_THIS_CLIEN_NAME, true, MQTT_THIS_CLIEN_NAME);
  64. //设置连接确认回调
  65. mosquitto_connect_callback_set(m_hMqtt, my_connect_callback);
  66. //设置断开连接确认回调
  67. mosquitto_disconnect_callback_set(m_hMqtt, my_disconnect_callback);
  68. //设置收到订阅回调
  69. //mosquitto_message_callback_set(m_hMqtt, my_message_callback);
  70. //设置订阅回调(mosquitto_subscribe()订阅后回调)
  71. //mosquitto_subscribe_callback_set(m_hMqtt, my_subscribe_callback);
  72. mosquitto_username_pw_set(m_hMqtt, MQTT_SERVER_USER, MQTT_SERVER_PASSWORD);
  73. //开始连接服务器
  74. if (MOSQ_ERR_SUCCESS == mosquitto_connect(m_hMqtt, MQTT_SERVER_IP, MQTT_SERVER_PORT, 20))
  75. {
  76. printf("connect server: %s:%d success\n", MQTT_SERVER_IP, MQTT_SERVER_PORT);
  77. }
  78. else
  79. {
  80. printf("connect server: %s:%d failed\n", MQTT_SERVER_IP, MQTT_SERVER_PORT);
  81. exit(-1);
  82. }
  83. //mosquitto_subscribe(m_hMqtt,NULL,topic1,1);
  84. //mosquitto_subscribe(m_hMqtt,NULL,topic2,1);
  85. //mosquitto_loop_start(m_hMqtt); //mosquitto会创建一个进程实时运行发送心跳包和接收订阅的主题消息
  86. while (1)
  87. {
  88. sleep(2);
  89. long uptime;
  90. float load; // 5分钟的负载
  91. int *procs;
  92. if (get_system_info(&uptime, &load, &procs) < 0)
  93. {
  94. printf("get_system_info fail!\n");
  95. continue;
  96. }
  97. sprintf(content, "{\"uptime\" : \"%d\", \"load\" : \"%0.1f\", \"procs\" : \"%d\", \"cpu_temp\" : \"%d\", \"hdd_temp\" : \"%d\", \"cpu_rate\" : \"%0.1f\", \"mem_rate\" : \"%0.1f\"}",
  98. uptime, load, procs, get_cpu_temp(), get_sata_hddtemp(), get_sysCpuUsage(), cal_mem_occupy());
  99. mosquitto_publish(m_hMqtt, NULL, MQTT_PUB_TOPIC, strlen(content), content, 0, false);
  100. }
  101. //mosquitto_loop_stop(m_hMqtt, false);
  102. mosquitto_destroy(m_hMqtt);
  103. mosquitto_lib_cleanup();
  104. return 0;
  105. }