Axial Monitoring System through Gyroscope Sensor (I2C Device) using Node-Red

Axial Monitoring System through Gyroscope Sensor (I2C Device) using Node-RedVarul JainBlockedUnblockFollowFollowingFeb 11The Compact I2C devices like Gyroscope Sensor can easily help you out in the axial monitoring system which detects the parameters and shares it to the user through MQTT and Email nodes of node red servicesIn this Blogtut we made some simple but advanced online axial monitoring system by using basic nodes of the node-red.

Hardware RequiredGyroscope Sensor — MPU6000Adafruit Huzzah I2C shieldAdafruit HuzzahI2C CableSoftwareArduino IDEGMAILNode redFor newbies and intermediates can check out the ESP8266 installation and resolution for the issues while installing the ESP8266 from here.

If you are not familiar with Node-Red Installation and using MQTT node in Node-Red, I suggest you check out my previous blogtut in which I have shared all the detailed information for Getting Started with Node-Red services.

Note: Kindly do not use the I/O and dashboard nodes in the flow page which is used in the previous blog.

We have mentioned about nodes which we used in this project in the below discussions.

About Gyroscope Sensor — MPU6000 I2C moduleThe Gyroscope sensor has been used a various technical field like Automotive, Industrial, Electronics and many more.

It unveils various kind of application in many Real-Time Concepts.

Not going very far, even our Smartphones consist of Gyroscope sensor which specially used to detect the shaking or vibration while using the camera, playing video games, phone rotates etc.

Using these sensors, we can increase the stability in various High-tech devices or a specialized Anti-skid System.

MPU6000 I2C based breakout board is 3-Axis I2C Gyroscope and 3-Axis I2C Accelerometer with full-scale ranges and has the Digital-Output of 6 or 9-Axis MotionFusion™ Data in Rotation Matrix.

The sensor can easily be connected to I2C adapters compatible with IoT development boards like Arduino, Raspberry Pi, Particle, Pycom etc.

and perform the functions by using different I2C libraries available for different programming language on Github.

CodeInitialize the Wire.

h file called as I2C Library especially use in Arduino IDE#include <Wire.

h>Initialize the I2C registers of sensor module which is specified to work with 2 wire protocol.

#define Addr 0x68Begin the I2C transmission and Initialize the baud rate as per the requirements for serial communication.

void setup() { Wire.

begin(2,14); Serial.

begin(115200); setup_wifi(); client.

setServer(mqtt_server, 1883); // Initialize MQTT Server client.

setClient(espClient); // Esp Client Setup }Request for 6 bytes of Data which we want to read from the sensor through I2C connection for X, Y, Z axis to read the axis as well as rotation// 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(); }If 2 bytes of data is available then use the mentioned below formula will help to convert the data bytes and display desired values// Convert the data for axis (x,y,z) int xAccl = data[0] * 256 + data[1]; int yAccl = data[2] * 256 + data[3]; int zAccl = data[4] * 256 + data[5]; // Convert the data gyro rotation int xGyro = data[0] * 256 + data[1]; int yGyro = data[2] * 256 + data[3]; int zGyro = data[4] * 256 + data[5];Manipulate the axis parameters as per the requirement through Sensitivity and resolution settings given in datasheet// Axis Data float acclX = xAccl/4096; float acclY = yAccl/4096; float acclZ = zAccl/4096; // Rotation Data in degrees per second float gyroX = xGyro/131; float gyroY = yGyro/131; float gyroZ = zGyro/131;Using Serial.

print you will be able to read the sensor data in the serial monitor screen.

Serial.

print("AccelX: ");Serial.

println(AccelX);Serial.

print("AccelY: ");Serial.

println(AccelX);Serial.

print("AccelZ ");Serial.

println(AccelX)Serial.

print("gyroX: ");Serial.

println(yaw1);Serial.

print("gyroY: ");Serial.

println(yaw2);Serial.

print("gyroZ: ");Serial.

println(yaw3)With the help of MPU6000 Sensor, I have figured out use YAW rotation in the individual axis which used to detect the rotation of the rigid body which changes the direction using mentioned below formulaNode-RedAfter working with an above different way to monitor Gyro Sensor(MPU6000) with Arduino, let’s detect the Axial direction and YAW rotation with Node-RED dashboard.

For installation of Node-Red and to install the different nodes in node red in windows operating and to use MQTT please go through the previous blogtut.

In this project, We will be using MQTT, Join, JSON nodes, email in Node-RED dashboard via ESP8266.

JOIN NodeWith help of JOIN node, we can join the output from more than one node at same and using this node we are going to club all the MQTT nodes with different topicsOpen the Node-RED using the command node-red in command promptCopy the loop-back IP mentioned in command prompt (Loopback IP:1880) and paste it on Web-browserClick on Manage Pallete by clicking top right “hamburger” iconSelect and drag the Join Node (from the left side of node options) in a flow page.

Double Click on Join nodeClick on MODESelect Manual modeClick on the CheckBox of “ and every subsequent message”Click DoneJoin all the MQTT node with Join nodeNote: You can also set as per your requirementJSON NodeJSON node will help us to convert the output of different nodes in JSON string which can be further used in many different waysSelect and drag the JSON Node (from the left side of node options) in a flow page.

Double Click on JSON nodeClick on Action drop-down and select Convert between JSON String & ObjectClick DoneEmail NodeUsing email output node we are going to send the email alert to the user and using it with node red is one of the best way by just entering the email id and credentials.

After drag and drop of email nodeDouble click on Email nodeWrite Email ID you want to sendFill out the user credential in Username and Password TextboxClick DoneInject NodeThe Most Importantly using and inject node to write down message to be shown in message title while sending the data in email id and will connect the inject node with JOIN node only which combines and make one string.

After Drag and Drop double click on Inject nodeEnter the Topic name what you want to display in the email titleClick on Repeat drop-down setup the intervals as per requirementsAlso, set the interval to automatically inject this nodeClick on DoneAfter that join all the nodes as mentioned below in the snapStart and reset the ESP8266 and open the Node-Red dashboard in your laptop or your smartphone with help my I2C Relay Blogtut which help you to open the Node-Red dashboard in your smartphone in a much secure way by making it work on the same network only.

Not only that you can also check the parameters which have sent on your Email ID also in JSON format.

OutputSucceedingThe MPU6000 I2C module will help you to do the axial monitoring of rigid equipment application especially in the field of Automotive, Industrial, AerodynamicsUsing this kind of sensor with node red services will help the user in many industrial applications and will give access to the user to monitor the data through the smart very securely within the networkAs while working with YAW rotation we have tested the steering Alignment of Car and with some consultation the data we made the rotation formula more accurateMPU6000 sensor will help to detect the car coordinates and detect the Acceleration of vehicles, Distance traveled from initial place to final place and keep you posted through email alerts as well as through google maps(using the cellular device to connect with internet)Not gone so vast but it does help aerospace vehicles to detect the roll, Pitch and Yaw Axis detection through physics laws and formulas specialized to use with these sensorsCodeMPU6000_Axial_Esp8266.

ino #include <PubSubClient.

h> #include <ESP8266WiFi.

h> #include <Wire.

h> //Define I2C address #define Addr 0x68 //Wifi Credentials #define wifi_ssid "ESSID" #define wifi_password "Password" //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/16.

4; float gyroY = yGyro/16.

4; float gyroZ = zGyro/16.

4; 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 = map(gyroZ, 0,4000, -90, 90); yaw2 = map(gyroY, 0,4000, -90, 90); yaw1 = map(gyroX, 0,4000, -90, 90); Xtopic = acclX; Ytopic = acclY; Ztopic = acclZ; XRtopic = yaw1; YRtopic = yaw2; ZRtopic = yaw3; delay(10); }NodeRedFlow[{“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}]CreditsControlEverythingCommunityRaw Data Conversion of MPU6000Join all the MQTT data in one JSON String.. More details

Leave a Reply