#define BLYNK_TEMPLATE_ID "TMPLUXIx19gP"
#define BLYNK_TEMPLATE_NAME "Sensor Pengukuran"
#define BLYNK_AUTH_TOKEN "DllHdA1-jPByhGkCbXXew3cCmdVj-QhA"
#include <BlynkSimpleEsp32.h>
#include <SPI.h>
#include "FS.h"
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "DHT.h"
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
float vol=0.0;
float volm=0.0;
String lv="";
String lh="";
String sema="";
String semb="";
int relay=13;
#define DHTPIN 12
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
int h=0;
float t=0;
volatile int flow_frequency;
float l_hour; // Calculated litres/hour
unsigned char flowsensor = 14; // Sensor Input
unsigned long currentTime;
unsigned long cloopTime;
ICACHE_RAM_ATTR void flow () // Interrupt function
{
   flow_frequency++;
}
BLYNK_WRITE(V0) // Executes when the value of virtual pin 0 changes
{
  if(param.asInt() == 1)
  {
    // execute this code if the switch widget is now ON
    digitalWrite(relay,HIGH);  // Set digital pin 2 HIGH
  }
  else
  {
    // execute this code if the switch widget is now OFF
    digitalWrite(relay,LOW);  // Set digital pin 2 LOW    
  }
}
void setup() {
  pinMode(relay,OUTPUT);
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  dht.begin();
  pinMode(flowsensor, INPUT);
  attachInterrupt(digitalPinToInterrupt(flowsensor), flow, RISING);
  sei();
  Serial.begin(115200);
  
  bool ok = SPIFFS.begin();
  delay(3000);
  if (ok) {
    Serial.println("SPIFFS initialized successfully");
    }
  else{
    Serial.println("SPIFFS intialization error");
    }
  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }
  display.clearDisplay();

  currentTime = millis();
  cloopTime = currentTime;
  File file = SPIFFS.open("/log.txt", "r");
        if(!file){
          Serial.println("Failed to open file for reading");
          return;
        }
        while(file.available()){
          String baca=file.readStringUntil('\n');
          lv=baca.c_str();
          Serial.write(file.read());
        }
        file.close();
}
const unsigned char flow2222 [] PROGMEM = {
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0x00, 0x00, 0x07, 0xff, 0xc0, 0x38, 0x3f, 0xc3, 0xe0, 
  0x3f, 0xfe, 0x00, 0xe0, 0x1f, 0xf8, 0x00, 0x00, 0x07, 0xc0, 0x7e, 0x00, 0x00, 0x03, 0xff, 0x80, 
  0x10, 0x0f, 0xff, 0xc0, 0x3e, 0xff, 0x81, 0xe0, 0x3f, 0xfc, 0x00, 0x40, 0x0f, 0xf0, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
const unsigned char fire [] PROGMEM = {
  0x00, 0x00, 0x06, 0x00, 0x06, 0x00, 0x09, 0x00, 0x11, 0x40, 0x21, 0xc0, 0x20, 0x20, 0x20, 0x20, 
  0x20, 0x20, 0x20, 0x20, 0x10, 0x40, 0x0f, 0x80
};
const unsigned char hum [] PROGMEM = {
  0x00, 0x00, 0x00, 0x08, 0x00, 0x60, 0x1c, 0x00, 0x70, 0x1e, 0x00, 0xf0, 0x3e, 0x10, 0xf8, 0x3e, 
  0x39, 0xf8, 0x3e, 0x39, 0xf8, 0x3e, 0x7c, 0xf0, 0x0c, 0x7c, 0x60, 0x00, 0x7c, 0x00, 0x00, 0x7c, 
  0x00, 0x00, 0x38, 0x00
};
void loop() {
  Blynk.run();
  currentTime = millis();
  if(currentTime >= (cloopTime + 1000))
   {
    display.clearDisplay();
    lv="";
      File file = SPIFFS.open("/log.txt", "r");
        if(!file){
          Serial.println("Failed to open file for reading");
          return;
        }
        while(file.available()){
          String baca=file.readStringUntil('\n');
          lv=baca.c_str();
          Serial.write(file.read());
        }
        file.close();
      Serial.println(lv);  
      vol=lv.toDouble();
      cloopTime = currentTime; // Updates cloopTime
      // Pulse frequency (Hz) = 7.5Q, Q is flow rate in L/min.
      l_hour = (flow_frequency / 0.45); // (Pulse frequency x 60 min) / 7.5Q = flowrate in L/hour
      flow_frequency = 0; // Reset Counter
      Serial.print(l_hour, DEC); // Print litres/hour
      Serial.println(" L/m");
      volm=(l_hour/60)*0.99932;
      vol=vol+volm;
      if(vol>999999){
        vol=0;
      }
      //Serial.println(volm,DEC);
      file = SPIFFS.open("/log.txt", "w");
      if (!file) {
        //Serial.println("file open failed");
      }
      Blynk.virtualWrite(V0, t);
      Blynk.virtualWrite(V3, h);
      Blynk.virtualWrite(V10, sema);
      Blynk.virtualWrite(V11, semb);
      file.println(vol);
      //Serial.println(vol);
      file.close();
   }
  h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  t = dht.readTemperature();
  if (isnan(h) || isnan(t)) {
    //Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }
//  Serial.print(F("Humidity: "));
//  Serial.print(h);
//  Serial.print(F("%  Temperature: "));
//  Serial.print(t);
//  Serial.println(F("°C "));
  
  display.drawBitmap(5, 24, flow2222, 28, 20, WHITE);
  display.drawBitmap(5, 49, fire, 12, 12, WHITE);
  display.drawBitmap(70, 49, hum, 22, 12, WHITE);
  display.drawRect(0, 0, 128, 22, SSD1306_WHITE);
  display.drawRect(0, 24, 128, 22, SSD1306_WHITE);
  display.drawRect(0, 47, 63, 17, SSD1306_WHITE);
  display.drawRect(65, 47, 63, 17, SSD1306_WHITE);
  display.setTextSize(2);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text   
  lv="";  
  double sementara=vol/1000;
  
  //vol=
  if(sementara<10){
    lv+="000000";
  }
  else if(sementara<100){
    lv+="0000";
  }
  else if(sementara<1000){
    lv+="0000";
  }
  else if(sementara<10000){
    lv+="000";
  }
  else if(sementara<100000){
    lv+="00";
  }
  else if(vol<1000000){
    lv+="0";
  }
  
  
  lv+=String(sementara,2);
  //lv+="m3";
  
  display.setCursor(4,4);
  display.print(lv);
  
  //Serial.println(lv);
  sema=lv;
  sema+="m3";
  lv="";
  display.setCursor(40,28);
  
  if(l_hour<10){
    lh+="000";
  }
  else if(l_hour<100){
    lh+="00";
  }
  else if(l_hour<1000){
    lh+="0";
  }
  int bulat=l_hour;
  lh+=String(bulat);
  lh+="L/m";
  
  display.print(lh);
  semb=lh;
  lh="";
  display.setTextSize(1);
  display.setCursor(23,52);
  String suhu=String(t,1);
  suhu+=" C";
  display.print(suhu);
  display.setCursor(99,52);
  String hum=String(h);
  hum+=" %";
  display.print(hum);
  display.display();
}
NOCOMNCVCCGNDINLED1PWRRelay Module