나는 며칠 동안 이것들에 상당히 매달 렸습니다. arduino uno에 연결된 모터를 제어하는 안드로이드 응용 프로그램을 사용하고 있습니다. 연결을 설정하고 보낼 수 있습니다. 그러나 문제는 탭에 응답 할 수있는 양에 제한이 있으며 때로는 명령을 정확하게 실행할 수 없다는 것입니다. 이이 내가 또 다시 안드로이드로 아두 이노를 사용하여 데이터를 전송 일하고 안드로이드안드로이드 응용 프로그램, arduino 및 esp8266 wifi 모듈을 사용하여 데이터를 보내고받습니다.
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;
}
});
의 코드이지만, 이러한 오류가 발생하는과 내가 계속할 수 없습니다 아두 이노
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("==============================");
}
에 대한 코드입니다. html이 처리 할 수있는 정도에 대한 제한이 있다는 것을 읽었습니다. 너무 깊이 파지 않습니다. 직렬 창을 보는 동안 발생하는 오류는 "Accept-ecoding gzip"이며 때로는 "수신 된 것"으로 인해 붙잡혀 나중에 입력 된 단추에 응답하지 않습니다.
귀하의 문제는 더 순수 코드보다 건축입니다. 대기열을 사용하지 않고 각 명령에 대해 http 연결을 계속 열고 닫으면 요청간에 큰 오버 헤드 및 경쟁 조건이 발생합니다. 핸들러를 사용하여 요청을 마샬링하거나 websocket을 (더 잘) 들여다 봅니다. – mach
난 그냥 다른 방법으로 아마 어쩌면 그것을 사용하여, 당신은 웹 사이트에 문자열을 가져 와서 설립 된 연결을 볼 수 있지만 내가 필요로하는 웹 사이트 및 arduino에 그것을 전달하십시오. – John
안타깝게도 내 arduino 프로그램 만 변경하면 안드로이드에서 전송 된 데이터는 읽기 쉽고 믿을 수 없을 정도로 빠릅니다. – John