Arduino + Android = Internet Garage Door. Works but could use input.


Last Updated: 2012-04-18 08:43:31
  1. dlevans

    dlevans New Member

    Like the title says, I've got an Arduino with an Asynclabs WiShield connected to the LAN that is also port-forwarded so it can connect anywhere there is an Internet connection. The app works but sometimes it hangs. I believe the problem is with threading inside the app but I'm not exactly sure how to go around fixing it. For now, like I said, it works I'm just wondering if anyone can help optimize the Android side of things.

    For the Android code you'll need JSOUP from http://jsoup.org/download
    and to add it to your Eclipse project you'll need to:

    Download the library to your host development system.
    Create a new folder, libs, in your Eclipse/Android project.
    Right-click libs and choose Import -> General -> File System, then Next, Browse in the filesystem to find the library's parent directory (i.e.: where you downloaded it to).
    Click OK, then click the directory name (not the checkbox) in the left pane, then check the relevant JAR in the right pane. This puts the library into your project (physically).
    Right-click on your project, choose Build Path -> Configure Build Path, then click the Libraries tab, then Add JARs..., navigate to your new JAR in the libs directory and add it. (This, incidentally, is the moment at which your new JAR is converted for use on Android.)

    Install instructions from: http://stackoverflow.com/questions/...to-an-eclipse-android-project/3643015#3643015

    Arduino Code:

    Code (Text):
    1.  
    2. #include <WiServer.h>
    3. #include <string.h>
    4. #define WIRELESS_MODE_INFRA 1
    5. #define WIRELESS_MODE_ADHOC 2
    6. #define ledPin1 5  //extra pin
    7. #define ledPin2 6  //pin wired to the garage door remote
    8. #define ledPin3 9  //pin that turns off the red LED on the WiShield
    9. // Wireless configuration parameters -----------
    10. unsigned char local_ip[] = {192,168,1,225}; // IP address of WiShield
    11. unsigned char gateway_ip[] = {192,168,1,1}; // router or gateway IP address
    12. unsigned char subnet_mask[] = {255,255,255,0}; // subnet mask for the local network
    13. char ssid[] = {"Linksys"}; // max 32 bytes
    14. unsigned char security_type = 0; // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2
    15. // WPA/WPA2 passphrase
    16. const prog_char security_passphrase[] PROGMEM = {"password"};  
    17. // max 64 characters
    18. // WEP 128-bit keys
    19. prog_uchar wep_keys[] PROGMEM = {
    20. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,   // Key 0
    21. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   // Key 1
    22. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   // Key 2
    23. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00    // Key 3
    24. };
    25. // setup the wireless mode; infrastructure -
    26. //connect to AP; adhoc - connect to another WiFi device
    27. #define WIRELESS_MODE_INFRA 1
    28. #define WIRELESS_MODE_ADHOC 2
    29. unsigned char wireless_mode = WIRELESS_MODE_INFRA;
    30. unsigned char ssid_len;
    31. unsigned char security_passphrase_len;
    32. // End of wireless configuration parameters ------------------
    33. boolean states[3]; //holds led states
    34. char stateCounter; //used as a temporary variable
    35. char tmpStrCat[100]; //used in processing the web page
    36. char stateBuff[4]; //used in text processing around boolToString()
    37. char numAsCharBuff[2];
    38. char ledChange;
    39. void boolToString (boolean test, char returnBuffer[4])
    40. {
    41. returnBuffer[0] = '\0';
    42. if (test)
    43. {
    44. strcat(returnBuffer, "Open");
    45. }
    46. else
    47. {
    48. strcat(returnBuffer, "Closed");
    49. }
    50. }
    51. void printStates()
    52. {
    53. for (stateCounter = 0 ; stateCounter < 3; stateCounter++)
    54. {
    55. boolToString(states[stateCounter], stateBuff);
    56. Serial.print("State of ");
    57. Serial.print(stateCounter);
    58. Serial.print(": ");
    59. Serial.println(stateBuff);
    60. }
    61. }
    62. void writeStates()
    63. {
    64. //set led states
    65. digitalWrite(ledPin1, states[0]);
    66. digitalWrite(ledPin2, states[1]);
    67. digitalWrite(ledPin3, states[2]);
    68. }
    69. // This is our page serving function that generates web pages
    70. boolean sendPage(char* URL) {
    71. Serial.println("Page printing begun");
    72. printStates();
    73. writeStates();
    74. //check whether we need to change the led state
    75. if (URL[1] == '?' && URL[2] == 'L' && URL[3] == 'E' && URL[4] == 'D')
    76. //url has a leading /
    77. {
    78. ledChange = (int)(URL[5] - 48); //get the led to change.
    79. for (stateCounter = 0 ; stateCounter < 3; stateCounter++)
    80. {
    81. if (ledChange == stateCounter)
    82. {
    83. states[stateCounter] = !states[stateCounter];
    84. Serial.print("Have changed ");
    85. Serial.println(ledChange);
    86. }
    87. }
    88. //after having change state, return the user to the index page.
    89. WiServer.print("<HTML><HEAD><meta http-equiv='REFRESH' content='0;url=/'></HEAD></HTML>");
    90. return true;
    91. }
    92. if (strcmp(URL, "/") == false) //why is this not true?
    93. {
    94. WiServer.print("<html><head><title>Door Opener</title></head>");
    95. WiServer.print("<body><center>GARAGE DOOR OPENER<center>\n<center><BR><BR>");
    96. for (stateCounter = 0; stateCounter < 3; stateCounter++) //for each led
    97. {
    98. numAsCharBuff[0] = (char)(stateCounter + 49); //as this is displayed use 1 - 3 rather than 0 - 2
    99. numAsCharBuff[1] = '\0'; //strcat expects a string (array of chars) rather than a single character.
    100. //This string is a character plus string terminator.
    101. tmpStrCat[0] = '\0'; //initialise string
    102. strcat(tmpStrCat, "<input type=\"submit\" onclick=\"location=\'/?LED \'\" value=\"Opener \""); //start the string
    103. tmpStrCat[45] = (char)(stateCounter + 48); //add the led number
    104. tmpStrCat[63] = (char)(stateCounter + 49);
    105. tmpStrCat[66] = '\0'; //terminate the string properly for later.
    106. strcat(tmpStrCat, ">");
    107. boolToString(states[stateCounter], stateBuff);
    108. strcat(tmpStrCat, "Door ");
    109. strcat(tmpStrCat, stateBuff);
    110. strcat(tmpStrCat, "<br>");
    111. //strcat(tmpStrCat, "</a> ");
    112. //we now have something in the range of <a href=?LED0>Led 0: Off</a>
    113. WiServer.print(tmpStrCat);
    114. }
    115. WiServer.print("</html> ");
    116. return true;
    117. }
    118. }
    119. void setup() {
    120. // Initialize WiServer and have it use the sendMyPage function to serve pages
    121. pinMode(ledPin1, OUTPUT);
    122. pinMode(ledPin2, OUTPUT);
    123. pinMode(ledPin3, OUTPUT);
    124. Serial.begin(9600);
    125. WiServer.init(sendPage);
    126. states[0] = false;
    127. states[1] = false;
    128. states[2] = false;
    129. }
    130. void loop(){
    131. // Run WiServer
    132. WiServer.server_task();
    133. delay(10);
    134. }
    135.  
    **EDIT: Updated Android code.**

    To do: add logic / automate closing of the arduino circuit

    Right now it works like this:
    -------------------------------------------------------------------
    Door | Circuit
    closed | off -- default: door closed circuit off
    open | on -- button pushed 1st time: door open circuit on
    open | off -- button pushed 2nd time: door open circuit off
    closed | on -- button pushed 3rd time: door closed circuit on
    closed | off -- button pushed 4th time: door closed circuit off
    I think it should work like this:

    ---------------------------------------------------------------------
    Door | Circuit
    closed | off -- default: door closed circuit off
    open | on -- button pushed 1st time: door open circuit on
    open | off -- after a set time app automatically closes circuit
    closed | on -- button pushed 2nd time: door closed circuit on
    closed | off -- after a set time app automatically closes circuit

    Android Code:
    Code (Text):
    1.  
    2. package com.garagedoor.opener;
    3. import java.io.BufferedReader;
    4. import java.io.InputStreamReader;
    5. import java.net.URI;
    6.  
    7. import org.apache.http.HttpEntity;
    8. import org.apache.http.HttpResponse;
    9. import org.apache.http.client.HttpClient;
    10. import org.apache.http.client.methods.HttpGet;
    11. import org.apache.http.impl.client.DefaultHttpClient;
    12.  
    13. import android.app.Activity;
    14. import android.app.ProgressDialog;
    15. import android.content.Intent;
    16. import android.content.SharedPreferences;
    17. import android.os.AsyncTask;
    18. import android.os.Bundle;
    19. import android.view.Menu;
    20. import android.view.MenuInflater;
    21. import android.view.MenuItem;
    22. import android.view.View;
    23. import android.widget.Button;
    24. import android.widget.EditText;
    25. import android.widget.Toast;
    26.  
    27. public class GarageDoorOpenerActivity extends Activity {
    28.    
    29.         //external ip holder
    30.     public String publicIP = "";
    31.         //whatever your arduino ip address was set to holder
    32.     public String localIP = "";
    33.     public String readURL = "";
    34.     public String portNumber = "";
    35.     public String readpublicIP = "";
    36.     public String readlocalIP = "";
    37.     public String readportNumber = "";
    38.     public String URL = "";
    39.     public String garagestate = "";
    40.     public String pin = "";
    41.     public String pinName = "";
    42.     public String networkName = "";
    43.    
    44.     public int pinCheck = 0;
    45.     boolean useExternal;
    46.     private static final int REQUEST_CODE = 100;
    47.     public String customURL[] = {"http://","/"};
    48.     private AsyncTask<?, ?, ?> mTask;
    49.     private ProgressDialog dialog = null;
    50.     boolean viewLocal = true;
    51.     boolean viewPublic = true;
    52.     boolean viewPort = true;
    53.     boolean viewPin = true;
    54.     int buttondDefault = R.drawable.buttondefault;
    55.     int doorClosed = R.drawable.doorclosed;
    56.     int doorOpen = R.drawable.dooropen;
    57.     int option1On = R.drawable.option1on;
    58.     int option1Off = R.drawable.option1off;
    59.     int arduinoOn = R.drawable.arduinoon;
    60.     int arduinoOff = R.drawable.arduinooff;
    61.     int option1default = R.drawable.option1default;
    62.     int doorDefault = R.drawable.doordefault;
    63.     int arduinoDefault = R.drawable.arduinodefault;
    64.    
    65.     int saved_button = R.drawable.buttondefault;
    66.    
    67.     public String data = null;
    68.     String publicIPpart1 = "";
    69.     String publicIPpart2 = "";
    70.     String publicIPpart3 = "";
    71.     HttpResponse responseGet = null;
    72.  
    73.      @Override
    74.         public void onCreate(Bundle savedInstanceState) {
    75.             super.onCreate(savedInstanceState);
    76.             setContentView(R.layout.main);
    77.            
    78.             SharedPreferences settings = getSharedPreferences("app_settings", 0);
    79.             publicIP = settings.getString("public_ip", publicIP);
    80.             localIP = settings.getString("local_ip",localIP);
    81.             useExternal = settings.getBoolean("use_external", useExternal);
    82.             pin = settings.getString("arduino_pin",pin);
    83.             portNumber = settings.getString("port_number", portNumber);
    84.             URL = settings.getString("passed_url", URL);
    85.             publicIPpart1 = settings.getString("publicIP_part1", publicIPpart1);
    86.             publicIPpart3 = settings.getString("publicIP_part3", publicIPpart3);
    87.             pinName = settings.getString("pin_name", pinName);
    88.             networkName = settings.getString("network_name", networkName);
    89.             pinCheck = settings.getInt("pin_check",pinCheck);
    90.            
    91.             saved_button = settings.getInt("saved_button",saved_button);
    92.             findViewById(R.id.button1).setBackgroundResource(saved_button);
    93.            
    94.             EditText displayInternal = (EditText) findViewById(R.id.displayInternal);
    95.             EditText displayExternal = (EditText) findViewById(R.id.displayExternal);
    96.             EditText displayPort = (EditText) findViewById(R.id.displayPort);
    97.             displayInternal.setText(localIP);
    98.             displayExternal.setText(publicIPpart1);
    99.             displayPort.setText(publicIPpart3);
    100.             EditText displayPin = (EditText) findViewById(R.id.displayPin);
    101.             displayPin.setText(pinName);
    102.             EditText displayNetwork = (EditText) findViewById(R.id.displayNetwork);
    103.            
    104.             if(networkName == "External"){
    105.                 URL = publicIP;
    106.             }
    107.             if(networkName == "Local"){
    108.                 URL = localIP;
    109.             }
    110.            
    111.             displayNetwork.setText(networkName);
    112.                        
    113.             final Button button = (Button) findViewById(R.id.button1);
    114.             button.setOnClickListener(new View.OnClickListener() {
    115.                 public void onClick(View v) {
    116.                   // Perform action on click
    117.                     mTask = new httpTask().execute();
    118.                     }
    119.             });
    120.         }
    121. //OPTIONS MENU INFLATER - FILLS THE OPTIONS MENU
    122.     public boolean onCreateOptionsMenu(Menu menu) {
    123.         MenuInflater inflater = getMenuInflater();
    124.         inflater.inflate(R.menu.menu, menu);
    125.         return true;
    126.     }
    127. //OPTIONS MENU
    128.     public boolean onOptionsItemSelected(MenuItem item) {
    129.         EditText displayPin = (EditText) findViewById(R.id.displayPin);
    130.         EditText displayNetwork = (EditText) findViewById(R.id.displayNetwork);
    131.         EditText displayInternal = (EditText) findViewById(R.id.displayInternal);
    132.         EditText displayExternal = (EditText) findViewById(R.id.displayExternal);
    133.         EditText displayPort = (EditText) findViewById(R.id.displayPort);
    134.         switch (item.getItemId()) {
    135.                 case R.id.save:
    136.                     Toast.makeText(getBaseContext(), "Settings saved.", Toast.LENGTH_SHORT).show();
    137.                     save();
    138.                     break;
    139.                 case R.id.pins:
    140.                     break;
    141.                     case R.id.pin1:
    142.                         pin = "?LED0";
    143.                         pinCheck = 1;
    144.                         pinName = "Option 1";
    145.                         displayPin.setText(pinName);
    146.                         findViewById(R.id.button1).setBackgroundResource(R.drawable.option1default);
    147.                         saved_button = R.drawable.option1default;
    148.                         save();
    149.                         break;
    150.                     case R.id.pin2:
    151.                         pin = "?LED1";
    152.                         pinCheck = 2;
    153.                         pinName = "Garage Door";
    154.                         displayPin.setText(pinName);
    155.                         findViewById(R.id.button1).setBackgroundResource(R.drawable.doordefault);
    156.                         saved_button = R.drawable.doordefault;
    157.                         save();
    158.                         break;
    159.                     case R.id.pin3:
    160.                         pin = "?LED2";
    161.                         pinCheck = 3;
    162.                         pinName = "Arduino LED";
    163.                         displayPin.setText(pinName);
    164.                         findViewById(R.id.button1).setBackgroundResource(R.drawable.arduinodefault);
    165.                         saved_button = R.drawable.arduinodefault;
    166.                         save();
    167.                         break;
    168.                 case R.id.useExternal:
    169.                     if(useExternal == false){
    170.                         URL = localIP;
    171.                         useExternal = true;
    172.                         networkName = "Local";
    173.                         displayNetwork.setText(networkName);
    174.                         save();
    175.                         break;
    176.                     }
    177.                     else if(useExternal == true){
    178.                         URL = publicIP;
    179.                         useExternal = false;
    180.                         networkName = "External";
    181.                         displayNetwork.setText(networkName);
    182.                         save();
    183.                         break;
    184.                     }else {
    185.                     break;
    186.                     }
    187.                 case R.id.inputIP:
    188.                     enterIP();
    189.                     break;
    190.                 case R.id.clear:
    191.                     publicIP = "";
    192.                     readURL = "";
    193.                     portNumber = "";
    194.                     readpublicIP = "";
    195.                     readlocalIP = "";
    196.                     readportNumber = "";
    197.                     URL = "";
    198.                     garagestate = "";
    199.                     pin = "";
    200.                     publicIPpart2 = "";
    201.                     networkName = "";
    202.                     pinName = "";
    203.                     localIP = "";  
    204.                     publicIPpart1 = "";
    205.                     publicIPpart3 = "";
    206.                     displayNetwork.setText(networkName);
    207.                     displayPin.setText(pinName);
    208.                     displayInternal.setText(localIP);
    209.                     displayExternal.setText(publicIPpart1);
    210.                     displayPort.setText(publicIPpart3);
    211.                     findViewById(R.id.button1).setBackgroundResource(R.drawable.buttondefault);
    212.                     saved_button = R.drawable.buttondefault;
    213.                     save();
    214.                     break;                 
    215.         }
    216.         return true;
    217.     }
    218. //Http Task
    219.     private class httpTask extends AsyncTask<Void, Void, Void> {
    220.     HttpEntity resEntityGet = null;
    221.  
    222.         @Override
    223.         protected void onPreExecute() {
    224.             GetMethod test = new GetMethod();
    225.             String returned;
    226.             try {
    227.                 returned = test.getInternetData();
    228.                 if (pinCheck == 1){
    229.                     //Toast.makeText(getBaseContext(), "pinCheck = 1.", Toast.LENGTH_SHORT).show();
    230.                     if(returned.toString().contains("\"Opener 1\">Door Closed")){  //"\"Opener 1\">Door Closed"
    231.                         //Toast.makeText(getBaseContext(), "Option 1 on.", Toast.LENGTH_SHORT).show();
    232.                         findViewById(R.id.button1).setBackgroundResource(R.drawable.option1on);
    233.                     }
    234.                     if(returned.toString().contains("\"Opener 1\">Door Open")){
    235.                         //Toast.makeText(getBaseContext(), "Option 1 off.", Toast.LENGTH_SHORT).show();
    236.                         findViewById(R.id.button1).setBackgroundResource(R.drawable.option1off);                   
    237.                     }
    238.                     }
    239.                     if (pinCheck == 2){
    240.                     //Toast.makeText(getBaseContext(), "pinCheck = 2.", Toast.LENGTH_SHORT).show();
    241.                     if(returned.toString().contains("\"Opener 2\">Door Closed")){  //"\"Opener 2\">Door Closed"
    242.                         //Toast.makeText(getBaseContext(), "Opening the door.", Toast.LENGTH_SHORT).show();
    243.                         findViewById(R.id.button1).setBackgroundResource(R.drawable.dooropen);                 
    244.                     }
    245.                     if(returned.toString().contains("\"Opener 2\">Door Open")){
    246.                         //Toast.makeText(getBaseContext(), "Closing the door.", Toast.LENGTH_SHORT).show();
    247.                         findViewById(R.id.button1).setBackgroundResource(R.drawable.doorclosed);                   
    248.                     }
    249.                     }
    250.                     if (pinCheck == 3){
    251.                     //Toast.makeText(getBaseContext(), "pinCheck = 3.", Toast.LENGTH_SHORT).show();
    252.                     if(returned.toString().contains("\"Opener 3\">Door Closed")){  //"\"Opener 3\">Door Closed"
    253.                         //Toast.makeText(getBaseContext(), "Turning on the LED.", Toast.LENGTH_SHORT).show();
    254.                         findViewById(R.id.button1).setBackgroundResource(R.drawable.arduinoon);                
    255.                     }
    256.                     if(returned.toString().contains("\"Opener 3\">Door Open")){
    257.                         //Toast.makeText(getBaseContext(), "Turning off the LED.", Toast.LENGTH_SHORT).show();
    258.                         findViewById(R.id.button1).setBackgroundResource(R.drawable.arduinooff);                   
    259.                     }
    260.                     }
    261.             } catch (Exception e) {
    262.                 // TODO Auto-generated catch block
    263.                 e.printStackTrace();
    264.             }
    265.             dialog = new ProgressDialog(GarageDoorOpenerActivity.this);
    266.             dialog.setMessage("Working");
    267.             dialog.show();
    268.         }
    269.         @Override
    270.         protected Void doInBackground(Void... params) {
    271.           try {
    272.         HttpClient client = new DefaultHttpClient();
    273.         HttpGet get = new HttpGet(customURL[0] + URL + customURL[1] + pin);
    274.         responseGet = client.execute(get);  
    275.         resEntityGet = responseGet.getEntity();
    276.       } catch (Exception e) {
    277.           e.printStackTrace();
    278.       }
    279.         return null;
    280.         }
    281.         @Override
    282.         protected void onPostExecute(Void params) {
    283.             if(dialog.isShowing())
    284.                 dialog.dismiss();
    285.         if (resEntityGet != null) {  
    286.             Toast.makeText(getBaseContext(), "Done. ", Toast.LENGTH_SHORT).show();
    287.             resEntityGet = null;
    288.         }
    289.         }
    290.     }
    291.     public class GetMethod {
    292.         public String getInternetData() throws Exception{
    293.             BufferedReader in = null;
    294.             String data = null;
    295.             try{
    296.                 HttpClient client = new DefaultHttpClient();
    297.                 URI url = new URI(customURL[0] + URL + customURL[1]); //customURL[0] + URL + customURL[1]
    298.                 HttpGet request = new HttpGet();
    299.                 request.setURI(url);
    300.                 HttpResponse response = client.execute(request);
    301.                 in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
    302.                 StringBuffer sb = new StringBuffer("");
    303.                 String l = "";
    304.                 String nl = System.getProperty("line.separator");
    305.                 while ((l = in.readLine()) !=null){
    306.                     sb.append(l + nl);             
    307.                 }
    308.                 in.close();
    309.                 data = sb.toString();
    310.                 return data;
    311.             } finally{
    312.                 if (in !=null){
    313.                     try{
    314.                         in.close();
    315.                         return data;
    316.                     } catch (Exception e){
    317.                         e.printStackTrace();
    318.                     }
    319.                 }
    320.             }
    321.         }
    322.     }
    323. //Local / Public IP Address Entry
    324.     public void enterIP(){
    325.         Intent j = new Intent(this, enterIP.class);
    326.         j.putExtra("public_ip", publicIP);
    327.         j.putExtra("local_ip", localIP);
    328.         j.putExtra("port_number", portNumber);
    329.         startActivityForResult(j, REQUEST_CODE);
    330.     }
    331. //Activity Result
    332.     public void onActivityResult(int requestCode, int resultCode, Intent data) {
    333.         if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
    334.             if (data.hasExtra("public_ip")){
    335.                 readpublicIP = data.getExtras().getString("public_ip");
    336.                 publicIPpart1 = String.valueOf(readpublicIP);
    337.             }
    338.             if (data.hasExtra("local_ip")){
    339.                 readlocalIP = data.getExtras().getString("local_ip");
    340.                 localIP = String.valueOf(readlocalIP);
    341.             }
    342.             if (data.hasExtra("port_number")){
    343.                 readportNumber = data.getExtras().getString("port_number");
    344.                 publicIPpart3 = String.valueOf(readportNumber);
    345.             }
    346.                 publicIPpart2 = ":";
    347.                 publicIP = (publicIPpart1 + publicIPpart2 + publicIPpart3);
    348.             }
    349.         EditText displayInternal = (EditText) findViewById(R.id.displayInternal);
    350.         EditText displayExternal = (EditText) findViewById(R.id.displayExternal);
    351.         EditText displayPort = (EditText) findViewById(R.id.displayPort);
    352.         displayInternal.setText(localIP);
    353.         displayExternal.setText(publicIPpart1);
    354.         displayPort.setText(publicIPpart3);
    355.         save();
    356.         }
    357. //SAVE FUNCTION
    358.     private void save() {
    359.         SharedPreferences settings = getSharedPreferences("app_settings", 0);  
    360.         SharedPreferences.Editor prefEditor = settings.edit();
    361.         prefEditor.putString("publicIP_part1", publicIPpart1);
    362.         prefEditor.putString("publicIP_part3", publicIPpart3);
    363.         prefEditor.putString("public_ip", publicIP);
    364.         prefEditor.putString("local_ip",localIP);
    365.         prefEditor.putBoolean("use_external", useExternal);
    366.         prefEditor.putString("arduino_pin",pin);
    367.         prefEditor.putString("port_number", portNumber);
    368.         prefEditor.putString("passed_url", URL);
    369.         prefEditor.putString("pin_name", pinName);
    370.         prefEditor.putString("network_name", networkName);
    371.         prefEditor.putInt("pin_check",pinCheck);
    372.         prefEditor.putInt("saved_button",saved_button);
    373.         prefEditor.commit();               
    374.     }
    375. //On Pause
    376.     @Override
    377.     protected void onPause() {
    378.         super.onPause();
    379.         if(dialog != null && dialog.isShowing())
    380.             dialog.dismiss();
    381.         if(mTask != null && mTask.getStatus() == AsyncTask.Status.RUNNING)
    382.             mTask.cancel(true);
    383.     }
    384. }
    385.  
    enterIP class:
    Code (Text):
    1.  
    2. package com.garagedoor.opener;
    3.  
    4. import android.app.Activity;
    5. import android.content.Intent;
    6. import android.os.Bundle;
    7. import android.view.View;
    8. import android.view.View.OnClickListener;
    9. import android.widget.Button;
    10. import android.widget.EditText;
    11.  
    12. public class enterIP extends Activity {
    13.     String publicIP = "";
    14.     String localIP = "";
    15.     String portNumber = "";
    16.     private EditText publicIpText;
    17.     private EditText localIpText;
    18.     private EditText portNumberText;
    19.    
    20.      @Override
    21.         protected void onCreate(final Bundle savedInstanceState) {
    22.             super.onCreate(savedInstanceState);
    23.             setContentView(R.layout.enterip);
    24.  
    25.             //Button to set the user's upc code
    26.             final Button button1 = (Button) findViewById(R.id.savebutton);
    27.                button1.setOnClickListener(new OnClickListener() {
    28.                      public void onClick(View v) {
    29.                          
    30.                         localIpText = (EditText) findViewById(R.id.editText1);
    31.                         localIP = localIpText.getText().toString();
    32.                        
    33.                         publicIpText = (EditText) findViewById(R.id.editText2);
    34.                         publicIP = publicIpText.getText().toString();
    35.                        
    36.                         portNumberText = (EditText) findViewById(R.id.editText3);
    37.                         portNumber = portNumberText.getText().toString();
    38.                         finish();
    39.                     }
    40.               });
    41.         }
    42.         public void finish() {
    43.             //Returns the user's code back to the main app
    44.                 Intent data = new Intent();
    45.                 // Return some hard-coded values
    46.                 data.putExtra("public_ip", publicIP);
    47.                 data.putExtra("local_ip", localIP);
    48.                 data.putExtra("port_number", portNumber);
    49.                 setResult(RESULT_OK, data);
    50.                 //Finish is what is called whenever you hit Back on your phone
    51.                 super.finish();
    52.             }      
    53.     }
    54.  
    enterIP allows the user input their IP addresses for local and external. The app takes the IP addresses and adds http:// to the beginning and / to the end. The menu option about the LEDs adds ?LED0 ?LED1 or ?LED2 to the end of the / that was added to the end of the URL. If the ip being used is on the external network a port number that was provided is added after the IP address before the / (8080 is what is port forwarded on the router as port 80 doesn't work with my service provider)

    So when you configure everything and press the button the app sends either:

    http://192.168.1.100/?LED2 or http://74.125.224.72/?LED2 depending on if you're on the local network or not.

    Video of the Arduino side of things:

    http://www.youtube.com/watch?v=XKPXZB3v7dU

    Any help anyone can provide would be great. Thanks.

    Advertisement
  2. dlevans

    dlevans New Member

Share This Page