Get an Arduino and Android phone to communicate over the web
- by Saleem
I am writing an Android application to communicate with my Arduino over the web. The Arduino is running a web server through an Ethernet shield. I am attaching my code, but I will explain it here so you will understand what I am trying to do.
The Android sends an HTTP request in the format http://192.168.1.148/?Lights=1. The Arduino gets the request, executes the command (in this case turning on some lights) and then responds to the Android device by simply sending the string "Lights=On". The Android will then change the color of the button to notify the user that the command was executed successfully.
The Arduino is getting the instruction and executing it and sending the response but my button color is not changing. I know that the Android device is getting the string because I added a debug line to change the text on the button to the received response. The relevant code for the Android device is:
((Button) v).setText(sb.toString()); //This works and the button text changes to "Lights=On".
//Test response and update button
if(sb.toString()=="Lights=On"){
v.getBackground().setColorFilter(0xFFFFFF00, PorterDuff.Mode.MULTIPLY);
Drawable d = lightOff.getBackground();
lightOff.invalidateDrawable(d);
d.clearColorFilter();
}
The Arduino code is:
if(s=="Lights"){
switch(client.read()){
case '0':
digitalWrite(LightPin,0);
client.print("Lights=Off");
//debug
Serial.println("Lights=Off");
break;
case '1':
digitalWrite(LightPin,1);
client.print("Lights=On");
Serial.println("Lights=On");
break;
}
}
Please let me know if you need more of the code to answer this question.