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.


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;

    protected void onCreate(Bundle savedInstanceState) {

        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());


    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]);



    public void onAccuracyChanged(Sensor sensor, int accuracy) {


This DataSender is the class for sending the data to Arduino.


public class DataSender extends AsyncTask<String, Void, Void> {

    Socket s;
    DataOutputStream dos;
    PrintWriter pw;

    protected Void doInBackground(String... voids) {

        String data = voids[0];

        try {
            s = new Socket("",80);
            pw = new PrintWriter(s.getOutputStream());

        } catch (IOException e){

        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

  //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
  sendCommand("AT+CIFSR\r\n", 1000, DEBUG); // get ip address
  sendCommand("AT+CIPSTA=\"\"\r\n", 1000, DEBUG);
  sendCommand("AT+CIPMUX=1\r\n", 1000, DEBUG); // configure for multiple 
  sendCommand("AT+CIPSERVER=1,80\r\n", 1000, DEBUG); // turn on server on port 
  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)");


void loop() { // run over and over

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

    // 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.print("Sensor log#: ");
      Serial.print("Val[0]: ");
      Serial.print("Val[1]: ");
      Serial.print("Val[2]: ");

    // 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. 

  panVal = map(panVal, -10, 10, 0, 179);  // Map Accelerometer X value to pan 
servo angle.

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) {
  return response;
  • 1
    "SoftwareSerial esp8266(0, 1); // RX, TX" you can't do that without breaking the serial monitor - whatever tutorial you are following is dead wrong. Those pins are the hardware serial, you need to move your ESP to some other pins. May 6, 2018 at 3:41
  • 1
    Possible duplicate of cannot read from softwareserial which covers the pin conflict mistake you have repeated here. May 6, 2018 at 3:46
  • i change it to (10, 11) sir.. but still no data.. May 6, 2018 at 4:06
  • 1
    Try some basic ESP8266 examples before your worry about the whole Android integration problem. Until you've proved your ESP8266 is connected and working, this has far too many parts to debug. May 6, 2018 at 4:10
  • do you use Arduino Uno WiFi? you did tag the question with it
    – Juraj
    May 6, 2018 at 5:20

1 Answer 1


In the loop function you have a line

If (Serial.available() < 1) return;

This prevents further code to be processed if there is nothing in the hardware serial buffer. Think about that.


Not the answer you're looking for? Browse other questions tagged or ask your own question.