Temperature and Humidity together

Hi,
Using Android API, data from those two modules were streaming, however, at another attempt, the humidity ScheduledTask gets faulted. I was not calling stop/remove() to the ScheduledTask, could this be the reason? Every time now it is faulted. Is there a way to have the device a fresh start (or async reset/reboot?).
Tnx.

Comments

  • Not enough information.

  • edited February 3

    Hi,
    As said, I need to read from a single MTR device's Humid module, Temperature, and Settings Battery modules. The Humid module gets faulted at the ScheduledTask call, but the Temperature always can read. The Battery also not getting to the reading level all the time. Below is the code.

    private final Timer[] mTimers = new Timer[3];
    private Timer.ScheduledTask[] mScheduledTasks = new Timer.ScheduledTask[3];
    private Temperature.Sensor[] mTempSensors;
    private HumidityBme280 mHumidity;
    private Settings mSettings;    
    
    private void deviceSettings() {
        mMetaWearBoard.connectAsync().onSuccessTask(task -> mMetaWearBoard.getModule(Debug.class).resetAsync()).onSuccessTask(task -> {
                    mSettings = mMetaWearBoard.getModuleOrThrow(Settings.class);
                    mTimers[2] = mMetaWearBoard.getModuleOrThrow(Timer.class);
                    return mSettings.battery().addRouteAsync(source -> source.stream((data, env) -> {
                        Log.d(TAG_HEAD, TAG + ">>> battery state = " + data.value(Settings.BatteryState.class));
                    }));
                }).onSuccessTask( task -> {
                        Log.d(TAG_HEAD, TAG + ">>> setting battery routing continued to timer-scheduler");
                        return mTimers[2].scheduleAsync(TEMP_SAMPLE_PERIOD_MILLI, false, mSettings.battery()::read);
                }).continueWithTask( task -> {
                    if (task.isFaulted()) {
                        Log.w(TAG_HEAD, TAG + ">>> setting battery timer-scheduler faulted");
                        // recursive call helps?
                        //
                        Log.d(TAG_HEAD, TAG+">>> Recursive deviceSettings call ...");
                        deviceSettings();
                    } else {
                        mScheduledTasks[2] = task.getResult();
                        mScheduledTasks[2].start();
                        // Connect for temperature.
                        //
                        connectTemperature();
                    }
                    return null;
                });
    }
    
    private void connectTemperature() {
        mMetaWearBoard.connectAsync().onSuccessTask(task -> {
            mTemperature = mMetaWearBoard.getModuleOrThrow((Temperature.class));
            mTimers[0] = mMetaWearBoard.getModuleOrThrow(Timer.class);
            mTempSensors = mTemperature.findSensors(Temperature.SensorType.PRESET_THERMISTOR);
            if (mTempSensors != null) {
                return mTempSensors[0].addRouteAsync(source -> source.stream((Subscriber) (data, env) -> {
                    // Data Reading
                    //
                    final Float celsius = data.value(Float.class);
                    Log.d(TAG_HEAD, TAG + ">>> Temperature Reading: " + celsius);
                }));
            } else {
                Log.e(TAG_HEAD, TAG+">>> Sensor not connected.");
            }
        }).continueWithTask( task -> {
            if (task.isFaulted()) {
                Log.d(TAG_HEAD, TAG+">>> Device is faulted.");
                if (!mMetaWearBoard.isConnected()) {
                    Log.w(TAG_HEAD, TAG+">>> Device seems unconnected.");
                }
                // Retry
                //
                // recursive call helps?
                //
                Log.d(TAG_HEAD, TAG+">>> Recursive connectAndReadTemperature call ...");
                connectAndReadTemperature();
            } else {
                // Schedule sampling rate
                //
                Log.d(TAG_HEAD, TAG+">>> Route/stream created and device not faulted yet.");
                return mTimers[0].scheduleAsync(TEMP_SAMPLE_PERIOD_MILLI, false, mTempSensors[0]::read);
            }
            return null;
        }).continueWithTask(task -> {
            if (task.isFaulted()) {
                Log.d(TAG_HEAD, TAG+">>> Scheduler is faulted.");
                if (!mMetaWearBoard.isConnected()) {
                    Log.w(TAG_HEAD, TAG+">>> Device seems unconnected.");
                }
                // Retry
                //
                // recursive call helps?
                //
                Log.d(TAG_HEAD, TAG+">>> Recursive connectAndReadTemperature call ...");
                connectAndReadTemperature();
            } else {
                // Start temperature read scheduler
                //
                mScheduledTasks[0] = task.getResult();
                mScheduledTasks[0].start();
                Log.d(TAG_HEAD, TAG + ">>> Temperature scheduled-task start called.");
                // Now connect Humidity module
                //
                connectAndReadHumidity();
            }
            return null;
        });
    }
    
    private void connectAndReadHumidity() {
        mMetaWearBoard.connectAsync().onSuccessTask(task -> {
            mHumidity = mMetaWearBoard.getModuleOrThrow((HumidityBme280.class));
            mTimers[1] = mMetaWearBoard.getModuleOrThrow(Timer.class);
            return mHumidity.value().addRouteAsync(source -> source.stream((data, env) -> {
                // Data Reading
                //
                final Float gram = data.value(Float.class);
                Log.d(TAG_HEAD, TAG + ">>> Humidity Reading: " + gram);
            }));
        }).continueWithTask( task -> {
            if (task.isFaulted()) {
                Log.d(TAG_HEAD, TAG+">>> Route is faulted.");
                if (!mMetaWearBoard.isConnected()) {
                    Log.w(TAG_HEAD, TAG+">>> Device seems unconnected.");
                }
                // Retry
                //
                // recursive call helps?
                //
                Log.d(TAG_HEAD, TAG+">>> Recursive connectAndReadHumidity call ...");
                connectAndReadHumidity();
            } else {
                // Schedule sampling rate
                //
                return mTimers[1].scheduleAsync(TEMP_SAMPLE_PERIOD_MILLI, false, mHumidity.value()::read);
            }
            return null;
        }).continueWithTask(task -> {
            if (task.isFaulted()) {
                Log.d(TAG_HEAD, TAG+">>> Scheduler is faulted.");
                if (!mMetaWearBoard.isConnected()) {
                    Log.w(TAG_HEAD, TAG+">>> Device seems unconnected.");
                }
                // Retry
                //
                // recursive call helps?
                //
                Log.d(TAG_HEAD, TAG+">>> Recursive connectAndReadHumidity call ...");
                connectAndReadHumidity();
            } else {
                // Start temperature read scheduler
                //
                mScheduledTasks[1] = task.getResult();
                mScheduledTasks[1].start();
                Log.d(TAG_HEAD, TAG+">>> Temperature scheduled-task start called.");
            }
            return null;
        });
    }
    
  • edited February 4

    Try doing only one thing at a time before adding more sensors. Are you able to do just the humidity?

  • Yes, humidity now working by itself with no code change. But when I kept it running the whole night in data reading mode, in the morning I found the device faulted and no reading coming. Also, could you explain - try doing only one at a time? I should start one sensor and once data is coming, then start the second sensor - like this? Any workable code or tutorial is appreciated for multi-sensor operation and fault management.

  • No you can turn on the sensors at the same time, this was just to help you troubleshoot and make sure the issue is with your code and not the sensors.
    If the humidity works then that's good.
    Now try to add the temperature.

Sign In or Register to comment.