-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKWH.ino
133 lines (104 loc) · 3.67 KB
/
KWH.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <PZEM004Tv30.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "Nice";
const char* password = "11111111";
// Alamat I2C LCD, dapat berbeda tergantung pada perangkat Anda
#define LCD_ADDRESS 0x27
// Jumlah kolom dan baris pada LCD
#define LCD_COLUMNS 16
#define LCD_ROWS 2
// Inisialisasi LCD
LiquidCrystal_I2C lcd(LCD_ADDRESS, LCD_COLUMNS, LCD_ROWS);
/* Hardware Serial2 is only available on certain boards.
* For example the Arduino MEGA 2560
*/
#if defined(ESP32)
PZEM004Tv30 pzem(Serial2, 16, 17);
#else
PZEM004Tv30 pzem(Serial2);
#endif
String watt,ampere,kwh;
void setup() {
Serial.begin(115200);
lcd.init(); // Inisialisasi LCD
lcd.backlight(); // Aktifkan backlight LCD
// Uncomment in order to reset the internal energy counter
// pzem.resetEnergy();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.println(".");
delay(1000);
}
Serial.println("WiFi connected");
}
void loop() {
Serial.print("Custom Address:");
Serial.println(pzem.readAddress(), HEX);
// Read the data from the sensor
float voltage = pzem.voltage();
float current = pzem.current();
float power = pzem.power();
float energy = pzem.energy();
float frequency = pzem.frequency();
float pf = pzem.pf();
// Check if the data is valid
if(isnan(voltage)){
Serial.println("Error reading voltage");
} else if (isnan(current)) {
Serial.println("Error reading current");
} else if (isnan(power)) {
Serial.println("Error reading power");
} else if (isnan(energy)) {
Serial.println("Error reading energy");
} else if (isnan(frequency)) {
Serial.println("Error reading frequency");
} else if (isnan(pf)) {
Serial.println("Error reading power factor");
} else {
// Print the values to the Serial console
Serial.print("Voltage: "); Serial.print(voltage); Serial.println("V");
Serial.print("Current: "); Serial.print(current); Serial.println("A");
Serial.print("Power: "); Serial.print(power); Serial.println("W");
Serial.print("Energy: "); Serial.print(energy,3); Serial.println("kWh");
Serial.print("Frequency: "); Serial.print(frequency, 1); Serial.println("Hz");
Serial.print("PF: "); Serial.println(pf);
Serial.print("RP: "); Serial.println((energy)*1500);
lcd.setCursor(0, 0); // Posisi kursor di kolom 0, baris 0
lcd.print(power);
watt = power;
lcd.print(" W ");
lcd.print(current);
ampere = current;
lcd.print(" A");
lcd.setCursor(0, 1); // Posisi kursor di kolom 0, baris 1
lcd.print(energy,2);
kwh = energy,2;
lcd.print(" kWh");
lcd.print(" RP ");
lcd.print(energy*1500);
}
Serial.println();
delay(2000);
SendData();
}
void SendData(){
HTTPClient http;
// Alamat API yang dituju
String serverAddress = "https://script.google.com/macros/s/AKfycbzo0LmvTZOk4arbrEMq9EjCUWzKczD-eI5w40RRKebjSyUe_CBI9oiNMjQG7wIDulNkIA/exec?watt="+watt+"&ere="+ampere+"&kwh="+kwh;
http.begin(serverAddress); // Mulai koneksi ke server
// Lakukan GET request
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
String response = http.getString(); // Dapatkan respons dari server
} else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
delay(2500);
}
http.end();
}