/**
 * BasicHTTPClient.ino
 *
 *  Created on: 24.05.2015
 *
 */

#include <Arduino.h>

#include <WiFi.h>
#include <WiFiMulti.h>

#include <HTTPClient.h>

#define USE_SERIAL Serial

WiFiMulti wifiMulti;

int oncount = 0; //For tracking repeating "on" commands
int offcount = 0; //For tracking repeating "off" commands

void setup() {

    USE_SERIAL.begin(115200);
    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", "");
    
    pinMode(25, OUTPUT); // blue LED
    pinMode(26, OUTPUT); // red LED
    pinMode(27, OUTPUT); // green LED    
}

void loop() {
    
    // wait for WiFi connection
    if((wifiMulti.run() == WL_CONNECTED)) {
        HTTPClient http;       
        http.begin("https://web.engr.oregonstate.edu/~bergkev/ENGR103/finalassignmenttest.txt"); //HTTP
        
        // start connection and send HTTP header
        int httpCode = http.GET();

        // Produces error if the file isn't found
        if(httpCode == 404) {
          Serial.println ("ERROR: File not found!");
           for (int i=0; i < 5; i++){
            digitalWrite(26, HIGH);
            delay(500);
            digitalWrite(26, LOW);
            delay(500);
          }
        }
        // file found at server
          else if (httpCode == 200) {
            if(httpCode == HTTP_CODE_OK) {
                Serial.print ("Command Received: ");
                String payload = http.getString();
                USE_SERIAL.println(payload); //prints command to serial
                if (payload == "on"){ 
                  oncount++; // Counts repeated on commands
                  offcount = 0;
                  if (oncount > 2){ //runs if on command is repeated twice
                    Serial.println ("Waiting for new command.");
                    Serial.print ("Repeat number: ");
                    Serial.println (oncount);
                    for (int i=0; i < 5; i++){
                      digitalWrite(25, HIGH);
                      delay(500);
                      digitalWrite(25, LOW);
                      delay(500);
                    }
                  }
                  else{ // runs on 1st and 2nd on command
                    digitalWrite(27, HIGH);
                    delay(5000);
                    }
                }
                else if (payload == "off"){
                  offcount++; //counts off commands
                   oncount = 0;
                  if (offcount > 2){ // runs if off command is repeated twice
                    Serial.println ("Waiting for new command.");
                    Serial.print ("Repeat number: ");
                    Serial.println (offcount);
                    for (int i=0; i < 5; i++){
                      digitalWrite(25, HIGH);
                      delay(500);
                      digitalWrite(25, LOW);
                      delay(500);
                    }
                  }
                  else{ //runs on 1st and 2nd off commands
                    digitalWrite(26, HIGH);
                    delay(5000);
                    }
                }
                else if (payload != "on" && payload != "off"){ //error if text received is not "on" or "off"
                  Serial.println ("ERROR: Invalid command received");
                  for (int i=0; i < 5; i++){
                    digitalWrite(26, HIGH);
                    delay(500);
                    digitalWrite(26, LOW);
                    delay(500);
                  }
                }
              }
            }
            else {
            USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); //error if there is a failed HTTP code
            for (int i=0; i < 5; i++){
              digitalWrite(26, HIGH);
              delay(500);
              digitalWrite(26, LOW);
              delay(500);
            }
        }
        Serial.println ("");
        http.end();
    }
    else {
      Serial.println ("Waiting for WiFi connection..."); //prints error if WiFi connection fails
      for (int i=0; i < 10; i++){
        digitalWrite(26, HIGH);
        delay(100);
        digitalWrite(26, LOW);
        delay(100);
      }
    }
    digitalWrite(25, LOW); //turns all LEDs off
    digitalWrite(26, LOW);
    digitalWrite(27, LOW);
    //delay(5000);
}
