From 38faeb5626f7e111bb2755104ada1eb7e145d5d2 Mon Sep 17 00:00:00 2001 From: Mohammad_Irnanda Date: Thu, 18 Jul 2024 15:29:08 +0700 Subject: [PATCH] Upload files to "Kode Program Alat Tulis Saldo Digital Berbasis Esp32" --- .../AlatTulisSaldoDigital_E32211535.ino | 356 ++++++++++++++++++ 1 file changed, 356 insertions(+) create mode 100644 Kode Program Alat Tulis Saldo Digital Berbasis Esp32/AlatTulisSaldoDigital_E32211535.ino diff --git a/Kode Program Alat Tulis Saldo Digital Berbasis Esp32/AlatTulisSaldoDigital_E32211535.ino b/Kode Program Alat Tulis Saldo Digital Berbasis Esp32/AlatTulisSaldoDigital_E32211535.ino new file mode 100644 index 0000000..2da72e5 --- /dev/null +++ b/Kode Program Alat Tulis Saldo Digital Berbasis Esp32/AlatTulisSaldoDigital_E32211535.ino @@ -0,0 +1,356 @@ +#include +#include +#include +#include +#include +#include +#include + +#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 "" +#define MQTT_TOPIC_PZEMIN3 "pzemin3/power" +#define MQTT_TOPIC_SISA_SALDO "home/Sisa_saldo_strin" +#define MQTT_TOPIC_RELAY1 "home/relay1" +#define MQTT_TOPIC_RELAY2 "home/relay2" + +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, power, Sisa_saldo_strin; + +const byte ROWS = 4; // Four rows +const byte COLS = 4; // Four columns +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 }; //connect to the row pinouts of the keypad +byte colPins[COLS] = { 4, 5, 6, 7 }; //connect to the column pinouts of the keypad + +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_PZEMIN3) == 0) { + power = message; + } + if (strcmp(topic, MQTT_TOPIC_SISA_SALDO) == 0) { + Sisa_saldo_strin = message; + } +} + +void setup() { + tft.begin(); + tft.setOrientation(1); // Adjust screen orientation as needed + tft.setFont(Terminal12x16); + tft.setBackgroundColor(COLOR_BLACK); // Set background color to 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"); + + // Connect to Wi-Fi + WiFi.begin(WIFI_SSID, WIFI_PASSWORD); + while (WiFi.status() != WL_CONNECTED) { + delay(1000); + Serial.println("Connecting to WiFi..."); + } + Serial.println("Connected to WiFi"); + + // Connect to MQTT broker + client.setServer(MQTT_SERVER, MQTT_PORT); + client.setCallback(callback); + reconnect(); +} + +void loop() { + + //baca keypad + char key = kpd.getKey(); + + //ganti halaman jika input berbeda dengan halaman sebelumnya + if (key != NO_KEY) { + display = key; + Serial.println(key); + tft.clear(); + } + + //buat ganti halaman + switch (display) { + case '1': + printText(10, 10, "Masukkan Nominal:"); + isisaldo(); + break; + case '2': + printText(10, 10, "Silahkan Tap "); + printText(10, 30, "Kartu"); + readMessageFromCard(2, 1); + break; + case '3': + cutoff(); + break; + case '0': + displayMenu(); + break; + case 'A': + sendRelayCommand(MQTT_TOPIC_RELAY1, "ON"); + break; + case 'B': + sendRelayCommand(MQTT_TOPIC_RELAY1, "OFF"); + break; + case 'C': + sendRelayCommand(MQTT_TOPIC_RELAY2, "ON"); + break; + case 'D': + sendRelayCommand(MQTT_TOPIC_RELAY2, "OFF"); + break; + default: + break; + } + + // Handle MQTT client + if (!client.connected()) { + reconnect(); + } + client.loop(); +} + +void isisaldo() { + int nominal = readNominalFromKeypad(); + tft.clear(); // Hapus teks sebelumnya + printText(10, 10, "Nominal: " + String(nominal)); + printText(10, 30, "Tap Kartu"); + writeNominalToCard(2, 1, nominal); + String payload = String(nominal); + client.publish(MQTT_TOPIC, payload.c_str()); + delay(4000); + tft.clear(); + display = '0'; +} +void reconnect() { + // Loop until we're reconnected + while (!client.connected()) { + printText(10, 10, "MQTT CONNECTING..."); + // Attempt to connect + if (client.connect("ESP323_Client")) { + printText(10, 30, "connected"); + tft.clear(); + client.subscribe(MQTT_TOPIC_PZEMIN3); + client.subscribe(MQTT_TOPIC_SISA_SALDO); + break; + } else { + Serial.print("failed, rc="); + Serial.print(client.state()); + Serial.println(" try again in 5 seconds"); + // Wait 5 seconds before retrying + delay(5000); + } + } +} + +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); + } + + printText(10, 10, "ID CARD : "); + printText(120, 10, idcard); + + 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(" "); + } + Serial.println(); + printText(10, 30, "Saldo : "); + printText(100, 30, (char*)readBlock); + printText(10, 50, "Kembali (0)"); + + delay(4000); + tft.clear(); + display = '0'; + } else { + Serial.println("Failed to read data from the card."); + } + } else { + Serial.println("Authentication failed."); + delay(4000); + tft.clear(); + display = '0'; + } + } +} + +void cutoff() { + if (millis() >= time_1 + interval) { + time_1 = millis(); + printText(50, 10, "Cut Off Menu"); + printText(10, 30, "Power : " + power + "W"); + printText(10, 50, "Saldo : " + Sisa_saldo_strin); + printText(10, 70, "A. Nyala K1"); + printText(10, 90, "B. Mati K1"); + printText(10, 110, "C. Nyala K2"); + printText(10, 130, "D. Mati K2"); + } +} + +void printText(int x, int y, String text) { + int lineHeight = 16; // Default font height + int charWidth = 12; // Default font width + + 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; + } + } +} + +int readNominalFromKeypad() { + String input = ""; // String untuk menyimpan input dari keypad + while (true) { + char key = kpd.getKey(); + if (key != NO_KEY) { + if (key == '#') { // Jika '#' ditekan, proses input selesai + break; + } else { + input += key; // Menambahkan karakter ke string input + } + } + } + return input.toInt(); +} + +void writeNominalToCard(uint8_t block, uint8_t sector, int nominal) { + char message[16]; + snprintf(message, sizeof(message), "%d", nominal); + writeMessageToCard(block, sector, message); +} + +void writeMessageToCard(uint8_t block, uint8_t sector, char message[16]) { + uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; + uint8_t uidLength; + + if (nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength)) { + uint8_t keyData[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; + + if (nfc.mifareclassic_AuthenticateBlock(uid, uidLength, sector, 0xFF, keyData)) { + printText(10, 50, "Autentikasi "); + printText(10, 70, "Berhasil"); + printText(10, 90, "Kembali (0)"); + + uint8_t messageData[16]; + for (int i = 0; i < 16; i++) { + if (i < strlen(message)) { + messageData[i] = message[i]; + } else { + messageData[i] = 0x00; // Fill the remaining bytes with 0x00 + } + } + + if (nfc.mifareclassic_WriteDataBlock(block, messageData)) { + Serial.print("Writing Message to Block "); + Serial.print(block); + Serial.println(" successful."); + } else { + Serial.println("Failed to write data to the card."); + } + } else { + Serial.println("Authentication failed."); + } + } +} + +void displayMenu() { + printText(10, 10, "MENU"); + printText(10, 30, "1. ISI SALDO"); + printText(10, 50, "2. CEK SALDO"); + printText(10, 70, "3. CUT OFF"); +} + +void sendRelayCommand(const char* topic, const char* command) { + if (client.publish(topic, command)) { + tft.clear(); + printText(10, 10, "Command Sent:"); + printText(10, 30, command); + delay(2000); + tft.clear(); + display = '0'; + } else { + tft.clear(); + printText(10, 10, "Failed to send:"); + printText(10, 30, command); + delay(2000); + tft.clear(); + display = '0'; + } +}