muezza29

pzem oled bt nfc

Jul 2nd, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.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.  
  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 = "0xC6 0xE6 0x0F 0x1E";
  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("1303167045");
  103.     display.display();
  104.  
  105.     Serial.println("\n  1303167045");
  106.   }
  107.   else
  108.   {
  109.     display.clearDisplay();
  110.     display.setTextSize(1);
  111.     display.setTextColor(WHITE);
  112.  
  113.     display.setCursor(0, 0);
  114.     display.print("Bukan pelanggan");
  115.     display.display();
  116.  
  117.     Serial.println("\n  Bukan pelanggan");
  118.   }
  119.   Serial.print("");
  120. }
  121. // STOP NFC
  122.  
  123. void mulai()
  124. {
  125.   Serial.begin(115200);   //serial untuk membaca PZEM
  126.   Serial2.begin(9600);    //serial BT HC 05
  127.   Serial2.println("\nBismillah");
  128.  
  129.   pzem = new PZEM004T(&Serial1);
  130.   pzem->setAddress(ip);
  131.  
  132.   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  133.   display.clearDisplay();
  134. }
  135.  
  136. void looping()
  137. {
  138.   float v = pzem->voltage(ip);
  139.   if (v < 0.0) v = 0.0;
  140.   Serial.print(v);
  141.   Serial.print("V; ");
  142.  
  143.   float i = pzem->current(ip);
  144.   if (i >= 0.0)
  145.   {
  146.     Serial.print(i);
  147.     Serial.print("A; ");
  148.   }
  149.  
  150.   float p = pzem->power(ip);
  151.   if (p >= 0.0)
  152.   {
  153.     Serial.print(p);
  154.     Serial.print("W; ");
  155.   }
  156.  
  157.   float e = pzem->energy(ip);
  158.   if (e >= 0.0)
  159.   {
  160.     Serial.print(e);
  161.     Serial.print("Wh; ");
  162.   }
  163.  
  164.   float c = p / (v * i);
  165.   float cosphi;
  166.  
  167.   Serial.println();
  168.  
  169.   displayData(v, i, p, e, c);
  170. }
  171.  
  172. void displayData(float tegangan, float arus, float daya, float energi, float cosphi)
  173. {
  174.   display.clearDisplay();
  175.   display.setTextSize(1);
  176.   display.setTextColor(WHITE);
  177.   display.setCursor(0, 0);
  178.   display.print("Tegangan: ");
  179.   display.setCursor(80, 0);
  180.   display.print(tegangan);
  181.   display.println(" V");
  182.  
  183.   display.setCursor(0, 8);
  184.   display.print("Arus: ");
  185.   display.setCursor(80, 8);
  186.   display.print(arus);
  187.   display.println(" A");
  188.  
  189.   display.setCursor(0, 16);
  190.   display.print("Daya: ");
  191.   display.setCursor(80, 16);
  192.   display.print(daya);
  193.   display.println(" W");
  194.  
  195.   display.setCursor(0, 24);
  196.   display.print("Energi: ");
  197.   display.setCursor(80, 24);
  198.   display.print(energi);
  199.   display.println(" Wh");
  200.  
  201.   display.display();
  202.  
  203.   char buffer[200];
  204.  
  205.   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);
  206.   Serial2.println(buffer);
  207. }
Advertisement
Add Comment
Please, Sign In to add comment