#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <esp_sleep.h>

#define Version "0.3"
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
#define OLED_RESET -1
#define SCREEN_ADDRESS 0x3C
//PINS
#define WTR_SENSOR 4     //GPIO04    D4
#define PIN_PUMP 26      //GPIO26    D26
#define wetSoil 1200     // Define max value we consider soil 'wet'
#define midSoil 1730     // Define max value we consider soil 'wet'
#define drySoil 2400     // Define min value we consider soil 'dry'
#define AIR 3150         // Define min value we consider soil 'dry'
#define cupOfWater 1024  // Define min value we consider soil 'dry'
// #define LED_RED_TANK 26  //GPIO25    D25
// #define WTR_LVL 33  //GPI
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

//MAIN VALUES VARIABLES
int moisture = 0;
int watertingCnt = 0;
int minValue = 5000;
int maxValue = 0;
int waitSec = 96;
int waitMs = 1000 * waitSec;

  void setup() {
  pinMode(WTR_SENSOR, INPUT_PULLDOWN);
  pinMode(PIN_PUMP, OUTPUT);
  digitalWrite(PIN_PUMP, HIGH);
  Serial.begin(9600);
  if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
  }

  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.clearDisplay();
  display.setCursor(0, 0);
  display.println("Version:");
  display.setCursor(60, 0);
  display.println(Version);
  display.display();
  moisture = analogRead(WTR_SENSOR);
  delay(5000);
  moisture = analogRead(WTR_SENSOR);

}

void loop() {
  // ###################################################
  // WYŚWIETLACZ
   moisture = analogRead(WTR_SENSOR);
  display.clearDisplay();
  display.setCursor(110, 0);
  display.println(Version);
  display.setCursor(0, 0);
  display.println("cnt:");
  display.setCursor(25, 0);
  display.println(watertingCnt);
  display.setCursor(45, 0);
  display.println("lvl:");
  display.setCursor(70, 0);
  display.println(moisture);
  display.setCursor(0, 10);
  display.println("threshold:>");
  display.setCursor(73, 10);
  display.println(drySoil);

if (moisture > maxValue){
  maxValue = moisture;
}
if (moisture < minValue){
  minValue = moisture;
}
  display.setCursor(0, 20);
  display.println("min:");
  display.setCursor(23, 20);
  display.println(minValue);
  display.setCursor(70, 20);
  display.println("max:");
  display.setCursor(93, 20);
  display.println(maxValue);

  Serial.print("Analog output: ");
  Serial.println(moisture);

  if (moisture < drySoil) {
    Serial.println("Status: Soil is too wet");
    digitalWrite(PIN_PUMP, HIGH);

  } else {
    display.fillCircle(110, 12, 5, SSD1306_WHITE);
    display.display();
    Serial.println("Status: Soil is too dry - time to water!");
    digitalWrite(PIN_PUMP, LOW);
    watertingCnt++;
    delay(waitMs/16);
    digitalWrite(PIN_PUMP, HIGH);
}
  Serial.println();

  display.fillCircle(110, 12, 5, SSD1306_BLACK);

  display.display();
  esp_sleep_enable_timer_wakeup(waitSec * 1000000ULL); // 5s w mikrosekundach
  esp_light_sleep_start();

}