连接到MPU6050时,Nodemcu无法发送数据

时间:2018-06-26 06:27:11

标签: arduino arduino-ide nodemcu arduino-esp8266 mpu6050

默认情况下,我已将NodeMCU与MPU6050连接到引脚d1和d2。我可以连接到网络并发送随机数据,还可以使用两个不同的程序从MPU6050提取数据。但是,当我将这两个程序组合在一起时,数据仍在提取中,但无法通过网络发送。返回的Httpcode为-5,即“连接丢失”。 我的程序如下:

included wire.h 

include ESP8266WiFi.h

include ESP8266HTTPClient.h

void send_data(String data);

const char* ssid = "network name";

const char* password = "Password";

String data;

String ch;

String url = "http://192.168.0.10:3131/";

const int MPU=0x68;  // I2C address of the MPU-6050

int16_t AcX,AcY,AcZ,Tmp,gyX,gyY,gyZ;

void setup(){

  Wire.begin(); // sda, scl

  Wire.beginTransmission(MPU);

  Wire.write(0x6B);  // PWR_MGMT_1 register

  Wire.write(0);     // set to zero (wakes up the MPU-6050)

  Wire.endTransmission(true);

  Serial.begin(9600);

  Serial.println("Setup completato...");

Serial.println("Connecting ");

  WiFi.begin(ssid, password);
  // Show ... until connected:

  while (WiFi.status() != WL_CONNECTED) {

    delay(500);

    Serial.print(".");

  }



  Serial.println("WiFi connected");

  // Print the IP address of the device:

  Serial.println(WiFi.localIP());


}

void loop(){

  Wire.beginTransmission(MPU);

  Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)

  Wire.endTransmission(false);

  Wire.requestFrom(MPU,14,true);  // request a total of 14 registers

  AcX=Wire.read()<<8|Wire.read();  // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)    

  AcY=Wire.read()<<8|Wire.read();  // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)

  AcZ=Wire.read()<<8|Wire.read();  // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)

  Tmp=Wire.read()<<8|Wire.read();  // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)

  Serial.print("AcX = "); Serial.print(AcX);

  Serial.print(" | AcY = "); Serial.print(AcY);

  Serial.print(" | AcZ = "); Serial.print(AcZ);

  Serial.print(" | Tmp = "); Serial.println(Tmp/340.00+36.53);  //equation for 

temperature in degrees C from datasheet


  Wire.beginTransmission(0x68); //I2C address of the MPU

  Wire.write(0x43); //Starting register for Gyro Readings

  Wire.endTransmission();

  Wire.requestFrom(0x68,6,true); //Request Gyro Registers (43 - 48)

  while(Wire.available() < 6);

  gyX = Wire.read()<<8|Wire.read(); //Store first two bytes into accelX

  gyY = Wire.read()<<8|Wire.read(); //Store middle two bytes into accelY

  gyZ = Wire.read()<<8|Wire.read();

  Serial.print("gyX = "); Serial.print(gyX);

  Serial.print(" | gyY = "); Serial.print(gyY);

  Serial.print(" | gyZ = "); Serial.print(gyZ);

  String buf="";

  buf=buf+AcX+"|"+AcY+"|"+AcZ+"|"+gyX+"|"+gyY+"|"+gyZ+"|";

  send_data("this is data");

  delay(333);

}




  void send_data(String data){

   if (WiFi.status() == WL_CONNECTED) {

    HTTPClient http;

   Serial.println("Sending Data");

     http.begin(url+""+data);  // Request destination.

     int httpCode = http.GET(); // Send the request.

     Serial.println(httpCode);

     if (httpCode > 0) { //Check the returning code

       Serial.println("!!!!!!!!!!!!Data Posted!!!!!!!!!!");

     }

     else {

       Serial.println("Something baaaaaaad happened!");

     }
           //Close connection
         http.end();

   delay(2000);
   }

  } 

0 个答案:

没有答案