

This is an Accelerometer logger which send the data directly to a server after completion. I wanted to see if I could make a device that could be attached to something to show its accelerations, speed and distance.
The logger uses an ESP8266/ESP-01 for WIFI and a 6050 Accelerometer for capturing the data.
Code for Project
GNU General Public License v3.0
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecureBearSSL.h>
#include<Wire.h>
// Fingerprint for demo URL, expires on June 2, 2021, needs to be updated well before this date
const uint8_t fingerprint[20] = {0x40, 0xaf, 0x00, 0x6b, 0xec, 0x90, 0x22, 0x41, 0x8e, 0xa3, 0xad, 0xfa, 0x1a, 0xe8, 0x25, 0x41, 0x1d, 0x1a, 0x54, 0xb3};
ESP8266WiFiMulti wifiMulti;
String URL="https://project.tinkersprojects.com/api.php";
String ApiKey ="KEY";
String ApiSecret ="SECRET";
String IotID ="ID_NUMBER";
const uint16_t AccelScaleFactor = 16384;
const uint16_t GyroScaleFactor = 131;
unsigned long LastTime;
unsigned long timeStart;
#define timelimit 10000
#define ButtonPin 3
// MPU6050 few configuration register addresses
const uint8_t MPU6050_REGISTER_SMPLRT_DIV = 0x19;
const uint8_t MPU6050_REGISTER_USER_CTRL = 0x6A;
const uint8_t MPU6050_REGISTER_PWR_MGMT_1 = 0x6B;
const uint8_t MPU6050_REGISTER_PWR_MGMT_2 = 0x6C;
const uint8_t MPU6050_REGISTER_CONFIG = 0x1A;
const uint8_t MPU6050_REGISTER_GYRO_CONFIG = 0x1B;
const uint8_t MPU6050_REGISTER_ACCEL_CONFIG = 0x1C;
const uint8_t MPU6050_REGISTER_FIFO_EN = 0x23;
const uint8_t MPU6050_REGISTER_INT_ENABLE = 0x38;
const uint8_t MPU6050_REGISTER_ACCEL_XOUT_H = 0x3B;
const uint8_t MPU6050_REGISTER_SIGNAL_PATH_RESET = 0x68;
int16_t AccelX, AccelY, AccelZ, Temperature, GyroX, GyroY, GyroZ;
double CurrentXPosition = 0;
double CurrentYPosition = 0;
double CurrentZPosition = 0;
double CurrentXVelocity = 0;
double CurrentYVelocity = 0;
double CurrentZVelocity = 0;
int CurrentXAcceleration = 0;
int CurrentYAcceleration = 0;
int CurrentZAcceleration = 0;
double CurrentTime=0;
double CurrentVelocity=0;
double CurrentDisplacement=0;
double MaxVelocity=0;
double MaxDisplacement=0;
void setup()
{
Serial.begin(115200);
Serial.println("started");
pinMode(ButtonPin,INPUT);
WiFi.mode(WIFI_OFF); //Prevents reconnection issue (taking too long to connect)
delay(1000);
WiFi.mode(WIFI_STA);
wifiMulti.addAP("WIFI", "PASSWORD");
wifiMulti.addAP("WIFI", "PASSWORD");
wifiMulti.addAP("WIFI", "PASSWORD");
wifiMulti.addAP("WIFI", "PASSWORD");
Serial.println("Connecting Wifi...");
if (wifiMulti.run() == WL_CONNECTED)
{
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
Serial.println("started");
Wire.begin(2,0);
Wire.beginTransmission(0x68);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
MPU6050_Init();
Serial.println("A");
}
void loop()
{
if(digitalRead(ButtonPin)==HIGH)
{
Serial.println("Pressed");
timeStart=millis();
while(timeStart+timelimit>millis())
{
getAcceleration();
Serial.println("getAcceleration");
CurrentXAcceleration = (double)AccelX/AccelScaleFactor;
CurrentYAcceleration = (double)AccelY/AccelScaleFactor;
CurrentZAcceleration = (double)AccelZ/AccelScaleFactor;
CurrentXPosition = calPosition(CurrentXPosition,CurrentXVelocity,CurrentXAcceleration);
CurrentXVelocity = calVelocity(CurrentXVelocity,CurrentXAcceleration);
Serial.println("X");
CurrentYPosition = calPosition(CurrentYPosition,CurrentYVelocity,CurrentYAcceleration);
CurrentYVelocity = calVelocity(CurrentYVelocity,CurrentYAcceleration);
Serial.println("Y");
CurrentZPosition = calPosition(CurrentZPosition,CurrentZVelocity,CurrentZAcceleration);
CurrentZVelocity = calVelocity(CurrentZVelocity,CurrentZAcceleration);
Serial.println("Z");
CurrentVelocity = Vector(CurrentXVelocity,CurrentYVelocity,CurrentZVelocity);
CurrentDisplacement = Vector(CurrentXPosition,CurrentYPosition,CurrentZPosition);
Serial.println("Current");
if(CurrentVelocity > MaxVelocity)
MaxVelocity = CurrentVelocity;
if(CurrentDisplacement > MaxDisplacement)
MaxDisplacement = CurrentDisplacement;
Serial.println("max");
delay(10);
}
Serial.println("send");
while (true)
{
if(wifiMulti.run() == WL_CONNECTED)
{
if(postData())
break;
else
delay(1000);
}
else
{
Serial.println("WiFi not connected!");
delay(1000);
}
}
}
}
void MPU6050_Init(){
delay(150);
I2C_Write(MPU6050_REGISTER_SMPLRT_DIV, 0x07);
I2C_Write(MPU6050_REGISTER_PWR_MGMT_1, 0x01);
I2C_Write(MPU6050_REGISTER_PWR_MGMT_2, 0x00);
I2C_Write(MPU6050_REGISTER_CONFIG, 0x00);
I2C_Write(MPU6050_REGISTER_GYRO_CONFIG, 0x00);//set +/-250 degree/second full scale
I2C_Write(MPU6050_REGISTER_ACCEL_CONFIG, 0x00);// set +/- 2g full scale
I2C_Write(MPU6050_REGISTER_FIFO_EN, 0x00);
I2C_Write(MPU6050_REGISTER_INT_ENABLE, 0x01);
I2C_Write(MPU6050_REGISTER_SIGNAL_PATH_RESET, 0x00);
I2C_Write(MPU6050_REGISTER_USER_CTRL, 0x00);
}
void I2C_Write( uint8_t regAddress, uint8_t data){
Wire.beginTransmission(0x68);
Wire.write(regAddress);
Wire.write(data);
Wire.endTransmission();
}
void getAcceleration()
{
Wire.beginTransmission(0x68);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(0x68,14,true); // request a total of 14 registers
CurrentTime = (millis()-LastTime)/1000;
LastTime = millis();
AccelX = (((int16_t)Wire.read()<<8) | Wire.read());
AccelY = (((int16_t)Wire.read()<<8) | Wire.read());
AccelZ = (((int16_t)Wire.read()<<8) | Wire.read());
Temperature = (((int16_t)Wire.read()<<8) | Wire.read());
GyroX = (((int16_t)Wire.read()<<8) | Wire.read());
GyroY = (((int16_t)Wire.read()<<8) | Wire.read());
GyroZ = (((int16_t)Wire.read()<<8) | Wire.read());
}
double calPosition(double LastPosition,double LastVelocity,double CurrentAcceleration)
{
return LastPosition + LastVelocity*CurrentTime + 0.5 * CurrentAcceleration * CurrentTime *CurrentTime;
}
double calVelocity(double LastVelocity,double CurrentAcceleration)
{
return LastVelocity + CurrentAcceleration * CurrentTime;
}
double Vector(double X,double Y,double Z)
{
return sqrt(X*X + Y*Y + Z*Z);
}
bool postData()
{
std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure);
//client->setFingerprint(fingerprint);
client->setInsecure();
HTTPClient http;
if(http.begin(*client,URL))
{
http.addHeader("Content-Type", "application/json");
http.addHeader("Http-Accept", "application/json");
String Data = "{\"Velocity\":" + String(CurrentVelocity) + ",\"Displacement\":" + String(CurrentDisplacement) + ",\"MaxVelocity\":" + String(MaxVelocity) + ",\"MaxDisplacement\":" + String(MaxDisplacement) + ",\"XPosition\":" + String(CurrentXPosition) + ",\"YPosition\":" + String(CurrentYPosition) + ",\"ZPosition\":" + String(CurrentZPosition) + "}";
int httpCode = http.POST("{\"ApiKey\":\""+ApiKey+"\",\"ApiSecret\":\""+ApiSecret+"\",\"Iot\":{\"Id\":"+IotID+",\"Data\":"+Data+"}}");
if (httpCode > 0)
{
String payload = http.getString();
Serial.printf("[HTTP] POST... code: %d\n", httpCode);
Serial.println(payload);
if(httpCode == HTTP_CODE_OK)
{
Serial.println("Sent");
http.end();
return true;
}
}
else
{
Serial.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpCode).c_str());
Serial.println(http.getString());
}
http.end();
}
else
{
Serial.printf("[HTTPS] Unable to connect\n");
}
return false;
}Code for Project
GNU General Public License v3.0
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecureBearSSL.h>
#include<Wire.h>
// Fingerprint for demo URL, expires on June 2, 2021, needs to be updated well before this date
const uint8_t fingerprint[20] = {0x40, 0xaf, 0x00, 0x6b, 0xec, 0x90, 0x22, 0x41, 0x8e, 0xa3, 0xad, 0xfa, 0x1a, 0xe8, 0x25, 0x41, 0x1d, 0x1a, 0x54, 0xb3};
ESP8266WiFiMulti wifiMulti;
String URL="https://project.tinkersprojects.com/api.php";
String ApiKey ="KEY";
String ApiSecret ="SECRET";
String IotID ="ID_NUMBER";
const uint16_t AccelScaleFactor = 16384;
const uint16_t GyroScaleFactor = 131;
unsigned long LastTime;
unsigned long timeStart;
#define timelimit 10000
#define ButtonPin 3
// MPU6050 few configuration register addresses
const uint8_t MPU6050_REGISTER_SMPLRT_DIV = 0x19;
const uint8_t MPU6050_REGISTER_USER_CTRL = 0x6A;
const uint8_t MPU6050_REGISTER_PWR_MGMT_1 = 0x6B;
const uint8_t MPU6050_REGISTER_PWR_MGMT_2 = 0x6C;
const uint8_t MPU6050_REGISTER_CONFIG = 0x1A;
const uint8_t MPU6050_REGISTER_GYRO_CONFIG = 0x1B;
const uint8_t MPU6050_REGISTER_ACCEL_CONFIG = 0x1C;
const uint8_t MPU6050_REGISTER_FIFO_EN = 0x23;
const uint8_t MPU6050_REGISTER_INT_ENABLE = 0x38;
const uint8_t MPU6050_REGISTER_ACCEL_XOUT_H = 0x3B;
const uint8_t MPU6050_REGISTER_SIGNAL_PATH_RESET = 0x68;
int16_t AccelX, AccelY, AccelZ, Temperature, GyroX, GyroY, GyroZ;
double CurrentXPosition = 0;
double CurrentYPosition = 0;
double CurrentZPosition = 0;
double CurrentXVelocity = 0;
double CurrentYVelocity = 0;
double CurrentZVelocity = 0;
int CurrentXAcceleration = 0;
int CurrentYAcceleration = 0;
int CurrentZAcceleration = 0;
double CurrentTime=0;
double CurrentVelocity=0;
double CurrentDisplacement=0;
double MaxVelocity=0;
double MaxDisplacement=0;
void setup()
{
Serial.begin(115200);
Serial.println("started");
pinMode(ButtonPin,INPUT);
WiFi.mode(WIFI_OFF); //Prevents reconnection issue (taking too long to connect)
delay(1000);
WiFi.mode(WIFI_STA);
wifiMulti.addAP("WIFI", "PASSWORD");
wifiMulti.addAP("WIFI", "PASSWORD");
wifiMulti.addAP("WIFI", "PASSWORD");
wifiMulti.addAP("WIFI", "PASSWORD");
Serial.println("Connecting Wifi...");
if (wifiMulti.run() == WL_CONNECTED)
{
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
Serial.println("started");
Wire.begin(2,0);
Wire.beginTransmission(0x68);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
MPU6050_Init();
Serial.println("A");
}
void loop()
{
if(digitalRead(ButtonPin)==HIGH)
{
Serial.println("Pressed");
timeStart=millis();
while(timeStart+timelimit>millis())
{
getAcceleration();
Serial.println("getAcceleration");
CurrentXAcceleration = (double)AccelX/AccelScaleFactor;
CurrentYAcceleration = (double)AccelY/AccelScaleFactor;
CurrentZAcceleration = (double)AccelZ/AccelScaleFactor;
CurrentXPosition = calPosition(CurrentXPosition,CurrentXVelocity,CurrentXAcceleration);
CurrentXVelocity = calVelocity(CurrentXVelocity,CurrentXAcceleration);
Serial.println("X");
CurrentYPosition = calPosition(CurrentYPosition,CurrentYVelocity,CurrentYAcceleration);
CurrentYVelocity = calVelocity(CurrentYVelocity,CurrentYAcceleration);
Serial.println("Y");
CurrentZPosition = calPosition(CurrentZPosition,CurrentZVelocity,CurrentZAcceleration);
CurrentZVelocity = calVelocity(CurrentZVelocity,CurrentZAcceleration);
Serial.println("Z");
CurrentVelocity = Vector(CurrentXVelocity,CurrentYVelocity,CurrentZVelocity);
CurrentDisplacement = Vector(CurrentXPosition,CurrentYPosition,CurrentZPosition);
Serial.println("Current");
if(CurrentVelocity > MaxVelocity)
MaxVelocity = CurrentVelocity;
if(CurrentDisplacement > MaxDisplacement)
MaxDisplacement = CurrentDisplacement;
Serial.println("max");
delay(10);
}
Serial.println("send");
while (true)
{
if(wifiMulti.run() == WL_CONNECTED)
{
if(postData())
break;
else
delay(1000);
}
else
{
Serial.println("WiFi not connected!");
delay(1000);
}
}
}
}
void MPU6050_Init(){
delay(150);
I2C_Write(MPU6050_REGISTER_SMPLRT_DIV, 0x07);
I2C_Write(MPU6050_REGISTER_PWR_MGMT_1, 0x01);
I2C_Write(MPU6050_REGISTER_PWR_MGMT_2, 0x00);
I2C_Write(MPU6050_REGISTER_CONFIG, 0x00);
I2C_Write(MPU6050_REGISTER_GYRO_CONFIG, 0x00);//set +/-250 degree/second full scale
I2C_Write(MPU6050_REGISTER_ACCEL_CONFIG, 0x00);// set +/- 2g full scale
I2C_Write(MPU6050_REGISTER_FIFO_EN, 0x00);
I2C_Write(MPU6050_REGISTER_INT_ENABLE, 0x01);
I2C_Write(MPU6050_REGISTER_SIGNAL_PATH_RESET, 0x00);
I2C_Write(MPU6050_REGISTER_USER_CTRL, 0x00);
}
void I2C_Write( uint8_t regAddress, uint8_t data){
Wire.beginTransmission(0x68);
Wire.write(regAddress);
Wire.write(data);
Wire.endTransmission();
}
void getAcceleration()
{
Wire.beginTransmission(0x68);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(0x68,14,true); // request a total of 14 registers
CurrentTime = (millis()-LastTime)/1000;
LastTime = millis();
AccelX = (((int16_t)Wire.read()<<8) | Wire.read());
AccelY = (((int16_t)Wire.read()<<8) | Wire.read());
AccelZ = (((int16_t)Wire.read()<<8) | Wire.read());
Temperature = (((int16_t)Wire.read()<<8) | Wire.read());
GyroX = (((int16_t)Wire.read()<<8) | Wire.read());
GyroY = (((int16_t)Wire.read()<<8) | Wire.read());
GyroZ = (((int16_t)Wire.read()<<8) | Wire.read());
}
double calPosition(double LastPosition,double LastVelocity,double CurrentAcceleration)
{
return LastPosition + LastVelocity*CurrentTime + 0.5 * CurrentAcceleration * CurrentTime *CurrentTime;
}
double calVelocity(double LastVelocity,double CurrentAcceleration)
{
return LastVelocity + CurrentAcceleration * CurrentTime;
}
double Vector(double X,double Y,double Z)
{
return sqrt(X*X + Y*Y + Z*Z);
}
bool postData()
{
std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure);
//client->setFingerprint(fingerprint);
client->setInsecure();
HTTPClient http;
if(http.begin(*client,URL))
{
http.addHeader("Content-Type", "application/json");
http.addHeader("Http-Accept", "application/json");
String Data = "{\"Velocity\":" + String(CurrentVelocity) + ",\"Displacement\":" + String(CurrentDisplacement) + ",\"MaxVelocity\":" + String(MaxVelocity) + ",\"MaxDisplacement\":" + String(MaxDisplacement) + ",\"XPosition\":" + String(CurrentXPosition) + ",\"YPosition\":" + String(CurrentYPosition) + ",\"ZPosition\":" + String(CurrentZPosition) + "}";
int httpCode = http.POST("{\"ApiKey\":\""+ApiKey+"\",\"ApiSecret\":\""+ApiSecret+"\",\"Iot\":{\"Id\":"+IotID+",\"Data\":"+Data+"}}");
if (httpCode > 0)
{
String payload = http.getString();
Serial.printf("[HTTP] POST... code: %d\n", httpCode);
Serial.println(payload);
if(httpCode == HTTP_CODE_OK)
{
Serial.println("Sent");
http.end();
return true;
}
}
else
{
Serial.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpCode).c_str());
Serial.println(http.getString());
}
http.end();
}
else
{
Serial.printf("[HTTPS] Unable to connect\n");
}
return false;
}Code for Project
GNU General Public License v3.0
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecureBearSSL.h>
#include<Wire.h>
// Fingerprint for demo URL, expires on June 2, 2021, needs to be updated well before this date
const uint8_t fingerprint[20] = {0x40, 0xaf, 0x00, 0x6b, 0xec, 0x90, 0x22, 0x41, 0x8e, 0xa3, 0xad, 0xfa, 0x1a, 0xe8, 0x25, 0x41, 0x1d, 0x1a, 0x54, 0xb3};
ESP8266WiFiMulti wifiMulti;
String URL="https://project.tinkersprojects.com/api.php";
String ApiKey ="KEY";
String ApiSecret ="SECRET";
String IotID ="ID_NUMBER";
const uint16_t AccelScaleFactor = 16384;
const uint16_t GyroScaleFactor = 131;
unsigned long LastTime;
unsigned long timeStart;
#define timelimit 10000
#define ButtonPin 3
// MPU6050 few configuration register addresses
const uint8_t MPU6050_REGISTER_SMPLRT_DIV = 0x19;
const uint8_t MPU6050_REGISTER_USER_CTRL = 0x6A;
const uint8_t MPU6050_REGISTER_PWR_MGMT_1 = 0x6B;
const uint8_t MPU6050_REGISTER_PWR_MGMT_2 = 0x6C;
const uint8_t MPU6050_REGISTER_CONFIG = 0x1A;
const uint8_t MPU6050_REGISTER_GYRO_CONFIG = 0x1B;
const uint8_t MPU6050_REGISTER_ACCEL_CONFIG = 0x1C;
const uint8_t MPU6050_REGISTER_FIFO_EN = 0x23;
const uint8_t MPU6050_REGISTER_INT_ENABLE = 0x38;
const uint8_t MPU6050_REGISTER_ACCEL_XOUT_H = 0x3B;
const uint8_t MPU6050_REGISTER_SIGNAL_PATH_RESET = 0x68;
int16_t AccelX, AccelY, AccelZ, Temperature, GyroX, GyroY, GyroZ;
double CurrentXPosition = 0;
double CurrentYPosition = 0;
double CurrentZPosition = 0;
double CurrentXVelocity = 0;
double CurrentYVelocity = 0;
double CurrentZVelocity = 0;
int CurrentXAcceleration = 0;
int CurrentYAcceleration = 0;
int CurrentZAcceleration = 0;
double CurrentTime=0;
double CurrentVelocity=0;
double CurrentDisplacement=0;
double MaxVelocity=0;
double MaxDisplacement=0;
void setup()
{
Serial.begin(115200);
Serial.println("started");
pinMode(ButtonPin,INPUT);
WiFi.mode(WIFI_OFF); //Prevents reconnection issue (taking too long to connect)
delay(1000);
WiFi.mode(WIFI_STA);
wifiMulti.addAP("WIFI", "PASSWORD");
wifiMulti.addAP("WIFI", "PASSWORD");
wifiMulti.addAP("WIFI", "PASSWORD");
wifiMulti.addAP("WIFI", "PASSWORD");
Serial.println("Connecting Wifi...");
if (wifiMulti.run() == WL_CONNECTED)
{
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
Serial.println("started");
Wire.begin(2,0);
Wire.beginTransmission(0x68);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
MPU6050_Init();
Serial.println("A");
}
void loop()
{
if(digitalRead(ButtonPin)==HIGH)
{
Serial.println("Pressed");
timeStart=millis();
while(timeStart+timelimit>millis())
{
getAcceleration();
Serial.println("getAcceleration");
CurrentXAcceleration = (double)AccelX/AccelScaleFactor;
CurrentYAcceleration = (double)AccelY/AccelScaleFactor;
CurrentZAcceleration = (double)AccelZ/AccelScaleFactor;
CurrentXPosition = calPosition(CurrentXPosition,CurrentXVelocity,CurrentXAcceleration);
CurrentXVelocity = calVelocity(CurrentXVelocity,CurrentXAcceleration);
Serial.println("X");
CurrentYPosition = calPosition(CurrentYPosition,CurrentYVelocity,CurrentYAcceleration);
CurrentYVelocity = calVelocity(CurrentYVelocity,CurrentYAcceleration);
Serial.println("Y");
CurrentZPosition = calPosition(CurrentZPosition,CurrentZVelocity,CurrentZAcceleration);
CurrentZVelocity = calVelocity(CurrentZVelocity,CurrentZAcceleration);
Serial.println("Z");
CurrentVelocity = Vector(CurrentXVelocity,CurrentYVelocity,CurrentZVelocity);
CurrentDisplacement = Vector(CurrentXPosition,CurrentYPosition,CurrentZPosition);
Serial.println("Current");
if(CurrentVelocity > MaxVelocity)
MaxVelocity = CurrentVelocity;
if(CurrentDisplacement > MaxDisplacement)
MaxDisplacement = CurrentDisplacement;
Serial.println("max");
delay(10);
}
Serial.println("send");
while (true)
{
if(wifiMulti.run() == WL_CONNECTED)
{
if(postData())
break;
else
delay(1000);
}
else
{
Serial.println("WiFi not connected!");
delay(1000);
}
}
}
}
void MPU6050_Init(){
delay(150);
I2C_Write(MPU6050_REGISTER_SMPLRT_DIV, 0x07);
I2C_Write(MPU6050_REGISTER_PWR_MGMT_1, 0x01);
I2C_Write(MPU6050_REGISTER_PWR_MGMT_2, 0x00);
I2C_Write(MPU6050_REGISTER_CONFIG, 0x00);
I2C_Write(MPU6050_REGISTER_GYRO_CONFIG, 0x00);//set +/-250 degree/second full scale
I2C_Write(MPU6050_REGISTER_ACCEL_CONFIG, 0x00);// set +/- 2g full scale
I2C_Write(MPU6050_REGISTER_FIFO_EN, 0x00);
I2C_Write(MPU6050_REGISTER_INT_ENABLE, 0x01);
I2C_Write(MPU6050_REGISTER_SIGNAL_PATH_RESET, 0x00);
I2C_Write(MPU6050_REGISTER_USER_CTRL, 0x00);
}
void I2C_Write( uint8_t regAddress, uint8_t data){
Wire.beginTransmission(0x68);
Wire.write(regAddress);
Wire.write(data);
Wire.endTransmission();
}
void getAcceleration()
{
Wire.beginTransmission(0x68);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(0x68,14,true); // request a total of 14 registers
CurrentTime = (millis()-LastTime)/1000;
LastTime = millis();
AccelX = (((int16_t)Wire.read()<<8) | Wire.read());
AccelY = (((int16_t)Wire.read()<<8) | Wire.read());
AccelZ = (((int16_t)Wire.read()<<8) | Wire.read());
Temperature = (((int16_t)Wire.read()<<8) | Wire.read());
GyroX = (((int16_t)Wire.read()<<8) | Wire.read());
GyroY = (((int16_t)Wire.read()<<8) | Wire.read());
GyroZ = (((int16_t)Wire.read()<<8) | Wire.read());
}
double calPosition(double LastPosition,double LastVelocity,double CurrentAcceleration)
{
return LastPosition + LastVelocity*CurrentTime + 0.5 * CurrentAcceleration * CurrentTime *CurrentTime;
}
double calVelocity(double LastVelocity,double CurrentAcceleration)
{
return LastVelocity + CurrentAcceleration * CurrentTime;
}
double Vector(double X,double Y,double Z)
{
return sqrt(X*X + Y*Y + Z*Z);
}
bool postData()
{
std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure);
//client->setFingerprint(fingerprint);
client->setInsecure();
HTTPClient http;
if(http.begin(*client,URL))
{
http.addHeader("Content-Type", "application/json");
http.addHeader("Http-Accept", "application/json");
String Data = "{\"Velocity\":" + String(CurrentVelocity) + ",\"Displacement\":" + String(CurrentDisplacement) + ",\"MaxVelocity\":" + String(MaxVelocity) + ",\"MaxDisplacement\":" + String(MaxDisplacement) + ",\"XPosition\":" + String(CurrentXPosition) + ",\"YPosition\":" + String(CurrentYPosition) + ",\"ZPosition\":" + String(CurrentZPosition) + "}";
int httpCode = http.POST("{\"ApiKey\":\""+ApiKey+"\",\"ApiSecret\":\""+ApiSecret+"\",\"Iot\":{\"Id\":"+IotID+",\"Data\":"+Data+"}}");
if (httpCode > 0)
{
String payload = http.getString();
Serial.printf("[HTTP] POST... code: %d\n", httpCode);
Serial.println(payload);
if(httpCode == HTTP_CODE_OK)
{
Serial.println("Sent");
http.end();
return true;
}
}
else
{
Serial.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpCode).c_str());
Serial.println(http.getString());
}
http.end();
}
else
{
Serial.printf("[HTTPS] Unable to connect\n");
}
return false;
}
