Advertisement
muezza29

pzem oled bt nfc v2

Jul 2nd, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.69 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.  
  38. // START NFC
  39. void setup(void)
  40. {
  41. #ifndef ESP8266
  42. #endif
  43.   Serial.begin(115200);
  44.   Serial.println("Hello Arif!");
  45.  
  46.   nfc.begin();
  47.  
  48.   uint32_t versiondata = nfc.getFirmwareVersion();
  49.   if (! versiondata) {
  50.     Serial.print("Didn't find PN53x board");
  51.     while (1); // halt
  52.   }
  53.  
  54.   nfc.SAMConfig();
  55.  
  56.   Serial.println("  Tempelkan kartu pelanggan");
  57.  
  58.   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  59.   display.clearDisplay();
  60. }
  61.  
  62. String str1, str3;
  63. String str2 = "0x49 0xB3 0x9E 0x2B";
  64. void loop(void)
  65. {
  66.   uint8_t success;
  67.   uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };
  68.   uint8_t uidLength;
  69.  
  70.   success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
  71.  
  72.   if (success)
  73.   {
  74.     String str22 = hexify(uid, uidLength);
  75.     results(str22);
  76.   }
  77. }
  78.  
  79. String hexify(uint8_t u[], byte len)
  80. {
  81.   String out = "";
  82.   for (byte i = 0; i < len; i++)
  83.   {
  84.     String a = String(u[i], HEX);
  85.     a.toUpperCase();
  86.     if (a.length() == 1) a = "0" + a;
  87.     out += "0x" + a + " ";
  88.   }
  89.   out.trim();
  90.   return out;
  91. }
  92.  
  93. void results(String str)
  94. {
  95.   if (str == str2)
  96.   {
  97.     display.clearDisplay();
  98.     display.setTextSize(1);
  99.     display.setTextColor(WHITE);
  100.  
  101.     display.setCursor(0, 0);
  102.     display.print("-> Pelanggan dikenali");
  103.     display.setCursor(0, 8);
  104.     display.print("   ID Pelanggan:");
  105.     display.setCursor(0, 16);
  106.     display.print("   1303167045");
  107.     display.display();
  108.  
  109.     Serial.println("\n  1303167045");
  110.   }
  111.   else
  112.   {
  113.     display.clearDisplay();
  114.     display.setTextSize(1);
  115.     display.setTextColor(WHITE);
  116.  
  117.     display.setCursor(0, 0);
  118.     display.print("-> Bukan pelanggan");
  119.     display.setCursor(0, 8);
  120.     display.print("   Gunakan tag");  
  121.     display.setCursor(0, 16);
  122.     display.print("   lainnya");
  123.     display.display();
  124.  
  125.     Serial.println("\n  Bukan pelanggan");
  126.   }
  127.   Serial.print("");
  128. }
  129. // STOP NFC
  130.  
  131. void mulai()
  132. {
  133.   Serial.begin(115200);   //serial untuk membaca PZEM
  134.  
  135.   pzem = new PZEM004T(&Serial1);
  136.   pzem->setAddress(ip);
  137.  
  138.   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  139.   display.clearDisplay();
  140. }
  141.  
  142. void looping()
  143. {
  144.   float v = pzem->voltage(ip);
  145.   if (v < 0.0) v = 0.0;
  146.   Serial.print(v);
  147.   Serial.print("V; ");
  148.  
  149.   float i = pzem->current(ip);
  150.   if (i >= 0.0)
  151.   {
  152.     Serial.print(i);
  153.     Serial.print("A; ");
  154.   }
  155.  
  156.   float p = pzem->power(ip);
  157.   if (p >= 0.0)
  158.   {
  159.     Serial.print(p);
  160.     Serial.print("W; ");
  161.   }
  162.  
  163.   float e = pzem->energy(ip);
  164.   if (e >= 0.0)
  165.   {
  166.     Serial.print(e);
  167.     Serial.print("Wh; ");
  168.   }
  169.  
  170.   float c = p / (v * i);
  171.   float cosphi;
  172.  
  173.   Serial.println();
  174.  
  175.   displayData(v, i, p, e, c);
  176. }
  177.  
  178. void displayData(float tegangan, float arus, float daya, float energi, float cosphi)
  179. {
  180.   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  181.   display.clearDisplay();
  182.  
  183.   Serial2.begin(9600);    //serial BT HC 05
  184.   Serial2.println("\nBismillah");
  185.  
  186.   char input = 0;
  187.  
  188.   if (Serial2.available() > 0)
  189.   {
  190.     input = Serial2.read();
  191.     if (input == '1')
  192.     {
  193.       display.clearDisplay();
  194.       display.setTextSize(1);
  195.       display.setTextColor(WHITE);
  196.       display.setCursor(0, 0);
  197.       display.print("Tegangan: ");
  198.       display.setCursor(80, 0);
  199.       display.print(tegangan);
  200.       display.println(" V");
  201.  
  202.       display.setCursor(0, 8);
  203.       display.print("Arus: ");
  204.       display.setCursor(80, 8);
  205.       display.print(arus);
  206.       display.println(" A");
  207.  
  208.       display.setCursor(0, 16);
  209.       display.print("Daya: ");
  210.       display.setCursor(80, 16);
  211.       display.print(daya);
  212.       display.println(" W");
  213.  
  214.       display.setCursor(0, 24);
  215.       display.print("Energi: ");
  216.       display.setCursor(80, 24);
  217.       display.print(energi);
  218.       display.println(" Wh");
  219.  
  220.       display.display();
  221.  
  222.       char buffer[200];
  223.  
  224.       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);
  225.       Serial2.println(buffer);
  226.     }
  227.   }
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement