Instantly share code, notes, and snippets.

Embed
What would you like to do?
package com.volgakarakus.johndeere;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.TextView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import java.util.HashMap;
import java.util.Map;
public class WiFiActivity extends Activity {
//This is where we decide which servo to control.
// 2 -> Worm Servo
// 3.1-> Linear Actuator
// 3.2 -> Stepper Motor
// We had board number 0 and 1 but the servos got either replaced or their functions changed.
private String boardSelect = "";
//The IP Address of the Tango that's sent to the Phone.
private String ipAddress = "";
//The spinner which has the value of which board is currently selected.
private boolean mSpinnerInitialized;
//This is the integer that contains the old value of the seek bar in the layout.
//We use this value in order to get the difference between the oldValue and the
//new so that we can move the servos accordingly to their position.
private int oldProgress = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
//Creating layout.
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wi_fi);
//Initializations
Spinner spinner =(Spinner) findViewById(R.id.spinner);
//Gets the value of the spinner.
boardSelect = spinner.getSelectedItem().toString();
//This is there to activate everytime someone touches or changes the spinner's value.
//Functionality consist of setting boardSelect to the value of the spinner.
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
if (!mSpinnerInitialized) {
mSpinnerInitialized = true;
return;
}
final TextView newValue = ((TextView) selectedItemView);
boardSelect = newValue.getText().toString();
Log.d("BOARD SELECTED : " , " |||||||||| " + boardSelect);
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
//We have our default to 1 already.
}
});
//Initializations of the text bar displaying the height value in inches.
final TextView t1 = (TextView) findViewById(R.id.heighTextView);
//Initialization of the seekbar which has the value of the height of the blade.
final SeekBar sk=(SeekBar) findViewById(R.id.seekBar);
//These three functions below are controlling the button actions on the layout
//The buttons are working for the Tango with the big stepper motor.
final Button buttonHigh = (Button) findViewById(R.id.button_high);
buttonHigh.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Code here executes on main thread after user presses button
sk.setProgress(25);
ipAddress = ((EditText) findViewById(R.id.address)).getText().toString();
//sendPostRequest(1);
sendRequest("/boardSelect=3/rot=11");
}
});
final Button buttonLow = (Button) findViewById(R.id.button_low);
buttonLow.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Code here executes on main thread after user presses button
sk.setProgress(0);
ipAddress = ((EditText) findViewById(R.id.address)).getText().toString();
sendRequest("/boardSelect=3/rot=10");
}
});
final Button buttonMed = (Button) findViewById(R.id.button_med);
buttonMed.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Code here executes on main thread after user presses button
sk.setProgress(13);
ipAddress = ((EditText) findViewById(R.id.address)).getText().toString();
sendRequest("/boardSelect=3/rot=01");
}
});
final Button buttonStop = (Button) findViewById(R.id.button_stop);
buttonStop.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ipAddress = ((EditText) findViewById(R.id.address)).getText().toString();
sk.setProgress(0);
sendRequest("/mtrOff=1/");
}
});
//Seek bar settings
//We are moving fromh height of 2 to 4,5 inch down and up for most cases, that is why our
//Max value is set as 25.
//This value is not very important as we have scaled the values according to
//the servos that are being controlled and their range of action.
sk.setMax(25);
sk.setProgress(0);
t1.setText("1.5 in");
sk.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
//This function gets called everytime we let go of the seek bar from our touch.
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
ipAddress = ((EditText) findViewById(R.id.address)).getText().toString();
//Getting the difference between the values of the seekbar to be able to understand
//which height the user wants to select.
int rot = seekBar.getProgress() - oldProgress;
//Getting the direction according to the sign of the rotation.
//-1 = Down
// 1 = Up
String dir = (oldProgress >= seekBar.getProgress()) ? "-1" : "1";
rot = Math.abs(rot);
sendRequest("/boardSelect=" + boardSelect + "/dir=" + dir + "/rot=" + ((boardSelect.equals("3")) ? rot*200/25 : ((boardSelect.equals("2")) ? rot*10/25 : rot)) + "/endTurn");
oldProgress = seekBar.getProgress();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
//This function is only there to change and display the value of the height of the blade.
@Override
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
// TODO Auto-generated method stub
//scaling so that we dont get a 0 inch progress.
progress += 15;
//The following is there for visual data representation.
//displays the value of "25" as 2.5 inch or "6" to 0.6 inch,
if(String.valueOf(progress).length() == 2) {
t1.setText(String.valueOf(progress).charAt(0) + "." +String.valueOf(progress).charAt(1) + " in");
}else{
t1.setText("0." + String.valueOf(progress).charAt(0) + " in");
}
}
});
}
//This is possibly the most important function in this Application as this is where we are sending
//an HTTP POST request that is sent to the Wi-Fi module.
//Giving any string parameter to this function will immediately send the request to the current
//ipAddress. The strings passed are unique for each board and contains data such as what height
//to lawn with.
void sendRequest(final String str){
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://" + ipAddress + str;
StringRequest postRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>()
{
@Override
public void onResponse(String response) {
// response
Log.d("Response", response);
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
// error
Log.d("Error.Response", "error at response");
}
}
) {
//This part of the function is unnecessary but required for the function to work.
//But why ? Because this is normally the way to send JSON values to the HTTP request
//But since our Arduino didn't require such complex packages of data, using this method would have been
//more of a waste of time as we would need to de-bundle the package when it came to the Arduino.
@Override
protected Map<String, String> getParams()
{
Map<String, String> params = new HashMap<String, String>();
params.put("gpio", "0");
return params;
}
};
Log.d("POST REQUEST : " , postRequest.getUrl().toString());
//Add request to the queue in case something bad happened, this way it can be sent again.
queue.add(postRequest);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment