muezza29

test integrasi bool aar global

Jul 4th, 2019
164
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.  
  53.   pzem = new PZEM004T(&Serial1);
  54.   pzem->setAddress(ip);
  55.  
  56.   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  57.   display.clearDisplay();
  58. }
  59.  
  60. void loop()
  61. {
  62.   float v = pzem->voltage(ip);
  63.   if (v < 0.0) v = 0.0;
  64.   Serial.print(v);
  65.   Serial.print("V; ");
  66.  
  67.   float i = pzem->current(ip);
  68.   if (i < 0.0) i = 0.0;
  69.   {
  70.     Serial.print(i);
  71.     Serial.print("A; ");
  72.   }
  73.  
  74.   float p = pzem->power(ip);
  75.   if (p <= 0.0) p = 0.0;
  76.   {
  77.     Serial.print(p);
  78.     Serial.print("W; ");
  79.   }
  80.  
  81.   float e = pzem->energy(ip);
  82.   if (e < 0.0) e = 0.0;
  83.   {
  84.     Serial.print(e);
  85.     Serial.print("Wh; ");
  86.   }
  87.  
  88.   float cosphi;
  89.   float c;
  90.   if (v * i == 0)
  91.   {
  92.     c = 0;
  93.   }
  94.   else
  95.   {
  96.     c = p / (v * i);
  97.   }
  98.  
  99.   float saldo;
  100.   float s = 100;
  101.   float t = s - e;
  102.   if (t <= 0.0) t = 0.0;
  103.  
  104.   Serial.println();
  105.  
  106.   if (i >= 4)
  107.   {
  108.     digitalWrite(relay, LOW);
  109.   }
  110.  
  111.   if (t <= 10)
  112.   {
  113.     tone(pinBuzzer, 750, 250);
  114.   }
  115.  
  116.   if (t <= 0)
  117.   {
  118.     digitalWrite(relay, LOW);
  119.   }
  120.   displayData(v, i, p, e, c, t);
  121.   // break loop atau lanjut loop
  122.   if (aar)
  123.   {
  124.     token();
  125.   }
  126.   else
  127.   {
  128.     displayData(v, i, p, e, c, t);
  129.   }
  130. }
  131. // STOP PZEM
  132.  
  133.  
  134. // START MONITOR PZEM BLUETOOTH & OLED
  135. void displayData(float tegangan, float arus, float daya, float energi, float cosphi, float token)
  136. {
  137.   display.clearDisplay();
  138.   display.setTextSize(1);
  139.   display.setTextColor(WHITE);
  140.  
  141.   display.setCursor(0, 0);
  142.   display.print("Tegangan: ");
  143.   display.setCursor(70, 0);
  144.   display.print(tegangan);
  145.   display.println(" V");
  146.  
  147.   display.setCursor(0, 8);
  148.   display.print("Arus: ");
  149.   display.setCursor(70, 8);
  150.   display.print(arus);
  151.   display.println(" A");
  152.  
  153.   display.setCursor(0, 16);
  154.   display.print("Daya: ");
  155.   display.setCursor(70, 16);
  156.   display.print(daya);
  157.   display.println(" W");
  158.  
  159.   display.setCursor(0, 24);
  160.   display.print("Token: ");
  161.   display.setCursor(70, 24);
  162.   display.print(token);
  163.   display.println(" Wh");
  164.  
  165.   display.display();
  166.  
  167.   char buffer[1000];
  168.  
  169.   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);
  170.   Serial2.println(buffer);
  171. }
  172. // STOP MONITOR PZEM BLUETOOTH & OLED
  173.  
  174.  
  175.  
  176. // START NFC
  177.  
  178. char input = 0;
  179.  
  180. void token(void)
  181. {
  182.   Serial2.begin(38400);
  183.  
  184.   if (Serial2.available() > 0)
  185.   {
  186.     input == Serial2.read();
  187.  
  188.     if (input == '1')
  189.       aar = true;
  190.     {
  191.       display.clearDisplay();
  192.       display.display();
  193.  
  194.       display.setCursor(0, 0);
  195.       display.print("Mulai NFC");
  196.       Serial.print("Mulai NFC");
  197. #ifndef ESP8266
  198. #endif
  199.       Serial.begin(9600);
  200.       //      Serial.println("Hello Arif!");
  201.  
  202.       nfc.begin();
  203.  
  204.       uint32_t versiondata = nfc.getFirmwareVersion();
  205.       if (! versiondata)
  206.       {
  207.         Serial.print("Didn't find PN53x board");
  208.         while (1); // halt
  209.       }
  210.  
  211.       nfc.SAMConfig();
  212.  
  213.       display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  214.       display.clearDisplay();
  215.  
  216.       display.setTextSize(1);
  217.       display.setTextColor(WHITE);
  218.  
  219.       Serial.println(" Tempelkan kartu pelanggan");
  220.  
  221.       display.setCursor(0, 0);
  222.       display.print("Tempelkan kartu");
  223.       display.setCursor(0, 8);
  224.       display.print("pelanggan");
  225.       display.display();
  226.     }
  227.   }
  228.   else
  229.   {
  230.     aar = false;
  231.   }
  232. }
  233.  
  234. String str1, str3;
  235. String str2 = "0x49 0xB3 0x9E 0x2B";
  236. void loop2(void)
  237. {
  238.   uint8_t success;
  239.   uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };
  240.   uint8_t uidLength;
  241.  
  242.   success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
  243.  
  244.   if (success)
  245.   {
  246.     String str22 = hexify(uid, uidLength);
  247.     results(str22);
  248.   }
  249. }
  250.  
  251. String hexify(uint8_t u[], byte len)
  252. {
  253.   String out = "";
  254.   for (byte i = 0; i < len; i++)
  255.   {
  256.     String a = String(u[i], HEX);
  257.     a.toUpperCase();
  258.     if (a.length() == 1) a = "0" + a;
  259.     out += "0x" + a + " ";
  260.   }
  261.   out.trim();
  262.   return out;
  263. }
  264.  
  265. void results(String str)
  266. {
  267.   if (str == str2)
  268.   {
  269.     display.clearDisplay();
  270.     display.setTextSize(1);
  271.     display.setTextColor(WHITE);
  272.  
  273.     display.setCursor(0, 0);
  274.     display.print("-> Pelanggan dikenali");
  275.     display.setCursor(0, 8);
  276.     display.print("   ID Pelanggan:");
  277.     display.setCursor(0, 16);
  278.     display.print("   1303167045");
  279.     display.display();
  280.  
  281.     Serial.println("\n  1303167045");
  282.   }
  283.   else
  284.   {
  285.     display.clearDisplay();
  286.     display.setTextSize(1);
  287.     display.setTextColor(WHITE);
  288.  
  289.     display.setCursor(0, 0);
  290.     display.print("-> Bukan pelanggan");
  291.     display.setCursor(0, 8);
  292.     display.print("   Gunakan tag");
  293.     display.setCursor(0, 16);
  294.     display.print("   lainnya");
  295.     display.display();
  296.  
  297.     Serial.println("\n  Bukan pelanggan");
  298.   }
  299.   Serial.print("");
  300. }
  301.  
  302. // STOP NFC
Advertisement
Add Comment
Please, Sign In to add comment