es_jm
es_jm

Reputation: 33

How to send MPU6050 device data to IoT Hub

I am trying to send accelerometer data from MPU6050 which is connected to a ESP8266 to the Azure IoT Hub. I am using the Arduino IDE.

I followed this tutorial from Microsoft: https://learn.microsoft.com/en-gb/azure/iot-hub/iot-hub-arduino-huzzah-esp8266-get-started? which uses the DHT11 sensor and the esp8266. I also did this tutorial: http://archicode.be/esp8266-the-iot-prototyping-graal/ which shows which libraries and methods are required to connect the ESP8266 to Azure IoTHub. The Code which I used for testing the MPU6050 is here below and from https://playground.arduino.cc/Main/MPU-6050/#short

// MPU-6050 Short Example Sketch
// By Arduino User JohnChi
// August 17, 2014
// Public Domain
#include<Wire.h>
const int MPU_addr=0x68;  // I2C address of the MPU-6050
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
void setup(){
  Wire.begin();
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x6B);  // PWR_MGMT_1 register
  Wire.write(0);     // set to zero (wakes up the MPU-6050)
  Wire.endTransmission(true);
  Serial.begin(9600);
}
void loop(){
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
  Wire.endTransmission(false);
  Wire.requestFrom(MPU_addr,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)
  GyX=Wire.read()<<8|Wire.read();  // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
  GyY=Wire.read()<<8|Wire.read();  // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
  GyZ=Wire.read()<<8|Wire.read();  // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
  Serial.print("AcX = "); Serial.print(AcX);
  Serial.print(" | AcY = "); Serial.print(AcY);
  Serial.print(" | AcZ = "); Serial.print(AcZ);
  Serial.print(" | Tmp = "); Serial.print(Tmp/340.00+36.53);  //equation for temperature in degrees C from datasheet
  Serial.print(" | GyX = "); Serial.print(GyX);
  Serial.print(" | GyY = "); Serial.print(GyY);
  Serial.print(" | GyZ = "); Serial.println(GyZ);
  delay(333);
}

How and where do I include this code? Does anyone have maybe a good reference? I would appreciate any help! :)

Upvotes: 0

Views: 386

Answers (1)

Mark Radbourne
Mark Radbourne

Reputation: 653

Take a look at the code here: https://github.com/Azure-Samples/iot-hub-feather-huzzah-client-app/blob/ab3d6e46f1b94192f8c8a96fb7b47d714df99439/app/app.ino#L114. This is calling a function that returns a JSON string in the buffer passed as the second parameter. This function is defined here: https://github.com/Azure-Samples/iot-hub-feather-huzzah-client-app/blob/ab3d6e46f1b94192f8c8a96fb7b47d714df99439/app/message.ino#L42.

You would need to replace the content of readMessage with your code above to return your data in JSON form. However, you will need a little additional code. The code in loop has a 10ms delay and calls IoTHubClient_LL_DoWork on each iteration. For the Azure IoT SDK to work reliably this is required. Since you are only reading your sensor every 333ms you will need to track the time for yourself. For example, in loop you would only call readMessage each time loop has been executed 33 times. Something like this (quick and dirty for the sake of demonstration only):

static int messageCount = 1;
static int loopCounter = 0;
void loop()
{
    if (++loopCounter == 33)
    {
        if (!messagePending && messageSending)
        {
            char messagePayload[MESSAGE_MAX_LEN];
            bool temperatureAlert = readMessage(messageCount, messagePayload);
            sendMessage(iotHubClientHandle, messagePayload, temperatureAlert);
            messageCount++;
            delay(interval);
        }
        loopCounter = 0;
    }
    IoTHubClient_LL_DoWork(iotHubClientHandle);
    delay(10);
}

This would give you a reading every 330ms. You will also need to add your connection string too.

Upvotes: 2

Related Questions