0

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Saleem
  • 3
  • 1
  • 3
  • `s == "Lights"` doesn't do what you expect. –  Jul 28 '12 at 12:39
  • Could you send me your codes for arduino and android if it's possible...I'm trying to share data between android and arduino but I'm only able to send data to arduino without getting data back from it. – Kareem Essam Gaber Mar 18 '17 at 00:01

1 Answers1

0

To simply change the background color of the button:

            String state = sb.toString().trim();
            ((Button) v).setText(state);
            if(state.contains("On")){
                v.setBackgroundColor(Color.DKGRAY);
            }
            else
            {
                v.setBackgroundColor(Color.MAGENTA);                    
            }
sww314
  • 1,252
  • 13
  • 17
  • You my friend are wonderful! The state.contains method worked perfectly. I didn't use the setBackGroundColor method because it was causing all of the formatting on the button to disappear so you end up with a plain rectangle with square edges and no shading. Just adding the String state and state.contains fixed the problem. – Saleem Jul 28 '12 at 01:28