GY-521 Module with Mesh devices

I have a Particle Argon, and was trying to interface the GY 521 module with it. As a test i modified the example code, however i am not getting anything on the console. Any thoughts what i am doing wrong?

// This #include statement was automatically added by the Particle IDE.
#include "MPU6050.h"

int ledPin = D7;

// MPU variables:
MPU6050 accelgyro;
int16_t ax, ay, az;
int16_t gx, gy, gz;


bool ledState = false;
void toggleLed() {
    ledState = !ledState;
    digitalWrite(ledPin, ledState);
}

void setup() {
    pinMode(ledPin, OUTPUT);
    accelgyro.initialize();


}

void loop() {
    // read raw accel/gyro measurements from device
    accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
   
   /* Serial.print("a/g:\t");
    Serial.print(ax); Serial.print("\t");
    Serial.print(ay); Serial.print("\t");
    Serial.print(az); Serial.print("\t");
    Serial.print(gx); Serial.print("\t");
    Serial.print(gy); Serial.print("\t");
    Serial.println(gz);*/
    
    Particle.publish("LOOP", PRIVATE);
    Particle.publish("AX", String(ax), PRIVATE);
    
    
}

Can’t say about the sensor, but your publishes will most likely violate the rate limit of max. 4/second.
After the first four events your device will be silenced.

Thanks for the reply, i was gonna add a 5 seconds delay. However, i am not able to flash my code. I am using the WEB IDE, and although my Argon is connected and showing its connected to the cloud, the WEB IDE is showing it as offline. Any idea why?

image

You can put the device in Safe Mode or flash via USB.

Thanks! Putting into safe Mode did the trick. But not the delay

void loop() {
    // read raw accel/gyro measurements from device
    accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
   
   /* Serial.print("a/g:\t");
    Serial.print(ax); Serial.print("\t");
    Serial.print(ay); Serial.print("\t");
    Serial.print(az); Serial.print("\t");
    Serial.print(gx); Serial.print("\t");
    Serial.print(gy); Serial.print("\t");
    Serial.println(gz);*/
    
    Particle.publish("LOOP", PRIVATE);
    Particle.publish("AX", String(ax), PRIVATE);
    
    delay(5000);
}

console just stops here

From the console it show offline … SO something happened somewhere - my goto test is this - flash it and make sure local output works, console works and led blinks. Not great code but a quick sanity check!

SYSTEM_THREAD(ENABLED);

ApplicationWatchdog wd(30000, System.reset);

SerialLogHandler logHandler(LOG_LEVEL_WARN, // Logging level for non-application messages
{ 
    { "app", LOG_LEVEL_ALL } // Logging level for application messages
});


int led = D7;
const int delayTime =  10000; //

//--------------------------------------------------------------------
void setup() 
//--------------------------------------------------------------------
{
  const char str_deviceOS[] = "DeviceOS";
  const int sizeOfTempText = 128;
  char tempText[sizeOfTempText];

  Serial.begin(115200);
  waitFor(Serial.isConnected, 30000);

  Log.info("Started.....waiting for Cloud");
  if (waitFor(Particle.connected, 30000)) {
    Log.info("Started at %s", Time.timeStr().c_str());
  }
  else {
    Log.info("Started.....no Cloud");
  }
  snprintf(tempText, sizeOfTempText, "%s", System.version().c_str());
  Log.info("%s %s", str_deviceOS, tempText);
  Particle.publish(str_deviceOS, tempText, PRIVATE);

  pinMode(led, OUTPUT);

  pinMode(PWR, INPUT);
  pinMode(CHG, INPUT);
}

//--------------------------------------------------------------------
void loop() 
//--------------------------------------------------------------------
{
    wd.checkin(); // resets the AWDT count
    Serial.println(Time.timeStr()); //
    
    digitalWrite(led, !digitalRead(led));
    checkBattV();
    checkBattC();
    checkVUSB();
    delay(delayTime);
}

// ------------------------------------------------------------------------------
float checkBattV()
// ------------------------------------------------------------------------------
{   
    float battVoltage = analogRead(BATT) * 0.0011224;
    Particle.publish("battery_voltage", String::format("%.2f",battVoltage), PUBLIC);
    Log.info("Batt level:%.2fV",battVoltage);
    return battVoltage;
}
// ------------------------------------------------------------------------------
bool checkBattC()
// ------------------------------------------------------------------------------
{   
    bool charging = !digitalRead(CHG);
    Particle.publish("battery_charging", charging ? "yes" : "no", PUBLIC);
    Log.info("Batt %s charging", charging ? "is" : "not");
    return charging;
}
// ------------------------------------------------------------------------------
bool checkVUSB() 
// ------------------------------------------------------------------------------
{
    bool powerState = digitalRead(PWR);
    Log.warn(powerState ? "Power ON" : "Power FAIL");
    Particle.publish("power_state", powerState ? "on" : "off", PUBLIC);
    return powerState;
}



Here you go

ok so you now know your device is ok, the cloud is ok and the console is ok.

So comment out sections of your code until it works - I would start with this and add back in code until it fails …

Make the last line of your setup() a publish so you know the loop() will start next


void setup() {

// your start-up stuff

 Particle.publish("DEBUG", "setup() done", PRIVATE);

void loop() {
    // read raw accel/gyro measurements from device
    //accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
   
   /* Serial.print("a/g:\t");
    Serial.print(ax); Serial.print("\t");
    Serial.print(ay); Serial.print("\t");
    Serial.print(az); Serial.print("\t");
    Serial.print(gx); Serial.print("\t");
    Serial.print(gy); Serial.print("\t");
    Serial.println(gz);*/
    
    Particle.publish("DEBUG", loop()", PRIVATE);
    //Particle.publish("AX", String(ax), PRIVATE);
    
    delay(5000);
}

]
You should now have 5s message - then add back the serial.prints and check local serial output (making sure you have the vars ax/y/z and gx/y/z defined somewhere.

What happens?

Hey @jrjack, looking at the GY-521, it communicates via I2C, for that you need to initiate it first. I would take the original code you posted, and in the setup() add the line Wire.begin().

And secondly, as @ScruffR pointed out, add some delay to your publishing messages.

1 Like

thank you so much @muneebr1 Seems to be working now :smiley: