Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Ashwini U
Created September 8, 2016

Plotting IMU data on artik cloud

This project shows how to plot IMU data on artik cloud.

193
Plotting IMU data on artik cloud

Things used in this project

Hardware components

Redbear CC3200 Micro
×1
GY-521 MPU-6050 3 Axis Gyroscope + Accelerometer Module For Arduino
×1
Jumper wires (generic)
Jumper wires (generic)
×4

Software apps and online services

Energia
Texas Instruments Energia
ARTIK Cloud for IoT
Samsung ARTIK Cloud for IoT

Story

Read more

Schematics

Schematic

Code

IMU_artik.ino

Arduino
#include <WiFi.h>
#include <Wire.h>
#include <ArduinoJson.h>

#define MPU6050_ADDR  0x68
#define PWR_MGMT_1    0x6B
#define ACCEL_XOUT_H  0x3B

char server[] = "api.artik.cloud";  
int port = 443;

String AuthorizationData = "Authorization: _DEVICE_TOKEN_";

WiFiClient client;
char ssid[] = "xxxxx";
char pass[] = "xxxxx";
int status;

char buf[200];

void MPU_wakeup(void)
{
    Wire.begin();
    Wire.beginTransmission(MPU6050_ADDR);
    Wire.write(PWR_MGMT_1);
    Wire.write(0);
    Wire.endTransmission(true);
}


int MPU_read_values(void)
{
    StaticJsonBuffer<200> jsonBuffer; // reserve spot in memory

    JsonObject& root = jsonBuffer.createObject(); // create root objects
    root["sdid"] = "<Device id>";
    root["type"] = "message";

    JsonObject& dataPair = root.createNestedObject("data"); // create nested objects
    Wire.beginTransmission(MPU6050_ADDR);
    Wire.write(ACCEL_XOUT_H);
    Wire.endTransmission(false);
    Wire.requestFrom(MPU6050_ADDR,14,true);  // request a total of 14 registers
  
    dataPair["ACCEL_X"] = (Wire.read() << 8 | Wire.read());
    dataPair["ACCEL_Y"] = (Wire.read() << 8 | Wire.read());
    dataPair["ACCEL_Z"] = (Wire.read() << 8 | Wire.read());

    dataPair["TEMP"] = ((((double)(Wire.read() << 8 | Wire.read())) / 340.00) + 36.53);

    dataPair["GYRO_X"] = (Wire.read() << 8 | Wire.read());
    dataPair["GYRO_Y"] = (Wire.read() << 8 | Wire.read());
    dataPair["GYRO_Z"] = (Wire.read() << 8 | Wire.read());

    root.printTo(buf, sizeof(buf)); // JSON-print to buffer

    return (root.measureLength()); // also return length
}

void setup(){
    Serial.begin(115200);
    Serial.print("Trying to connect to WiFi");
    do {
        Serial.print(".");
        status = WiFi.begin(ssid,pass);
    } while (status != WL_CONNECTED);
      
    Serial.println("Connected to WiFi!!!");
    MPU_wakeup();
}

void loop(){
    client.sslConnect(server, port);
    delay(100);
    if (!client.connected()) { 
        Serial.println("Connection to server failed!");
    } else {
        client.println("POST /v1.1/messages HTTP/1.1");
        client.println("Host: api.artik.cloud");
        client.println("Accept: \*/\*");
        client.println("Content-Type: application/json");
        client.println(AuthorizationData);

        //Post accel, gyro and temperature values
        client.print("Content-Length: ");
        client.println(MPU_read_values());
        client.println();
        client.println(buf);
        Serial.println("Data Sent");
        client.stop();
    }
}     

Credits

Ashwini U

Ashwini U

1 project • 0 followers

Comments