Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // library PZEM
- #include <PZEM004T.h>
- // Libary OLED
- #include "Arduino.h"
- #include "Wire.h"
- #include "Adafruit_GFX.h"
- #include "Adafruit_SSD1306.h"
- // library NFC
- #include <SPI.h>
- #include <Adafruit_PN532.h>
- #define PN532_SCK (PA5)
- #define PN532_MOSI (PA7)
- #define PN532_SS (PA4)
- #define PN532_MISO (PA6)
- #define PN532_IRQ (2)
- #define PN532_RESET (3)
- Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);
- #if defined(ARDUINO_ARCH_SAMD)
- #define Serial SerialUSB
- #endif
- // oled pin = B6 B7 / SCL SDA
- // pzem pin = A9 A10 / TX RX / Serial1
- // nfc pin = SPI
- // bt pin = serial uart 2
- #define OLED_RESET 4
- Adafruit_SSD1306 display(OLED_RESET);
- PZEM004T* pzem;
- IPAddress ip(192, 168, 1, 1);
- // START NFC
- void setup(void)
- {
- #ifndef ESP8266
- #endif
- Serial.begin(115200);
- Serial.println("Hello Arif!");
- nfc.begin();
- uint32_t versiondata = nfc.getFirmwareVersion();
- if (! versiondata) {
- Serial.print("Didn't find PN53x board");
- while (1); // halt
- }
- nfc.SAMConfig();
- Serial.println(" Tempelkan kartu pelanggan");
- display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
- display.clearDisplay();
- }
- String str1, str3;
- String str2 = "0x49 0xB3 0x9E 0x2B";
- void loop(void)
- {
- 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)
- {
- String str22 = hexify(uid, uidLength);
- results(str22);
- }
- }
- String hexify(uint8_t u[], byte len)
- {
- String out = "";
- for (byte i = 0; i < len; i++)
- {
- String a = String(u[i], HEX);
- a.toUpperCase();
- if (a.length() == 1) a = "0" + a;
- out += "0x" + a + " ";
- }
- out.trim();
- return out;
- }
- void results(String str)
- {
- if (str == str2)
- {
- display.clearDisplay();
- display.setTextSize(1);
- display.setTextColor(WHITE);
- display.setCursor(0, 0);
- display.print("-> Pelanggan dikenali");
- display.setCursor(0, 8);
- display.print(" ID Pelanggan:");
- display.setCursor(0, 16);
- display.print(" 1303167045");
- display.display();
- Serial.println("\n 1303167045");
- }
- else
- {
- display.clearDisplay();
- display.setTextSize(1);
- display.setTextColor(WHITE);
- display.setCursor(0, 0);
- display.print("-> Bukan pelanggan");
- display.setCursor(0, 8);
- display.print(" Gunakan tag");
- display.setCursor(0, 16);
- display.print(" lainnya");
- display.display();
- Serial.println("\n Bukan pelanggan");
- }
- Serial.print("");
- }
- // STOP NFC
- void mulai()
- {
- Serial.begin(115200); //serial untuk membaca PZEM
- pzem = new PZEM004T(&Serial1);
- pzem->setAddress(ip);
- display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
- display.clearDisplay();
- }
- void looping()
- {
- float v = pzem->voltage(ip);
- if (v < 0.0) v = 0.0;
- Serial.print(v);
- Serial.print("V; ");
- float i = pzem->current(ip);
- if (i >= 0.0)
- {
- Serial.print(i);
- Serial.print("A; ");
- }
- float p = pzem->power(ip);
- if (p >= 0.0)
- {
- Serial.print(p);
- Serial.print("W; ");
- }
- float e = pzem->energy(ip);
- if (e >= 0.0)
- {
- Serial.print(e);
- Serial.print("Wh; ");
- }
- float c = p / (v * i);
- float cosphi;
- Serial.println();
- displayData(v, i, p, e, c);
- }
- void displayData(float tegangan, float arus, float daya, float energi, float cosphi)
- {
- display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
- display.clearDisplay();
- Serial2.begin(9600); //serial BT HC 05
- Serial2.println("\nBismillah");
- char input = 0;
- if (Serial2.available() > 0)
- {
- input = Serial2.read();
- if (input == '1')
- {
- display.clearDisplay();
- display.setTextSize(1);
- display.setTextColor(WHITE);
- display.setCursor(0, 0);
- display.print("Tegangan: ");
- display.setCursor(80, 0);
- display.print(tegangan);
- display.println(" V");
- display.setCursor(0, 8);
- display.print("Arus: ");
- display.setCursor(80, 8);
- display.print(arus);
- display.println(" A");
- display.setCursor(0, 16);
- display.print("Daya: ");
- display.setCursor(80, 16);
- display.print(daya);
- display.println(" W");
- display.setCursor(0, 24);
- display.print("Energi: ");
- display.setCursor(80, 24);
- display.print(energi);
- display.println(" Wh");
- display.display();
- char buffer[200];
- sprintf(buffer, "Tegangan : %0.2f V\nArus : %0.2f A\nDaya : %0.2f W\nEnergi : %0.2f Wh\nCos Phi : %0.2f \n", tegangan, arus, daya, energi, cosphi);
- Serial2.println(buffer);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement