Events to the cloud

Hi,

Im trying to send event to the cloud when compass direction changes. However the event is only seen in the cloud just once after login. My code:

@Override
    public void onSensorChanged(SensorEvent event) {

        if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {
           
            SensorManager.getRotationMatrixFromVector(rotationMatrix, event.values);

          
            azimuth = (int) ((Math.toDegrees(SensorManager.getOrientation(rotationMatrix, orientation)[0]) + 360) % 360);


                if (azimuth >= 337 && azimuth <= 360 || azimuth >= 0 && azimuth <= 22) {
                    n = "N"; // 0 deg
                    try {

                        ParticleCloudSDK.getCloud().publishEvent("north", "N", ParticleEventVisibility.PUBLIC, 60);
                    } catch (ParticleCloudException e) {
                        e.printStackTrace();
                    }
                } else if (azimuth > 22 && azimuth < 68) {
                    n = "NE"; //45 deg

                    try {
                        ParticleCloudSDK.getCloud().publishEvent("north", "NE", ParticleEventVisibility.PUBLIC, 60);
                    } catch (ParticleCloudException e) {
                        e.printStackTrace();
                    }
                } else if (azimuth >= 68 && azimuth <= 112) {
                    n = "E"; // 90deg
                    try {
                        ParticleCloudSDK.getCloud().publishEvent("north", "E", ParticleEventVisibility.PUBLIC, 60);
                    } catch (ParticleCloudException e) {
                        e.printStackTrace();
                    }
                } else if (azimuth > 112 && azimuth < 157) {
                    n = "SE"; //135deg
                    try {
                        ParticleCloudSDK.getCloud().publishEvent("north", "SE", ParticleEventVisibility.PUBLIC, 60);
                    } catch (ParticleCloudException e) {
                        e.printStackTrace();
                    }
                } else if (azimuth >= 157 && azimuth <= 203) {
                    n = "S"; //180deg
                    try {
                        ParticleCloudSDK.getCloud().publishEvent("north", "S", ParticleEventVisibility.PUBLIC, 60);
                    } catch (ParticleCloudException e) {
                        e.printStackTrace();
                    }
                } else if (azimuth > 203 && azimuth < 248) {
                    n = "SW"; //225deg
                    try {
                        ParticleCloudSDK.getCloud().publishEvent("north", "SW", ParticleEventVisibility.PUBLIC, 60);
                    } catch (ParticleCloudException e) {
                        e.printStackTrace();
                    }
                }

                if (azimuth >= 248 && azimuth <= 293) {
                    n = "W"; //270deg
                    try {
                        ParticleCloudSDK.getCloud().publishEvent("north", "W", ParticleEventVisibility.PUBLIC, 60);
                    } catch (ParticleCloudException e) {
                        e.printStackTrace();
                    }
                }

                if (azimuth > 293 && azimuth < 337) {
                    n = "NW";//315deg
                    try {
                        ParticleCloudSDK.getCloud().publishEvent("north", "NW", ParticleEventVisibility.PUBLIC, 60);
                    } catch (ParticleCloudException e) {
                        e.printStackTrace();
                    }
                }
                
                   //on the phone screen tle line below show correct info
                txt_azimuth.setText("Azimuth : " + Integer.toString(azimuth) + "° " + n);

            }
        }

Why the events are not published on sensor change. Azimuth and N values on the phone screen work correctly.

I don’t see anything that would limit the rate that you are publishing. There is a rate limit of one publish per second. You can burst out 4, but then after that, you need to wait 4 more seconds to publish again. It looks like that code will continuously publish data, which will hit the rate limit and no more publishes will go out until you stop publishing.

It also looks like as the orientation changes even slightly, you’ll keep transmitting, for example “N” over and over, even though it did not change. You probably only want to publish when the ordinal direction string changes, not when the rotation vector changes.

1 Like

Ideally I would like to get events published with the status of orientation once per second. I guess it’s more Android question how to get sensor readings at such rate.