I'm doing a project with Arduino and Android application. Here's how it goes, send from the application request "a=11&b=22&d=33" (http://192.168.0.17/?a=11&b=22&d=33), arduino read and return "Data_received" and I use it to do something (down is part of code from app). And everything is going well. But I do not know how to do reversed now, I know how to send the request from arduino to server, but I don't know how to receive in android app to get a = 11, b = 22, c = 33. Android studio code to receive:
@Override
protected void onPostExecute(String s) {
if (serverResponse.toString().equals("Data_received") ){
Toast.makeText(Test.this, "Data is ok...", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(Test.this, "Connection is not ok", Toast.LENGTH_SHORT).show();
}
}
Arduino code which receives request in arduino:
boolean currentLineIsBlank = true;
boolean currentLineIsGet = true;
int tCount = 0;
char tBuf[64];
int a;
int b;
int d;
char *pch;
while (client.connected()) {
while (client.available()) {
char c = client.read();
if (currentLineIsGet && tCount < 63)
{
tBuf[tCount] = c;
tCount++;
tBuf[tCount] = 0;
}
if (c == '\n' && currentLineIsBlank) {
while (client.available()) client.read();
Serial.print (tBuf);
pch = strtok(tBuf, "?");
while (pch != NULL) {
// http://192.168.0.17/?a=11&b=22&d=33
if (strncmp(pch, "a=", 2) == 0) {
a = atoi(pch + 2);
Serial.print("a=");
Serial.println(a, DEC);
}
if (strncmp(pch, "b=", 2) == 0) {
b = atoi(pch + 2);
Serial.print("b=");
Serial.println(b, DEC);
}
if (strncmp(pch, "d=", 2) == 0) {
d = atoi(pch + 2);
Serial.print("d=");
Serial.println(d, DEC);
}
pch = strtok(NULL, "& ");
} // while (pch != NULL)
client.stop();
}
}
}
All code:
public class Test extends AppCompatActivity {
public EditText text__T1mod;
public EditText text__T2mod;
public EditText text__T3mod;
private TextView Text_Arduino;
private Button SendRequest ;
public int a;
public int b;
public int c;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
SendRequest = (Button) findViewById(R.id.btn_SendRequest );
text__T1mod = (EditText) findViewById(R.id.TextEdit_T1);
text__T2mod = (EditText) findViewById(R.id.TextEdit_T2);
text__T3mod = (EditText) findViewById(R.id.TextEdit_T3);
Text_Arduino = (TextView) findViewById(R.id.textView_Arduino);
} // protected void onCreate END
public void clik_SendRequest (View view) {
String RequestString = "";
if (IpAddres.getText().toString().equals(""))
Toast.makeText(this, "Enter IP", Toast.LENGTH_SHORT).show();
else {
if (view == SendRequest )
// http://192.168.1.14/?a=11&b=22&d=33
RequestString = "a=" + text__T1mod.getText().toString() + "&"
+ "b=" + text__T2mod.getText().toString() + "&"
+ "d=" + text__T3mod.getText().toString() ;
// 192.168.1.14 // 80
String serverAdress = IpAddres.getText().toString() + ":" + Port.getText().toString() + "?";
HttpRequestTask requestTask = new HttpRequestTask(serverAdress);
requestTask.execute(RequestString);
} // else END
} // clik_SendRequest END
private class HttpRequestTask extends AsyncTask<String, Void, String> {
private String serverAdress;
private String serverResponse = "";
private AlertDialog dialog;
public HttpRequestTask(String serverAdress) {
this.serverAdress = serverAdress;
dialog = new AlertDialog.Builder(context)
.setTitle("HTTP zahtev za Ip adresu: ")
.setCancelable(true)
.create();
}
@Override
protected String doInBackground(String... params) {
String val = params[0];
final String url = "http://" + serverAdress + val;
try {
HttpClient client = new DefaultHttpClient();
HttpGet getRequest = new HttpGet();
getRequest.setURI(new URI(url));
HttpResponse response = client.execute(getRequest);
InputStream inputStream = null;
inputStream = response.getEntity().getContent();
BufferedReader bufferedReader =
new BufferedReader(new InputStreamReader(inputStream));
serverResponse = bufferedReader.readLine();
inputStream.close();
} catch (URISyntaxException e) {
Log.e("", "parse error2: " + e.toString());
e.printStackTrace();
serverResponse = e.getMessage();
}
return serverResponse;
}
@Override
protected void onPostExecute(String s) {
/*
----- HERE I NEED IF SOMETHING LIKE arrives ----
if (serverResponse.toString().equals("?a=11&b=22&d=33") ){
Toast.makeText(Test.this, "a=" a + "b=" b + "c=" c , Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(Test.this, "Connection is not ok", Toast.LENGTH_SHORT).show();
}
*/
}
@Override
protected void onPreExecute() {
Text_Arduino.setText("pleas wait...");
}
} // private class HttpRequestTask
} // public class Test END
You have 2 options:
Poll the server: write a function on the server that returns the results you need. Then setup a timer in android that calls this function every few seconds
Push messages from the server: use a push service to send your results to the android app. ex: https://firebase.google.com/docs/cloud-messaging/
To parse your server response string: