Monday, May 15, 2023

Monitoring Level Tangki Air dengan Sensor Pressure Berbasis ESP 8266

#include <ThingSpeak.h>
#include <ESP8266WiFi.h>
int inputpressure = A0;

//lcd
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

// defenisi variabel
float bacapressure;
float pressure;
float rumus; 
float konstan = 0.13158; 
float maxi = 935;
float zero = 175; //(kalo kekecilan hasil psi-nya, ini naikkan) paling 168
float range = 100;


// Hostpot
const char *ssid =  "ATKNet_MeetingRoom";
const char *pass =  "";

//const char *ssid =  "P_Guest";
//const char *pass =  "Phktjaya";

//const char *ssid =  "RANACITTA";
//const char *pass =  "Pandakgede999";

WiFiClient client;

//thingspeak settings
unsigned long channel = 1995294; //your channel ID number
const char *apiKey = "X1VWYOYR7WD8G3SI"; //your channel write API Key

void setup() {
  lcd.begin(); //lcd inisiasi
  pinMode(inputpressure, INPUT);
  Serial.begin(115200);
  delay(10);

  //connect to WiFi
  Serial.print("Connecting to: "); Serial.println(ssid);
  WiFi.begin(ssid, pass);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("\nWiFi connected\n");
  ThingSpeak.begin(client); //initialize ThingSpeak
}

void loop() {
bacapressure = analogRead(inputpressure);

//rumus = ((bacapressure*5)/1024);
//rumus = ((bacapressure * 100)/1024); //dikurangin 29, karena saat nol, ada nilai 29
//rumus = ((bacapressure-175)*konstan);
rumus = ((bacapressure-zero)*(range/(maxi-zero)));
pressure = map(rumus, 0, 6.2, 0, 100 ); 
//pressure = map(rumus, 0, 6, 0, 100 ); //dimapping agar pressure 0-6 psi mewakili 0-100%

  Serial.print("Pressure: ");
  Serial.println(rumus);
  delay(200);

  int x = ThingSpeak.writeField(channel, 1, pressure, apiKey);
  if (x == 200) {
    Serial.println("Data " + String(pressure));
    Serial.print("pressure: "); Serial.println(pressure);
    Serial.println("Update to ThingSpeak Successful.");
  }
  else {
    Serial.println("Data " + String(pressure));
    Serial.println("Problem updating data. HTTP error code " + String(x));
  }

  Serial.println("Waiting...\n");

  lcd.setCursor(0,0);
  lcd.print("TANK LEVEL LQ");
  lcd.setCursor(0,1);
  lcd.print(pressure, 1);
  lcd.print(" %");
  //lcd.print(bacapressure);
  //lcd.print(" bit");

  delay(20000);


}

No comments: