I’ve tried to find some help on this, but have failed miserably. I’m not an experienced Java coder, so any help is appreciated. Apologies for the untidiness of my code.
I’ve written some Java code which works on my Android phone to control and display the status of my home central heating system. So far so good. However, when the READ STATUS button is pressed, I’d like to have a progressbar displayed. The series of 8 calls to the Particle takes up to 10 seconds to complete, then the entire display updates - it would be awfully nice to achieve incremental updates as the data is received. Sorry the code is so vast - I’d like to use more nested code but haven’t figured out how yet. The Particle is running a modified version of Tinker.
package net.gotopcs.www.heatingcontroller;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.StrictMode;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import static android.provider.SyncStateContract.Helpers.update;
public class ControlHeating extends AppCompatActivity {
private ProgressBar firstBar = null;
private int i = 0;
RelativeLayout layout;
TextView ATextView;
TextView BTextView;
TextView CTextView;
TextView DTextView;
TextView FTextView;
TextView GTextView;
TextView HTextView;
String longstring;
String shortstring;
String TAG = "dgc";
Button Abutton;
Button Bbutton;
Button Cbutton;
TextView CHradio;
TextView HWradio;
BufferedReader in = null;
String access_code = "my access code";
String analogread = "https://api.particle.io/v1/devices/my device/analogread";
String digitalwrite = "https://api.particle.io/v1/devices/my device/digitalwrite";
String HeatFunction;
String DigNumber;
int foo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_control_heating);
StrictMode.ThreadPolicy policy = new StrictMode.
ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
firstBar = (ProgressBar)findViewById(R.id.firstBar);
CHradio = (TextView) findViewById(R.id.radioButton1);
CHradio.setText(" ---- ");
HWradio = (TextView) findViewById(R.id.radioButton2);
HWradio.setText(" ---- ");
ATextView = (TextView) findViewById(R.id.editText5);
ATextView.setText(" - - - - ");
BTextView = (TextView) findViewById(R.id.editText6);
BTextView.setText(" - - - - ");
CTextView = (TextView) findViewById(R.id.textView);
CTextView.setText(" - - - - ");
DTextView = (TextView) findViewById(R.id.textView8);
DTextView.setText(" - - - - ");
GTextView = (TextView) findViewById(R.id.textView28);
GTextView.setText(" - - - - ");
HTextView = (TextView) findViewById(R.id.textView38);
HTextView.setText(" - - - - ");
FTextView = (TextView) findViewById(R.id.editText29);
Cbutton = (Button) findViewById(R.id.button3);
/* layout.setVisibility(View.VISIBLE);
firstBar.setVisibility(View.VISIBLE);
firstBar.setMax(150);
//Set first progress bar value
i = 10;
firstBar.setProgress(i);
RefreshStatus("CH");
i = 50;
firstBar.setProgress(i);
RefreshStatus("HW");
i = 100;
firstBar.setProgress(i);
RefreshStatus("Pump");
i = 150;
firstBar.setProgress(i);
firstBar.setVisibility(View.GONE);
*/
Cbutton.setOnClickListener(new View.OnClickListener() {
@Override
//On click function
public void onClick(View view) { // code run when "Read Status" button clicked
/*Create whatever the button does
//make the progress bar visible
firstBar.setVisibility(View.VISIBLE);
firstBar.setMax(150);
//Set first progress bar value
i = 10;
firstBar.setProgress(i);
*/
//Create an object of HttpClient
HttpClient httpclient = new DefaultHttpClient();
//Create an object of Httppost
HttpPost httppost = new HttpPost(analogread);
// Extract reading for top temperature
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("access_token", access_code));
nameValuePairs.add(new BasicNameValuePair("String pin", "A0"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
FTextView.setText("Error 001");
} catch (IOException e) {
// TODO Auto-generated catch block
FTextView.setText("Error 002");
}
// response collected in buffer and print in logcat
String s = null;
longstring = "";
try {
Log.d(TAG, "Top temp");
while ((s = in.readLine()) != null) {
Log.d(TAG, s);
// build up a string of s's here
longstring = longstring + s;
}
ss(longstring, 90);
ATextView.setText(shortstring.substring(1, 3) + "." + shortstring.substring(3, 4) + " °C");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //------------------------------------------------------------------------------
//i = 40;
//AsyncTask(i,stuff,things);
//firstBar.setProgress(i);
//Create an object of Httppost for bottom temperature
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("access_token", access_code));
nameValuePairs.add(new BasicNameValuePair("String pin", "A1"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
FTextView.setText("Error 003");
} catch (IOException e) {
// TODO Auto-generated catch block
FTextView.setText("Error 004");
}
// response collected in buffer and print in logcat
String t = null;
longstring = "";
try {
Log.d(TAG, "Bottom temp");
while ((t = in.readLine()) != null) {
Log.d(TAG, t);
// build up a string of s's here
longstring = longstring + t;
}
ss(longstring, 90);
BTextView.setText(shortstring.substring(1, 3) + "." + shortstring.substring(3, 4) + " °C");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //------------------------------------------------------------------------------
//i = 80;
//firstBar.setProgress(i);
//Create an object of Httppost for Supply voltage
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("access_token", access_code));
nameValuePairs.add(new BasicNameValuePair("String pin", "A2"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
FTextView.setText("Error 005");
} catch (IOException e) {
// TODO Auto-generated catch block
FTextView.setText("Error 006");
}
// response collected in buffer and print in logcat
String u = null;
longstring = "";
try {
Log.d(TAG, "Supply volts");
while ((u = in.readLine()) != null) {
Log.d(TAG, u);
// build up a string of s's here
longstring = longstring + u;
}
ss(longstring, 90);
CTextView.setText(shortstring.substring(1, 3) + "." + shortstring.substring(3, 5) + " V");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //------------------------------------------------------------------------------
//i = 120;
//firstBar.setProgress(i);
//Create an object of Httppost for external temperature
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("access_token", access_code));
nameValuePairs.add(new BasicNameValuePair("String pin", "A5"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
FTextView.setText("Error 005");
} catch (IOException e) {
// TODO Auto-generated catch block
FTextView.setText("Error 006");
}
// response collected in buffer and print in logcat
String v = null;
longstring = "";
try {
Log.d(TAG, "External temperature");
while ((v = in.readLine()) != null) {
Log.d(TAG, v);
// build up a string of s's here
longstring = longstring + v;
}
ss(longstring, 90);
GTextView.setText(shortstring.substring(1, 3) + "." + shortstring.substring(3, 4) + " °C");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //------------------------------------------------------------------------------
//i = 130;
//firstBar.setProgress(i);
//Create an object of Httppost for spare indicator
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("access_token", access_code));
nameValuePairs.add(new BasicNameValuePair("String pin", "X1"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
FTextView.setText("Error 025");
} catch (IOException e) {
// TODO Auto-generated catch block
FTextView.setText("Error 026");
}
// response collected in buffer and print in logcat
String w = null;
longstring = "";
try {
Log.d(TAG, " t3 ");
while ((w = in.readLine()) != null) {
Log.d(TAG, w);
// build up a string of s's here
longstring = longstring + w;
}
ss(longstring, 90);
HTextView.setText(shortstring.substring(1, 3) + "." + shortstring.substring(3, 4) + " °C");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //------------------------------------------------------------------------------
//Create an object of Httppost for CH status etc
RefreshStatus("CH");
RefreshStatus("HW");
RefreshStatus("Pump");
//i = 150;
//firstBar.setProgress(i);
//firstBar.setVisibility(View.GONE);
}// end of onClick stuff
}); // end of Cbutton stuff
Abutton = (Button) findViewById(R.id.button);
Abutton.setOnClickListener(new View.OnClickListener() {
@Override
//On click function
public void onClick(View view) { // code run when "Toggle CH" button clicked
//Create an object of HttpClient
HttpClient httpclient = new DefaultHttpClient();
//Create an object of Httppost
HttpPost httppost = new HttpPost(digitalwrite);
// toggle CH status button command
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("access_token", access_code));
nameValuePairs.add(new BasicNameValuePair("D0,HIGH", "D0,HIGH"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
FTextView.setText("Error 007");
} catch (IOException e) {
// TODO Auto-generated catch block
FTextView.setText("Error 008");
}
// response collected in buffer and print in logcat
String x = null;
longstring = "";
HeatFunction = "CH";
try {
Log.d(TAG, "toggle CH");
while ((x = in.readLine()) != null) {
Log.d(TAG, x);
// build up a string of s's here
longstring = longstring + x;
}
ss(longstring, 89);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //------------------------------------------------------------------------------
RefreshStatus(HeatFunction);
}
}); // end of CH toggle button stuff
Bbutton = (Button) findViewById(R.id.button2);
Bbutton.setOnClickListener(new View.OnClickListener() {
@Override
//On click function
public void onClick(View view) { // code run when "Toggle HW" button clicked
//Create an object of HttpClient
//progress = ProgressDialog.show(this, "Reading Status", "One moment please", true);
//progress.dismiss();
HttpClient httpclient = new DefaultHttpClient();
//Create an object of Httppost
HttpPost httppost = new HttpPost(digitalwrite);
// toggle HW status button command
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("access_token", access_code));
nameValuePairs.add(new BasicNameValuePair("D1,HIGH", "D1,HIGH"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
FTextView.setText("Error 009");
} catch (IOException e) {
// TODO Auto-generated catch block
FTextView.setText("Error 010");
}
// response collected in buffer and print in logcat
String y = null;
longstring = "";
HeatFunction = "HW";
try {
Log.d(TAG, "toggle HW");
while ((y = in.readLine()) != null) {
Log.d(TAG, y);
// build up a string of s's here
longstring = longstring + y;
}
ss(longstring, 89);
//DTextView.setText(shortstring);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//------------------------------------------------------------------------------*/
RefreshStatus(HeatFunction);
// end http post stuff
}// end of onClick stuff
}); // end of HW toggle button stuff
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_control_heating, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public String ss(String ls, int cutoff) {
// stuff
shortstring = ls.substring(cutoff); // remove lead 89 chars
return shortstring;
}
public void RefreshStatus(String HeatFunction) {
// subroutine to check heating function after toggle button is pressed
if (HeatFunction == "CH") { DigNumber = "D2"; }
if (HeatFunction == "HW") { DigNumber = "D3"; }
if (HeatFunction == "Pump") { DigNumber = "D4"; }
HttpClient httpclient = new DefaultHttpClient();
//Create an object of Httppost
HttpPost httppost = new HttpPost(analogread);
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("access_token", access_code));
nameValuePairs.add(new BasicNameValuePair("String pin", DigNumber));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
FTextView.setText("Error 011");
} catch (IOException e) {
// TODO Auto-generated catch block
FTextView.setText("Error 012");
}
// response collected in buffer and print in logcat
String v = null;
longstring = "";
try {
Log.d(TAG, HeatFunction + "status");
while ((v = in.readLine()) != null) {
Log.d(TAG, v);
// build up a string of s's here
longstring = longstring + v;
}
shortstring = longstring.substring(91, 92); // remove lead 91 chars
foo = Integer.parseInt(shortstring);
if (HeatFunction == "CH") {
if (foo > 0) {
CHradio.setText(" " + HeatFunction + " OFF");
} else {
CHradio.setText(" " + HeatFunction + " ON");
}
}
if (HeatFunction == "HW") {
if (foo > 0) {
HWradio.setText(" " + HeatFunction + " OFF");
} else {
HWradio.setText(" " + HeatFunction + " ON");
}
}
if (HeatFunction == "Pump") {
if (foo > 0) {
DTextView.setText(HeatFunction + " is ON");
} else {
DTextView.setText(HeatFunction + " is OFF");
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //------------------------------------------------------------------------------
}
}