很抱歉这样做,我真的不知道在哪里发帖,因为这是Android工作室代码和arduino代码所以我发布我的两个网站。
我想创建一个使用智能手机中的加速度计和陀螺仪数据来控制云台(2个伺服组合)的项目。创建Android应用程序并将加速度计和gryroscope数据发送到arduino,以便平移可以确定其位置。
我的问题是当我将Android应用程序连接到arduino时,没有数据显示在arduino的串行监视器中,而且云台没有找到智能手机。 请帮我解决这个问题.....
此 MainActivity 是用于识别加速度计和gyrscope数据的类。
MainActivity:
public class MainActivity extends AppCompatActivity implements SensorEventListener{
private final String TAG = "MainActivity";//log our activity
SensorManager sm;//define sensor manager
Sensor accelerometer, gyrometer;//define accelerometer
TextView xValue, yValue, zValue, xGyroValue, yGyroValue, zGyroValue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
xValue = (TextView)findViewById(R.id.xValue);
yValue = (TextView)findViewById(R.id.yValue);
zValue = (TextView)findViewById(R.id.zValue);
xGyroValue = (TextView)findViewById(R.id.xGyroValue);
yGyroValue = (TextView)findViewById(R.id.yGyroValue);
zGyroValue = (TextView)findViewById(R.id.zGyroValue);
Log.d(TAG, "onCreate: Initializing Sensor Services");
sm =(SensorManager) getSystemService(Context.SENSOR_SERVICE);//permission to use the sensor
accelerometer = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
if (accelerometer != null) {
sm.registerListener(MainActivity.this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
Log.d(TAG, "onCreate: Registered accelerometer listerner");
}else {
xValue.setText("accelerometer is not supported");
yValue.setText("accelerometer is not supported");
zValue.setText("accelerometer is not supported");
}
gyrometer = sm.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
if (gyrometer != null) {
sm.registerListener(MainActivity.this, gyrometer, SensorManager.SENSOR_DELAY_NORMAL);
Log.d(TAG, "onCreate: Registered gyrometer listerner");
}else {
xGyroValue.setText("gyrometer is not supported");
yGyroValue.setText("gyrometer is not supported");
zGyroValue.setText("gyrometer is not supported");
}
}
public void connect(View v){
DataSender dataSender = new DataSender();
dataSender.execute((String) xValue.getText(), toString());
dataSender.execute((String) yValue.getText(), toString());
dataSender.execute((String) zValue.getText(), toString());
dataSender.execute((String) xGyroValue.getText(), toString());
dataSender.execute((String) yGyroValue.getText(), toString());
dataSender.execute((String) zGyroValue.getText(), toString());
}
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
Sensor sensor = sensorEvent.sensor;
if(sensor.getType() == Sensor.TYPE_ACCELEROMETER){
Log.d(TAG, "onSensorChanged: X: " + sensorEvent.values[0] + "Y: " + sensorEvent.values[1] + "Z:" + sensorEvent.values[2]);
xValue.setText("xValue: " + sensorEvent.values[0]);
yValue.setText("yValue: " + sensorEvent.values[1]);
zValue.setText("zValue: " + sensorEvent.values[2]);
}else if (sensor.getType() == Sensor.TYPE_GYROSCOPE){
xGyroValue.setText("xGyroValue: " + sensorEvent.values[0]);
yGyroValue.setText("yGyroValue: " + sensorEvent.values[1]);
zGyroValue.setText("zGyroValue: " + sensorEvent.values[2]);
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
此DataSender是用于将数据发送到arduino的类。
DataSender:
public class DataSender extends AsyncTask<String, Void, Void> {
Socket s;
DataOutputStream dos;
PrintWriter pw;
@Override
protected Void doInBackground(String... voids) {
String data = voids[0];
try {
s = new Socket("192.168.1.100",80);
pw = new PrintWriter(s.getOutputStream());
pw.write(data);
pw.flush();
pw.close();
s.close();
} catch (IOException e){
e.printStackTrace();
}
return null;
}
}
这是在arduino中连接的wifi模块(esp8266-01)中上传的代码。
Arduino code:
#include <SoftwareSerial.h>
#include <Servo.h>
#define DEBUG true
char tiltChannel=0, panChannel=1;
//These are the objects for each servo.
Servo servoTilt, servoPan;
//This is a character that will hold data from the Serial port.
char serialChar=0;
// Center servos
int tiltVal = 90;
int panVal =90;
//smaartphone value
String inText;
float value0, value1, value2;
SoftwareSerial esp8266(10, 11); // RX, TX
void setup() { // Open serial communications and wait for port to open:
servoTilt.attach(2); //The Tilt servo is attached to pin 2.
servoPan.attach(3); //The Pan servo is attached to pin 3.
servoTilt.write(90); //Initially put the servos both
servoPan.write(90); //at 90 degress.
Serial.begin(9600); //for monitoring purposes
esp8266.begin(9600);
//sendCommand("AT+CIFS+RST\r\n", 2000, DEBUG); // reset module
sendCommand("AT+IPR=9600\r\n", 1000, DEBUG);
sendCommand("AT+CWMODE=1\r\n", 1000, DEBUG); // configure as access point
sendCommand("AT+CWJAP=\"EceConnect\",\"1234\"\r\n", 3000, DEBUG); //connec to
a network with name EceConnect with password 1234
delay(1000);
sendCommand("AT+CIFSR\r\n", 1000, DEBUG); // get ip address
sendCommand("AT+CIPSTA=\"192.168.1.100\"\r\n", 1000, DEBUG);
sendCommand("AT+CIPMUX=1\r\n", 1000, DEBUG); // configure for multiple
connections
sendCommand("AT+CIPSERVER=1,80\r\n", 1000, DEBUG); // turn on server on port
80
Serial.println("Server Ready");
Serial.println("Android Sensor Type No: ");
Serial.println("1- ACCELEROMETER (m/s^2 - X,Y,Z)");
Serial.println("2- GYROSCOPE (rad/s - X,Y,Z)");
Serial.flush();
}
void loop() { // run over and over
Serial.flush();
int inCommand = 0;
int sensorType = 0;
unsigned long logCount = 0L;
if (Serial.available() < 1) return; // if serial empty, return to loop().
char getChar = ' ';
if (esp8266.available()) {
if (esp8266.find("+IPD,0,")) {
delay(10);
esp8266.find(":");
delay(10);
char letter = esp8266.read();
Serial.print(letter); //for monitoring purposes
//Gets the value/char from android app
}
}
// parse incoming command start flag
if (getChar != serialChar) return; // if no command start flag, return to
loop().
// parse incoming pin# and value
sensorType = Serial.parseInt(); // read sensor typr
logCount = Serial.parseInt(); // read total logged sensor readings
value0 = Serial.parseFloat(); // 1st sensor value
value1 = Serial.parseFloat(); // 2rd sensor value if exists
value2 = Serial.parseFloat(); // 3rd sensor value if exists
// send smartphone readings to serial monitor/terminal
if (DEBUG) {
Serial.print("Sensor type: ");
Serial.println(sensorType);
Serial.print("Sensor log#: ");
Serial.println(logCount);
Serial.print("Val[0]: ");
Serial.println(value0);
Serial.print("Val[1]: ");
Serial.println(value1);
Serial.print("Val[2]: ");
Serial.println(value2);
Serial.println("-----------------------");
delay(10);
}
// Check sensor type. If not for Accelerometer (#1) then ignore readings
// sensorType 1 is the Accelerometer sensor
if (sensorType !=1) return;
panVal = value0; // value0 = X sensor reading
tiltVal = value1; // value1 = Y sensor reading
tiltVal = map(tiltVal, 10, -10, 0, 179); // Map Accelerometer Y value to
tilt servo angle.
servoTilt.write(tiltVal);
delay(10);
panVal = map(panVal, -10, 10, 0, 179); // Map Accelerometer X value to pan
servo angle.
servoPan.write(panVal);
delay(10);
}
String sendCommand(String command, const int timeout, boolean debug) {
String response = "";
esp8266.print(command); // send the read character to the esp8266
long int time = millis();
while ((time + timeout) > millis()) {
while (esp8266.available()) {
// The esp has data so display its output to the serial window
char c = esp8266.read(); // read the next character.
response += c;
}
}
if (debug) {
Serial.print(response);
}
return response;
}