Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: libmDot mbed-rtos mbed
Fork of Adafruit_I2C_9dof_mDot_TTN by
Diff: main.cpp
- Revision:
- 9:e3756ad55310
- Parent:
- 8:ebec2e50d421
- Child:
- 10:798cac7d3dcb
diff -r ebec2e50d421 -r e3756ad55310 main.cpp
--- a/main.cpp Tue Aug 02 19:49:55 2016 +0000
+++ b/main.cpp Sun Aug 21 23:28:04 2016 +0000
@@ -31,9 +31,17 @@
#include <algorithm>
// TTN Keys, setup your application and get your own keys at https://www.thethingsnetwork.org/wiki/Backend/Overview
-static const uint8_t netowork_session_key_array[] = { 0x35, 0x41, 0x32, 0x67, 0x79, 0x76, 0x49, 0x8B, 0xBE, 0x98, 0x10, 0x80, 0x76, 0xB7, 0x61, 0x8B };
-static const uint8_t data_session_key_array[] = { 0xD3, 0x6F, 0x5E, 0x66, 0x4A, 0x1B, 0xEC, 0x0C, 0x4A, 0x63, 0x8E, 0x1C, 0x2D, 0xB3, 0x18, 0xA4 };
-static const uint8_t network_address_array[] = { 0x10, 0x81, 0xDA, 0x6F };
+static const uint8_t netowork_session_key_array[] = { 0x22, 0xDF, 0x20, 0x0C, 0x71, 0x5A, 0xCC, 0x35, 0xB8, 0x20, 0x5F, 0xAE, 0xC7, 0x00, 0x5A, 0x63 };//
+
+//app session key
+static const uint8_t data_session_key_array[] = { 0x66, 0xF2, 0x8B, 0x88, 0x76, 0x0D, 0xB0, 0xE2, 0xAE, 0x16, 0x29, 0x81, 0x29, 0xC9, 0x25, 0x11 };
+
+//dev address (keep in mind these are all under abp)
+static const uint8_t network_address_array[] = { 0x30, 0xE2, 0x6C, 0xAF };
+
+//static const uint8_t netowork_session_key_array[] = { 0x35, 0x41, 0x32, 0x67, 0x79, 0x76, 0x49, 0x8B, 0xBE, 0x98, 0x10, 0x80, 0x76, 0xB7, 0x61, 0x8B };
+//static const uint8_t data_session_key_array[] = { 0xD3, 0x6F, 0x5E, 0x66, 0x4A, 0x1B, 0xEC, 0x0C, 0x4A, 0x63, 0x8E, 0x1C, 0x2D, 0xB3, 0x18, 0xA4 };
+//static const uint8_t network_address_array[] = { 0x10, 0x81, 0xDA, 0x6F };
static std::vector<uint8_t> netowork_session_key (netowork_session_key_array, netowork_session_key_array + sizeof(netowork_session_key_array) / sizeof(uint8_t));
static std::vector<uint8_t> data_session_key (data_session_key_array, data_session_key_array + sizeof(data_session_key_array) / sizeof(uint8_t));
@@ -43,7 +51,7 @@
// Initialize I2C
I2C i2c(I2C_SDA , I2C_SCL );
-const int addr7bit = 0x18; // 7 bit I2C address
+const int addr7bit = 0x1E; // 7 bit I2C address
const int addr8bit = addr7bit << 1; // 8bit I2C address, 0x90
@@ -53,8 +61,10 @@
int32_t ret;
mDot* dot;
char cmd[2]; // I2C command address byte 8-bit
- char my_data[2]; // I2C return address bytes 16-bit
- uint16_t amb; // Intermediate variable
+ char my_data[6]; // I2C return address bytes 16-bit
+ uint16_t mag_x; // Intermediate variable
+ uint16_t mag_y; // Intermediate variable
+ uint16_t mag_z; // Intermediate variable
std::vector<uint8_t> data; // mDot data->send variable
char data_str[64]; // Intermediate conversion variable
uint32_t update_interval = 15000; // TTN transmission interval (loop interval)
@@ -109,40 +119,39 @@
while (true) {
- // Wake temp sensor
- cmd[0] = 0x01;
- cmd[1] = 0x00;
- i2c.write(addr8bit, cmd, 2);
- logInfo("Wake temp sensor");
+ // Enable the Magnetometer
+ cmd[0] = 0x02;
+ //cmd[1] = 0x00;
+ i2c.write(addr8bit, cmd, 1);
+ logInfo("Wake magnetometer");
- // Read ambient temperature
- cmd[0] = 0x05; // For list of MCP9808 commands and addresses, see https://github.com/adafruit/Adafruit_MCP9808_Library/blob/master/Adafruit_MCP9808.h
+ // Read magnetic field
+ cmd[0] = 0x03; // see for a list of addresses and commands https://github.com/adafruit/Adafruit_LSM303/blob/master/Adafruit_LSM303.h
i2c.write(addr8bit, cmd, 1);
- i2c.read( addr8bit, my_data, 2);
+ i2c.read( addr8bit, my_data, 6);
// This section is converted from Arduino version: https://github.com/adafruit/Adafruit_MCP9808_Library/blob/master/Adafruit_MCP9808.cpp
- amb = my_data[0];
- amb <<= 8;
- amb |= my_data[1];
+ mag_x = my_data[0];
+ mag_x <<= 8;
+ mag_x |= my_data[1];
- float tmpC = float(amb & 0x0FFF);
- tmpC /= 16.0;
- if (amb & 0x1000) tmpC -= 256;
- float tmpF = (tmpC * 9.0 / 5.0 + 32.0);
+ mag_y = my_data[2];
+ mag_y <<= 8;
+ mag_y |= my_data[3];
+
+ mag_z = my_data[4];
+ mag_z <<= 8;
+ mag_z |= my_data[5];
+
+
+ logInfo("X_MAG = %d Y_MAG = %d; Z_MAG = %d", mag_x, mag_y, mag_z);
- logInfo("Temp = %4.2f*C \t\t %4.2f*F", tmpC, tmpF);
-
- // Shutdown temp sensor
- cmd[0] = 0x00;
- cmd[1] = 0x00;
- i2c.write(addr8bit, cmd, 2);
- logInfo("Shutdown temp sensor\n");
// Empty data vector
data.clear();
// Push temperature value into data array
- sprintf(data_str, "%4.2f", tmpF);
+ sprintf(data_str, "%d", mag_x);
for (int i = 0; i<strlen(data_str); i++)
{
data.push_back(((char*)data_str)[i]);
