We don’t have much personal experience with DOF hardware, but this Arduino library which reads and compensates for three-axis magnetometer and accelerometer data looks very impressive. It should work ...
The magnetic compass has been a crucial navigational tool for around a thousand years or so, perhaps longer. While classical versions still work perfectly well, you can now get digital magnetometers ...