Use this forum to chat about hardware specific topics for the ESP8266 (peripherals, memory, clocks, JTAG, programming)

User avatar
By Stevenelson
#29492 I'm trying to get a CJ-M49 compass working with nodelua esp12e v1.0 using the arduino IDE 1.6.5.

I've installed the HMC5883L library and loaded the sample code:

Code: Select all#include <Wire.h>
#include <HMC5883L.h>

HMC5883L compass;

void setup()
{
  Serial.begin(9600);
 
  // Initialize Initialize HMC5883L
  Serial.println("Initialize HMC5883L");
  while (!compass.begin())
  {
    Serial.println("Could not find a valid HMC5883L sensor, check wiring!");
    delay(500);
  }

  // Set measurement range
  compass.setRange(HMC5883L_RANGE_1_3GA);

  // Set measurement mode
  compass.setMeasurementMode(HMC5883L_CONTINOUS);

  // Set data rate
  compass.setDataRate(HMC5883L_DATARATE_30HZ);

  // Set number of samples averaged
  compass.setSamples(HMC5883L_SAMPLES_8);

  // Set calibration offset. See HMC5883L_calibration.ino
  compass.setOffset(151,-626);
}

void loop()
{
  Vector norm = compass.readNormalize();

  // Calculate heading
  float heading = atan2(norm.YAxis, norm.XAxis);

  // Set declination angle on your location and fix heading
  // You can find your declination on: http://magnetic-declination.com/
  // (+) Positive or (-) for negative
  // For Bytom / Poland declination angle is 4'26E (positive)
  // Formula: (deg + (min / 60.0)) / (180 / M_PI);
  float declinationAngle = (-10.0 + (49.0 / 60.0)) / (180 / M_PI);
  heading += declinationAngle;

  // Correct for heading < 0deg and heading > 360deg
  if (heading < 0)
  {
    heading += 2 * PI;
  }

  if (heading > 2 * PI)
  {
    heading -= 2 * PI;
  }

  // Convert to degrees
  float headingDegrees = heading * 180/M_PI;

  // Output
  Serial.print(" Heading = ");
  Serial.print(heading);
  Serial.print(" Degress = ");
  Serial.print(headingDegrees);
  Serial.println();

  delay(100);
}


I have the SDA in GPIO4 (D2 on the nodelua) and SCL GPIO5 (D1 on the nodelua). It is getting data from the compass, and it *kind of* changes when I rotate the compass. Here's some data:

Heading = 4.70 Degress = 269.33
Heading = 4.71 Degress = 269.73
Heading = 4.71 Degress = 269.97
Heading = 4.71 Degress = 270.00
Heading = 4.70 Degress = 269.24
Heading = 4.69 Degress = 268.99
Heading = 4.71 Degress = 269.84
Heading = 4.76 Degress = 272.81
Heading = 4.91 Degress = 281.09
Heading = 5.10 Degress = 292.35
Heading = 5.26 Degress = 301.51
Heading = 5.34 Degress = 306.19
Heading = 5.37 Degress = 307.90
Heading = 5.40 Degress = 309.59
Heading = 5.41 Degress = 309.98
Heading = 5.42 Degress = 310.46
Heading = 5.42 Degress = 310.60
Heading = 5.42 Degress = 310.49
Heading = 5.42 Degress = 310.54
Heading = 5.42 Degress = 310.38
Heading = 5.42 Degress = 310.28

But that's rotating the module 180 degrees. Does anyone have any idea why it's giving me messed up data?
User avatar
By Stevenelson
#29520 I've gotten this chip to work on an arduino mega. I did have to figure out the declinationAngle and a compass offset.

Once I did that on the mega it gave me very accurate headings. But doing the same thing on the ESP8266 12e is still giving me unusual results. That leads me to believe that something is wrong in the library going from the arduino over the esp.

I can't possibly be the only person who has attempted to get this compass working with an esp, am i?
User avatar
By Stevenelson
#29585 Sweet, I'd love the help. I still suck at programming C, it's a screwy language!

I'm not certain how to go about debugging this library. I was going to try and just put a bunch of Serial.println() in the library until something seems wrong. But I'm not exactly sure what I'm looking for.