使用Android应用程序,arduino和esp8266 wifi模块发送和接收数据

时间:2017-03-03 07:20:52

标签: android arduino esp8266

我几天坚持这些,我使用android应用程序来控制附加到arduino uno的电机,我可以建立连接并发送数据,但问题是它有多少限制它可以响应被点击的按钮,有时它无法准确执行命令。 这是arduino的代码

    void loop()
    {
    if(esp8266.available()) // check if the esp is sending a message 
    {
    Serial.println("Something received"); 
    delay(50);
    if(esp8266.find("+IPD,"))
    {
        String action;
        Serial.println("+IPD, found");
        int connectionId = esp8266.read()-48; // subtract 48 because the read() function returns 
                                           // the ASCII decimal value and 0 (the first decimal number) starts at 48
        Serial.println("connectionId: " + String(connectionId));

        esp8266.find("motor=");
        char s = esp8266.read();
        delay(50);
        //right
        if(s=='1'){
          action = "Motor is turning right";
          digitalWrite(5,HIGH);
          digitalWrite(6,LOW);
          digitalWrite(9,LOW);
          digitalWrite(10,HIGH);
        }
        else if(s=='0'){
          action = "Motor is stopping";
          digitalWrite(5,LOW);
          digitalWrite(6,LOW);
          digitalWrite(9,LOW);
          digitalWrite(10,LOW);
        }

        //left
         else if(s=='2'){
          action = "Motor is turning left";
          digitalWrite(5,LOW);
          digitalWrite(6,HIGH);
          digitalWrite(9,HIGH);
          digitalWrite(10,LOW);
        }

        //forward
         else if(s=='3'){
          action = "Motor is moving forward";
          digitalWrite(5,HIGH);
          digitalWrite(6,LOW);
          digitalWrite(9,HIGH);
          digitalWrite(10,LOW);
        }

        //backward
          else if(s=='4'){
          action = "Motor is moving backward";
          digitalWrite(5,LOW);
          digitalWrite(6,HIGH);
          digitalWrite(9,LOW);
          digitalWrite(10,HIGH);
        }

        else{
          action = "Try another command";
        }
        Serial.println(action);
      sendHTTPResponse(connectionId, action);

  }
}
}
void sendHTTPResponse(int id, String content)
{
  String response;
  response = "HTTP/1.1 200 OK\r\n";
  response += "Content-Type: text/html; charset=UTF-8\r\n"; 
  response += "Content-Length: ";
  response += content.length();
  response += "\r\n";
  response +="Connection: close\r\n\r\n";
  response += content;

  String cmd = "AT+CIPSEND=";
  cmd += id;
  cmd += ",";
  cmd += response.length();

 Serial.println("--- AT+CIPSEND ---");
 sendESP8266Cmdln(cmd, 50);

 Serial.println("--- data ---");
 sendESP8266Data(response, 50);

}
void sendESP8266Cmdln(String cmd, int waitTime)
{
  esp8266.println(cmd);
  delay(waitTime);
  clearESP8266SerialBuffer();
}

void sendESP8266Data(String data, int waitTime)
{
  esp8266.print(data);
  delay(waitTime);
  clearESP8266SerialBuffer();
}

void clearESP8266SerialBuffer()
{
  Serial.println("= clearESP8266SerialBuffer() =");
  while (esp8266.available() > 0) {
    char a = esp8266.read();
    Serial.write(a);

  }
  Serial.println("==============================");
}

这是android的代码

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

     @Override
        protected String doInBackground(String... params) {
            try {
                //change IP to the IP you set in the ARDUINO
                URL url = new URL("http://192.168.254.101/?" + params[0]);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();

                BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                StringBuilder result = new StringBuilder();
                String inputLine;
                while ((inputLine = in.readLine()) != null)
                    result.append(inputLine).append("\n");

                in.close();
                connection.disconnect();
                return result.toString();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    }

    public void Motion() {

        Button right = (Button) findViewById(R.id.button_right);
        Button left = (Button) findViewById(R.id.button_left);
        Button forward = (Button) findViewById(R.id.button_forward);
        Button backward = (Button) findViewById(R.id.button_backward);
        Button led_on = (Button) findViewById(R.id.led_on);
        Button led_off = (Button) findViewById(R.id.led_off);


        right.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    TextView text = (TextView) findViewById(R.id.textView);
                    text.setText("Right");
                    new FeedTask().execute("motor=1");

                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    TextView text = (TextView) findViewById(R.id.textView);
                    text.setText("Tap again");
                    new FeedTask().execute("motor=0");
                }

                return true;
            }
        });

我也在努力使用arduino将数据发送回android,但是出现这些错误我无法继续。我已经读过,html只能处理多少才有限制,我不会深入研究。我通过查看串行窗口时遇到的错误是“Accept-ecoding gzip”,有时会遇到“收到的东西”,并且不会响应稍后点击的任何按钮。

1 个答案:

答案 0 :(得分:0)

我有同样的问题..在解决方案中,您可以在网页上的固定时间(如10秒或20秒)后刷新页面,以便刷新缓冲区代码并使其正常工作。

代码是:

<script type="text/javascript">    
    setInterval(page_refresh, 5*60000); //NOTE: period is passed in milliseconds
</script>