Skip to content

Instantly share code, notes, and snippets.

@ubi-gists
Last active November 7, 2019 20:08
Show Gist options
  • Save ubi-gists/d87189ebf8216c427f92064ab593a3ce to your computer and use it in GitHub Desktop.
Save ubi-gists/d87189ebf8216c427f92064ab593a3ce to your computer and use it in GitHub Desktop.
/*
* The sample code requires the ESP8266 v.2.0.0 library for proper operation.
* (https://github.com/ubidots/ubidots-esp8266/releases/tag/2.0.0)
* However, is strongly recommended to migrate the code to the latest library version.
* (https://github.com/ubidots/ubidots-esp8266)
*/
#include <Wire.h>
#include "UbidotsMicroESP8266.h"
#define TOKEN "Your_token_here" // Put here your Ubidots TOKEN
#define WIFISSID "Your_WiFi_SSID" // Put here your Wi-Fi SSID
#define PASSWORD "Your_WiFi_Password" // Put here your Wi-Fi password
Ubidots client(TOKEN);
//*****************************************************
// MMC5883MA Register map
//*****************************************************
#define XOUT_LSB 0x00
#define XOUT_MSB 0x01
#define YOUT_LSB 0x02
#define YOUT_MSB 0x03
#define ZOUT_LSB 0x04
#define ZOUT_MSB 0x05
#define TEMPERATURE 0x06
#define STATUS 0x07
#define INT_CTRL0 0x08
#define INT_CTRL1 0x09
#define INT_CTRL2 0x0A
#define X_THRESHOLD 0x0B
#define Y_THRESHOLD 0x0C
#define Z_THRESHOLD 0x0D
#define PROD_ID1 0x2F
#define MMC5883MA 0x30 // Sensor I2C address
#define MMC5883MA_DYNAMIC_RANGE 16
#define MMC5883MA_RESOLUTION 65536
//*****************************************************
// Functions declaration
//*****************************************************
char read_register(byte REG_ADDR);
void write_register(byte REG_ADDR, byte VALUE);
void wait_meas(void);
void reset_sensor(void);
float parser(char axis_msb, char axis_lsb);
//*****************************************************
// Setup
//*****************************************************
void setup()
{
Wire.begin(); // Join I2C bus (address optional for master)
Serial.begin(9600); // Start USB serial port
client.wifiConnection(WIFISSID, PASSWORD);
write_register(INT_CTRL0, 0x04); // SET instruction
}
//*****************************************************
// Main
//*****************************************************
void loop()
{
Serial.println("---------");
// Variables initialization
char x_lsb, x_msb, y_lsb, y_msb, z_lsb, z_msb;
float x_val, y_val, z_val;
uint8_t status_reg = 0;
uint8_t id = 0;
uint8_t payload[6] = {};
reset_sensor();
write_register(INT_CTRL2, 0x40); // Enables measurement interrupt
write_register(STATUS, 0x01); // Clean measurement interrupt
// Check status register before start magnetic field measurement
status_reg = read_register(STATUS);
//Serial.print("Status register before: ");
//Serial.println(status_reg, BIN);
//Serial.println("Starting measurement");
write_register(INT_CTRL0, 0X01); // Start magnetic field measurement
wait_meas();
// Check status register after complete magnetif field measurement
status_reg = read_register(STATUS);
//Serial.print("Status register after: ");
//Serial.println(status_reg, BIN);
x_lsb = read_register(XOUT_LSB); // Read magnetic field - x lsb
x_msb = read_register(XOUT_MSB); // Read magnetic field - x msb
y_lsb = read_register(YOUT_LSB); // Read magnetic field - y lsb
y_msb = read_register(YOUT_MSB); // Read magnetic field - y msb
z_lsb = read_register(ZOUT_LSB); // Read magnetic field - z lsb
z_msb = read_register(ZOUT_MSB); // Read magnetic field - z msb
x_val = parser(x_msb, x_lsb);
y_val = parser(y_msb, y_lsb);
z_val = parser(z_msb, z_lsb);
Serial.print("Xout: ");
Serial.println(x_val, 7);
Serial.print("Yout: ");
Serial.println(y_val, 7);
Serial.print("Zout: ");
Serial.println(z_val, 7);
//-------------------------------------------------
// Ubidots payload
client.setDataSourceName("nodemcu_mmc5883ma");
client.setDataSourceLabel("nodemcu_mmc5883ma");
client.add("x", x_val);
client.add("y", y_val);
client.add("z", z_val);
client.sendAll(true);
//-------------------------------------------------
delay(1000);
}
//*****************************************************
// Functions definition
//*****************************************************
char read_register(byte REG_ADDR)
{
char reg_value = 0;
Wire.beginTransmission(byte(MMC5883MA)); // Adress of I2C device
Wire.write(byte(REG_ADDR)); // Register address
Wire.endTransmission();
Wire.requestFrom(byte(MMC5883MA), 1); // Request 1 byte from I2C slave device
if (Wire.available() == 1)
{
reg_value = Wire.read(); // Receive a byte as character
}
Wire.endTransmission();
return reg_value;
}
//-------------------------------------------------
void write_register(byte REG_ADDR, byte VALUE)
{
Wire.beginTransmission(byte(MMC5883MA)); // Adress of I2C device
Wire.write(byte(REG_ADDR)); // Register address
Wire.write(byte(VALUE)); // Value to be written
Wire.endTransmission();
}
//-------------------------------------------------
void reset_sensor()
{
write_register(INT_CTRL1, 0x80);
//Serial.println("Sensor reseted");
}
//-------------------------------------------------
void wait_meas()
{
//Serial.println("Waiting for measurement");
uint8_t status_reg = 0;
uint8_t meas_finish = 0;
byte mask = 1;
while(meas_finish == 0)
{
status_reg = read_register(STATUS);
meas_finish = status_reg & mask;
}
//Serial.println("Measurement finished");
}
//-------------------------------------------------
float parser(char MSB, char LSB)
{
float ans = (float)(MSB << 8 | LSB) * MMC5883MA_DYNAMIC_RANGE / MMC5883MA_RESOLUTION - (float)MMC5883MA_DYNAMIC_RANGE / 2;
return ans;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment