#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include <WiFi.h>
#include <ThingSpeak.h> // Include the ThingSpeak library
// Konfigurasi WiFi
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Konfigurasi ThingSpeak
unsigned long myChannelNumber = 2635819;
const char* myWriteAPIKey = "R1R3T28EXW2ZVV4T";
WiFiClient client;
// Define pins for ILI9341
#define TFT_CS 15 // Chip select
#define TFT_RST -1 // Reset pin, -1 if connected to reset on board
#define TFT_DC 2 // Data/Command pin
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
float voltage1, current1, power1, energy1, frequency1, pf1, va1, VAR1;
float voltage2, current2, power2, energy2, frequency2, pf2, va2, VAR2;
float voltage3, current3, power3, energy3, frequency3, pf3, va3, VAR3;
float energytot;
// Timing variables
unsigned long lastValueUpdate = 0;
unsigned long lastDisplayUpdate = 0;
const unsigned long valueUpdateInterval = 2000; // 2 seconds
const unsigned long displayUpdateInterval = 10000; // 10 seconds for each slide
int currentSlide = 0; // Track which slide is currently displayed
void setup() {
Serial.begin(115200);
tft.begin();
tft.setRotation(0); // Orientasi Landscape
tft.fillScreen(ILI9341_BLACK); // Use ILI9341 color constants
tft.setTextSize(1);
tft.setTextColor(ILI9341_CYAN, ILI9341_BLACK); // Use ILI9341 color constants
tft.setCursor(28, 5);
tft.print("UP3 Palangka");
tft.setTextSize(1);
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
tft.setCursor(38, 18);
tft.print("PZEM-004T");
tft.setTextColor(ILI9341_YELLOW, ILI9341_BLACK);
tft.setCursor(20, 105);
tft.print("ALAT UKUR TRAFO");
tft.setTextSize(1);
tft.setTextColor(ILI9341_RED, ILI9341_BLACK);
tft.setCursor(40, 120);
tft.print("3-PHASE");
tft.setTextColor(ILI9341_GREEN, ILI9341_BLACK);
tft.setCursor(11, 135);
tft.print("PZEM004T + PZEM017");
Serial.print("Connecting to WiFi SSID: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
int max_attempts = 20;
int attempts = 0;
while (WiFi.status() != WL_CONNECTED && attempts < max_attempts) {
delay(500);
Serial.print(".");
attempts++;
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println();
Serial.println("Connected to WiFi");
} else {
Serial.println();
Serial.println("Failed to connect to WiFi");
while (true) {
delay(1000); // Stay here forever
}
}
delay(3000);
ThingSpeak.begin(client); // Initialize ThingSpeak
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - lastValueUpdate >= valueUpdateInterval) {
lastValueUpdate = currentMillis;
updateValues();
}
if (currentMillis - lastDisplayUpdate >= displayUpdateInterval) {
lastDisplayUpdate = currentMillis;
updateDisplay(currentSlide);
currentSlide = (currentSlide + 1) % 2;
}
}
void updateValues() {
// Read values from PZEM004T or other sources
}
void updateDisplay(int slide) {
tft.fillScreen(ILI9341_BLACK);
if (slide == 0) {
tft.setCursor(5, 2);
tft.setTextSize(1);
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
tft.println("PHASE R");
tft.setTextColor(ILI9341_GREEN, ILI9341_BLACK);
tft.setCursor(5, 15);
tft.printf("VOLTAGE ");
tft.setCursor(54, 15);
tft.printf("%6.2fV", voltage1);
tft.setTextColor(ILI9341_YELLOW, ILI9341_BLACK);
tft.setCursor(5, 25);
tft.printf("CURRENT ");
tft.setCursor(54, 25);
tft.printf("%6.2fA", current1);
tft.setTextColor(ILI9341_CYAN, ILI9341_BLACK);
tft.setCursor(5, 35);
tft.printf("POWER ");
tft.setCursor(54, 35);
tft.printf("%6.2fW", power1);
tft.setTextColor(ILI9341_ORANGE, ILI9341_BLACK);
tft.setCursor(5, 45);
tft.printf("ENERGY ");
tft.setCursor(54, 45);
tft.printf("%6.2fkWh", energy1);
tft.setCursor(58, 2);
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
tft.println("PHASE S");
tft.setTextColor(ILI9341_GREEN, ILI9341_BLACK);
tft.setCursor(58, 15);
tft.printf("VOLTAGE ");
tft.setCursor(107, 15);
tft.printf("%6.2fV", voltage2);
tft.setTextColor(ILI9341_YELLOW, ILI9341_BLACK);
tft.setCursor(58, 25);
tft.printf("CURRENT ");
tft.setCursor(107, 25);
tft.printf("%6.2fA", current2);
tft.setTextColor(ILI9341_CYAN, ILI9341_BLACK);
tft.setCursor(58, 35);
tft.printf("POWER ");
tft.setCursor(107, 35);
tft.printf("%6.2fW", power2);
tft.setTextColor(ILI9341_ORANGE, ILI9341_BLACK);
tft.setCursor(58, 45);
tft.printf("ENERGY ");
tft.setCursor(107, 45);
tft.printf("%6.2fkWh", energy2);
} else {
tft.setCursor(5, 2);
tft.setTextSize(1);
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
tft.println("PHASE T");
tft.setTextColor(ILI9341_GREEN, ILI9341_BLACK);
tft.setCursor(5, 15);
tft.printf("VOLTAGE ");
tft.setCursor(54, 15);
tft.printf("%6.2fV", voltage3);
tft.setTextColor(ILI9341_YELLOW, ILI9341_BLACK);
tft.setCursor(5, 25);
tft.printf("CURRENT ");
tft.setCursor(54, 25);
tft.printf("%6.2fA", current3);
tft.setTextColor(ILI9341_CYAN, ILI9341_BLACK);
tft.setCursor(5, 35);
tft.printf("POWER ");
tft.setCursor(54, 35);
tft.printf("%6.2fW", power3);
tft.setTextColor(ILI9341_ORANGE, ILI9341_BLACK);
tft.setCursor(5, 45);
tft.printf("ENERGY ");
tft.setCursor(54, 45);
tft.printf("%6.2fkWh", energy3);
tft.setCursor(5, 65);
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
tft.println("TOTAL ENERGY");
tft.setCursor(5, 75);
tft.setTextColor(ILI9341_ORANGE, ILI9341_BLACK);
tft.printf("%6.2fkWh", energytot);
}
}