Отправка и получение данных с помощью приложения Android, Arduino и модуля Wi-Fi esp8266Android

Форум для тех, кто программирует под Android
Ответить Пред. темаСлед. тема
Anonymous
 Отправка и получение данных с помощью приложения Android, Arduino и модуля Wi-Fi esp8266

Сообщение Anonymous »

Я застрял с этим на несколько дней, я использую приложение для 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 {

@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 может обрабатывать или около того, я не копаю слишком глубоко в этом вопросе. Ошибки, которые возникают при просмотре последовательных окон, - это «Принять-кодирование gzip», иногда я застреваю с «что-то полученным» и не реагирую ни на какие кнопки, нажимаемые позже.

Подробнее здесь: https://stackoverflow.com/questions/425 ... p8266-wifi
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение
  • Отправка и получение данных с помощью приложения Android, Arduino и модуля Wi-Fi esp8266
    Anonymous » » в форуме Android
    0 Ответы
    21 Просмотры
    Последнее сообщение Anonymous
  • Отправка и получение данных с использованием Android Application, Arduino и ESP8266 WiFi модуль
    Anonymous » » в форуме Android
    0 Ответы
    2 Просмотры
    Последнее сообщение Anonymous
  • Отправка данных с помощью ESP8266 и получение с помощью Django
    Anonymous » » в форуме Python
    0 Ответы
    27 Просмотры
    Последнее сообщение Anonymous
  • Arduino-esp8266 использовать с C# вместо приложения RemoteXY [закрыто]
    Anonymous » » в форуме C#
    0 Ответы
    19 Просмотры
    Последнее сообщение Anonymous
  • ESP8266 Arduino — комбинированное событие двух кнопок
    Anonymous » » в форуме C++
    0 Ответы
    11 Просмотры
    Последнее сообщение Anonymous

Вернуться в «Android»