Reading Electron variable in Android studio

am a new to particle cloud api and am unable to read the variable i set in my .ino file i upload to the electron to get sensor values. I need to read the value in android studio and use it in my app.

i used the particle android sdk example from github as reference to implement it in my project. Everything works fine through the android app, i can log in particle cloud, see device info and description, but am unable to read variable values from the electron.

here’s my code:

package com.example.footy;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import java.io.IOException;
import java.util.Map;

import io.particle.android.sdk.cloud.ParticleCloud;
import io.particle.android.sdk.cloud.exceptions.ParticleCloudException;
import io.particle.android.sdk.cloud.ParticleCloudSDK;
import io.particle.android.sdk.cloud.ParticleDevice;
import io.particle.android.sdk.utils.Async;
import io.particle.android.sdk.utils.Toaster;

public class ValueActivity extends AppCompatActivity {

    private static final String ARG_VALUE = "ARG_VALUE";
    private static final String ARG_DEVICEID = "ARG_DEVICEID";

    private TextView tv, tv2;
    private ParticleDevice device;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_value);
        tv = findViewById(R.id.value);
        tv.setText(String.valueOf(getIntent().getIntExtra(ARG_VALUE, 0)));

        tv2 = findViewById(R.id.value2);
        //tv2.setText(String.valueOf(getIntent().getIntExtra(ARG_VALUE, 0)));



        findViewById(R.id.refresh_button).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v){

                        // Do network work on background thread
                        Async.executeAsync(ParticleCloudSDK.getCloud(), new Async.ApiWork<ParticleCloud, Integer>() {
                            @Override
                            public Integer callApi(@NonNull ParticleCloud ParticleCloud) throws ParticleCloudException, IOException {
                                device = ParticleCloud.getDevice(getIntent().getStringExtra(ARG_DEVICEID));
                                int variable;
                                try {

                                    variable = device.getIntVariable("distance");
                                    Toaster.l(ValueActivity.this, variable + " cm");

                                } catch (ParticleDevice.VariableDoesNotExistException e) {
                                    Toaster.l(ValueActivity.this, e.getMessage());
                                    variable = -1;
                                }
                                return variable;
                            }

                            @Override
                            public void onSuccess(@NonNull Integer i) { // this goes on the main thread
                                tv.setText(String.valueOf(i));

                            }

                            @Override
                            public void onFailure(@NonNull ParticleCloudException e) {
                                e.printStackTrace();
                            }
                        });

                    }
                }
        );

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        new MenuInflater(this).inflate(R.menu.menu_value, menu);
        return (super.onCreateOptionsMenu(menu));
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.action_info) {
            Intent intent = DeviceInfoActivity.buildIntent(ValueActivity.this, getIntent().getStringExtra(ARG_DEVICEID));
            startActivity(intent);
        }
        return super.onOptionsItemSelected(item);
    }


    public static Intent buildIntent(Context ctx, Integer value, String deviceId) {
        Intent intent = new Intent(ctx, ValueActivity.class);
        intent.putExtra(ARG_VALUE, value);
        intent.putExtra(ARG_DEVICEID, deviceId);

        return intent;
    }


}

what am i missing? the variable name is the same as in the code, but i get the error “variable distance does not exists on the device”

please help out

You may also want to show the source code of the application running on your Electron.
Are you sure you are targeting the correct device ID?

here is my electron code :

const int trigPin = 2;
const int echoPin = 6;

long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT); 
Serial.begin(9600); 
}
void loop() {

digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance= duration*0.034/2;

Serial.print("Distance: ");
Serial.println(distance);
}

the variable i want to be able to read is the distance i get from the ultrasonic sensor.

i am sure i have the correct device ID because i am able to get data about its information, last seen, status, description etc (in another android activity). also the output of the device id given in the android is the correct one. so its getting any other data of the electron just fine. but not the variable.

Your code is not exposing a Particle.variable("distance", distance) in setup() so it’s no surprise that non-registered variable can’t be accessed by the cloud.

You may want to read up on Particle.variable()

1 Like

that did the trick! thank you so very much.

1 Like