#define BLYNK_TEMPLATE_ID "TMPL3F_G7vV88"
#define BLYNK_TEMPLATE_NAME "smart soil Nutrision  analysis"
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>

// WiFi credentials
char ssid[] = "Wokwi-GUEST";
char pass[] = "";

// Blynk Auth TokenE9"
char auth[] ="uidjhqmf9z7a59w7gLrVP2tn0nBQESIq";
// Blynk virtual pins
#define VPIN_BUTTON V0

// OLED display settings
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

bool deviceOn = false;
bool displayFirstSet = true;
BlynkTimer timer;

void setup() {
  Serial.begin(115200);

  // Initialize Blynk
  Blynk.begin(auth, ssid, pass);

  // Initialize OLED display
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;);
  }
  display.display();
  delay(2000); // Pause for 2 seconds
  display.clearDisplay();

  // Display the name "John" once on startup
  display.setTextSize(2);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0, 0);
  display.println(F("soil nutrition program"));
  display.display();
  delay(2000); // Display for 2 seconds
  display.clearDisplay();
  display.display();

  // Setup Blynk button
  pinMode(VPIN_BUTTON, INPUT_PULLUP);
  Blynk.virtualWrite(VPIN_BUTTON, deviceOn); // Initialize button state

  // Setup a timer to update the display every 5 seconds
  timer.setInterval(5000L, updateDisplay);
}

void loop() {
  Blynk.run();
  timer.run();
}

BLYNK_WRITE(VPIN_BUTTON) {
  deviceOn = param.asInt();
  if (deviceOn) {
    display.clearDisplay();
    display.display();
  } else {
    display.clearDisplay();
    display.display();
  }
}

void updateDisplay() {
  if (deviceOn) {
    // Generate random values for Potassium, Nitrogen, Soil Moisture, and Phosphorus
    int potassium = random(0, 101);
    int nitrogen = random(0, 101);
    int soilMoisture = random(0, 101);
    int phosphorus = random(0, 101);

    // Display data on OLED
    display.clearDisplay();
    display.setTextSize(1);
    display.setTextColor(SSD1306_WHITE);
    display.setCursor(0, 0);

    if (displayFirstSet) {
      display.println(F("Potassium:"));
      display.setTextSize(2);
      display.println(potassium);
      display.setTextSize(1);
      display.println(F(" %"));
      display.setTextSize(1);
      display.println(F("Nitrogen:"));
      display.setTextSize(2);
      display.println(nitrogen);
      display.setTextSize(1);
      display.println(F(" %"));
    } else {
      display.println(F("Soil Moisture:"));
      display.setTextSize(2);
      display.println(soilMoisture);
      display.setTextSize(1);
      display.println(F(" %"));
      display.setTextSize(1);
      display.println(F("Phosphorus:"));
      display.setTextSize(2);
      display.println(phosphorus);
      display.setTextSize(1);
      display.println(F(" %"));
    }
    
    display.display();

    // Toggle the flag to alternate the display
    displayFirstSet = !displayFirstSet;
  }
}