Advertisement
muezza29

pzem oled bt

Jun 30th, 2019
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.01 KB | None | 0 0
  1. #include <PZEM004T.h>             //library PZEM
  2.  
  3. #define relay (PB1)
  4. const int pinBuzzer = PB0;
  5.  
  6. #include "Arduino.h"              //library OLED
  7. #include "Wire.h"                 //library OLED
  8. #include "Adafruit_GFX.h"         //library OLED
  9. #include "Adafruit_SSD1306.h"     //library OLED
  10.  
  11. //oled pin = B6 B7 / SCL SDA
  12. ///pzem pin = A9 A10 / TX RX / Serial1
  13.  
  14. #define OLED_RESET 4
  15. Adafruit_SSD1306 display(OLED_RESET);
  16.  
  17. PZEM004T* pzem;
  18. IPAddress ip(192, 168, 1, 1);
  19.  
  20. void setup()
  21. {
  22.   pinMode(relay, OUTPUT);
  23.   digitalWrite(relay, HIGH);
  24.  
  25.   pinMode(pinBuzzer, OUTPUT);
  26.  
  27.   Serial.begin(115200); //serial untuk membaca PZEM
  28.   Serial2.begin(38400); //serial BT HC 05
  29.   Serial2.println("\nBismillah");
  30.  
  31.   pzem = new PZEM004T(&Serial1);
  32.   pzem->setAddress(ip);
  33.  
  34.   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  35.   display.clearDisplay();
  36. }
  37.  
  38. void loop()
  39. {
  40.   float v = pzem->voltage(ip);
  41.   if (v < 0.0) v = 0.0;
  42.   Serial.print(v);
  43.   Serial.println("V; ");
  44.  
  45.   float i = pzem->current(ip);
  46.   if (i < 0.0) i = 0.0;
  47.   {
  48.     Serial.print(i);
  49.     Serial.println("A; ");
  50.   }
  51.  
  52.   float p = pzem->power(ip);
  53.   if (p <= 0.0) p = 0.0;
  54.   {
  55.     Serial.print(p);
  56.     Serial.println("W; ");
  57.   }
  58.  
  59.   float e = pzem->energy(ip);
  60.   if (e < 0.0) e = 0.0;
  61.   {
  62.     Serial.print(e);
  63.     Serial.println("Wh; ");
  64.   }
  65.  
  66.   float cosphi;
  67.   float c;
  68.   if (v * i == 0)
  69.   {
  70.     Serial.println(c);
  71.     c = 0;
  72.   }
  73.   else
  74.   {
  75.     c = p / (v * i);
  76.   }
  77.  
  78.   float saldo;
  79.   float s = 10000;
  80.   float t = s - e;
  81.  
  82.   Serial.println();
  83.  
  84.   if (i >= 6)
  85.   {
  86.     digitalWrite(relay, LOW);
  87.     display.clearDisplay();
  88.     display.setTextSize(1);
  89.     display.setTextColor(WHITE);
  90.  
  91.     display.setCursor(0, 0);
  92.     display.print("Arus lebih!");
  93.     display.setCursor(0, 8);
  94.     display.print("Beban dimatikan");
  95.     display.display();
  96.   }
  97.  
  98.   if (t <= 10)
  99.   {
  100.     tone(pinBuzzer, 750, 250);
  101.   }
  102.  
  103.   if (t <= 0)
  104.   {
  105.     digitalWrite(relay, LOW);
  106.   }
  107.  
  108.   displayData(v, i, p, e, c, t);
  109. }
  110.  
  111.  
  112. void displayData(float tegangan, float arus, float daya, float energi, float cosphi, float token)
  113. {
  114.   display.clearDisplay();
  115.   display.setTextSize(1);
  116.   display.setTextColor(WHITE);
  117.  
  118.   display.setCursor(0, 0);
  119.   display.print("Tegangan: ");
  120.   display.setCursor(70, 0);
  121.   display.print(tegangan);
  122.   display.println(" V");
  123.  
  124.   display.setCursor(0, 8);
  125.   display.print("Arus: ");
  126.   display.setCursor(70, 8);
  127.   display.print(arus);
  128.   display.println(" A");
  129.  
  130.   display.setCursor(0, 16);
  131.   display.print("Daya: ");
  132.   display.setCursor(70, 16);
  133.   display.print(daya);
  134.   display.println(" W");
  135.  
  136.   display.setCursor(0, 24);
  137.   display.print("Cos Phi: ");
  138.   display.setCursor(70, 24);
  139.   display.print(cosphi);
  140.   display.println(" ");
  141.  
  142.   display.display();
  143.  
  144.   char buffer[200];
  145.  
  146.   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);
  147.   Serial2.println(buffer);
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement