#include <Arduino.h>
#include <WiFi.h>
#include <WifiMulti.h>
#include <HTTPClient.h>

//allows all the above libraries to be accessed in this program

WiFiMulti wifiMulti;

// creating functions and assigning them pin values
  int blue_light_pin = 14;
  int green_light_pin = 12;
  int red_light_pin = 27;
  
void setup() {
   Serial.begin(115200);  //baud rate to 115200
   delay(5000);
   wifiMulti.addAP("OSU_Access", "");   //ensures connection to OSU_Access
   
   //pinMode tells the pin to behave in a certain way either as an input or an output 
   pinMode(red_light_pin, OUTPUT);
   pinMode(green_light_pin, OUTPUT);
   pinMode(blue_light_pin, OUTPUT);

}

void loop() {
  if((wifiMulti.run() == WL_CONNECTED)) {    //wait for WiFi connection
    HTTPClient http;                         //Creates an HTTPClient object to be used
                                             //Begins a request we will eventually POST
    http.begin("https://web.engr.oregonstate.edu/~patteann/Dog_Door.csv");    //where the data is accessed from
    int httpCode = http.GET();
    if(httpCode > 0){           //Did we get a valid response?
      if(httpCode == HTTP_CODE_OK){       // the POST happened correctly
       String payload = http.getString();   //defines the payload as a pullable string
       Serial.println(payload);         
       payload.trim();

//substring creates a new string for each axis average 
//indexOf defines what span of index the new string is for (i.e. zeroeth index to index containing comma)
       String xavg = payload.substring(0, payload.indexOf(','));
       String yavg = payload.substring(payload.indexOf(',') + 1, payload.lastIndexOf(','));
       String zavg = payload.substring(payload.lastIndexOf(',') + 1);

//creates/redefines the axis averages as floats
       float xavgValue = xavg.toFloat();
       float yavgValue = yavg.toFloat();
       float zavgValue = zavg.toFloat();

       Serial.print("xavg: ");
       Serial.println(xavgValue);

       Serial.print("yavg: ");
       Serial.println(yavgValue);


        Serial.print("zavg: ");
       Serial.println(zavgValue);


//for conditions where x/y/z averages are within the specified range, turns on LED color; turns off other LED colors
       if(-17000 < xavgValue && xavgValue < -14000 && -1500 < yavgValue && yavgValue < 600 && 500 < zavgValue && zavgValue < 5000){
        digitalWrite(14, HIGH);     //red
        digitalWrite(12, LOW);
        digitalWrite(27, LOW);
       }

       else if(-13500 < xavgValue && xavgValue < 3000 && -600 < yavgValue && yavgValue < 800 && 4000 < zavgValue && zavgValue < 17000){
        digitalWrite(14, LOW);
        digitalWrite(12, HIGH);    //green
        digitalWrite(27, LOW);
       }

       else if(-15000 < xavgValue && xavgValue < -4000 && -1500 < yavgValue && yavgValue < 100 && -16000 < zavgValue && zavgValue < -1000){
        digitalWrite(14, LOW);
        digitalWrite(12, LOW);
        digitalWrite(27, HIGH);   //blue
       }
       else{
        Serial.println("Door at intermediate position.");   //if the door is at none of the specified options
       }
      }
    http.end(); //End this request
      }
      delay(1000);
}
}
