Advertisement
muezza29

integrasi + callNFC

Jul 4th, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.83 KB | None | 0 0
  1. // library PZEM
  2. #include <PZEM004T.h>
  3.  
  4. #define relay (PB1)
  5. const int pinBuzzer = PB0;
  6.  
  7. // Libary OLED
  8. #include "Arduino.h"
  9. #include "Wire.h"
  10. #include "Adafruit_GFX.h"
  11. #include "Adafruit_SSD1306.h"
  12.  
  13. // library NFC
  14. #include <SPI.h>
  15. #include <Adafruit_PN532.h>
  16.  
  17. #define PN532_SCK  (PA5)
  18. #define PN532_MOSI (PA7)
  19. #define PN532_SS   (PA4)
  20. #define PN532_MISO (PA6)
  21. #define PN532_IRQ   (2)
  22. #define PN532_RESET (3)
  23.  
  24. Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);
  25. #if defined(ARDUINO_ARCH_SAMD)
  26. #define Serial SerialUSB
  27. #endif
  28.  
  29. // oled pin = B6 B7 / SCL SDA
  30. // pzem pin = A9 A10 / TX RX / Serial1
  31. // nfc pin = SPI
  32. // bt pin = serial uart 2
  33.  
  34. #define OLED_RESET 4
  35. Adafruit_SSD1306 display(OLED_RESET);
  36.  
  37. PZEM004T* pzem;
  38. IPAddress ip(192, 168, 1, 1);
  39.  
  40. bool aar = false;
  41. // START PZEM
  42. void setup()
  43. {
  44.   pinMode(relay, OUTPUT);
  45.   digitalWrite(relay, HIGH);
  46.  
  47.   pinMode(pinBuzzer, OUTPUT);
  48.  
  49.   Serial.begin(115200);   //serial untuk membaca PZEM
  50.   Serial2.begin(38400); //serial BT HC 05
  51.   Serial2.println("\nBismillah");
  52.   Serial2.println("\nBluetooth Connected");
  53.  
  54.   pzem = new PZEM004T(&Serial1);
  55.   pzem->setAddress(ip);
  56.  
  57.   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  58.   display.clearDisplay();
  59. }
  60.  
  61. void loop()
  62. {
  63.   float v = pzem->voltage(ip);
  64.   if (v < 0.0) v = 0.0;
  65.   Serial.print(v);
  66.   Serial.print("V; ");
  67.  
  68.   float i = pzem->current(ip);
  69.   if (i < 0.0) i = 0.0;
  70.   {
  71.     Serial.print(i);
  72.     Serial.print("A; ");
  73.   }
  74.  
  75.   float p = pzem->power(ip);
  76.   if (p <= 0.0) p = 0.0;
  77.   {
  78.     Serial.print(p);
  79.     Serial.print("W; ");
  80.   }
  81.  
  82.   float e = pzem->energy(ip);
  83.   if (e < 0.0) e = 0.0;
  84.   {
  85.     Serial.print(e);
  86.     Serial.print("Wh; ");
  87.   }
  88.  
  89.   float cosphi;
  90.   float c;
  91.   if (v * i == 0)
  92.   {
  93.     c = 0;
  94.   }
  95.   else
  96.   {
  97.     c = p / (v * i);
  98.   }
  99.  
  100.   float saldo;
  101.   float s = 100;
  102.   float t = s - e;
  103.   if (t <= 0.0) t = 0.0;
  104.  
  105.   Serial.println();
  106.  
  107.   if (i >= 4)
  108.   {
  109.     digitalWrite(relay, LOW);
  110.   }
  111.  
  112.   if (t <= 10)
  113.   {
  114.     tone(pinBuzzer, 750, 250);
  115.   }
  116.  
  117.   if (t <= 0)
  118.   {
  119.     digitalWrite(relay, LOW);
  120.   }
  121.   displayData(v, i, p, e, c, t);
  122.   // break loop atau lanjut loop
  123.   if (aar)
  124.   {
  125.     token();
  126.   }
  127.   else
  128.   {
  129.     displayData(v, i, p, e, c, t);
  130.   }
  131. }
  132. // STOP PZEM
  133.  
  134.  
  135. // START MONITOR PZEM BLUETOOTH & OLED
  136. void displayData(float tegangan, float arus, float daya, float energi, float cosphi, float token)
  137. {
  138.   display.clearDisplay();
  139.   display.setTextSize(1);
  140.   display.setTextColor(WHITE);
  141.  
  142.   display.setCursor(0, 0);
  143.   display.print("Tegangan: ");
  144.   display.setCursor(70, 0);
  145.   display.print(tegangan);
  146.   display.println(" V");
  147.  
  148.   display.setCursor(0, 8);
  149.   display.print("Arus: ");
  150.   display.setCursor(70, 8);
  151.   display.print(arus);
  152.   display.println(" A");
  153.  
  154.   display.setCursor(0, 16);
  155.   display.print("Daya: ");
  156.   display.setCursor(70, 16);
  157.   display.print(daya);
  158.   display.println(" W");
  159.  
  160.   display.setCursor(0, 24);
  161.   display.print("Token: ");
  162.   display.setCursor(70, 24);
  163.   display.print(token);
  164.   display.println(" Wh");
  165.  
  166.   display.display();
  167.  
  168.   char buffer[1000];
  169.  
  170.   sprintf(buffer, "Tegangan  : %0.2f V\nArus           : %0.2f A\nDaya          : %0.2f W\nEnergi        : %0.2f Wh\nCos Phi       : %0.2f \nToken          : %0.2f Wh\n", tegangan, arus, daya, energi, cosphi, token);
  171.   Serial2.println(buffer);
  172. }
  173. // STOP MONITOR PZEM BLUETOOTH & OLED
  174.  
  175.  
  176.  
  177. // START NFC
  178.  
  179. char input = 0;
  180. void callNFC(void)
  181. {
  182.   Serial2.begin(38400);
  183.   if (Serial2.available() > 0)
  184.   {
  185.     input == Serial2.read();
  186.  
  187.     if (input == '1')
  188.       aar = true;
  189.     {
  190.       display.clearDisplay();
  191.       display.display();
  192.  
  193.       display.setCursor(0, 0);
  194.       display.print("Mulai NFC");
  195.       Serial.print("Mulai NFC");
  196.       token();
  197.     }
  198.   }
  199.   else
  200.   {
  201.     aar = false;
  202.   }
  203. }
  204.  
  205. void token(void)
  206. {
  207. #ifndef ESP8266
  208. #endif
  209.   Serial.begin(9600);
  210.   //      Serial.println("Hello Arif!");
  211.  
  212.   nfc.begin();
  213.  
  214.   uint32_t versiondata = nfc.getFirmwareVersion();
  215.   if (! versiondata)
  216.   {
  217.     Serial.print("Didn't find PN53x board");
  218.     while (1); // halt
  219.   }
  220.  
  221.   nfc.SAMConfig();
  222.  
  223.   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  224.   display.clearDisplay();
  225.  
  226.   display.setTextSize(1);
  227.   display.setTextColor(WHITE);
  228.  
  229.   Serial.println(" Tempelkan kartu pelanggan");
  230.  
  231.   display.setCursor(0, 0);
  232.   display.print("Tempelkan kartu");
  233.   display.setCursor(0, 8);
  234.   display.print("pelanggan");
  235.   display.display();
  236. }
  237.  
  238. String str1, str3;
  239. String str2 = "0x49 0xB3 0x9E 0x2B";
  240. void loop2(void)
  241. {
  242.   uint8_t success;
  243.   uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };
  244.   uint8_t uidLength;
  245.  
  246.   success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
  247.  
  248.   if (success)
  249.   {
  250.     String str22 = hexify(uid, uidLength);
  251.     results(str22);
  252.   }
  253. }
  254.  
  255. String hexify(uint8_t u[], byte len)
  256. {
  257.   String out = "";
  258.   for (byte i = 0; i < len; i++)
  259.   {
  260.     String a = String(u[i], HEX);
  261.     a.toUpperCase();
  262.     if (a.length() == 1) a = "0" + a;
  263.     out += "0x" + a + " ";
  264.   }
  265.   out.trim();
  266.   return out;
  267. }
  268.  
  269. void results(String str)
  270. {
  271.   if (str == str2)
  272.   {
  273.     display.clearDisplay();
  274.     display.setTextSize(1);
  275.     display.setTextColor(WHITE);
  276.  
  277.     display.setCursor(0, 0);
  278.     display.print("-> Pelanggan dikenali");
  279.     display.setCursor(0, 8);
  280.     display.print("   ID Pelanggan:");
  281.     display.setCursor(0, 16);
  282.     display.print("   1303167045");
  283.     display.display();
  284.  
  285.     Serial.println("\n  1303167045");
  286.   }
  287.   else
  288.   {
  289.     display.clearDisplay();
  290.     display.setTextSize(1);
  291.     display.setTextColor(WHITE);
  292.  
  293.     display.setCursor(0, 0);
  294.     display.print("-> Bukan pelanggan");
  295.     display.setCursor(0, 8);
  296.     display.print("   Gunakan tag");
  297.     display.setCursor(0, 16);
  298.     display.print("   lainnya");
  299.     display.display();
  300.  
  301.     Serial.println("\n  Bukan pelanggan");
  302.   }
  303.   Serial.print("");
  304. }
  305.  
  306. // STOP NFC
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement