Skip to main content
Post Closed as "Needs more focus" by Chris Stratton, sempaiscuba, gre_gor, jose can u c, MatsK
removed arduino-uno-wifi tag
Link
Juraj
  • 18.3k
  • 4
  • 32
  • 50
Indented unreadable code. Improved spelling, grammar and title.
Source Link

noob here, need help Problem connecting Android app to Arduino

sorrySorry for doing this, iI really dontdon't know where to post beacuse this is both android studioAndroid Studio code and arduinoArduino code so i post iI've posted to both sitesites.

I want to create a project of using controlling pan-tilt  (2 servos combinecombined) using the accelerometer and gyroscope data in smartphone. Creating androidAndroid app and sending the accelerometer and gryroscope data to arduinoArduino so that the pan-tilt can determine its location.

My problem is when iI connect the androidAndroid app to arduinoArduino, no data is shown in serial monitor of arduinoArduino and the pan-tilt is not locating the smartphone. Please help me to solve this problem.....

thisThis MainActivity is the class to identify the data of accelerometer and gyrscope.

MainActivity:

 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) {

    }
}

thisThis DataSender is the class for sending the data to arduinoArduino.

DataSender:

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;
    }
}

thisThis is code that are uploaded in wifi module  (esp8266-01) that are connected in arduinoArduino.

Arduino code:

Arduino code:
#include <SoftwareSerial.h>
#include <Servo.h>

#define DEBUG true

SoftwareSerial esp8266(10, 11); // RX, TX

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;

 


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;
  }

noob here, need help

sorry for doing this, i really dont know where to post beacuse this is both android studio code and arduino code so i post i both site.

I want to create a project of using controlling pan-tilt(2 servos combine) using the accelerometer and gyroscope data in smartphone. Creating android app and sending the accelerometer and gryroscope data to arduino so that the pan-tilt can determine its location.

My problem is when i connect the android app to arduino, no data shown in serial monitor of arduino and the pan-tilt is not locating the smartphone. Please help me to solve this problem.....

this MainActivity is class to identify the data of accelerometer and 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) {

}
}

this DataSender is class for sending the data to 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;
}
}

this is code that are uploaded in wifi module(esp8266-01) that are connected in arduino.

Arduino code:
#include <SoftwareSerial.h>
#include <Servo.h>

#define DEBUG true

SoftwareSerial esp8266(10, 11); // RX, TX

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;

 


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;
  }

Problem connecting Android app to Arduino

Sorry for doing this, I really don't know where to post beacuse this is both Android Studio code and Arduino code so I've posted to both sites.

I want to create a project of using controlling pan-tilt  (2 servos combined) using the accelerometer and gyroscope data in smartphone. Creating Android app and sending the accelerometer and gryroscope data to Arduino so that the pan-tilt can determine its location.

My problem is when I connect the Android app to Arduino, no data is shown in serial monitor of Arduino and the pan-tilt is not locating the smartphone. Please help me to solve this problem.

This MainActivity is the class to identify the data of accelerometer and 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) {

    }
}

This DataSender is the class for sending the data to 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;
    }
}

This is code that are uploaded in wifi module  (esp8266-01) that are connected in Arduino.

Arduino code:

#include <SoftwareSerial.h>
#include <Servo.h>

#define DEBUG true

SoftwareSerial esp8266(10, 11); // RX, TX

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;


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;
}
added 3 characters in body
Source Link
Arduino code:
#include <SoftwareSerial.h>
#include <Servo.h>

#define DEBUG true
 

SoftwareSerial esp8266(10, 11); // RX, TX

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(0, 1); // 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;
  }
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(0, 1); // 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;
  }
Arduino code:
#include <SoftwareSerial.h>
#include <Servo.h>

#define DEBUG true

SoftwareSerial esp8266(10, 11); // RX, TX

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;




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;
  }
Source Link
Loading