#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define BLYNK_PRINT Serial

//Network settings
char ssid[] = "your ssid";
char password[] = "your password";
char auth[] = "your auth token";

const int ledPin = 16;
const int waterQuantity = 100;
int waterUsed = 0;
int waterPercent = 0;

WidgetLED led0(V0);
BlynkTimer timer;

void connectionLED()
{
  if(WiFi.status() == WL_CONNECTED)
  {
    digitalWrite(ledPin, (millis() / 1000) % 2);
  }
}

BLYNK_WRITE(V2)
{
  int value = param.asInt();
  if(value)
  {
    waterUsed = waterUsed + 2;
  }
  if(waterUsed >= waterQuantity)
  {
    waterUsed = 0;
    Blynk.notify("Watertank empty!");
  }
  waterPercent = waterQuantity - waterUsed;
  Blynk.virtualWrite(V1, waterPercent);
}

BLYNK_READ(V1)
{
  Blynk.virtualWrite(V1, waterPercent);
}

BLYNK_CONNECTED()
{
  Blynk.syncAll();
}

void setup() {
  pinMode(ledPin, OUTPUT);
  digitalWrite(7, HIGH);
  Serial.begin(115200);
  Blynk.begin(auth, ssid, password);
  timer.setInterval(1000L, connectionLED);
  led0.off();
}

void loop() {
  Blynk.run();
  timer.run();
}
