-
Notifications
You must be signed in to change notification settings - Fork 1
/
arduino
173 lines (125 loc) · 4.64 KB
/
arduino
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
#include "ESP8266.h"
#include "SoftwareSerial.h"
byte nuidPICC[4];
//MPU6050配置部分开始***
#include "I2Cdev.h"
#include "MPU6050.h"
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
#include "Wire.h"
#endif
MPU6050 accelgyro;
int16_t ax, ay, az;
int16_t gx, gy, gz;
int8_t axx, ayy, azz;
int8_t gxx, gyy, gzz;
int8_t axx_l, ayy_l, azz_l;
int8_t gxx_l, gyy_l, gzz_l;
#define OUTPUT_READABLE_ACCELGYRO
//MPU6050配置部分结束***
//配置ESP8266WIFI设置
#define SSID "hsy" //填写2.4GHz的WIFI名称,不要使用校园网,ChinaNet-6yRD,667788
#define PASSWORD "20001010"//填写自己的WIFI密码,pnqyntds,ab753951
#define HOST_NAME "101.43.164.112" //49.232.76.26API主机名称,连接到OneNET平台,无需修改,192.168.137.1,,127.0.0.1,lyr49.232.76.26
#define DEVICE_ID "562324123" //填写自己的OneNet设备ID,562324123
#define HOST_PORT (8000) //API端口,连接到OneNET平台,无需修改,3000
String APIKey = "LgaoqvCcR2SlZl861B1AVpZH9DE="; //与设备绑定的APIKey
//#define INTERVAL_SENSOR 1 //定义传感器采样及发送时间间隔
//定义ESP8266所连接的软串口
SoftwareSerial mySerial(3, 2);
ESP8266 wifi(mySerial);
void setup()
{
ax = 0;
ay = 0;
az = 0;
gx = 0;
gy = 0;
gz = 0;
//
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
Wire.begin();
#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
Fastwire::setup(400, true);
#endif
//mpu6050结束
//
mySerial.begin(115200); //初始化软串口
Serial.begin(9600); //初始化串口
Serial.println("开始初始化");
//ESP8266初始化
if (wifi.setOprToStation()) {
Serial.println("成功连接到站点");
} else {
Serial.println("连接站点失败");
}
//ESP8266接入WIFI
if (wifi.joinAP(SSID, PASSWORD)) {
Serial.println("成功接入WiFi");
Serial.print("IP: ");
Serial.println(wifi.getLocalIP().c_str());
} else {
Serial.println("连接WiFi失败");
}
mySerial.println("AT+UART_CUR=9600,8,1,0,0");
mySerial.begin(9600);
Serial.println("初始化完成");
//
Serial.println("Initializing I2C devices...");
accelgyro.initialize();
Serial.println("Testing device connections...");
Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
//
}
//unsigned long net_time1 = millis(); //数据上传服务器时间
void loop(){
//if (net_time1 > millis())
// net_time1 = millis();
// if (millis() - net_time1 > INTERVAL_SENSOR) //发送数据时间间隔
// {
if (wifi.createTCP(HOST_NAME, HOST_PORT)) { //建立TCP连接,如果失败,不能发送该数据
Serial.print("成功建立TCP连接\r\n");
//MPU6050开始
//MPU6050结束
//拼接POST请求字符串
String data = "";
//String jsonToSend = "/testTrans?testData=1";
for (int i = 0; i < 1; i++) {
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
data += "["+String(ax) +"," + String(ay) +","+ String(az) +","+ String(gx) +","+ String(gy) +","+ String(gz)+"]"+(i==0?"":",");
delay(100);
}
//Serial.println(data);
/*accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
String data = "["+String(ax) +"," + String(ay) +","+ String(az) +","+ String(gx) +","+ String(gy) +","+ String(gz)+"]";*/
String postString = "GET /predict?testData="+data+" HTTP/1.1\r\nHost: 101.43.164.112\r\nConnection:Keep-Alive\r\n\r\n";//\r\nUser-Agent: curl/7.71.1\r\nAccept: */*\r\nAccept-Encoding: deflate, gzip\r\n\r\n close
const char *postArray = postString.c_str(); //将str转化为char数组
//char * buffer;//用于存储数据
//char buffer[150];2022.4.15
Serial.println(postArray);
wifi.send((const uint8_t *)postArray, strlen(postArray)); //send发送命令,参数必须是这两种格式,尤其是(const uint8_t*)
Serial.println("send success");
//delay(100);
/*uint32_t len = wifi.recv(buffer, sizeof(buffer), 10000);
Serial.println(len);
if (len > 0) {
Serial.print("Received:[");
for(uint32_t i = 0; i < len; i++) {
Serial.print((char)buffer[i]);
}
Serial.print("]\r\n");
}
delay(100);*/
/*if (wifi.releaseTCP()) { //释放TCP连接
Serial.print("release tcp ok\r\n");
} else {
Serial.print("release tcp err\r\n");
}*/
//
postArray = NULL; //清空数组,等待下次传输数据
} else {
Serial.print("create tcp err\r\n");
}
Serial.println("");
//net_time1 = millis();
// }
}