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

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

Also assuming you have done this ^

1 Like

Doh… and it would help if I had remembered to put in the timer call :stuck_out_tongue_winking_eye: :blush:

void loop()
{
  Blynk.run();
  timer.run();  <--- this is kinda important 
}
1 Like

And finally (and this one is on you :stuck_out_tongue_winking_eye: ) you have some misplaced brackets. Always count brackets… each { needs it’s matching } (which you have - else no compile) and in proper order (which you don’t quite have).

Should be like this…

Blynk.notify("BIKE BEING STOLEN!");
    }  else {
      Blynk.virtualWrite(V1, "Bike Secur");
    }
  }
}
1 Like

And finally… hopefully…

Add in this line to the end of your void setup()

  Blynk.syncVirtual(V15);

As this will check to see the ON/OFF status of your App button after boot… otherwise the App button may look ON, but the sketch wouldn’t know it until you toggled it manually.

And as you can see with my monolog of addendums… this isn’t easy for me ether :stuck_out_tongue:

1 Like

@Gunner A lot to take in, I’ll report back once my brain overload finishes … :exploding_head:

1 Like

I’m back, confused but back. :upside_down_face:

I’ve now have two NodeMCU’s sitting side by side, one running the original code and the other running the last version.

My original code, this one, is on the left NodeMCU.

#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[] = "df89c91d00b340aba902516b4e0cc9cd";
char ssid[] = "BTHub6-P38S";
char pass[] = "wAwhd7rKg3cT";

#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 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();
}

and gave me almost instant movement detected alerts.

But this last version that I added all the extra stuff too, which is on the right hand one.

#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=0;

  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 Secure");
    Blynk.syncVirtual(V15);
   }
 }
}

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

gives me movement detected alerts when it feels like it, sometimes not at all?

I’ve tried to figure out why, at first I thought is was because I was using the mobile network on my phone rather than my home WiFi and that was delaying things to the Blunk app, but that wasn’t it. The response from the original is instant, the response from the last version is whenever, if at all? The best I can come up with is there’s an obvious timing/delay issue that’s doing a great job of avoiding me.

@Gunner It would seem the MPU-5060 sensor requires a few minutes to stabilise. I’ve left both of them running side by side and now they react pretty much equally. I’ll Google it, maybe someone else has already posted something about it …

Edit.
According to Jeff Rowberg, the guy who wrote the i2cdevlib library for this MPU-6050 sensor, it does need to stabilise. The sensors algorithms wait for the sensor to be almost completely still before calibrating it. So if the sensor is moving even slightly during initialisation things go a bit weird until the algorithms get chance to do their thing.

This sensitivity and delay thing is annoying …

“gives me movement detected alerts when it feels like it, sometimes not at all?”

@Shadeyman, if you switch from “blynk read” to “blynk write” method, you also have to change the settings in the app for the respective widget (v15) from “xx seconds” to “push”!

ie:

for blynk read you have to set some seconds in the widget

for blynk write you have to set the widget to “push”

probably this is the cause?

2 Likes

A couple of things… The Blynk.syncVirtual(V15); needs to go in the void setup() loop (anywhere after Blynk.begin() ), not in your void MotionSensorRead() loop.

This is set to check for motion every 1000ms (1 second)… just for testing.

You can lower the 1000 to something like 250 (1/4 second for more frequent checks… just don’t go to low. or Blynk can disconnect from lack of attention from the NodeMCU :wink:

1 Like

@wanek the widget is set to push.

@Gunner Getting tired, its after 1am. Putting things in the wrong place, need some sleep, off to bed after posting this.

The first code I used was amazing, super sensitive and lightening fast responses.

But my latest code is hopeless in comparison.

I tweaked your code to scan at 10ms (100 times a second) and re-fixed the sync command location :smiley:

PS, On my workbench, I have “replaced” your sensor with a simple press button to test, and this code works perfectly for me now…

#define BLYNK_PRINT Serial
#include <Wire.h>
#include <ESP8266WiFi.h>`
#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 = 0;

BlynkTimer timer;

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  timer.setInterval(10L, MotionSensorRead); // run the scan for motion function loop 10x/second
  while (!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G))
  {
    Blynk.notify("BIKE ALARM NOT WORKING!");
    delay(500);
  }
  mpu.setThreshold(3);  
  Blynk.syncVirtual(V15);  // Checks current status of "power" button and updates this sketches awareness of it.
}

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");
    }
  }
}

void loop()
{
  Blynk.run();
  timer.run();
}
1 Like

Morning @Gunner
Thanks, I’ll give your tweaked version a try when I get to the office(shipping container. :rofl:).
Only 5 hours sleep, I’m late for work and i don’t care. Being the boss must have its perks, but I’m yet to find them… :upside_down_face:

Well, the good news is I made it to work and its not raining for a change. :smiley:

I had planned to study this tweaked version but:

The bad news is the tweaked version is no better, no change. Not sure why but those 100ms scans are just not making it to the Blynk app on my phone. Tried 3 different WiFi networks and my mobile network, all the same so I’m ruling out local network issues. It just doesn’t compare to the original code I posted. :cry:

I must have been completely out of it… I STILL put that darn sync command in the wrong place!!! :blush: when putting your motion sensor code back in…

That code was doing nothing but scanning the sensor…

But you now want Blynk, messages, buttons and all the wizzbang stuff, right? But I dropped the timing down to 10ms… if that is all you are running for sensors, then should be fine (not overloading Blynk or anything else).

I “fixed” my last posted code… try it one more time… please.

I blame it on my cuddly cat :innocent:… hard to write good code with only one hand… but, priorities :stuck_out_tongue_winking_eye: :cat:

2 Likes

Looks like I wasn’t the only one feeling tired after a long day.

I’ll blame the dog, not usually cuddly because he’s as mad as a March hare normally.

At work only 5 hours and my phone rings, its my neighbour. She informs me that someone has dumped tyres on my front lawn. :rage:
So I’m home now, not got much work done this week so far.

Turn’s out the tyre’s are mine, I forgot I’d ordered new boots for the Volvo and they turned up 2 days early. :roll_eyes:

I will, got to get the Volvo sorted first, then meet the wife for “afternoon tea” I think she calls it. I call it “two beers and a pork pie”. Back later, with good news I hope … :+1: