#include <Wire.h>
#include <Arduino.h>
#include <WiFi.h>
#include <WiFiMulti.h>
#include <HTTPClient.h>
#define USE_SERIAL Serial
WiFiMulti wifiMulti;

int16_t x,y,z; //Use this type, not 'int'
int accelerometerValue = 0;
int16_t xArray[100], yArray[100], zArray[100]; //Use this type, not 'int'
float xavg, yavg, zavg;

void setup(){
  Wire.begin(33, 32, 400000);   //SCL and SDA have to be connected to pin 32 and 33 respectively
  Wire.beginTransmission(0x68);
  Wire.write(0x6B); 
  Wire.write(0);    
  Wire.endTransmission(true);
  Serial.begin(115200);
  pinMode(27,OUTPUT);
  //Above chunk is getting info from accelerometer\

    USE_SERIAL.begin(115200);
    USE_SERIAL.println();
    USE_SERIAL.println();
    USE_SERIAL.println();

    for(uint8_t t = 4; t > 0; t--) {
        USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
        USE_SERIAL.flush();
        delay(1000);
    }
    wifiMulti.addAP("OSU_Access", "");
} //sets up WiFi connection

//function for website connection
void PostToSiteOnCall(String call) {
    // wait for WiFi connection
    if((wifiMulti.run() == WL_CONNECTED)) {

        HTTPClient http;

        USE_SERIAL.print("[HTTP] begin...\n");
        http.begin("https://web.engr.oregonstate.edu/~rivasmig/finalAssignment.php"); //SITE TO POST
        http.addHeader("Content-Type", "application/x-www-form-urlencoded");
        USE_SERIAL.print("[HTTP] GET...\n");
        String postString = call; //String input
        int httpCode_posted = http.POST(postString); //POST
        
        if(httpCode_posted > 0) {
            // HTTP header has been send and Server response header has been handled
            USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode_posted);
            // file found at server
            if(httpCode_posted == HTTP_CODE_OK){
                String payload = http.getString();
                USE_SERIAL.println(payload);
            }
        } else {
            USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode_posted).c_str());
        }
        http.end();
    }
    delay(500);
}

void loop(){
  for (int i = 0; i < 100; i++){
    Wire.beginTransmission(0x68);
    Wire.write(0x3B);  
    Wire.endTransmission(false);
    Wire.requestFrom(0x68,6,true);  //again takes info from accelerometer
    x=Wire.read()<<8|Wire.read();    
    y=Wire.read()<<8|Wire.read(); 
    z=Wire.read()<<8|Wire.read(); 

    xArray[i] = (x)*180.0/32000;
    yArray[i] = (y)*180.0/32000;
    zArray[i] = (z)*180.0/32000; //converts normal number into degrees
    xavg = 0;
    yavg = 0;
    zavg = 0;
    for (int j = 0; j < 100; j++){
      xavg = xArray[j] + xavg;
      yavg = yArray[j] + yavg;
      zavg = zArray[j] + zavg;
    }
    xavg = xavg / 100;
    yavg = yavg / 100;
    zavg = zavg / 100;
   
    Serial.print(xavg);
    Serial.print(", "); 
    Serial.print(yavg);
    Serial.print(", "); 
    Serial.println(zavg);

    delay(50);
    }//accelerometer data

//checks if phone is in a "call" position and sends a 1 or 0 depending on phone postion
  if ((xavg > 60) && (xavg < 100) && (zavg > -40) && (zavg < 15)){
   PostToSiteOnCall("onCall=1"); 
  }else{
  }
}
