0
我相當堅持這些幾天,我使用android應用程序來控制連接到arduino uno的電機,我可以建立連接併發送數據,但問題是,它對可以響應按鈕的響應有多少限制,有時它不能準確執行命令。 這是Arduino的使用android應用程序,arduino和esp8266 wifi模塊發送和接收數據
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的回機器人的數據,但這些錯誤存在的,我不能繼續。我讀過的HTML有多少隻能處理的限制,所以我不會深究這一點。我通過查看串行窗口所發生的錯誤是「Accept-ecoding gzip」,有時會被「接收到的東西」卡住,並且不會響應稍後點擊的任何按鈕。
您的問題,更是一個建築一個比純代碼。您連續打開和關閉每個命令的http連接,而不會導致請求之間的大量開銷和爭用條件。您可以使用處理程序編組您的請求,或者(更好地)使用websocket進行查看。 – mach
我只是以另一種方式實現它可能使用okhttp,你可以指導我如何使它,我已經看到一個建立的連接,通過獲取字符串在網站上,但我需要的是在網站上添加數據和把它傳遞給arduino。 – John
對不起,我正在研究它看起來像我只需要改變我的arduino程序,從android發送的數據是可讀的和令人難以置信的快速。 – John