Search This Blog

Saturday, 21 June 2025

4 INPUT VOLMETER AC WEB WIFI AP

 #include <Wire.h>

#include <Adafruit_GFX.h>

#include <Adafruit_SSD1306.h>

#include <WiFi.h>

#include <WebServer.h>

#include <Preferences.h>


#define SCREEN_WIDTH 128

#define SCREEN_HEIGHT 64

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);


#define NUM_SAMPLES 500

#define VREF 3.3

#define ADC_RES 4095.0

#define noiseThreshold 0.5

#define voltageCalibration 312.0


const int pinUPS1 = 32;

const int pinUPS2 = 33;

const int pinPLN = 34;

const int pinGENSET = 35;


float vUPS1 = 0, vUPS2 = 0, vPLN = 0, vGENSET = 0;


Preferences preferences;

WebServer server(80);


String ssid, password;


float readVoltageAC(int pin) {

  float offset = 0;

  for (int i = 0; i < NUM_SAMPLES; i++) {

    offset += analogRead(pin);

  }

  offset /= NUM_SAMPLES;


  float sumSq = 0;

  for (int i = 0; i < NUM_SAMPLES; i++) {

    float val = analogRead(pin);

    float centered = val - offset;

    sumSq += centered * centered;

    delayMicroseconds(100);

  }


  float rms = sqrt(sumSq / NUM_SAMPLES);

  float voltageADC = (rms / ADC_RES) * VREF;

  float acVoltage = voltageADC * voltageCalibration;


  if (acVoltage < noiseThreshold) acVoltage = 0.0;

  return acVoltage;

}


void handleRoot() {

  String html = "<html><head><style>"

                "body { font-family: Arial; background:#f2f2f2; text-align:center; }"

                ".judul { font-size: 28px; font-weight: bold; margin-bottom: 20px; }"

                ".nilai { font-size: 80px; font-weight: bold; color: #000066; margin: 20px; }"

                ".off { color: red; }"

                "</style></head><body>";


  html += "<div class='judul'>PUTU-TECKNO-WIFI-SETING</div><form action='/save' method='post'>"

          "SSID: <input type='text' name='ssid'><br>"

          "Password: <input type='password' name='password'><br>"

          "<input type='submit' value='Save'></form><hr>";


  html += "<div class='judul'>MONITOR TEGANGGAN SERVER UTAMA</div>";

  html += "<div class='nilai'>UPS1: " + String(vUPS1 < 50.0 ? "<span class='off'>OFF</span>" : String(vUPS1, 1) + "V") + "</div>";

  html += "<div class='nilai'>UPS2: " + String(vUPS2 < 50.0 ? "<span class='off'>OFF</span>" : String(vUPS2, 1) + "V") + "</div>";

  html += "<div class='nilai'>PLN: " + String(vPLN < 50.0 ? "<span class='off'>OFF</span>" : String(vPLN, 1) + "V") + "</div>";

  html += "<div class='nilai'>GENSET: " + String(vGENSET < 50.0 ? "<span class='off'>OFF</span>" : String(vGENSET, 1) + "V") + "</div>";


  html += "</body></html>";

  server.send(200, "text/html", html);

}


void handleSave() {

  ssid = server.arg("ssid");

  password = server.arg("password");


  preferences.begin("wifi", false);

  preferences.putString("ssid", ssid);

  preferences.putString("password", password);

  preferences.end();


  server.send(200, "text/html", "<html><body><h1>Saved! Restarting...</h1></body></html>");

  delay(2000);

  ESP.restart();

}


void setupWiFi() {

  preferences.begin("wifi", true);

  ssid = preferences.getString("ssid", "");

  password = preferences.getString("password", "");

  preferences.end();


  if (ssid != "") {

    WiFi.begin(ssid.c_str(), password.c_str());

    for (int i = 0; i < 10; i++) {

      if (WiFi.status() == WL_CONNECTED) {

        Serial.println("Connected to WiFi");

        break;

      }

      delay(500);

    }

  }


  if (WiFi.status() != WL_CONNECTED) {

    WiFi.softAP("PUTU@TECKNO-4AC", "rejeki88");

    Serial.println("Access Point started: PUTU TECKNO");

  }


  server.on("/", handleRoot);

  server.on("/save", HTTP_POST, handleSave);

  server.begin();

}


void setup() {

  Serial.begin(115200);

  analogReadResolution(12);


  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {

    Serial.println(F("OLED tidak ditemukan"));

    while (1);

  }


  display.clearDisplay();

  display.setTextColor(WHITE);

  display.setTextSize(1);

  display.setCursor(10, 10);

  display.println("Monitoring 4 Tegangan...");

  display.display();

  delay(1500);


  setupWiFi();

}


void loop() {

  vUPS1 = readVoltageAC(pinUPS1);

  vUPS2 = readVoltageAC(pinUPS2);

  vPLN = readVoltageAC(pinPLN);

  vGENSET = readVoltageAC(pinGENSET);


  Serial.printf("UPS1: %.1fV | UPS2: %.1fV | PLN: %.1fV | GENSET: %.1fV\n", vUPS1, vUPS2, vPLN, vGENSET);


  display.clearDisplay();

  display.setTextSize(1);

  display.setCursor(0, 0);

  display.printf("UPS1   : %s\n", vUPS1 < 50.0 ? "OFF" : String(vUPS1, 1).c_str());

  display.setCursor(0, 16);

  display.printf("UPS2   : %s\n", vUPS2 < 50.0 ? "OFF" : String(vUPS2, 1).c_str());

  display.setCursor(0, 32);

  display.printf("PLN    : %s\n", vPLN < 50.0 ? "OFF" : String(vPLN, 1).c_str());

  display.setCursor(0, 48);

  display.printf("GENSET : %s\n", vGENSET < 50.0 ? "OFF" : String(vGENSET, 1).c_str());

  display.display();


  server.handleClient();

  delay(1000);

}


No comments:

Post a Comment

Tandon Air Otomatis (ESP32 Master & Display) + Kode Lengkap

  Panduan Lengkap Tandon Air Otomatis (ESP32 Master & Display) + Kode Lengkap Diperbarui: 09 August 2025 Artikel ini memandu Anda memban...