Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <PZEM004T.h> //library PZEM
- #define relay (PB1)
- const int pinBuzzer = PB0;
- #include "Arduino.h" //library OLED
- #include "Wire.h" //library OLED
- #include "Adafruit_GFX.h" //library OLED
- #include "Adafruit_SSD1306.h" //library OLED
- //oled pin = B6 B7 / SCL SDA
- ///pzem pin = A9 A10 / TX RX / Serial1
- #define OLED_RESET 4
- Adafruit_SSD1306 display(OLED_RESET);
- PZEM004T* pzem;
- IPAddress ip(192, 168, 1, 1);
- void setup()
- {
- pinMode(relay, OUTPUT);
- digitalWrite(relay, HIGH);
- pinMode(pinBuzzer, OUTPUT);
- Serial.begin(115200); //serial untuk membaca PZEM
- Serial2.begin(38400); //serial BT HC 05
- Serial2.println("\nBismillah");
- pzem = new PZEM004T(&Serial1);
- pzem->setAddress(ip);
- display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
- display.clearDisplay();
- }
- void loop()
- {
- float v = pzem->voltage(ip);
- if (v < 0.0) v = 0.0;
- Serial.print(v);
- Serial.println("V; ");
- float i = pzem->current(ip);
- if (i < 0.0) i = 0.0;
- {
- Serial.print(i);
- Serial.println("A; ");
- }
- float p = pzem->power(ip);
- if (p <= 0.0) p = 0.0;
- {
- Serial.print(p);
- Serial.println("W; ");
- }
- float e = pzem->energy(ip);
- if (e < 0.0) e = 0.0;
- {
- Serial.print(e);
- Serial.println("Wh; ");
- }
- float cosphi;
- float c;
- if (v * i == 0)
- {
- Serial.println(c);
- c = 0;
- }
- else
- {
- c = p / (v * i);
- }
- float saldo;
- float s = 10000;
- float t = s - e;
- Serial.println();
- if (i >= 6)
- {
- digitalWrite(relay, LOW);
- display.clearDisplay();
- display.setTextSize(1);
- display.setTextColor(WHITE);
- display.setCursor(0, 0);
- display.print("Arus lebih!");
- display.setCursor(0, 8);
- display.print("Beban dimatikan");
- display.display();
- }
- if (t <= 10)
- {
- tone(pinBuzzer, 750, 250);
- }
- if (t <= 0)
- {
- digitalWrite(relay, LOW);
- }
- displayData(v, i, p, e, c, t);
- }
- void displayData(float tegangan, float arus, float daya, float energi, float cosphi, float token)
- {
- display.clearDisplay();
- display.setTextSize(1);
- display.setTextColor(WHITE);
- display.setCursor(0, 0);
- display.print("Tegangan: ");
- display.setCursor(70, 0);
- display.print(tegangan);
- display.println(" V");
- display.setCursor(0, 8);
- display.print("Arus: ");
- display.setCursor(70, 8);
- display.print(arus);
- display.println(" A");
- display.setCursor(0, 16);
- display.print("Daya: ");
- display.setCursor(70, 16);
- display.print(daya);
- display.println(" W");
- display.setCursor(0, 24);
- display.print("Cos Phi: ");
- display.setCursor(70, 24);
- display.print(cosphi);
- display.println(" ");
- display.display();
- char buffer[200];
- 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);
- Serial2.println(buffer);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement