Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <PZEM004T.h> //library PZEM
- #include "Arduino.h" //library OLED
- #include "Wire.h" //library OLED
- #include "Adafruit_GFX.h" //library OLED
- #include "Adafruit_SSD1306.h" //library OLED
- //#include "mbed.h"
- //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()
- {
- while (!Serial) { }
- Serial.begin(9600); //serial untuk membaca data serial
- while (!Serial1) { } //serial untuk debugging
- 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.print("V; ");
- float i = pzem->current(ip);
- if (i >= 0.0) {
- Serial.print(i);
- Serial.print("A; ");
- }
- float p = pzem->power(ip);
- if (p >= 0.0) {
- Serial.print(p);
- Serial.print("W; ");
- }
- float e = pzem->energy(ip);
- if (e >= 0.0) {
- Serial.print(e);
- Serial.print("Wh; ");
- }
- Serial.println();
- displayData(v, i, p, e);
- }
- void displayData(float tegangan, float arus, float daya, float energi)
- {
- display.setTextSize(1);
- display.setTextColor(WHITE);
- display.setCursor(0, 0);
- display.print("Tegangan: ");
- display.setCursor(80, 0);
- display.print(tegangan);
- display.println(" V");
- display.setCursor(0, 8);
- display.print("Arus: ");
- display.setCursor(80, 8);
- display.print(arus);
- display.println(" A");
- display.setCursor(0, 16);
- display.print("Daya: ");
- display.setCursor(80, 16);
- display.print(daya);
- display.println(" W");
- display.setCursor(0, 24);
- display.print("Energi: ");
- display.setCursor(80, 24);
- display.print(energi);
- display.println(" Wh");
- display.display();
- display.clearDisplay();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement