Varul Jain
Published

Axial Monitoring System Through Gyroscope Sensor (MPU6000)

In this blogtut, we made a simple but advanced online axial monitoring system using basic nodes of the Node-RED.

IntermediateFull instructions provided4 hours1,047
Axial Monitoring System Through Gyroscope Sensor (MPU6000)

Things used in this project

Hardware components

National Control Devices Gyroscope Sensor - MPU6000
×1
National Control Devices Adafruit Huzzah i2c Adapter
×1
Adafruit Huzzah Esp8266
×1
National Control Devices I2C cable
×1

Software apps and online services

Arduino IDE
Arduino IDE
Node-RED
Node-RED
Google Gmail

Story

Read more

Code

MPU6000_Axial_Monitoring

C/C++
#include <PubSubClient.h>
#include <ESP8266WiFi.h>
#include <Wire.h>

//Define I2C address
#define Addr 0x68

//Wifi Credentials
#define wifi_ssid "DcubeAirtel"
#define wifi_password "D@Airtel190"

//Define MQTT server and topics
#define mqtt_server "iot.eclipse.org"
#define X_topic "AccelX"
#define Y_topic "AccelY"
#define Z_topic "AccelZ"
#define XR_topic "RotateX"
#define YR_topic "RotateY"
#define ZR_topic "RotateZ"

WiFiClient espClient;
PubSubClient client;

//Global Variable
volatile float Xtopic, Ytopic, Ztopic, XRtopic, YRtopic, ZRtopic; // using volatile with variable - It tells the compiler that the value of the variable may change at any time--without any action being taken by the code the compiler finds nearby.
uint32_t timer;
double  yaw1,yaw2,yaw3; //These are the angles in the complementary filter(roll, pitch)
float rollangle,pitchangle;

// Setup
void setup() {
  Wire.begin(2,14);
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setClient(espClient);
//  //start a timer
//  timer = micros();
  }

//Wifi Setup
void setup_wifi() {
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(wifi_ssid);

  WiFi.begin(wifi_ssid, wifi_password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

//Reconnect
void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
      if (client.connect("ESP8266Client")) {
      Serial.println("connected");
    } 
    else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(500);
    }
  }
}


void loop()
{
//  delay(100);
//  Timer = millis();
//  while(millis()- Timer<=Interval)// use intervels as per mentioned earlier
//  {
  tempTask();
  delay(100);
//  }
  
  if (!client.connected()) {
    reconnect();
  }
 
  //Mentioned below directly executed in String url
  
  Serial.print("X Axis: ");
  Serial.println(String(Xtopic).c_str());
  client.publish(X_topic, String(Xtopic).c_str(), true);

  Serial.print("Y Axis: ");
  Serial.println(String(Ytopic).c_str());
  client.publish(Y_topic, String(Ytopic).c_str(), true);
  
  Serial.print("Z Axis: ");
  Serial.println(String(Ztopic).c_str());
  client.publish(Z_topic, String(Ztopic).c_str(), true);

  Serial.print("X Rotate: ");
  Serial.println(String(XRtopic).c_str());
  client.publish(XR_topic, String(XRtopic).c_str(), true);

  Serial.print("Y Rotate: ");
  Serial.println(String(YRtopic).c_str());
  client.publish(YR_topic, String(YRtopic).c_str(), true);

  Serial.print("Z Rotate: ");
  Serial.println(String(ZRtopic).c_str());
  client.publish(ZR_topic, String(ZRtopic).c_str(), true);
  client.loop();
  }
  

void tempTask()
{
   // Start I2C transmission
  Wire.beginTransmission(Addr);
  // Select gyroscope configuration register
  Wire.write(0x1B);
  // Full scale range = 2000 dps
  Wire.write(0x18);
  // Stop I2C transmission
  Wire.endTransmission();
  
  // Start I2C transmission
  Wire.beginTransmission(Addr);
  // Select accelerometer configuration register
  Wire.write(0x1C);
  // Full scale range = +/-16g
  Wire.write(0x18);
  // Stop I2C transmission
  Wire.endTransmission();
  
  // Start I2C transmission
  Wire.beginTransmission(Addr);
  // Select power management register
  Wire.write(0x6B);
  // PLL with xGyro reference
  Wire.write(0x01);
  // Stop I2C transmission
  Wire.endTransmission();
  delay(300);
  
  unsigned int data[6];

  // Start I2C transmission
  Wire.beginTransmission(Addr);
  // Select data register
  Wire.write(0x3B);
  // Stop I2C transmission
  Wire.endTransmission();
  
  // Request 6 bytes of data
  Wire.requestFrom(Addr, 6);
  
  // Read 6 byte of data 
  if(Wire.available() == 6)
  {
    data[0] = Wire.read();
    data[1] = Wire.read();
    data[2] = Wire.read();
    data[3] = Wire.read();
    data[4] = Wire.read();
    data[5] = Wire.read(); 
  }
  
  // Convert the data
  int xAccl = data[0] * 256 + data[1];
  int yAccl = data[2] * 256 + data[3];
  int zAccl = data[4] * 256 + data[5];

  // Start I2C transmission
  Wire.beginTransmission(Addr);
  // Select data register 
  Wire.write(0x43);
  // Stop I2C transmission
  Wire.endTransmission();
  
  // Request 6 bytes of data
  Wire.requestFrom(Addr, 6);
  
  // Read 6 byte of data 
  if(Wire.available() == 6)
  {
    data[0] = Wire.read();
    data[1] = Wire.read();
    data[2] = Wire.read();
    data[3] = Wire.read();
    data[4] = Wire.read();
    data[5] = Wire.read(); 
  }
  // Convert the data
  int xGyro = data[0] * 256 + data[1];
  int yGyro = data[2] * 256 + data[3];
  int zGyro = data[4] * 256 + data[5];
  
  float gyroX = xGyro/262.2;
  float gyroY = yGyro/262.2;
  float gyroZ = zGyro/262.2;

  float acclX = xAccl/4096;
  float acclY = yAccl/4096;
  float acclZ = zAccl/4096;

//  double dt = (double)(micros() - timer) / 1000000; //This line does three things: 1) stops the timer, 2)converts the timer's output to seconds from microseconds, 3)casts the value as a double saved to "dt".
//  timer = micros(); //start the timer again so that we can calculate the next dt.
  
//  the next two lines calculate the orientation of the accelerometer relative to the earth and convert the output of atan2 from radians to degrees
//  We will use this data to correct any cumulative errors in the orientation that the gyroscope develops.
//  rollangle=atan2(acclY,acclZ)*180/PI; // FORMULA FOUND ON INTERNET
//  pitchangle=atan2(acclX,sqrt(acclY*acclY+acclZ*acclZ))*180/PI; //FORMULA FOUND ON INTERNET

//  THE COMPLEMENTARY FILTER
//  This filter calculates the angle based MOSTLY on integrating the angular velocity to an angular displacement.
//  dt, recall, is the time between gathering data from the MPU6050.  We'll pretend that the 
//  angular velocity has remained constant over the time dt, and multiply angular velocity by 
//  time to get displacement.
//  The filter then adds a small correcting factor from the accelerometer ("roll" or "pitch"), so the gyroscope knows which way is down. 
//  roll = 0.99 * (roll+ gyroX * dt) + 0.01 * rollangle; // Calculate the angle using a Complimentary filter
//  pitch = 0.99 * (pitch + gyroY * dt) + 0.01 * pitchangle; 
  

  yaw3 = gyroZ;
  yaw2 = gyroY;
  yaw1 = gyroX;
  
  
  Xtopic = acclX;
  Ytopic = acclY;
  Ztopic = acclZ;
  XRtopic = yaw1; 
  YRtopic = yaw2;
  ZRtopic = yaw3;
  
  delay(10);
}

Node_Red_Flow(MQTT and Gmail)

JavaScript
[{"id":"70bb51d9.36fd7","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"90b72cb9.a576b","type":"mqtt in","z":"70bb51d9.36fd7","name":"","topic":"AccelX","qos":"2","broker":"2f70a0d2.a0ff7","x":149,"y":62,"wires":[["2caa73c6.f4668c","6b2455db.5dd54c"]]},{"id":"4b196317.f1353c","type":"mqtt in","z":"70bb51d9.36fd7","name":"","topic":"AccelY","qos":"2","broker":"2f70a0d2.a0ff7","x":212,"y":142,"wires":[["d878dabf.c44648","6b2455db.5dd54c"]]},{"id":"9121d6b9.db3b48","type":"mqtt in","z":"70bb51d9.36fd7","name":"","topic":"AccelZ","qos":"2","broker":"2f70a0d2.a0ff7","x":118,"y":225,"wires":[["9d9c16d2.ffefc8","6b2455db.5dd54c"]]},{"id":"d5454e1f.25e7c","type":"mqtt in","z":"70bb51d9.36fd7","name":"","topic":"RotateX","qos":"2","broker":"2f70a0d2.a0ff7","x":103,"y":319,"wires":[["445e5804.310f18","6b2455db.5dd54c"]]},{"id":"e936435a.bcbdb","type":"mqtt in","z":"70bb51d9.36fd7","name":"","topic":"RotateY","qos":"2","broker":"2f70a0d2.a0ff7","x":72,"y":435,"wires":[["d3579215.5808d","6b2455db.5dd54c"]]},{"id":"ca6d5386.ca764","type":"mqtt in","z":"70bb51d9.36fd7","name":"","topic":"RotateZ","qos":"2","broker":"2f70a0d2.a0ff7","x":245,"y":439,"wires":[["2821f222.561f7e","6b2455db.5dd54c"]]},{"id":"2caa73c6.f4668c","type":"ui_gauge","z":"70bb51d9.36fd7","name":"","group":"41966ddc.701f44","order":1,"width":0,"height":0,"gtype":"compass","title":"X","label":"units","format":"{{value}}","min":0,"max":"50","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":155,"y":20,"wires":[]},{"id":"d878dabf.c44648","type":"ui_gauge","z":"70bb51d9.36fd7","name":"","group":"214bf69f.fbceca","order":1,"width":0,"height":0,"gtype":"compass","title":"Y","label":"units","format":"{{value}}","min":0,"max":"50","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":210,"y":101,"wires":[]},{"id":"9d9c16d2.ffefc8","type":"ui_gauge","z":"70bb51d9.36fd7","name":"","group":"15c53cc2.e75d93","order":1,"width":0,"height":0,"gtype":"compass","title":"Z","label":"units","format":"{{value}}","min":0,"max":10,"colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":106,"y":175,"wires":[]},{"id":"445e5804.310f18","type":"ui_gauge","z":"70bb51d9.36fd7","name":"","group":"41966ddc.701f44","order":2,"width":0,"height":0,"gtype":"wave","title":"X Rotate","label":"units","format":"{{value}}","min":0,"max":"65536","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":125,"y":274,"wires":[]},{"id":"d3579215.5808d","type":"ui_gauge","z":"70bb51d9.36fd7","name":"","group":"214bf69f.fbceca","order":2,"width":0,"height":0,"gtype":"wave","title":"Y Rotate","label":"units","format":"{{value}}","min":0,"max":10,"colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":101,"y":389,"wires":[]},{"id":"2821f222.561f7e","type":"ui_gauge","z":"70bb51d9.36fd7","name":"","group":"15c53cc2.e75d93","order":2,"width":0,"height":0,"gtype":"wave","title":"Z Rotate","label":"units","format":"{{value}}","min":0,"max":10,"colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":220,"y":504,"wires":[]},{"id":"a32d75a0.856d38","type":"json","z":"70bb51d9.36fd7","name":"","property":"payload","action":"","pretty":false,"x":602,"y":181,"wires":[["8a10a1e5.28817","f246da29.c51b48"]]},{"id":"8a10a1e5.28817","type":"debug","z":"70bb51d9.36fd7","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":641,"y":118,"wires":[]},{"id":"6b2455db.5dd54c","type":"join","z":"70bb51d9.36fd7","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":",","joinerType":"str","accumulate":false,"timeout":"6","count":"6","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":482,"y":182,"wires":[["a32d75a0.856d38"]]},{"id":"f246da29.c51b48","type":"e-mail","z":"70bb51d9.36fd7","server":"smtp.gmail.com","port":"465","secure":true,"name":"EmailID","dname":"","x":676,"y":232,"wires":[]},{"id":"171b3d70.b15b43","type":"inject","z":"70bb51d9.36fd7","name":"Alignment Parameters","topic":"Your Alignment Parameters","payload":"","payloadType":"date","repeat":"60","crontab":"","once":true,"onceDelay":"60","x":416,"y":39,"wires":[["6b2455db.5dd54c"]]},{"id":"2f70a0d2.a0ff7","type":"mqtt-broker","z":"","name":"iot.eclipse.org","broker":"iot.eclipse.org","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"5","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"41966ddc.701f44","type":"ui_group","z":"","name":"X Axis","tab":"65f4b922.5ef4a8","order":1,"disp":true,"width":"6","collapse":false},{"id":"214bf69f.fbceca","type":"ui_group","z":"","name":"Y Axis","tab":"65f4b922.5ef4a8","order":2,"disp":true,"width":"6","collapse":false},{"id":"15c53cc2.e75d93","type":"ui_group","z":"","name":"Z Axis","tab":"65f4b922.5ef4a8","order":3,"disp":true,"width":"6","collapse":false},{"id":"65f4b922.5ef4a8","type":"ui_tab","z":"","name":"X","icon":"dashboard","order":2}]

MPU6000_Esp8266

Credits

Varul Jain

Varul Jain

12 projects • 13 followers
Beginner, Creator, IOT Developer, working with IOT Prototypes

Comments