Advertisement
muezza29

pzem oled bt nfc v3

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