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.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import java.util.Map;


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;

    protected void onCreate(Bundle savedInstanceState) {
        tv = findViewById(;
        tv.setText(String.valueOf(getIntent().getIntExtra(ARG_VALUE, 0)));

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

                new View.OnClickListener() {
                    public void onClick(View v){

                        // Do network work on background thread
                        Async.executeAsync(ParticleCloudSDK.getCloud(), new Async.ApiWork<ParticleCloud, Integer>() {
                            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;

                            public void onSuccess(@NonNull Integer i) { // this goes on the main thread


                            public void onFailure(@NonNull ParticleCloudException e) {



    public boolean onCreateOptionsMenu(Menu menu) {
        new MenuInflater(this).inflate(, menu);
        return (super.onCreateOptionsMenu(menu));

    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == {
            Intent intent = DeviceInfoActivity.buildIntent(ValueActivity.this, getIntent().getStringExtra(ARG_DEVICEID));
        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); 
void loop() {

digitalWrite(trigPin, LOW);

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

duration = pulseIn(echoPin, HIGH);

distance= duration*0.034/2;

Serial.print("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