Advertisement
muezza29

PZEM & OLED

Jun 29th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.95 KB | None | 0 0
  1. #include <PZEM004T.h>             //library PZEM
  2.  
  3. #include "Arduino.h"              //library OLED
  4. #include "Wire.h"                 //library OLED
  5. #include "Adafruit_GFX.h"         //library OLED
  6. #include "Adafruit_SSD1306.h"     //library OLED
  7.  
  8. //#include "mbed.h"
  9.  
  10. //oled pin = B6 B7 / SCL SDA
  11. ///pzem pin = A9 A10 / TX RX / Serial1
  12.  
  13. #define OLED_RESET 4
  14. Adafruit_SSD1306 display(OLED_RESET);
  15.  
  16. PZEM004T* pzem;
  17. IPAddress ip(192, 168, 1, 1);
  18.  
  19. void setup()
  20. {
  21.   while (!Serial) { }
  22.   Serial.begin(9600); //serial untuk membaca data serial
  23.  
  24.   while (!Serial1) { } //serial untuk debugging
  25.   pzem = new PZEM004T(&Serial1);
  26.   pzem->setAddress(ip);
  27.  
  28.   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  29.   display.clearDisplay();
  30. }
  31.  
  32. void loop() {
  33.   float v = pzem->voltage(ip);
  34.   if (v < 0.0) v = 0.0;
  35.   Serial.print(v);
  36.   Serial.print("V; ");
  37.  
  38.   float i = pzem->current(ip);
  39.   if (i >= 0.0) {
  40.     Serial.print(i);
  41.     Serial.print("A; ");
  42.   }
  43.  
  44.   float p = pzem->power(ip);
  45.   if (p >= 0.0) {
  46.     Serial.print(p);
  47.     Serial.print("W; ");
  48.   }
  49.  
  50.   float e = pzem->energy(ip);
  51.   if (e >= 0.0) {
  52.     Serial.print(e);
  53.     Serial.print("Wh; ");
  54.   }
  55.   Serial.println();
  56.  
  57.   displayData(v, i, p, e);
  58. }
  59.  
  60. void displayData(float tegangan, float arus, float daya, float energi)
  61. {
  62.   display.setTextSize(1);
  63.   display.setTextColor(WHITE);
  64.  
  65.   display.setCursor(0, 0);
  66.   display.print("Tegangan: ");
  67.   display.setCursor(80, 0);
  68.   display.print(tegangan);
  69.   display.println(" V");
  70.  
  71.   display.setCursor(0, 8);
  72.   display.print("Arus: ");
  73.   display.setCursor(80, 8);
  74.   display.print(arus);
  75.   display.println(" A");
  76.  
  77.   display.setCursor(0, 16);
  78.   display.print("Daya: ");
  79.   display.setCursor(80, 16);
  80.   display.print(daya);
  81.   display.println(" W");
  82.  
  83.   display.setCursor(0, 24);
  84.   display.print("Energi: ");
  85.   display.setCursor(80, 24);
  86.   display.print(energi);
  87.   display.println(" Wh");
  88.  
  89.   display.display();
  90.  
  91.   display.clearDisplay();
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement