Translate

Monitor Kemiringan Menggunakan IP Local Sensor GY-521 (MPU-6050)

Monitor Kemiringan Menggunakan IP Local Sensor GY-521 (MPU-6050) 

         Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah alat yang dapat mengukur tingkat kemiringan suatu dataran atau benda dengan menggunakan sensor GY-521 / MPU-6050 dan mikrokontroller yang dipakai yaitu wemos D1 mini. alat ini bisa memonitor tingkat kemiringan dengan menggunakan IP Address local sehingga membutuhkan suatu router sebagai medianya pengiriman sinyalnya dikarenakan menggunakan wifi. untuk lebih jelasnya berikut adalah komponen dan kodingnya.


a. Komponen

 

b. Program Arduino IDE

#include<Wire.h>
#include <ESP8266WiFi.h>
#include <EEPROM.h>

const char *ssid = "TP-Link_65A4"; // Enter your WiFi name
const char *password = "";  // Enter WiFi password

//int ledPin = D0;
WiFiServer server(80);

//I2C Address for MPU6050
const int MPU=0x68;  // Can be 0x69 if AD0 Pin is True (VCC)

//Variables for raw values from MPU ADC
int AcX,AcY,AcZ,GyX,GyY,GyZ;
int16_t Tmp;

int minVal=0;
int maxVal=65536; //2^16 levels from MPU6050 ADC

int offsetTemp = 35; // from MPU datasheet
int offsetTempLevels = 521; // from MPU datasheet

void setup() {
 
  Serial.begin(9600); // Begin Serial communication
  Wire.begin();
  Wire.beginTransmission(MPU); // begin transmission with MPU address
  Wire.write(0x6B);
   
  //Start MPU-6050 sensor
  Wire.write(0);
  Wire.endTransmission(true);    

  delay(10);

//  pinMode(ledPin, OUTPUT);
//  digitalWrite(ledPin, LOW);

  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

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

  // Start the server
  server.begin();
  Serial.println("Server started");

  // Print the IP address
  Serial.print("Use this URL to connect: ");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");

}

void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }

  // Wait until the client sends some data
  Serial.println("new client");
  while(!client.available()){
    delay(1);
  }

  // Read the first line of the request
  String request = client.readStringUntil('\r');
  Serial.println(request);
  client.flush();

 Wire.beginTransmission(MPU);
  Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
  Wire.endTransmission(false);
  //Get data from Sensor (14 consecutive bytes)
  Wire.requestFrom(MPU,14,true);  
  //Read data byte by byte (16 bits is 8 bits | 8 bits)
  //Each value is composed by 16 bits (2 bytes)
  GyX=Wire.read()<<8|Wire.read();  //0x3B (GYRO_XOUT_H) & 0x3C (GYRO_XOUT_L)     
  GyY=Wire.read()<<8|Wire.read();  //0x3D (GYRO_YOUT_H) & 0x3E (GYRO_YOUT_L)
  GyZ=Wire.read()<<8|Wire.read();  //0x3F (GYRO_ZOUT_H) & 0x40 (GYRO_ZOUT_L)
  Tmp=Wire.read()<<8|Wire.read();  //0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
  AcX=Wire.read()<<8|Wire.read();  //0x43 (ACCEL_XOUT_H) & 0x44 (ACCEL_XOUT_L)
  AcY=Wire.read()<<8|Wire.read();  //0x45 (ACCEL_YOUT_H) & 0x46 (ACCEL_YOUT_L)
  AcZ=Wire.read()<<8|Wire.read();  //0x47 (ACCEL_ZOUT_H) & 0x48 (ACCEL_ZOUT_L)

  //Adapts to range from 0 to 2000 m/s^2
  AcX = map(AcX,minVal,maxVal,2000,0);
  AcY = map(AcY,minVal,maxVal,2000,0);
  AcZ = map(AcZ,minVal,maxVal,2000,0);
 
  //Adapts to range from -180º to 180º
  int xAng = map(GyX,minVal,maxVal,180,-180);
  int yAng = map(GyY,minVal,maxVal,180,-180);
  int zAng = map(GyZ,minVal,maxVal,180,-180);
   
  //Send X axis accelerometer value for serial monitor
  Serial.print("AcX = "); Serial.print(AcX);
   
  //Send Y axis accelerometer value for serial monitor
  Serial.print(" | AcY = "); Serial.print(AcY);

  //Send Z axis accelerometer value for serial monitor
  Serial.print(" | AcZ = "); Serial.print(AcZ);


  //Send Temperature value for serial
  //Calculates the temperature given the datasheet values (ºC)
  // 340 is the number of levels per ºC (from datasheet)
  Serial.print(" | Tmp = "); Serial.print(float(Tmp + offsetTempLevels)/340 + offsetTemp);   
  //Send X axis gyroscope angle value for serial monitor
  Serial.print(" | GyX = "); Serial.print(xAng);   
  //Send Y axis gyroscope angle value for serial monitor  
  Serial.print(" | GyY = "); Serial.print(yAng);   
  //Send Z axis gyroscope angle value for serial monitor
  Serial.print(" | GyZ = "); Serial.println(zAng);
 
  // Return the response
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println("Refresh: 1");
  client.println(""); //  do not forget this one
  client.println("<!DOCTYPE HTML>");
  client.println("<html>");
   
  client.println("<center>");
  client.println("<h1>");
  client.println("Monitoring Kemiringan");
  client.println("</h1>");
   
  client.println("<h3>");
   
  client.println("X = ");
  client.println(AcX);
  client.println("<br>");
 
  client.println("Y = ");
  client.println(AcY);
  client.println("<br>");
 
  client.println("Z = ");
  client.println(AcZ);
  client.println("<br>");
 
  client.println("</h3>");
  client.println("<a href = \"/?lighton\"\"><input type = 'button' value = 'DATA LOGGER' ></a>");
  client.println("<a href=" " download>");  
  client.println("</center>");
  client.println("</html>");

  delay(1);
  Serial.println("Client disonnected");
  Serial.println("");

  delay(300);

}


c. VIDEO HASILNYA


No comments:

Post a Comment