first commit
This commit is contained in:
commit
9a8b4aa33c
|
@ -0,0 +1,277 @@
|
|||
#include <SPI.h>
|
||||
#include <TFT_22_ILI9225.h>
|
||||
#include <Wire.h>
|
||||
#include <Keypad_I2C.h>
|
||||
#include <Adafruit_PN532.h>
|
||||
#include <WiFi.h>
|
||||
#include <PubSubClient.h>
|
||||
|
||||
#define PN532_SCK (13)
|
||||
#define PN532_MISO (12)
|
||||
#define PN532_MOSI (14)
|
||||
#define PN532_SS (33)
|
||||
|
||||
#define TFT_CS 5
|
||||
#define TFT_RST 16
|
||||
#define TFT_RS 17
|
||||
#define TFT_SDI 23
|
||||
#define TFT_CLK 18
|
||||
#define TFT_LED 4
|
||||
|
||||
#define WIFI_SSID "KOSAN KAMAR DEPAN"
|
||||
#define WIFI_PASSWORD "ngoooook"
|
||||
|
||||
#define MQTT_SERVER "192.168.100.77"
|
||||
#define MQTT_PORT 1883
|
||||
#define MQTT_TOPIC_PZEMOUT1 "pzemout1/voltage"
|
||||
#define MQTT_TOPIC_PZEMOUT2 "pzemout2/current"
|
||||
#define MQTT_TOPIC_PZEMOUT3 "pzemout3/power"
|
||||
#define MQTT_TOPIC_PZEMOUT4 "pzemout4/energy"
|
||||
#define MQTT_TOPIC_BIAYA "home/biaya"
|
||||
#define MQTT_TOPIC_SISA_SALDO "home/Sisa_saldo_str"
|
||||
#define MQTT_TOPIC_SALDO_FIX "home/Sisa_saldo_strin"
|
||||
TFT_22_ILI9225 tft = TFT_22_ILI9225(TFT_RST, TFT_RS, TFT_CS, TFT_SDI, TFT_CLK, TFT_LED);
|
||||
Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);
|
||||
String idcard, voltage, current, power, energy, biayaStr, saldoStr, Sisa_saldo_strin;
|
||||
float biaya, saldo, Sisa_saldo;
|
||||
|
||||
const byte ROWS = 4;
|
||||
const byte COLS = 4;
|
||||
char keys[ROWS][COLS] = {
|
||||
{ 'D', '#', '0', '*' },
|
||||
{ 'C', '9', '8', '7' },
|
||||
{ 'B', '6', '5', '4' },
|
||||
{ 'A', '3', '2', '1' }
|
||||
};
|
||||
|
||||
byte rowPins[ROWS] = { 0, 1, 2, 3 };
|
||||
byte colPins[COLS] = { 4, 5, 6, 7 };
|
||||
|
||||
Keypad_I2C kpd(makeKeymap(keys), rowPins, colPins, ROWS, COLS, 0x20, PCF8574);
|
||||
|
||||
WiFiClient wifiClient;
|
||||
PubSubClient client(wifiClient);
|
||||
long time_1 = 0;
|
||||
int interval = 1000;
|
||||
char display = '0';
|
||||
|
||||
void callback(char* topic, byte* payload, unsigned int length) {
|
||||
String message = "";
|
||||
for (int i = 0; i < length; i++) {
|
||||
message += (char)payload[i];
|
||||
}
|
||||
|
||||
Serial.println(message);
|
||||
Serial.print("Message arrived [");
|
||||
Serial.print(topic);
|
||||
Serial.print("] ");
|
||||
if (strcmp(topic, MQTT_TOPIC_PZEMOUT1) == 0) {
|
||||
voltage = message;
|
||||
}
|
||||
if (strcmp(topic, MQTT_TOPIC_PZEMOUT2) == 0) {
|
||||
current = message;
|
||||
}
|
||||
if (strcmp(topic, MQTT_TOPIC_PZEMOUT3) == 0) {
|
||||
power = message;
|
||||
}
|
||||
if (strcmp(topic, MQTT_TOPIC_PZEMOUT4) == 0) {
|
||||
energy = message;
|
||||
}
|
||||
if (strcmp(topic, MQTT_TOPIC_BIAYA) == 0) {
|
||||
biayaStr = message;
|
||||
biaya = biayaStr.toFloat();
|
||||
}
|
||||
if (strcmp(topic, MQTT_TOPIC_SALDO_FIX) == 0) {
|
||||
Sisa_saldo_strin = message;
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
tft.begin();
|
||||
tft.setOrientation(1);
|
||||
tft.setFont(Terminal12x16);
|
||||
tft.setBackgroundColor(COLOR_BLACK);
|
||||
tft.clear();
|
||||
nfc.begin();
|
||||
|
||||
uint32_t versiondata = nfc.getFirmwareVersion();
|
||||
if (!versiondata) {
|
||||
Serial.print("Didn't find PN53x board");
|
||||
while (1)
|
||||
;
|
||||
}
|
||||
|
||||
nfc.SAMConfig();
|
||||
Serial.println("Waiting for an NFC card...");
|
||||
Wire.begin();
|
||||
kpd.begin(makeKeymap(keys));
|
||||
Serial.begin(115200);
|
||||
Serial.println("start");
|
||||
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(1000);
|
||||
Serial.println("Connecting to WiFi...");
|
||||
}
|
||||
Serial.println("Connected to WiFi");
|
||||
|
||||
client.setServer(MQTT_SERVER, MQTT_PORT);
|
||||
client.setCallback(callback);
|
||||
reconnect();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
|
||||
char key = kpd.getKey();
|
||||
|
||||
|
||||
if (key != NO_KEY) {
|
||||
display = key;
|
||||
Serial.println(key);
|
||||
tft.clear();
|
||||
}
|
||||
|
||||
switch (display) {
|
||||
case '1':
|
||||
pilihKamar();
|
||||
break;
|
||||
case '2':
|
||||
printText(10, 10, "Silahkan Tap ");
|
||||
printText(10, 30, "Kartu");
|
||||
readMessageFromCard(2, 1);
|
||||
break;
|
||||
case 'A':
|
||||
dashboard();
|
||||
break;
|
||||
case 'B':
|
||||
dashboard();
|
||||
break;
|
||||
case '0':
|
||||
displayMenu();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (!client.connected()) {
|
||||
reconnect();
|
||||
}
|
||||
client.loop();
|
||||
kirimSaldo();
|
||||
}
|
||||
|
||||
void reconnect() {
|
||||
while (!client.connected()) {
|
||||
printText(10, 10, "MQTT CONNECTING...");
|
||||
if (client.connect("ESP322_Client")) {
|
||||
printText(10, 30, "connected");
|
||||
tft.clear();
|
||||
client.subscribe(MQTT_TOPIC_PZEMOUT1);
|
||||
client.subscribe(MQTT_TOPIC_PZEMOUT2);
|
||||
client.subscribe(MQTT_TOPIC_PZEMOUT3);
|
||||
client.subscribe(MQTT_TOPIC_PZEMOUT4);
|
||||
client.subscribe(MQTT_TOPIC_BIAYA);
|
||||
client.subscribe(MQTT_TOPIC_SALDO_FIX);
|
||||
|
||||
saldo = saldoStr.toFloat();
|
||||
Sisa_saldo = saldo - biaya;
|
||||
break;
|
||||
} else {
|
||||
Serial.print("failed, rc=");
|
||||
Serial.print(client.state());
|
||||
Serial.println(" try again in 5 seconds");
|
||||
delay(5000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void printText(int x, int y, String text) {
|
||||
int lineHeight = 16;
|
||||
int charWidth = 12;
|
||||
int cursorX = x;
|
||||
int cursorY = y;
|
||||
|
||||
for (int i = 0; i < text.length(); i++) {
|
||||
char character = text[i];
|
||||
|
||||
if (character == '\n') {
|
||||
cursorY += lineHeight;
|
||||
cursorX = x;
|
||||
} else {
|
||||
tft.drawChar(cursorX, cursorY, character);
|
||||
cursorX += charWidth;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void readMessageFromCard(uint8_t block, uint8_t sector) {
|
||||
uint8_t success;
|
||||
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };
|
||||
uint8_t uidLength;
|
||||
|
||||
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
|
||||
|
||||
if (success) {
|
||||
idcard = "";
|
||||
for (byte i = 0; i < uidLength; i++) {
|
||||
idcard += (uid[i] < 0x10 ? "0" : "") + String(uid[i], HEX);
|
||||
}
|
||||
|
||||
uint8_t keyData[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
|
||||
|
||||
if (nfc.mifareclassic_AuthenticateBlock(uid, uidLength, sector, 0xFF, keyData)) {
|
||||
Serial.println("Authentication successful.");
|
||||
|
||||
uint8_t readBlock[16];
|
||||
if (nfc.mifareclassic_ReadDataBlock(block, readBlock)) {
|
||||
Serial.print("Read Data from Block ");
|
||||
Serial.print(block);
|
||||
Serial.print(": ");
|
||||
for (byte i = 0; i < 16; i++) {
|
||||
Serial.print(readBlock[i], HEX);
|
||||
Serial.print(" ");
|
||||
saldoStr += (char)readBlock[i];
|
||||
}
|
||||
Serial.println();
|
||||
saldo = saldoStr.toFloat();
|
||||
printText(10, 10, "Saldo Terdeteksi ");
|
||||
printText(10, 30, "Saldo : " + saldoStr);
|
||||
printText(10, 50, "Tunggu 4 Detik");
|
||||
printText(10, 70, "Untuk Kembali");
|
||||
delay(4000);
|
||||
tft.clear();
|
||||
display = '0';
|
||||
} else {
|
||||
Serial.println("Failed to read data from the card.");
|
||||
}
|
||||
} else {
|
||||
Serial.println("Authentication failed.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void dashboard() {
|
||||
if (millis() >= time_1 + interval) {
|
||||
time_1 = millis();
|
||||
printText(10, 10, "Voltage : " + voltage + "V");
|
||||
printText(10, 30, "Current : " + current + "A");
|
||||
printText(10, 50, "Power : " + power + "W");
|
||||
printText(10, 70, "Energy :" + energy + "Kwh");
|
||||
printText(10, 90, "Biaya : Rp." + biayaStr);
|
||||
printText(10, 110, "Saldo : Rp." + Sisa_saldo_strin);
|
||||
}
|
||||
}
|
||||
void kirimSaldo() {
|
||||
Sisa_saldo = saldo - biaya;
|
||||
String Sisa_saldo_str = String(Sisa_saldo, 2);
|
||||
client.publish(MQTT_TOPIC_SISA_SALDO, Sisa_saldo_str.c_str());
|
||||
}
|
||||
void displayMenu() {
|
||||
printText(10, 10, "MENU");
|
||||
printText(10, 30, "1. DASHBOARD");
|
||||
printText(10, 50, "2. TOP UP SALDO");
|
||||
}
|
||||
void pilihKamar() {
|
||||
printText(10, 10, "A. Kamar 1");
|
||||
printText(10, 30, "B. Kamar 2");
|
||||
}
|
|
@ -0,0 +1,151 @@
|
|||
#include <PZEM004Tv30.h>
|
||||
#include <WiFi.h>
|
||||
#include <PubSubClient.h>
|
||||
#include <Wire.h>
|
||||
|
||||
#define RELAY_PIN1 2
|
||||
#define RELAY_PIN2 15
|
||||
#define MQTT_SERVER "192.168.100.77"
|
||||
#define MQTT_PORT 1883
|
||||
#define MQTT_TOPIC_BIAYA "home/biaya"
|
||||
#define MQTT_TOPIC_PZEMOUT1 "pzemout1/voltage"
|
||||
#define MQTT_TOPIC_PZEMOUT2 "pzemout2/current"
|
||||
#define MQTT_TOPIC_PZEMOUT3 "pzemout3/power"
|
||||
#define MQTT_TOPIC_PZEMOUT4 "pzemout4/energy"
|
||||
#define WIFI_SSID "KOSAN KAMAR DEPAN"
|
||||
#define WIFI_PASSWORD "ngoooook"
|
||||
#define MQTT_TOPIC_RELAY1 "home/relay1"
|
||||
#define MQTT_TOPIC_RELAY2 "home/relay2"
|
||||
bool relayState1 = false;
|
||||
bool relayState2 = false;
|
||||
WiFiClient espClient;
|
||||
PubSubClient client(espClient);
|
||||
|
||||
#if !defined(PZEM_RX_PIN) && !defined(PZEM_TX_PIN)
|
||||
#define PZEM_RX_PIN 14
|
||||
#define PZEM_TX_PIN 12
|
||||
#endif
|
||||
|
||||
#if !defined(PZEM_SERIAL)
|
||||
#define PZEM_SERIAL Serial2
|
||||
#endif
|
||||
|
||||
#if defined(ESP32)
|
||||
PZEM004Tv30 pzem(PZEM_SERIAL, PZEM_RX_PIN, PZEM_TX_PIN);
|
||||
#elif defined(ESP8266)
|
||||
#else
|
||||
PZEM004Tv30 pzem(PZEM_SERIAL);
|
||||
#endif
|
||||
|
||||
long time_1 = 0;
|
||||
int interval = 500;
|
||||
|
||||
void callback(char* topic, byte* payload, unsigned int length) {
|
||||
String message = "";
|
||||
for (int i = 0; i < length; i++) {
|
||||
message += (char)payload[i];
|
||||
}
|
||||
|
||||
Serial.print("Message arrived [");
|
||||
Serial.print(topic);
|
||||
Serial.print("] ");
|
||||
Serial.println(message);
|
||||
if (strcmp(topic, MQTT_TOPIC_RELAY1) == 0) {
|
||||
if (message == "ON") {
|
||||
digitalWrite(RELAY_PIN1, LOW);
|
||||
relayState1 = true;
|
||||
} else if (message == "OFF") {
|
||||
digitalWrite(RELAY_PIN1, HIGH);
|
||||
relayState1 = false;
|
||||
}
|
||||
} else if (strcmp(topic, MQTT_TOPIC_RELAY2) == 0) {
|
||||
if (message == "ON") {
|
||||
digitalWrite(RELAY_PIN2, LOW);
|
||||
relayState2 = true;
|
||||
} else if (message == "OFF") {
|
||||
digitalWrite(RELAY_PIN2, HIGH);
|
||||
relayState2 = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
|
||||
Serial.print("Connecting");
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
Serial.print(".");
|
||||
delay(500);
|
||||
}
|
||||
Serial.println("\nConnected to WiFi");
|
||||
|
||||
pinMode(RELAY_PIN1, OUTPUT);
|
||||
digitalWrite(RELAY_PIN1, HIGH);
|
||||
pinMode(RELAY_PIN2, OUTPUT);
|
||||
digitalWrite(RELAY_PIN2, HIGH);
|
||||
|
||||
client.setServer(MQTT_SERVER, MQTT_PORT);
|
||||
client.setCallback(callback);
|
||||
|
||||
reconnect();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (!client.connected()) {
|
||||
reconnect();
|
||||
}
|
||||
client.loop();
|
||||
kirimData();
|
||||
}
|
||||
|
||||
void kirimData() {
|
||||
if (millis() >= time_1 + interval) {
|
||||
time_1 = millis();
|
||||
Serial.print("Kamar :");
|
||||
Serial.println(pzem.readAddress(), HEX);
|
||||
|
||||
float voltage = pzem.voltage();
|
||||
float current = pzem.current();
|
||||
float power = pzem.power();
|
||||
float energy = pzem.energy();
|
||||
float tarifPerKWh = 1444;
|
||||
float biaya = (energy * tarifPerKWh);
|
||||
|
||||
client.publish(MQTT_TOPIC_PZEMOUT1, String(voltage).c_str());
|
||||
client.publish(MQTT_TOPIC_PZEMOUT2, String(current).c_str());
|
||||
client.publish(MQTT_TOPIC_PZEMOUT3, String(power).c_str());
|
||||
client.publish(MQTT_TOPIC_PZEMOUT4, String(energy, 3).c_str());
|
||||
client.publish(MQTT_TOPIC_BIAYA, String(biaya).c_str());
|
||||
|
||||
Serial.print("Voltage: ");
|
||||
Serial.print(voltage);
|
||||
Serial.println("V");
|
||||
Serial.print("Current: ");
|
||||
Serial.print(current);
|
||||
Serial.println("A");
|
||||
Serial.print("Power: ");
|
||||
Serial.print(power);
|
||||
Serial.println("W");
|
||||
Serial.print("Energy: ");
|
||||
Serial.print(energy, 3);
|
||||
Serial.println("kWh");
|
||||
Serial.print("Biaya: Rp. ");
|
||||
Serial.println(biaya);
|
||||
}
|
||||
}
|
||||
|
||||
void reconnect() {
|
||||
while (!client.connected()) {
|
||||
Serial.print("Connecting to MQTT broker...");
|
||||
if (client.connect("ESP321_Client")) {
|
||||
Serial.println("connected");
|
||||
client.subscribe(MQTT_TOPIC_RELAY1);
|
||||
client.subscribe(MQTT_TOPIC_RELAY2);
|
||||
} else {
|
||||
Serial.print("failed, rc=");
|
||||
Serial.print(client.state());
|
||||
Serial.println(" retrying in 5 seconds");
|
||||
delay(5000);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue