#include <ESP8266WiFi.h> 
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>

const int servoPin = 2;
const int servoStatusPin = 0;

void setup() {
    pinMode(LED_BUILTIN, OUTPUT); 
    pinMode(servoPin, OUTPUT);
    pinMode(servoStatusPin, INPUT); 
    Serial.begin(115200);
    WiFiManager wifiManager;
    //wifiManager.resetSettings();
    wifiManager.autoConnect("FishFeeder");
    if (WiFi.status()==WL_CONNECTED){
      Serial.println("connected...yeey :)");
      fastblink();
      }
      digitalWrite(servoPin, LOW);
      updateStatus();
}

void loop() {
    if ((WiFi.status() == WL_CONNECTED)) {
      String value = getResult();
      Serial.println(value);
          if(value == "1" && !digitalRead(servoStatusPin)){
            digitalWrite(servoPin, HIGH);
            }
         if(value == "1" && digitalRead(servoStatusPin)){
              updateStatus();
              digitalWrite(servoPin, LOW);
            } 
        }
  delay(10000);
}

void fastblink(){
  for(int i=0;i<9;i++){
    digitalWrite(LED_BUILTIN, LOW);
    delay(500);
    digitalWrite(LED_BUILTIN, HIGH);
    delay(500);
  }
}

String getResult(){
  String value ="";
  WiFiClient client;
  HTTPClient http;
  if (http.begin(client, "http://api.thingspeak.com/channels/665683/fields/2.json?api_key=QOIEGTM7XT0EKI0V&results=1")) {
      int httpCode = http.GET();
      if (httpCode > 0) {
        if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
          String payload = http.getString();
          Serial.println(payload);
          const size_t capacity = JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(11) + 300;
          DynamicJsonBuffer jsonBuffer(capacity); 
          JsonObject& root = jsonBuffer.parseObject(http.getString());        
          JsonObject& feeds_0 = root["feeds"][0];
          const char* feeds_0_created_at = feeds_0["created_at"]; // "2019-05-27T14:57:36Z"
          int feeds_0_entry_id = feeds_0["entry_id"]; // 168
          const char* feeds_0_field2 = feeds_0["field2"]; // "0"
          Serial.println(feeds_0_field2);
          value = feeds_0_field2;
        } else {
        Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
      }
      http.end();
    } else {
      Serial.printf("[HTTP} Unable to connect\n");
    }
  }
  return value;
}

void updateStatus(){
  WiFiClient client;
  HTTPClient http;
  if (http.begin(client, "http://api.thingspeak.com/update.json?api_key=8FC9LUB2AXVCZJ6L&field2=0")) {
      int httpCode = http.GET();
      if (httpCode > 0) {
        if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
        } else {
        Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
      }
      http.end();
    } else {
      Serial.printf("[HTTP} Unable to connect\n");
    }
  }
 }
