0% found this document useful (0 votes)
95 views3 pages

Arduino - Notepad PDF

This document contains Arduino code for a power monitoring system that measures voltage, current, temperature, and run time. It uses a PZEM-004T sensor to measure voltage and current from the power line. A Dallas temperature sensor measures ambient temperature. An RTC module keeps track of time and run time when voltage is above 200V. Readings are displayed on an I2C LCD and sent via serial to another device in a formatted string every second. Relay control pins allow the load to be turned on/off remotely.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
95 views3 pages

Arduino - Notepad PDF

This document contains Arduino code for a power monitoring system that measures voltage, current, temperature, and run time. It uses a PZEM-004T sensor to measure voltage and current from the power line. A Dallas temperature sensor measures ambient temperature. An RTC module keeps track of time and run time when voltage is above 200V. Readings are displayed on an I2C LCD and sent via serial to another device in a formatted string every second. Relay control pins allow the load to be turned on/off remotely.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 3

arduino

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h> // Arduino IDE <1.6.6
#include <PZEM004T.h>
#include <OneWire.h>
#include <DallasTemperature.h>

#include "RTClib.h"
#define pin_relay 12
#define ONE_WIRE_BUS A0

#define relay_on digitalWrite(pin_relay,HIGH)


#define relay_off digitalWrite(pin_relay,LOW)

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

LiquidCrystal_I2C lcd(0x27, 16, 2);


RTC_Millis rtc;

byte second = 0;
byte minute = 0;
byte hour = 0;
byte weekday = 0;
byte monthday = 0;
byte month = 0;
int year = 0;
const int DS1307 = 0x68; // Alamat pin RTC pada arduino

PZEM004T pzem(2,3); // (RX,TX) connect to TX,RX of PZEM


IPAddress ip(192,168,1,1);

void setup()
{
pinMode(pin_relay,OUTPUT);
Wire.begin();
Serial.begin(9600);
pzem.setAddress(ip);
sensors.begin();
lcd.begin();
lcd.print("POWER MONITOR");
relay_off;
}

float v,i,p,c;
int suhu;
long last_millis = millis(),HM,HMmenit;
int index,lastminute;

void loop(){

Page 1
arduino
if (Serial.available()){
String data = Serial.readStringUntil('\n');
int a = data.indexOf("A");
int b =data.indexOf("B");
if (b>0){
data=data.substring(a+1,b);
int dat= data.length();
if(dat==1){
if (data=="1"){
relay_on;
}else if (data=="2"){
relay_off;
}
}else{
cek(data);
}
}
}
float v = pzem.voltage(ip);
if (v < 0.0) v = 0.0;

float i = pzem.current(ip);
if(i <= 0.0){ i=0;}

sensors.requestTemperatures(); // Send the command to get temperatures


suhu= sensors.getTempCByIndex(0);

time_read();
if (v>200){
if (lastminute!=minute){
lastminute=minute;
HMmenit++;
if (HMmenit>=60){HM++;HMmenit=0;}
}
}
lcd.setCursor(0,0);
lcd.print(karakter(hour));
lcd.print(":");
lcd.print(karakter(minute));
lcd.print(":");
lcd.print(karakter(second));
lcd.print(" ");
lcd.print(karakter(HM));
lcd.print(":");
lcd.print(karakter(HMmenit));
lcd.print(" ");

lcd.setCursor(0,1);
lcd.print("V");
lcd.print((int)v);
lcd.print(" I");
Page 2
arduino
lcd.print(i);
lcd.print(" T");
lcd.print(suhu);
lcd.print(" ");

if (millis() - last_millis >= 1000) {


String protokol = "A" + String((int)v) + "B" + String(i) +
"C"+String(suhu)+"D"+karakter(HM)+":"+karakter(HMmenit)+"E";
protokol=protokol+protokol.length()+"F";
Serial.println(protokol);
last_millis = millis();
}

Page 3

You might also like