How to make the accelerometer on asset tracker work every times it has a change?

I try to use the accelerometer on asset tracker but it not plot on thingspeak every times I shake. As shown on the graph, it sometimes works but sometimes not.

And this is the value I set on the on the IDE

And on the last picture, I change the code from
void loop() {
// Check if there’s been a big acceleration
if(t.readXYZmagnitude() > accelThreshold ){
// Create a nice string with commas between x,y,z
String pubAccel = String::format("%d,%d,%d",t.readX(),t.readY(),t.readZ());

    // Send that acceleration to the serial port where it can be read by USB
    //Serial.println(pubAccel);
    Serial.println(t.readXYZmagnitude());
    
    // If it's set to transmit AND it's been at least delayMinutes since the last one...
    if(transmittingData && ((millis()-lastPublish) > (delayMinutes*60*1000))){
        lastPublish = millis();
        Particle.publish("A", pubAccel, 60, PRIVATE);
    }
    
}

}

I think you’re probably exceeding the publish rate limit of 1 per second (on average).

You set delayMinutes to a floating point number 0.005, but the variable type is an integer so the actual value will be 0, so the publishes are going out as fast as they can. If you exceed the publish rate, none of them will go out until you stop moving for a few seconds.

2 Likes

It would be nice if, in the future, you could post the entire code, as code, rather than a screenshot. That way, we can test it without having to type it all out again.
Furthermore, some code formatting would also be really appreciated. Have a look for ‘forum tips’ to see how you can achieve that.

1 Like

@rickkas7 @Moors7 Thank you so much for your help. Well, I change try to change from int to float and try to use delay instead of if loop. It get a better result. It detect almost every minute but still not every time I shake.

Without some code to look at, it’ll be very hard (impossible) to tell what’s going on.

Here it is

#include "ThingSpeak/ThingSpeak.h"
#include "AssetTracker/AssetTracker.h"

int transmittingData = 1;

// Used to keep track of the last time we published data
long lastPublish = 0;

// How many minutes minimum between publishes? 10+ recommended!
float delayMinutes = (0.005);

// Threshold to trigger a publish
// 9000 is VERY sensitive, 12000 will still detect small bumps
int accelThreshold = 1200;

// Creating an AssetTracker named 't' for us to reference
AssetTracker t = AssetTracker();

// A FuelGauge named 'fuel' for checking on the battery state
FuelGauge fuel;

// setup() and loop() are both required. setup() runs once when the device starts
// and is used for registering functions and variables and initializing things
void setup() {
    // Sets up all the necessary AssetTracker bits
    t.begin();
    
    // Opens up a Serial port so you can listen over USB
    Serial.begin(9600);
    
    // These three functions are useful for remote diagnostics. Read more below.
    Particle.function("aThresh",accelThresholder);
    Particle.function("tmode", transmitMode);
    Particle.function("batt", batteryStatus);
}

// loop() runs continuously
void loop() {
    
    
    // Check if there's been a big acceleration
    if(t.readXYZmagnitude() > accelThreshold ){
        // Create a nice string with commas between x,y,z
        String pubAccel = String::format("%d,%d,%d",t.readX(),t.readY(),t.readZ());
        
        // Send that acceleration to the serial port where it can be read by USB
        //Serial.println(pubAccel);
        Serial.println(t.readXYZmagnitude());
        
        // If it's set to transmit AND it's been at least delayMinutes since the last one...
        
            Particle.publish("Accelerometer", pubAccel, PRIVATE);
            delay(500);
        
        
    }
}

// Remotely change the trigger threshold!
int accelThresholder(String command){
    accelThreshold = atoi(command);
    return 1;
}

// Allows you to remotely change whether a device is publishing to the cloud
// or is only reporting data over Serial. Saves data when using only Serial!
// Change the default at the top of the code.
int transmitMode(String command){
    transmittingData = atoi(command);
    return 1;
}

// Lets you remotely check the battery status by calling the function "batt"
// Triggers a publish with the info (so subscribe or watch the dashboard)
// and also returns a '1' if there's >10% battery left and a '0' if below
int batteryStatus(String command){
    // Publish the battery voltage and percentage of battery remaining
    // if you want to be really efficient, just report one of these
    // the String::format("%f.2") part gives us a string to publish,
    // but with only 2 decimal points to save space
    Particle.publish("B", 
          "v:" + String::format("%.2f",fuel.getVCell()) + 
          ",c:" + String::format("%.2f",fuel.getSoC()),
          1, PRIVATE
    );
    // if there's more than 10% of the battery left, then return 1
    if(fuel.getSoC()>10){ return 1;} 
    // if you're running out of battery, return 0
    else { return 0;}
}

And this is the result

I'd take a second look at the following things if I were you:

// Threshold to trigger a publish
// 9000 is VERY sensitive, 12000 will still detect small bumps
int accelThreshold = 1200;

Particle.publish("Accelerometer", pubAccel, PRIVATE);
delay(500);


----------
And last, but not least:

[quote="Moors7, post:3, topic:27269"]
Furthermore, some code formatting would also be really appreciated. Have a look for 'forum tips' to see how you can achieve that.
[/quote]

So, you mean that the device can spend at least 1 second to detect the next event and the device threshold is between 9000-12000 and the most sensitivity is 9000, do I got the right understanding? And I have already look in the forum tip but I didn’t see the title that similar to mine. If you find it, could you please tell me.

I’m saying that there’s a limit in how many publishes you can make per second. That limit it documented as 1 per second, as mentioned by Rick as well. If you go over that limit, it will place you on a time-out until you get below the limit again. The loop runs extremely fast, so after that 500 millisecond delay, it will almost immediately publish again. Since 500 < 1000, you’re going over the limit, and thus will be rate-limited.

The sensitivity is mentioned to be EXTREMELY sensitive at 9000, and you’ve placed it at 1200. Again, 1200 < 9000 so it will be ridiculously sensitive to the point where I’m almost certain it would trigger randomly without actual movement.

As for the code formatting, it’s this one (which is the first hit if you search for those terms I mentioned…):

1 Like

Thank you sir