#include <WiFi.h>
#include <WebServer.h>
#include <EEPROM.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// Pin sensor
const int pinUPS1 = 32;
const int pinUPS2 = 33;
const int pinPLN = 34;
const int pinGENSET = 35;
// Sampling dan Kalibrasi
#define NUM_SAMPLES 500
#define VREF 3.3
#define ADC_RES 4095.0
#define noiseThreshold 0.5
#define voltageCalibration 312.0
// EEPROM address
#define EEPROM_SIZE 100
#define ADDR_SSID 0
#define ADDR_PASS 50
WebServer server(80);
// Global voltase
float vUPS1, vUPS2, vPLN, vGENSET;
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; }"
"</style></head><body>";
html += "<div class='judul'>WiFi Setup</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'>VOLTASE SERVER DEPAN</div>";
html += "<div class='nilai'>UPS1: " + String(vUPS1 < 10.0 ? "OFF" : String(vUPS1, 1)) + "V</div>";
html += "<div class='nilai'>UPS2: " + String(vUPS2 < 10.0 ? "OFF" : String(vUPS2, 1)) + "V</div>";
html += "<div class='nilai'>PLN: " + String(vPLN < 10.0 ? "OFF" : String(vPLN, 1)) + "V</div>";
html += "<div class='nilai'>GENSET: " + String(vGENSET < 10.0 ? "OFF" : String(vGENSET, 1)) + "V</div>";
html += "</body></html>";
server.send(200, "text/html", html);
}
void handleSave() {
ssid = server.arg("ssid");
password = server.arg("password");
for (int i = 0; i < 32; i++) {
EEPROM.write(ADDR_SSID + i, i < ssid.length() ? ssid[i] : 0);
EEPROM.write(ADDR_PASS + i, i < password.length() ? password[i] : 0);
}
EEPROM.commit();
server.send(200, "text/html", "<html><body><h1>Disimpan! Restart...</h1></body></html>");
delay(2000);
ESP.restart();
}
void startAccessPoint() {
WiFi.softAP("PUTU TECKNO", "rejeki88");
IPAddress IP = WiFi.softAPIP();
Serial.print("Access Point aktif: ");
Serial.println(IP);
}
void connectToWiFi() {
if (ssid != "") {
WiFi.begin(ssid.c_str(), password.c_str());
Serial.print("Menyambung ke WiFi: ");
Serial.println(ssid);
unsigned long startAttemptTime = millis();
while (WiFi.status() != WL_CONNECTED && millis() - startAttemptTime < 10000) {
delay(500);
Serial.print(".");
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("Terhubung ke WiFi!");
Serial.print("IP: ");
Serial.println(WiFi.localIP());
return;
} else {
Serial.println("Gagal konek, aktifkan AP mode...");
}
}
startAccessPoint();
}
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("Memulai...");
display.display();
delay(1000);
EEPROM.begin(EEPROM_SIZE);
char ssidBuf[32], passBuf[32];
for (int i = 0; i < 32; i++) {
ssidBuf[i] = EEPROM.read(ADDR_SSID + i);
passBuf[i] = EEPROM.read(ADDR_PASS + i);
}
ssid = String(ssidBuf);
password = String(passBuf);
connectToWiFi();
server.on("/", handleRoot);
server.on("/save", HTTP_POST, handleSave);
server.begin();
}
void loop() {
vUPS1 = readVoltageAC(pinUPS1);
vUPS2 = readVoltageAC(pinUPS2);
vPLN = readVoltageAC(pinPLN);
vGENSET = readVoltageAC(pinGENSET);
server.handleClient();
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0, 0);
display.printf("UPS1 : %s\n", vUPS1 < 10.0 ? "OFF" : String(vUPS1, 1).c_str());
display.setCursor(0, 16);
display.printf("UPS2 : %s\n", vUPS2 < 10.0 ? "OFF" : String(vUPS2, 1).c_str());
display.setCursor(0, 32);
display.printf("PLN : %s\n", vPLN < 10.0 ? "OFF" : String(vPLN, 1).c_str());
display.setCursor(0, 48);
display.printf("GENSET : %s\n", vGENSET < 10.0 ? "OFF" : String(vGENSET, 1).c_str());
display.display();
delay(1000);
}
No comments:
Post a Comment