How do I link my MPU6050 to cayenne?

Hello, thank you for the link. I am able to connect the MPU6050 to nodemcu and am getting good data to the serial monitor. I have the MPU6050 connected to an ESP8266 board. At this point I am not sure how to modify the code to send data to Cayenne?

Using the following code:

#include <Wire.h>

// MPU6050 Slave Device Address
const uint8_t MPU6050SlaveAddress = 0x68;

// Select SDA and SCL pins for I2C communication 
const uint8_t scl = D6;
const uint8_t sda = D7;

// sensitivity scale factor respective to full scale setting provided in datasheet 
const uint16_t AccelScaleFactor = 16384;
const uint16_t GyroScaleFactor = 131;

// MPU6050 few configuration register addresses
const uint8_t MPU6050_REGISTER_SMPLRT_DIV   =  0x19;
const uint8_t MPU6050_REGISTER_USER_CTRL    =  0x6A;
const uint8_t MPU6050_REGISTER_PWR_MGMT_1   =  0x6B;
const uint8_t MPU6050_REGISTER_PWR_MGMT_2   =  0x6C;
const uint8_t MPU6050_REGISTER_CONFIG       =  0x1A;
const uint8_t MPU6050_REGISTER_GYRO_CONFIG  =  0x1B;
const uint8_t MPU6050_REGISTER_ACCEL_CONFIG =  0x1C;
const uint8_t MPU6050_REGISTER_FIFO_EN      =  0x23;
const uint8_t MPU6050_REGISTER_INT_ENABLE   =  0x38;
const uint8_t MPU6050_REGISTER_ACCEL_XOUT_H =  0x3B;
const uint8_t MPU6050_REGISTER_SIGNAL_PATH_RESET  = 0x68;

int16_t AccelX, AccelY, AccelZ, Temperature, GyroX, GyroY, GyroZ;

void setup() {
  Serial.begin(9600);
  Wire.begin(sda, scl);
  MPU6050_Init();
}

void loop() {
  double Ax, Ay, Az, T, Gx, Gy, Gz;
  
  Read_RawValue(MPU6050SlaveAddress, MPU6050_REGISTER_ACCEL_XOUT_H);
  
  //divide each with their sensitivity scale factor
  Ax = (double)AccelX/AccelScaleFactor;
  Ay = (double)AccelY/AccelScaleFactor;
  Az = (double)AccelZ/AccelScaleFactor;
  T = (double)Temperature/340+36.53; //temperature formula
  Gx = (double)GyroX/GyroScaleFactor;
  Gy = (double)GyroY/GyroScaleFactor;
  Gz = (double)GyroZ/GyroScaleFactor;

  Serial.print("Ax: "); Serial.print(Ax);
  Serial.print(" Ay: "); Serial.print(Ay);
  Serial.print(" Az: "); Serial.print(Az);
  Serial.print(" T: "); Serial.print(T);
  Serial.print(" Gx: "); Serial.print(Gx);
  Serial.print(" Gy: "); Serial.print(Gy);
  Serial.print(" Gz: "); Serial.println(Gz);

  delay(100);
}

void I2C_Write(uint8_t deviceAddress, uint8_t regAddress, uint8_t data){
  Wire.beginTransmission(deviceAddress);
  Wire.write(regAddress);
  Wire.write(data);
  Wire.endTransmission();
}

// read all 14 register
void Read_RawValue(uint8_t deviceAddress, uint8_t regAddress){
  Wire.beginTransmission(deviceAddress);
  Wire.write(regAddress);
  Wire.endTransmission();
  Wire.requestFrom(deviceAddress, (uint8_t)14);
  AccelX = (((int16_t)Wire.read()<<8) | Wire.read());
  AccelY = (((int16_t)Wire.read()<<8) | Wire.read());
  AccelZ = (((int16_t)Wire.read()<<8) | Wire.read());
  Temperature = (((int16_t)Wire.read()<<8) | Wire.read());
  GyroX = (((int16_t)Wire.read()<<8) | Wire.read());
  GyroY = (((int16_t)Wire.read()<<8) | Wire.read());
  GyroZ = (((int16_t)Wire.read()<<8) | Wire.read());
}

//configure MPU6050
void MPU6050_Init(){
  delay(150);
  I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_SMPLRT_DIV, 0x07);
  I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_PWR_MGMT_1, 0x01);
  I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_PWR_MGMT_2, 0x00);
  I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_CONFIG, 0x00);
  I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_GYRO_CONFIG, 0x00);//set +/-250 degree/second full scale
  I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_ACCEL_CONFIG, 0x00);// set +/- 2g full scale
  I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_FIFO_EN, 0x00);
  I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_INT_ENABLE, 0x01);
  I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_SIGNAL_PATH_RESET, 0x00);
  I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_USER_CTRL, 0x00);
}

have a look at this, Adding a New Device using MQTT for sending sensor data to cayenne.

I was successful connecting to Cayenne dashboard and sending data, thanks! Can you direct me to where I can get information on controlling Cayenne triggers so I can limit/control how often they send a text message once triggered. The way it is now when a trigger goes off Cayenne sends continuous text about every 1 second until the trigger condition no longer exists. Other than that, the trigger works great!

this feature to avoid trigger flooding is on roadmap adnd will be out soon.

any idea of when feature will be available?

cant give a ETA as there lot of features to be released.

can you post your code, i can provide a workaround for not flooding trigger.

Hello, any update on when the new trigger system will be available. I tried a work around in my code based on one of your posts. Can’t get it to work. I commented out some of the lines in the following to try and get proper data sent to the dashboard on at least one channel. I don’t think I’m setting up the “CAx” correctly in the int statements. I’m trying have the CAx variable get its value from the Ax that is being calculated in the code. Any help would be appreciated.

//int y;
//int x;
int CAx;
int a;

int TiltP = 6;// you need to set this value
unsigned long lastMillis = 0;

void setup() {
  Serial.begin(9600);
  Wire.begin(sda, scl);
  MPU6050_Init();
  Cayenne.begin(username, password, clientID, ssid, wifiPassword);
}

void loop() {
  Cayenne.loop();
  double Ax, Ay, Az, T, Gx, Gy, Gz;
  
  Read_RawValue(MPU6050SlaveAddress, MPU6050_REGISTER_ACCEL_XOUT_H);
  
  //divide each with their sensitivity scale factor
  Ax = (-100*((double)AccelX/AccelScaleFactor));
  Ay = (100*((double)AccelY/AccelScaleFactor));
  Az = (double)AccelZ/AccelScaleFactor;
  T = (double)Temperature/340+36.53; //temperature formula
  Gx = (double)GyroX/GyroScaleFactor;
  Gy = (double)GyroY/GyroScaleFactor;
  Gz = (double)GyroZ/GyroScaleFactor;
  
  int CAx = (Ax);
  Serial.print("Ax: "); Serial.print(Ax);
  Serial.print(" Ay: "); Serial.print(Ay);
  Serial.print(" Az: "); Serial.print(Az);
  Serial.print(" T: "); Serial.print(T);
  Serial.print(" Gx: "); Serial.print(Gx);
  Serial.print(" Gy: "); Serial.print(Gy);
  Serial.print(" Gz: "); Serial.println(Gz);
  send_data();
  trigger();

  //delay(2000);

  Cayenne.virtualWrite(1, CAx);
  //Cayenne.virtualWrite(2, Ay);
  //Cayenne.virtualWrite(3, T, TYPE_TEMPERATURE, UNIT_CELSIUS);
}

void send_data()
{
  if (CAx > TiltP)
  {
    a = 1;
    if (millis() - lastMillis > 10000) {
      lastMillis = millis();
      Cayenne.virtualWrite(4, CAx);
    }
  }
}

void trigger()
{
  if (CAx <= TiltP)
  {
    if (millis() - lastMillis > 10000)
    {
      lastMillis = millis();
      Cayenne.virtualWrite(5, CAx);
    }
    if ( a == 1)
    {
      Serial.println("Sending trigger");
      Cayenne.virtualWrite(6, CAx);
      a = 0;
    }
  }
}

i have made changes to your code. you need the previous widget. when you upload the below code it will automatically produce 2 value widget with channel 4 and channel 5. you need to add them by clicking on the “+”. Next you need to add your trigger to channel 5.

int y;
int x;
int CAx;
int a;
double Ax, Ay, Az, T, Gx, Gy, Gz;

int TiltP = 6;// you need to set this value
unsigned long lastMillis = 0;

void setup() {
  Serial.begin(9600);
  Wire.begin(sda, scl);
  MPU6050_Init();
  Cayenne.begin(username, password, clientID, ssid, wifiPassword);
}

void loop() {
  Cayenne.loop();

  Read_RawValue(MPU6050SlaveAddress, MPU6050_REGISTER_ACCEL_XOUT_H);

  //divide each with their sensitivity scale factor
  Ax = (-100 * ((double)AccelX / AccelScaleFactor));
  Ay = (100 * ((double)AccelY / AccelScaleFactor));
  Az = (double)AccelZ / AccelScaleFactor;
  T = (double)Temperature / 340 + 36.53; //temperature formula
  Gx = (double)GyroX / GyroScaleFactor;
  Gy = (double)GyroY / GyroScaleFactor;
  Gz = (double)GyroZ / GyroScaleFactor;

  int CAx = Ax;
  Serial.print("Ax: "); Serial.print(Ax);
  Serial.print(" Ay: "); Serial.print(Ay);
  Serial.print(" Az: "); Serial.print(Az);
  Serial.print(" T: "); Serial.print(T);
  Serial.print(" Gx: "); Serial.print(Gx);
  Serial.print(" Gy: "); Serial.print(Gy);
  Serial.print(" Gz: "); Serial.println(Gz);
  send_data();
  trigger();
}

void send_data()
{
  if (CAx > TiltP)
  {
    a = 1;
    if (millis() - lastMillis > 10000) {
      lastMillis = millis();
      Cayenne.virtualWrite(4, CAx);
    }
  }
}

void trigger()
{
  if (CAx <= TiltP)
  {
    if (millis() - lastMillis > 10000)
    {
      lastMillis = millis();
      Cayenne.virtualWrite(4, CAx);
    }
    if ( a == 1)
    {
      Serial.println("Sending trigger");
      Cayenne.virtualWrite(5, CAx);
      a = 0;
    }
  }
}

Thank you for response. I made your changes to code and still does not work. Dashboard created one value widget for channel 4 and only displays a value of 0 and does not change based on position of sensor. Data view screen display incoming lines of data only for channel 4 and a data value of 0. I checked the serial monitor and it shows the board is sending full data and values which do change based on position of sensor. As a test, I added back the line of code to send the Ay data to Dashboard using Cayenne,virtualWrite(2, Ay); A new value widget is created for channel 2 and it displays the proper Ay data and updates correctly based on any changes in the position of the sensor.

can you share the entire output of your serial monitor?

//This is the updated code

int y;
int x;
int CAx;
int a;
double Ax, Ay, Az, T, Gx, Gy, Gz;

int TiltP = 6;// you need to set this value
unsigned long lastMillis = 0;

void setup() {
Serial.begin(9600);
Wire.begin(sda, scl);
MPU6050_Init();
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
}

void loop() {
Cayenne.loop();

Read_RawValue(MPU6050SlaveAddress, MPU6050_REGISTER_ACCEL_XOUT_H);

//divide each with their sensitivity scale factor
Ax = (-100*((double)AccelX/AccelScaleFactor));
Ay = (100*((double)AccelY/AccelScaleFactor));
Az = (double)AccelZ/AccelScaleFactor;
T = (double)Temperature/340+36.53; //temperature formula
Gx = (double)GyroX/GyroScaleFactor;
Gy = (double)GyroY/GyroScaleFactor;
Gz = (double)GyroZ/GyroScaleFactor;

int CAx = Ax ;
Serial.print("Ax: “); Serial.print(Ax);
Serial.print(” Ay: “); Serial.print(Ay);
Serial.print(” Az: “); Serial.print(Az);
Serial.print(” T: “); Serial.print(T);
Serial.print(” Gx: “); Serial.print(Gx);
Serial.print(” Gy: “); Serial.print(Gy);
Serial.print(” Gz: "); Serial.println(Gz);
send_data();
trigger();

//Cayenne.virtualWrite(1, CAx);
Cayenne.virtualWrite(2, Ay);
//Cayenne.virtualWrite(3, T, TYPE_TEMPERATURE, UNIT_CELSIUS);
}

void send_data()
{
if (CAx > TiltP)
{
a = 1;
if (millis() - lastMillis > 10000) {
lastMillis = millis();
Cayenne.virtualWrite(4, CAx);
}
}
}

void trigger()
{
if (CAx <= TiltP)
{
if (millis() - lastMillis > 10000)
{
lastMillis = millis();
Cayenne.virtualWrite(4, CAx);
}
if ( a == 1)
{
Serial.println(“Sending trigger”);
Cayenne.virtualWrite(5, CAx);
a = 0;
}
}
}

Try this code and share the output serial

    int y;
    int x;
    int CAx;
    int a;
    double Ax, Ay, Az, T, Gx, Gy, Gz;
    #define CAYENNE_DEBUG;
    int TiltP = 0.2;// you need to set this value
    unsigned long lastMillis = 0;
    unsigned long lastMillis1 = 0;

    void setup() {
    Serial.begin(9600);
    Wire.begin(sda, scl);
    MPU6050_Init();
    Cayenne.begin(username, password, clientID, ssid, wifiPassword);
    }

    void loop() {
    Cayenne.loop();

    Read_RawValue(MPU6050SlaveAddress, MPU6050_REGISTER_ACCEL_XOUT_H);

    //divide each with their sensitivity scale factor
    Ax = (-100*((double)AccelX/AccelScaleFactor));
    Ay = (100*((double)AccelY/AccelScaleFactor));
    Az = (double)AccelZ/AccelScaleFactor;
    T = (double)Temperature/340+36.53; //temperature formula
    Gx = (double)GyroX/GyroScaleFactor;
    Gy = (double)GyroY/GyroScaleFactor;
    Gz = (double)GyroZ/GyroScaleFactor;

    int CAx = Ax ;
    Serial.print("Ax: “); Serial.print(Ax);
    Serial.print(” Ay: “); Serial.print(Ay);
    Serial.print(” Az: “); Serial.print(Az);
    Serial.print(” T: “); Serial.print(T);
    Serial.print(” Gx: “); Serial.print(Gx);
    Serial.print(” Gy: “); Serial.print(Gy);
    Serial.print(” Gz: "); Serial.println(Gz);
    send_data();
    trigger();
    if (millis() -  lastMillis1 > 10000){ 
    Cayenne.virtualWrite(1, CAx);
    Cayenne.virtualWrite(2, Ay);
    //Cayenne.virtualWrite(3, T, TYPE_TEMPERATURE, UNIT_CELSIUS);
    lastMillis1 = millis();
    }
    }
    void send_data()
    {
    if (CAx > TiltP)
    {
    a = 1;
    if (millis() - lastMillis > 10000) {
    Serial.println(“Sending value greater”);
    lastMillis = millis();
    Cayenne.virtualWrite(4, CAx);
    }
    }
    }

    void trigger()
    {
    if (CAx <= TiltP)
    {
    if (millis() - lastMillis > 10000)
    {
    Serial.println(“Sending value smaller”);
    lastMillis = millis();
    Cayenne.virtualWrite(4, CAx);
    }
    if ( a == 1)
    {
    Serial.println(“Sending trigger”);
    Cayenne.virtualWrite(5, CAx);
    a = 0;
    }
    }
    }

add this line #define CAYENNE_DEBUG; and share output.