Storing last eight hours of magnetometer and accelerometer data

magnet.rar

Hello,

How can I store last X hours of magnetometer and accelerometer data, I tried logging it:

import Toybox.Graphics;
import Toybox.WatchUi;
import Toybox.System;
import Toybox.Timer;
import Toybox.Sensor;

class magnetView extends WatchUi.View {
    var dataTimer = new Timer.Timer();
    var lastAccel;
    var lastMag;

    function initialize() {
        View.initialize();
        lastAccel = [0, 0, 0];  // Initialize last accelerometer reading
        lastMag = [0, 0, 0];    // Initialize last magnetometer reading
    }

    // Load your resources here
    function onLayout(dc as Dc) as Void {
        setLayout(Rez.Layouts.MainLayout(dc));
    }

    // Called when this View is brought to the foreground. Restore
    // the state of this View and prepare it to be shown. This includes
    // loading resources into memory.
    function onShow() as Void {
        if (dataTimer == null) {
            dataTimer = new Timer.Timer();
            dataTimer.start(method(:updateSensors), 1000, true); // Update sensors every second
        }
    }

    // Update the view
    function onUpdate(dc as Dc) as Void {
        // Call the parent onUpdate function to redraw the layout
        View.onUpdate(dc);
    }

    // Called when this View is removed from the screen. Save the
    // state of this View here. This includes freeing resources from
    // memory.
    function onHide() as Void {
        dataTimer.stop();
    }

    // Method to update sensor data
    function updateSensors() as Void {
        var sensorInfo = Sensor.getInfo();
        if (sensorInfo has :accel && sensorInfo.accel != null) {
            lastAccel = sensorInfo.accel;
        }
        if (sensorInfo has :mag && sensorInfo.mag != null) {
            lastMag = sensorInfo.mag;
        }
        System.println("Accelerometer: " + lastAccel[0] + ", " + lastAccel[1] + ", " + lastAccel[2]);
        System.println("Magnetometer: " + lastMag[0] + ", " + lastMag[1] + ", " + lastMag[2]);
    }
}

but nothing got saved.
  • There are 2 things that you DON'T do in your code: 1. you don't display anything (onUpdate is empty), 2. you don't save anything anywhere (unless you mean the variables lastAccel, lastMag)

  • import Toybox.Graphics;
    import Toybox.WatchUi;
    import Toybox.System;
    import Toybox.Timer;
    import Toybox.Sensor;
    
    class magnetView extends WatchUi.View {
        var dataTimer = new Timer.Timer();
        var lastAccel;
        var lastMag;
    
        function initialize() {
            View.initialize();
            lastAccel = [0, 0, 0];  // Initialize last accelerometer reading
            lastMag = [0, 0, 0];    // Initialize last magnetometer reading
        }
    
        // Load your resources here
        function onLayout(dc as Dc) as Void {
            if (dataTimer == null) {
                dataTimer = new Timer.Timer();
                dataTimer.start(method(:updateSensors), 1000, true); // Update sensors every second
            }
            setLayout(Rez.Layouts.MainLayout(dc));
        }
    
        // Called when this View is brought to the foreground. Restore
        // the state of this View and prepare it to be shown. This includes
        // loading resources into memory.
        function onShow() as Void {
    
            if (dataTimer == null) {
                dataTimer = new Timer.Timer();
                dataTimer.start(method(:updateSensors), 1000, true); // Update sensors every second
            }
        }
    
        // Update the view
        function onUpdate(dc as Dc) as Void {
            // Call the parent onUpdate function to redraw the layout
            if (dataTimer == null) {
                dataTimer = new Timer.Timer();
                dataTimer.start(method(:updateSensors), 1000, true); // Update sensors every second
            }
            View.onUpdate(dc);
        }
    
        // Called when this View is removed from the screen. Save the
        // state of this View here. This includes freeing resources from
        // memory.
        function onHide() as Void {
            if (dataTimer == null) {
                dataTimer = new Timer.Timer();
                dataTimer.start(method(:updateSensors), 1000, true); // Update sensors every second
            }
            dataTimer.stop();
        }
    
        // Method to update sensor data
        function updateSensors() as Void {
            var sensorInfo = Sensor.getInfo();
            if (sensorInfo has :accel && sensorInfo.accel != null) {
                lastAccel = sensorInfo.accel;
            }
            if (sensorInfo has :mag && sensorInfo.mag != null) {
                lastMag = sensorInfo.mag;
            }
            System.println("Accelerometer: " + lastAccel[0] + ", " + lastAccel[1] + ", " + lastAccel[2]);
            System.println("Magnetometer: " + lastMag[0] + ", " + lastMag[1] + ", " + lastMag[2]);
        }
    }

    It's still not printing anything on the watch screen. How can I store these measurements?

  • start from the AccelMag example in the SDK