How to add Blynk to my MPU-6050 "Movement Detector"

@PeteKnight Now I’ve got it working but its giving me constant earth quakes. :sweat_smile:

Stop moving than :smiley:

1 Like

@Lichtsignaal

:sweat_smile::sweat_smile:
I’m at work now, on site, using work SSID and Password now. Shhhh. :wink:
There’s large plant here, I’m pretty sure that’s whats setting it off. Need to adjust the sensitivity.

@PeteKnight Well that seems to have solved it. Now, how to include telemetry. :thinking:
And thank you to @isiktantanis for supplying the code. :+1:

#define BLYNK_PRINT Serial
#include <Wire.h>
#include <ESP8266WiFi.h>`
#include <BlynkSimpleEsp8266.h>
#include <MPU6050.h>
#define minval -1
#define maxval 1

MPU6050 mpu;

char auth[] = "";
char ssid[] = "";
char pass[] = "";

#define PIN_UPTIME V14

  BLYNK_READ(PIN_UPTIME)
{
  
  Vector rawGyro = mpu.readRawGyro();

  Vector normGyro = mpu.readNormalizeGyro();


if(normGyro.XAxis > maxval || normGyro.XAxis < minval && normGyro.YAxis > maxval || normGyro.YAxis  < minval && normGyro.ZAxis > maxval || normGyro.ZAxis  < minval) {
   
  Blynk.virtualWrite(V14, "Bike Movement");
  Blynk.notify("Bike Theft");

}

 else{
   Blynk.virtualWrite(V14, "Bike Secure");
 }
 
  }

void setup()
{
 
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);

while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G))

  { 
   Blynk.notify("Earthquake sensor stopped working.");

   delay(500);
    }

   mpu.setThreshold(3); 
}

void loop()
{
  Blynk.run();
}

No movement

Movement detected.

Some scumbag is trying his luck.

2 Likes

If I recall correctly, BLYNK_READ() only works when the App is active, since the associated Widget determines the call frequency. So if your project is ever offline, so is your motion monitoring.

Eventually you might want to switch the function to a BLYNK_WRITE() function, set the Widget to PUSH and setup a timer to call the function every few seconds.

Either way, you will also want to have a notification flag and flag reset on timer so you don’t exceed the 1 notification per 15 seconds allotment, if the sensor is constantly detecting motion.

1 Like

@Gunner But if I had my own local server this allotment wouldn’t apply and I could mess around without annoying anyone?

And, I need to be able to turn off the “bike alarm” when I’m riding the bike so by turning off the project it disables the alarm, allotment time not exceeded?

I think it is more about not overloading the Cloud Server… So yes, setting up a Local Server is an option (I don’t think is has the Notification limitation), but if you think a few lines of code to change what you have now is tricky… :stuck_out_tongue_winking_eye:

Easily done with another enable/disable flag and Button Widget.

Do it whatever way you want, but remember to keep the Project started and the App at least active in the background, else no alarm, whereas the BLYNK_WRITE() method is the more common way and always active… as in the MCU itself could have an alarm that is triggered after say two or three unacknowledged notifications.

You are so going to love the options you can have with Blynk :smiley:

1 Like

I’ve only been doing this 3 weeks, ALL code is tricky to me. :confounded:

For you maybe, I’ll give it a try. :thinking:

I already do and I’ve only skimmed the surface.

NO! not at all … I constantly have to look up syntax, references, etc. check past code, test, test, and test again. Can take me hours to get something, relatively simple, working properly :weary:

But that’s where we can all help… since you are actually trying and not just begging like some noobs :stuck_out_tongue_winking_eye:

Here is some rough (untested) example code…

int MotionOnFlag;
int LimitationFlag;

BLYNK_WRITE(Vx) {  // Motion Alarm ON/OFF Button set to SWITCH mode
  MotionOnFlag = param.asInt();
}

BLYNK_WRITE(Vy) {  // Motion sensor read
  if (MotionOnFlag == 1 && LimitationFlag == 0) {  // Check if both ON and that limit flag is inactive
    LimitationFlag = 1; // Set time limit flag for notifications
    timer.setTimeout(16000L, LimitationFlagReset);  // Start 16 second timer for limiting flag reset
    // do sensor reading etc.
    // send notification if needed
  } else {
    //do nothing
  }
}

Void LimitationFlagReset() {
  LimitationFlag = 0;  // Reset limitation flag
}
1 Like

My code was full of syntax errors… I think? I fixed them all :stuck_out_tongue_winking_eye:

1 Like

Strangely, I feel better knowing that.

Fortunately its raining here again, pretty much all of the site is flooded. So I’m hiding away in the office(a large shipping container) and most others have gone home. I’m doing very little work wise because of the weather and I’m engrossed in my new found hobby(Blynk and coding). So now that you’ve just supplied me with more interesting code. I shall abandon site and retreat to the sanctuary of my dinning room table and a beer at home to see if I can do as you’ve suggested. Wish me luck. :beer:

2 Likes

I am not just full of silly ideas… wait, that didn’t sound right :thinking:

1 Like

Everything I do just lately is full of syntax errors, seeing them everywhere. I’m sure my BEST(bacon, sausage, egg and tomato) breakfast sandwich was full of them. :crazy_face:

I suspect someone is putting me to test? :thinking:

@Gunner OK, I’m home now and it’s compiling, but no “Earth Quake”, so what have I done wrong? :grimacing:

 #define BLYNK_PRINT Serial
#include <Wire.h>
#include <ESP8266WiFi.h>`
#include <BlynkSimpleEsp8266.h>
#include <MPU6050.h>
#define minval -1
#define maxval 1

MPU6050 mpu;

char auth[] = "060348058fd549f083b9b649e6784852";
char ssid[] = "BTHub6-P38S";
char pass[] = "wAwhd7rKg3cT";

int MotionOnFlag;
int LimitationFlag;

  BlynkTimer timer;

  BLYNK_WRITE(V15) {  // Motion Alarm ON/OFF Button set to SWITCH mode
  MotionOnFlag = param.asInt();
}

  BLYNK_WRITE(V14) {  // Motion sensor read
  if (MotionOnFlag == 1 && LimitationFlag == 0) {  // Check if both ON and that limit flag is inactive
    LimitationFlag = 1; // Set time limit flag for notifications
    timer.setTimeout(16000L, LimitationFlagReset);  // Start 16 second timer for limiting flag reset
    // do sensor reading etc.
    // send notification if needed
  } else {
    //do nothing
  }
}

  void LimitationFlagReset() {
  LimitationFlag = 0;  // Reset limitation flag
  
  Vector rawGyro = mpu.readRawGyro();

  Vector normGyro = mpu.readNormalizeGyro();


if(normGyro.XAxis > maxval || normGyro.XAxis < minval && normGyro.YAxis > maxval || normGyro.YAxis  < minval && normGyro.ZAxis > maxval || normGyro.ZAxis  < minval) {
   
  Blynk.virtualWrite(V14, "Bike Movement Detected");
  Blynk.notify("BIKE BEING STOLEN!");

}
 else{
   Blynk.virtualWrite(V14, "Bike Secure");
 }
 
  }

void setup()
{
 
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);

while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G))

  { 
   Blynk.notify("BIKE ALARM NOT WORKING!");

   delay(500);
    }

   mpu.setThreshold(3); 
}

void loop()
{
  Blynk.run();
}

This is where you put all your other sensor code… inside this ‘BLYNK_WRITE()’ function instead of in your BLYNK_READ() function. Not in the reset loop.

1 Like

Ha… and this is where I goof… it shouldn’t even be a BLYNK_WRITE() function, just a void somthingorother() loop… and then called by a timer :stuck_out_tongue:

Give me a moment, or 100 moments, and I will see if I can whip it into shape :stuck_out_tongue_winking_eye:

1 Like

:scream::scream::scream::rofl:

:+1: :beer::beer:

Sometimes I get bored of waiting for things to compile so I open another Arduino IDE and start messing again while the other is compiling. If both are compiling my PC sounds like its hoovering up. :sweat_smile:

Do I need a better PC?

Always… and with lots of RAM and at least dual monitors so you can have multiple projects and references open at the same time… like this :stuck_out_tongue_winking_eye: and yes that is 6 different IDEs and three Chrome windows with multi tabs… all so I can hopefully find the info, reference, code snippet, etc. that I know I need, but can’t remember :blush:

Unfortunately I only have a older, slower and 8GB MAX system


And speaking of doing stuff…

Here is your code. Sorry, I had to shuffle things around to fit my brain :smiley:

It compiles, yes!.. but works? Well that is another story :thinking:

#define BLYNK_PRINT Serial
#include <Wire.h>
#include <ESP8266WiFi.h>`
#include <BlynkSimpleEsp8266.h>
#include <MPU6050.h>
#define minval -1
#define maxval 1

MPU6050 mpu;

char auth[] = "060348058fd549f083b9b649e6784852";
char ssid[] = "BTHub6-P38S";
char pass[] = "wAwhd7rKg3cT";
int MotionOnFlag;
int LimitationFlag;

BlynkTimer timer;

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  timer.setInterval(1000L, MotionSensorRead); // run the scan for motion function loop
  while (!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G))
  {
    Blynk.notify("BIKE ALARM NOT WORKING!");
    delay(500);
  }
  mpu.setThreshold(3);
}

void loop()
{
  Blynk.run();
}



BLYNK_WRITE(V15) {  // Motion Alarm ON/OFF Button set to SWITCH mode
  MotionOnFlag = param.asInt();
}


void LimitationFlagReset() {
  LimitationFlag = 0;  // Reset limitation flag
}


void MotionSensorRead() {  // Motion sensor read
  if (MotionOnFlag == 1 && LimitationFlag == 0) {  // Check if both ON and that limit flag is inactive
    LimitationFlag = 1; // Set time limit flag for notifications
    timer.setTimeout(16000L, LimitationFlagReset);  // Start 16 second timer for limiting flag reset
    Vector rawGyro = mpu.readRawGyro();
    Vector normGyro = mpu.readNormalizeGyro();
    if (normGyro.XAxis > maxval || normGyro.XAxis < minval && normGyro.YAxis > maxval || normGyro.YAxis  < minval && normGyro.ZAxis > maxval || normGyro.ZAxis  < minval) {
      Blynk.virtualWrite(V14, "Bike Movement Detected");
      Blynk.notify("BIKE BEING STOLEN!");
    }
    else {
      Blynk.virtualWrite(V14, "Bike Secure");
    }
  } else {
    //do nothing
  }
}

Remember to Add in a Button/Switch on V15 and change V14 to PUSH mode.

1 Like

@Gunner Compiles, uploads, connects to Blynk, app shows it online, then nothing. I can’t figure out why, whats stopping it talking to the app? :exploding_head:

#define BLYNK_PRINT Serial
#include <Wire.h>
#include <ESP8266WiFi.h>`
#include <BlynkSimpleEsp8266.h>
#include <MPU6050.h>
#define minval -1
#define maxval 1

MPU6050 mpu;

char auth[] = "060348058fd549f083b9b649e6784852";
char ssid[] = "BTHub6-P38S";
char pass[] = "wAwhd7rKg3cT";
int MotionOnFlag;
int LimitationFlag;

  BlynkTimer timer;

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  timer.setInterval(1000L, MotionSensorRead); // run the scan for motion function loop
  while (!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G))
  {
    Blynk.notify("BIKE ALARM NOT WORKING!");
    delay(500);
  }
  mpu.setThreshold(3);
}

  BLYNK_WRITE(V15) {  // Motion Alarm ON/OFF Button set to SWITCH mode
  MotionOnFlag = param.asInt();
}

  void LimitationFlagReset() {
  LimitationFlag = 0;  // Reset limitation flag
}

  void MotionSensorRead() {  // Motion sensor read
  if (MotionOnFlag == 1 && LimitationFlag == 0) {  // Check if both ON and that limit flag is inactive
    LimitationFlag = 1; // Set time limit flag for notifications
    timer.setTimeout(16000L, LimitationFlagReset);  // Start 16 second timer for limiting flag reset
    Vector rawGyro = mpu.readRawGyro();
    Vector normGyro = mpu.readNormalizeGyro();
    if (normGyro.XAxis > maxval || normGyro.XAxis < minval && normGyro.YAxis > maxval || normGyro.YAxis  < minval && normGyro.ZAxis > maxval || normGyro.ZAxis  < minval) {
    Blynk.virtualWrite(V14, "Bike Movement Detected");
    Blynk.notify("BIKE BEING STOLEN!");
  }
}  else {
    Blynk.virtualWrite(V14, "Bike Secur");
  }

}

void loop()
{
  Blynk.run();
}

since I don’t have that sensor, this is just a guess… but…

These two parts ^ mean that it will only read from the sensor if your Button on V15 (set to switch mode) is ON

Also change this ^ to this, as it will make sure the limit flag is off at boot.

int LimitationFlag=0;
1 Like