mpu6050_meas.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-09-19 liYony first version
  9. */
  10. #include <rtthread.h>
  11. #include "mpu6xxx.h"
  12. #define DBG_TAG "mpu6050"
  13. #define DBG_LVL DBG_LOG
  14. #include <rtdbg.h>
  15. #define MPU6050_DEVICE_NAME "i2c1"
  16. #ifdef BSP_USING_MQTT_HW_CLOUD
  17. #include "mqttclient.h"
  18. #include <netdev_ipaddr.h>
  19. #include <netdev.h>
  20. #include <cJSON.h>
  21. #ifndef KAWAII_MQTT_HOST
  22. #define KAWAII_MQTT_HOST "jiejie01.top"
  23. #endif
  24. #ifndef KAWAII_MQTT_PORT
  25. #define KAWAII_MQTT_PORT "1883"
  26. #endif
  27. #ifndef KAWAII_MQTT_CLIENTID
  28. #define KAWAII_MQTT_CLIENTID "rtthread001"
  29. #endif
  30. #ifndef KAWAII_MQTT_USERNAME
  31. #define KAWAII_MQTT_USERNAME "rt-thread"
  32. #endif
  33. #ifndef KAWAII_MQTT_PASSWORD
  34. #define KAWAII_MQTT_PASSWORD "rt-thread"
  35. #endif
  36. #ifndef KAWAII_MQTT_SUBTOPIC
  37. #define KAWAII_MQTT_SUBTOPIC "rtt-sub"
  38. #endif
  39. #ifndef KAWAII_MQTT_PUBTOPIC
  40. #define KAWAII_MQTT_PUBTOPIC "rtt-pub"
  41. #endif
  42. static char payload_buf[256];///////////////////////
  43. static rt_bool_t is_upload = RT_TRUE;
  44. static void mqtt_subscribe_handle(void *client, message_data_t *msg)
  45. {
  46. (void)client;
  47. KAWAII_MQTT_LOG_I("-----------------------------------------------------------------------------------");
  48. KAWAII_MQTT_LOG_I("%s:%d %s()...\ntopic: %s\nmessage:%s", __FILE__, __LINE__, __FUNCTION__, msg->topic_name, (char *)msg->message->payload);
  49. KAWAII_MQTT_LOG_I("-----------------------------------------------------------------------------------");
  50. cJSON* json_data = RT_NULL;
  51. cJSON* json_upload = RT_NULL;
  52. json_data = cJSON_Parse((char *)msg->message->payload);
  53. if (json_data == RT_NULL)
  54. {
  55. return;
  56. }
  57. json_upload = cJSON_GetObjectItem(json_data, "paras");
  58. json_upload = cJSON_GetObjectItem(json_upload, "upload_bool");
  59. if (json_upload->valueint == RT_TRUE)
  60. {
  61. is_upload = RT_TRUE;
  62. LOG_I("start upload.");
  63. }
  64. else
  65. {
  66. is_upload = RT_FALSE;
  67. LOG_I("stop upload.");
  68. }
  69. }
  70. static int mqtt_publish_handle(mqtt_client_t *client, void *payload)
  71. {
  72. mqtt_message_t msg;
  73. memset(&msg, 0, sizeof(msg));
  74. msg.qos = QOS1;
  75. msg.payload = payload;
  76. return mqtt_publish(client, KAWAII_MQTT_PUBTOPIC, &msg);
  77. }
  78. #endif /* BSP_USING_MQTT_HW_CLOUD */
  79. static void mpu6050_accel_entry(void *parameter)
  80. {
  81. struct mpu6xxx_device *dev;
  82. struct mpu6xxx_3axes accel;
  83. /* Initialize mpu6050, The parameter is RT_NULL, means auto probing for i2c*/
  84. dev = mpu6xxx_init(MPU6050_DEVICE_NAME, RT_NULL);
  85. if (dev == RT_NULL)
  86. {
  87. LOG_E("mpu6050 init failed.");
  88. return;
  89. }
  90. LOG_I("mpu6050 init succeed.");
  91. #ifdef BSP_USING_MQTT_HW_CLOUD
  92. /* wait network init. */
  93. while(!netdev_get_first_by_flags(NETDEV_FLAG_INTERNET_UP))
  94. {
  95. rt_thread_mdelay(2);
  96. }
  97. LOG_I("network init succeed.");
  98. mqtt_log_init();
  99. mqtt_client_t *client = mqtt_lease();
  100. mqtt_set_host(client, KAWAII_MQTT_HOST);
  101. mqtt_set_port(client, KAWAII_MQTT_PORT);
  102. mqtt_set_user_name(client, KAWAII_MQTT_USERNAME);
  103. mqtt_set_password(client, KAWAII_MQTT_PASSWORD);
  104. mqtt_set_client_id(client, KAWAII_MQTT_CLIENTID);
  105. mqtt_set_clean_session(client, 1);
  106. KAWAII_MQTT_LOG_I("The ID of the Kawaii client is: %s ", KAWAII_MQTT_CLIENTID);
  107. mqtt_connect(client);
  108. mqtt_subscribe(client, KAWAII_MQTT_SUBTOPIC, QOS1, mqtt_subscribe_handle);
  109. #endif /* BSP_USING_MQTT_HW_CLOUD */
  110. while(1)
  111. {
  112. mpu6xxx_get_accel(dev, &accel);
  113. #ifdef BSP_USING_MQTT_HW_CLOUD
  114. if (is_upload == RT_TRUE)
  115. {
  116. rt_sprintf(payload_buf, "{\"services\":[{\"service_id\":\"mpu6050\",\"properties\":{\"accel_x\":%3d,\"accel_y\":%3d,\"accel_z\":%3d}}]}", accel.x, accel.y, accel.z);
  117. mqtt_publish_handle(client, payload_buf);
  118. }
  119. #endif /* BSP_USING_MQTT_HW_CLOUD */
  120. LOG_D("accel.x = %3d, accel.y = %3d, accel.z = %3d", accel.x, accel.y, accel.z);
  121. rt_thread_mdelay(1000);
  122. }
  123. }
  124. static int rt_hw_mpu6050_init()
  125. {
  126. rt_thread_t tid_mpu;
  127. tid_mpu = rt_thread_create("mpu_accel", mpu6050_accel_entry, RT_NULL, 2048, 6, 10);
  128. if (tid_mpu == RT_NULL)
  129. {
  130. return -RT_ERROR;
  131. }
  132. rt_thread_startup(tid_mpu);
  133. return RT_EOK;
  134. }
  135. INIT_APP_EXPORT(rt_hw_mpu6050_init);