callFunction throwing ParticleCloudException

Hello There!
I was testing my newly made android app using the Android SDK for my project to switch on or off an led. Here is my code

public class MainActivity extends AppCompatActivity {

    //Variables...
    ArrayList<Integer> watt = new ArrayList<>();
    int sumwatt = 0;
    TextView txt;
    private ToggleButton button;
  //  RelativeLayout layout = (RelativeLayout) findViewById(R.id.Relative);
    private ParticleDevice myDevice;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (ToggleButton) findViewById(R.id.tb);

        //txt = (TextView)findViewById(R.id.tex2);
        ParticleCloudSDK.init(getApplicationContext());

        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    ParticleCloudSDK.getCloud().logIn("emailID", "password");
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(),"Connecting...", Toast.LENGTH_LONG).show();
                        }
                    });

                    myDevice = ParticleCloudSDK.getCloud().getDevices().get(0);
                    Log.d("Test1","Device ID:"+myDevice.getID());
                    final String nameString = myDevice.getName();

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Log.d("TESTING","Device Status:"+myDevice.getStatus());
                            if (myDevice.getStatus().equals("normal")){
                                Toast.makeText(getApplicationContext(), "Logged IN!", Toast.LENGTH_LONG).show();
                            }else{Toast.makeText(getApplicationContext(), "Not Connected!", Toast.LENGTH_LONG).show();}
                            Toast.makeText(getApplicationContext(), nameString + " is back!", Toast.LENGTH_LONG).show();
                        }
                    });
                } catch (ParticleCloudException e) {
                    e.printStackTrace();
                }


            }
        });

        t.start();

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    onToggleClick(v);
                } catch (ParticleCloudException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (ParticleDevice.FunctionDoesNotExistException e) {
                    e.printStackTrace();
                } catch (ParticleDevice.VariableDoesNotExistException e) {
                    e.printStackTrace();
                }
            }
        });
    }


    public void onToggleClick(View view) throws ParticleCloudException,IOException,ParticleDevice.FunctionDoesNotExistException,ParticleDevice.VariableDoesNotExistException {

        if (button.isChecked() == true) {
            //Board is Switched ON
            Log.d("Test2","Available functions:"+myDevice.getFunctions());

            int result = myDevice.callFunction("power", list("on"));
            Log.d("Test3","Recieved result="+result);
            if (result == 1) {
                Toast.makeText(this, "Power ON!", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "Not Working!!!", Toast.LENGTH_SHORT).show();
            }


        } else if (button.isChecked() == false) {
            //Board is Switched OFF
            Log.d("Test2","Available functions:"+myDevice.getFunctions());
            int result = myDevice.callFunction("power", list("off"));
            Log.d("Test3","Recieved result="+result);
            if (result == 0) {
                Toast.makeText(this, "Power OFF!", Toast.LENGTH_SHORT).show();
                int wattage = myDevice.getIntVariable("wattage");
                watt.add(wattage);

                //Array wattage usage values...
                Integer[] watts = watt.toArray(new Integer[0]);

                //Calculating net watts...
                for (int x : watts) {
                    sumwatt += x;
                }

                //Displaying net watts consumed...
                txt.setText("Net Wattage:" + sumwatt);

            }
        }
    }
}

My device is connecting as my log message is showing getstatus() result as “normal”(idk if this tells if the device and app are connected, if I am wrong please do tell me!). Now when I am pressing my toggle switch
, my log monitor is showing me this message

07-27 20:27:16.580 14700-14700/com.a09gmail.a23.adityaprakash.particleswitch D/Test2: Available functions:[power]
07-27 20:27:16.586 14700-14700/com.a09gmail.a23.adityaprakash.particleswitch W/System.err: io.particle.android.sdk.cloud.ParticleCloudException
07-27 20:27:16.586 14700-14700/com.a09gmail.a23.adityaprakash.particleswitch W/System.err:     at io.particle.android.sdk.cloud.ParticleDevice.callFunction(ParticleDevice.java:365)
07-27 20:27:16.586 14700-14700/com.a09gmail.a23.adityaprakash.particleswitch W/System.err:     at com.a09gmail.a23.adityaprakash.particleswitch.MainActivity.onToggleClick(MainActivity.java:105)
07-27 20:27:16.586 14700-14700/com.a09gmail.a23.adityaprakash.particleswitch W/System.err:     at com.a09gmail.a23.adityaprakash.particleswitch.MainActivity$2.onClick(MainActivity.java:84)
07-27 20:27:16.586 14700-14700/com.a09gmail.a23.adityaprakash.particleswitch W/System.err:     at android.view.View.performClick(View.java:5198)
07-27 20:27:16.586 14700-14700/com.a09gmail.a23.adityaprakash.particleswitch W/System.err:     at android.widget.CompoundButton.performClick(CompoundButton.java:122)
07-27 20:27:16.586 14700-14700/com.a09gmail.a23.adityaprakash.particleswitch W/System.err:     at android.view.View$PerformClick.run(View.java:21147)
07-27 20:27:16.586 14700-14700/com.a09gmail.a23.adityaprakash.particleswitch W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
07-27 20:27:16.586 14700-14700/com.a09gmail.a23.adityaprakash.particleswitch W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
07-27 20:27:16.586 14700-14700/com.a09gmail.a23.adityaprakash.particleswitch W/System.err:     at android.os.Looper.loop(Looper.java:148)
07-27 20:27:16.586 14700-14700/com.a09gmail.a23.adityaprakash.particleswitch W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5417)
07-27 20:27:16.586 14700-14700/com.a09gmail.a23.adityaprakash.particleswitch W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
07-27 20:27:16.586 14700-14700/com.a09gmail.a23.adityaprakash.particleswitch W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
07-27 20:27:16.586 14700-14700/com.a09gmail.a23.adityaprakash.particleswitch W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

It is throwing a ParticleCloudException whenever I am trying to call my function “power”. Please help me to solve this problem.
Thanks!

Looks like you are calling “callFunction” on main thread. “CallFunction” has to be called in separate thread same as “ParticleCloudSDK.getCloud().logIn()”.

2 Likes

Hi Julius!
I tried what you suggested.This is the change in my function code

public void onToggleClick(View view) throws ParticleCloudException,IOException,ParticleDevice.FunctionDoesNotExistException,ParticleDevice.VariableDoesNotExistException {

        if (button.isChecked() == true) {
            //Board is Switched ON
            Thread t = new Thread(new Runnable(){
                public void run(){
                    try {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    Log.d("Test2","Available functions:"+myDevice.getFunctions());
                                    int result = myDevice.callFunction("power", list("on"));
                                    Log.d("Test3", "Recieved result=" + result);

                                }catch(ParticleDevice.FunctionDoesNotExistException e){e.printStackTrace();}
                                 catch(IOException e){e.printStackTrace();}
                                 catch(ParticleCloudException e){e.printStackTrace();}
                            }
                        });
                    }catch (Exception e) {
                        e.printStackTrace();
                    }
            }
            });
            t.start();


        }else if (button.isChecked() == false) {
            //Board is Switched OFF
            final Thread t = new Thread(new Runnable(){
                public void run(){
                    try {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    Log.d("Test2","Available functions:"+myDevice.getFunctions());
                                    int result = myDevice.callFunction("power", list("off"));
                                    Log.d("Test3", "Recieved result=" + result);
                                    if (result == 0) {
                                        int wattage = myDevice.getIntVariable("wattage");
                                        watt.add(wattage);

                                        //Array wattage usage values...
                                        Integer[] watts = watt.toArray(new Integer[0]);
                                        //Calculating net watts...
                                        for (int x : watts) {
                                            sumwatt += x;
                                        }
                                        //Displaying net watts consumed...
                                        txt.setText("Net Wattage:" + sumwatt);
                                        }
                                }catch(ParticleDevice.FunctionDoesNotExistException e){e.printStackTrace();}
                                 catch(IOException e){e.printStackTrace();}
                                 catch(ParticleCloudException e){e.printStackTrace();}
                                 catch(ParticleDevice.VariableDoesNotExistException e){e.printStackTrace();}
                            }
                        });
                    }catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
            t.start();
        }
    }

But it’s still throwing the same ParticleCloudException

07-28 11:47:09.942 10319-10319/com.a09gmail.a23.adityaprakash.particleswitch D/Test2: Available functions:[power]
07-28 11:47:09.947 10319-10319/com.a09gmail.a23.adityaprakash.particleswitch W/System.err: io.particle.android.sdk.cloud.ParticleCloudException
07-28 11:47:09.947 10319-10319/com.a09gmail.a23.adityaprakash.particleswitch W/System.err:     at io.particle.android.sdk.cloud.ParticleDevice.callFunction(ParticleDevice.java:365)
07-28 11:47:09.947 10319-10319/com.a09gmail.a23.adityaprakash.particleswitch W/System.err:     at com.a09gmail.a23.adityaprakash.particleswitch.MainActivity$4$1.run(MainActivity.java:137)
07-28 11:47:09.947 10319-10319/com.a09gmail.a23.adityaprakash.particleswitch W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
07-28 11:47:09.947 10319-10319/com.a09gmail.a23.adityaprakash.particleswitch W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
07-28 11:47:09.947 10319-10319/com.a09gmail.a23.adityaprakash.particleswitch W/System.err:     at android.os.Looper.loop(Looper.java:148)
07-28 11:47:09.947 10319-10319/com.a09gmail.a23.adityaprakash.particleswitch W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5417)
07-28 11:47:09.947 10319-10319/com.a09gmail.a23.adityaprakash.particleswitch W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
07-28 11:47:09.947 10319-10319/com.a09gmail.a23.adityaprakash.particleswitch W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
07-28 11:47:09.947 10319-10319/com.a09gmail.a23.adityaprakash.particleswitch W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

What should I do to solve this?
Thanks!

Still on main thread - "runOnUiThread" runs code on main/UI thread. I suggest using particle async task.

public void onToggleClick(View view) throws ParticleCloudException,IOException,ParticleDevice.FunctionDoesNotExistException,ParticleDevice.VariableDoesNotExistException {

    if (button.isChecked() == true) {
        //Board is Switched ON
        Thread t = new Thread(new Runnable(){
            public void run(){
                try {
                    Log.d("Test2","Available functions:"+myDevice.getFunctions());
                    int result = myDevice.callFunction("power", list("on"));
                    Log.d("Test3", "Recieved result=" + result);
                }catch(ParticleDevice.FunctionDoesNotExistException e){e.printStackTrace();}
                    catch(IOException e){e.printStackTrace();}
                    catch(ParticleCloudException e){e.printStackTrace();}
        }
        });
        t.start();
    }else if (button.isChecked() == false) {
        //Board is Switched OFF
        final Thread t = new Thread(new Runnable(){
            public void run(){
                try {
                    Log.d("Test2","Available functions:"+myDevice.getFunctions());
                    int result = myDevice.callFunction("power", list("off"));
                    Log.d("Test3", "Recieved result=" + result);
                    if (result == 0) {
                        int wattage = myDevice.getIntVariable("wattage");
                        watt.add(wattage);
                        //Array wattage usage values...
                        Integer[] watts = watt.toArray(new Integer[0]);
                        //Calculating net watts...
                        for (int x : watts) {
                            sumwatt += x;
                        }
                        //Displaying net watts consumed...
                        txt.setText("Net Wattage:" + sumwatt);
                   }
               }catch(ParticleDevice.FunctionDoesNotExistException e){e.printStackTrace();}
                             catch(IOException e){e.printStackTrace();}
                             catch(ParticleCloudException e){e.printStackTrace();}
                             catch(ParticleDevice.VariableDoesNotExistException e){e.printStackTrace();}
            }
        });
        t.start();
    }
}
2 Likes