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