Android app kod
Senaste videon, EM-kval i bakgrunden så man var lite ofokuserad.
http://www.youtube.com/watch?v=NMCwcS6b2Zs
Ni behöver SendToPhp.java, STRINGS.XML och MAIN.XML i erat projekt.
Zippade hela min projektmapp, hoppas det funkar, APK filen är den färdiga filen redo att installeras på Androidluren.
För att appen vill använda Internet så måste ni lägga till:
Kod: Markera allt
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
I eran AndroidManifest.xml fil.
Snippen ska ligga
ovanför <uses-sdk>
SendToPhp.java
Ganska mycket kod, men det är många funktioner oxå

Den börjar med att importera allt nödvändigt.
Kom ihåg att den första raden package send.toPHP är ditt projekt namn i Eclipse.
Nu använder jag mig inte av PHP längre men skitsamma.
Kod: Markera allt
package send.toPHP;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
public class SendToPhp extends Activity {
private EditText text; //input text
private EditText ipString; //ip number string
private TextView textRandom; //the sensor value, which is random right now
private Timer autoUpdate;
private ToggleButton OnOff; //on off switch value
private SeekBar seekBar;
private ToggleButton ipConnect; //connect button value
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text = (EditText) findViewById(R.id.textBox);
ipString = (EditText) findViewById(R.id.ipBox);
OnOff = (ToggleButton) findViewById(R.id.ledOnOff);
seekBar = (SeekBar) findViewById(R.id.seekBar1);
textRandom = (TextView) findViewById(R.id.randomBox);
textRandom.setText("0"); //make the random number 0 on start-up
ipConnect = (ToggleButton) findViewById(R.id.connectButton);
final TextView seekBarValue = (TextView)findViewById(R.id.seekBarValue);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
//this is the slide bar, does NOTHING right now but I want to combine with PWM
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
seekBarValue.setText(String.valueOf(progress));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
});
}
//Here starts the post http function
public void postData(View view) {
// Create a new HttpClient and Post Header
//post data to web-server
try {
String IpNo = ipString.getText().toString(); //ip number
String text_write = text.getText().toString(); //take the text from input text
HttpClient client = new DefaultHttpClient();
//by posting like this the server will process the text_write string
String postURL = "http://" + IpNo + "/"+ text_write;
HttpPost post = new HttpPost(postURL);
//this part is not necessary because I only post on single line, but kept it for the future.
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("text1", text_write));
params.add(new BasicNameValuePair("text2", "")); //not actually in use, just empty
UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params,HTTP.UTF_8);
post.setEntity(ent);
HttpResponse responsePOST = client.execute(post);
HttpEntity resEntity = responsePOST.getEntity();
if (resEntity != null) {
Log.i("RESPONSE",EntityUtils.toString(resEntity));
}
//Toast code: simply sends "Text sent!"-alert after button click
Context context = getApplicationContext(); //not sure what this is
CharSequence text = "Text sent! " + "'" + text_write + "'"; //Toast text
int duration = Toast.LENGTH_SHORT; //Duration of the toast
Toast toast = Toast.makeText(context, text, duration);
toast.show();
} //try
catch (Exception e) {e.printStackTrace();}//catch
}//post
//Here starts the ON/OFF logic
public void f_onOff(View view) {
// Create a new HttpClient and Post Header
//post data to web server
if (OnOff.isChecked()) { //if onOff button is checked do this:
/*int duration1 = Toast.LENGTH_SHORT;
Context context1 = getApplicationContext();
CharSequence text = "On";
Toast toast = Toast.makeText(context1, text, duration1);
toast.show();*/ //good for trouble-shooting
try {
String IpNo = ipString.getText().toString(); //get the IP address from the ipBox
String strOn = "#"; //the LED ON character recognized by Arduino
HttpClient client = new DefaultHttpClient();
String postURL = "http://" + IpNo + "/"+ strOn; //post this to the server to initiate server side functions
HttpPost post = new HttpPost(postURL);
HttpResponse responsePOST = client.execute(post);
HttpEntity resEntity = responsePOST.getEntity();
if (resEntity != null) {
Log.i("RESPONSE",EntityUtils.toString(resEntity));
}//if
else{}
}
catch (Exception e) {e.printStackTrace();}
}//if
else{ //if the onOff button is NOT checked do this:
/*int duration2 = Toast.LENGTH_SHORT;
Context context2 = getApplicationContext(); //not sure what this is
CharSequence text = "off";
Toast toast = Toast.makeText(context2, text, duration2);
toast.show();*/ //good for trouble-shooting
try {
String IpNo = ipString.getText().toString(); //get the IP address from the ipBox
String strOff = ";"; //the LED OFF character recognized by Arduino
HttpClient client = new DefaultHttpClient();
String postURL = "http://" + IpNo + "/"+ strOff; //post this to the server to initiate server side functions
HttpPost post = new HttpPost(postURL);
HttpResponse responsePOST = client.execute(post);
HttpEntity resEntity = responsePOST.getEntity();
if (resEntity != null) {
Log.i("RESPONSE",EntityUtils.toString(resEntity));
}//if
else{}
}//try
catch (Exception e) {e.printStackTrace();}
}//else
} //LED control stops here
//Here starts the random text box function
public void f_random(View view){
if (ipConnect.isChecked()) { //make sure that we are connected if connected, do this:
try {
String IpNo = ipString.getText().toString();
HttpClient client = new DefaultHttpClient();
String getURL = "http://" + IpNo + "/index.esp";
HttpGet get = new HttpGet(getURL);
HttpResponse responseGet = client.execute(get);
HttpEntity resEntityGet = responseGet.getEntity();
if (resEntityGet != null) {
textRandom.setText(EntityUtils.toString(resEntityGet));
Log.i("GET RESPONSE",EntityUtils.toString(resEntityGet));
}//if
}//try
catch (Exception e) {e.printStackTrace();}
}
else{} //if not connected do nothing
}
//Here starts the re-run random text box, it has a auto-update function.
@Override
public void onResume() {
super.onResume();
autoUpdate = new Timer();
autoUpdate.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
f_random(textRandom);
}
});
}
}, 0, 5000); // updates each 5000 millisecond (5 sec) //if troubleshooting put some huge number
}
@Override
public void onPause() {
autoUpdate.cancel();
super.onPause();
}
//Here stops the refresh logic
} //activity bracket
STRINGS.XML
Här lagras strängar, typ knapp text och färger, osv.
Kod: Markera allt
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Write to Arduino:)</string>
<string name="app_name">Hello Arduino</string>
<color name="myColor">#3399CC</color>
<string name="postData">postData</string>
<string name="SendToHttp">Send</string>
<string name="ip_string">IP:</string>
<string name="random">Sensor value:</string>
<string name="onOff">onOff</string>
<string name="seekbarvalue">seekbarvalue</string>
</resources>
MAIN.XML
Kan man säga att detta är GUIn?, alla knappar/boxar/etc.
Kod: Markera allt
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/myColor"
>
<EditText
android:id="@+id/textBox"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:inputType="text"
android:paddingTop="0dp"
android:maxLength="16"
android:layout_marginLeft="60dp" >
<requestFocus></requestFocus>
</EditText>
<Button
android:id="@+id/SendToHttp"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="postData"
android:text="@string/SendToHttp"
android:layout_below="@+id/editText1"></Button>
<TextView
android:id="@+id/ipText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ip_string"
android:layout_below="@+id/SendToHttp"
android:layout_marginTop="10dp"
android:layout_marginLeft="15dp"
android:textStyle="bold"
android:textSize="20dp"
></TextView>
<EditText
android:id="@+id/ipBox"
android:layout_height="wrap_content"
android:text="192.168.1.103"
android:layout_width="171dp"
android:layout_marginLeft="50dp"
android:layout_below="@+id/SendToHttp"
></EditText>
<TextView
android:id="@+id/toggleText"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignLeft="@+id/textView2"
android:layout_marginTop="25dp"
android:layout_below="@+id/ipText"
android:text="LED switch"
android:textStyle="bold"
android:textSize="15dp"
></TextView>
<ToggleButton
android:id="@+id/connectButton"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="@+id/textBox"
android:layout_alignLeft="@+id/ipBox"
android:layout_marginLeft="180dp"
android:layout_marginTop="3dp"
android:textOff="Connect"
android:textOn="Connected"
android:onClick="f_random"
></ToggleButton>
<ToggleButton android:id="@+id/ledOnOff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ipText"
android:layout_marginTop="15dp"
android:textOff="Off"
android:textOn="On"
android:layout_marginLeft="85dp"
android:onClick="f_onOff"
></ToggleButton>
<TextView android:id="@+id/randomBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="[random no]"
android:layout_below="@+id/ledOnOff"
android:textStyle="bold"
android:textSize="20dp"
android:layout_marginLeft="135dp"></TextView>
<TextView android:id="@+id/randomText"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignLeft="@+id/textView2"
android:layout_marginTop="0dp"
android:layout_below="@+id/ledOnOff"
android:text="@string/random"
android:textStyle="bold"
android:textSize="20dp"
></TextView>
<SeekBar android:id="@+id/seekBar1"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_below="@+id/randomText"
></SeekBar>
<TextView
android:id="@+id/seekBarValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Value"
android:layout_alignTop="@+id/seekBar1"
android:layout_alignRight="@+id/ipBox"
></TextView>
</RelativeLayout>
Du har inte behörighet att öppna de filer som bifogats till detta inlägg.