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

I hate iPhones, zero prestige points from me. Take a family photo, need to put it on the PC to do a bit of editing, impossible unless I email to myself. Plug the cable in, nope, need iTunes. Bluetooth, nope. Text, yes but is a 10th of the original size. So yes, I’m a big fan of android.

2 Likes

Integrating Blynk into existing code has proven to be near impossible, as @PeteKnight said ealrier;

So, as I’m no coding guru my solution for now is to use two Wemos D1 Mini’s, 1 running my existing code and another running Blynk.
The first Wemos will deal with the MPU-6050 senor making all the calculations required and passing the results to the second Wemos which will bounce those results onto Blynk avoiding the need to restructure the original code.

Hands up everyone who thinks it will work? :thinking:

@PeteKnight As I’ve had little to no joy intergrating Blynk into my existing code I decided to have a go at what you suggested. It compiled and connected to Blynk but fails to detect an earth quake(me flicking the sensor). Bobs definitely not my uncle, in fact I think he hates me. :sweat_smile:

Post your code and details of which ESP you’re using and how you have it hooked-up. Also, what you’re seeing on the serial monitor.

Pete.

1 Like

@PeteKnight It compiles and loads ok, nothing on the serial monitor, only Blynk.
Looks like this is where he got his original code from:
https://circuitdigest.com/microcontroller-projects/arduino-earthquake-detector-alarm-circuit

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

MPU6050 mpu;

char auth[] = "060348058fd549f083b9b649e6784852";
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, "Earthquake!");
  Blynk.notify("Earthquake alert.");

}

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

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

@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