Translate

Monitor Daya (Arus dan Tegangan) KWH Server Thingspeak

Monitor Daya (Arus dan Tegangan) KWH Server Thingspeak


       Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah alat yang dapat memonitor tegangan arus dan daya dengan menggunakan sensor PZEM-004t dan server yang dipakai adalah Thingspeak.org untuk lebih jelasnya berikut adalah komponen dan programnya.


a. Komponen Penyusun




b. Program Arduino IDE

#include <PZEM004Tv30.h>
#include <Wire.h>  
#include <LiquidCrystal_I2C.h>  
PZEM004Tv30 pzem(2, 3);
LiquidCrystal_I2C lcd(0x27, 16, 2);

int relay1 = 6;
int relay2 = 7;
float voltage;
float current;
float power;
float energy;

int tegangan;
int arus;
int daya;
int kwh;

void setup() {
  Serial.begin(9600);
  lcd.begin();
  lcd.clear();
  lcd.noCursor();
  pinMode(relay1,OUTPUT);
  pinMode(relay2,OUTPUT);
  digitalWrite(relay1,HIGH); //off
  digitalWrite(relay2,HIGH); //off
}

void loop() {

  voltage = pzem.voltage();
  current = pzem.current();
  power = pzem.power();
  energy = pzem.energy();

  lcd.setCursor(0, 0);
  lcd.print("V/I:");
  lcd.print(voltage);
  lcd.print("/");
  lcd.print(current);
  lcd.print("        ");

  lcd.setCursor(0, 1);
  lcd.print("P/kwh:");
  lcd.print(power);
  lcd.print("/");
  lcd.print(energy,3);
  lcd.print("        ");

if(power > 450.0){
  digitalWrite(relay1,LOW); //on
  digitalWrite(relay2,LOW); //on
  delay(3000);
}

if(power < 450.0){
  digitalWrite(relay1,HIGH); //off
  digitalWrite(relay2,HIGH); //off
}

  tegangan = voltage * 100.0;
  arus = current * 100.0;
  daya = power * 100.0;
  kwh = energy * 1000.0;
   
Serial.print("*");
Serial.print(tegangan);
Serial.print(",");
Serial.print(arus);
Serial.print(",");
Serial.print(daya);
Serial.print(",");
Serial.print(kwh);
Serial.println("#");
 
delay(100);
}



c. Program Nodemcu ESP8266

#include <SPI.h>
#include <ESP8266WiFi.h>

int temp;
int x = 5;
int y;

int value1;
int value2;
int value3;
int value4;

float tegangan;
float arus;
float daya;
float kwh;

int datain1;
int datain2;
int datain3;
int datain4;

String dataIn;
String dt[10];
int i;
boolean parsing = false;

String apiKey = "YUGY12345HGJYTYH";     //  Enter your Write API key from ThingSpeak
const char* resource = "/update?api_key=";

const char *ssid =  "myhotspot";     // replace with your wifi ssid and wpa2 key
const char *pass =  "123456789";
const char* server = "api.thingspeak.com";

WiFiClient client;

void setup()
{
 
  dataIn="";
  Serial.begin(9600);
  delay(10);

  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, pass);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");

      while (WiFi.status() != WL_CONNECTED)
     {
            delay(500);
            Serial.print(".");
     }
      Serial.println("");
      Serial.println("WiFi connected");

}

void loop()
{

while(Serial.available()>0) {
//   dataIn="";
    char inChar = (char)Serial.read();
    dataIn += inChar;
    if (inChar == '\n') {
    parsing = true;
  }
}

if(parsing){
    parsingData();
       
  if (client.connect(server,80))   //   "184.106.153.149" or api.thingspeak.com
                      {
                           
                             String postStr = apiKey;
                             postStr +="&field1=";
                             postStr += String(tegangan);
                             postStr += "\r\n\r\n";

                             client.print(String("GET ") + resource + apiKey + "&field1=" + tegangan + "&field2=" + arus + "&field3=" + daya + "&field4=" + kwh + " HTTP/1.1\r\n" + "Host: " + server + "\r\n" + "Connection: close\r\n\r\n");
                                       
                             client.print("Host: api.thingspeak.com\n");
                             client.print("Connection: close\n");
                             client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
                             client.print("Content-Type: application/x-www-form-urlencoded\n");
                             client.print("Content-Length: ");
                             client.print(postStr.length());
                             client.print("\n\n");
                             client.print(postStr);
                           
                             Serial.print("Tegangan: ");
                             Serial.print(tegangan);
                             Serial.print(" Arus: ");
                             Serial.print(arus);
                             Serial.println(". Send to Thingspeak.");
                        }
                       
          client.stop();

          //Serial.println("Waiting...");

  delay(10000);
 
 }
}

void parsingData(){
int j=0;

//kirim data yang telah diterima sebelumnya
//Serial.print("data masuk : ");
//Serial.print(dataIn);
//Serial.print("\n");

//inisialisasi variabel, (reset isi variabel)
dt[j]="";
//proses parsing data
for(i=1;i<dataIn.length();i++){
//pengecekan tiap karakter dengan karakter (#) dan (,)
if ((dataIn[i] == '#') || (dataIn[i] == ','))
{
//increment variabel j, digunakan untuk merubah index array penampung
j++;
dt[j]="";       //inisialisasi variabel array dt[j]
}
else
{
//proses tampung data saat pengecekan karakter selesai.
dt[j] = dt[j] + dataIn[i];
}
}

 datain1 = dt[0].toInt();
 datain2 = dt[1].toInt();
 datain3 = dt[2].toInt();
 datain4 = dt[3].toInt();

//kirim data hasil parsing
Serial.print("data 1 : ");
Serial.print(datain1);
Serial.print("\n");
Serial.print("data 2 : ");
Serial.print(datain2);
Serial.print("\n");
Serial.print("data 3 : ");
Serial.print(datain3);
Serial.print("data 4 : ");
Serial.print(datain4);

 tegangan = datain1 / 100.0;
 arus = datain2 / 100.0;
 daya = datain3 / 100.0;
 kwh = datain4 / 1000.0;
 
}



d. VIDEO HASILNYA




No comments:

Post a Comment