#include #include #include #include #include PZEM004Tv30 pzem(D5, D6); float voltage; float current; float power ; float energy; float frequency; float pf; const char* ssid = "iSpark"; const char* password = "iSpark@28"; String url = "https://phsolutions.tech/iomachine/save-data.php?id=1&r="; // String getUrl = "https://phsolutions.tech/iomachine/get-data.php?id=1"; int del, del_1, del_2; //delimiter String payload="%0%0%0%0%0%"; //Declaration const int relay1 = 5; //D1 int stat,relay; String ldrValue; float voltage1; int count=0,i,m,j,k; int volt_sensor_pin = A0; float Vout = 0.00; float Vin = 0.00; float R1 = 10000.00; // resistance of R1 (10K) float R2 = 1000.00; // resistance of R2 (1K) int val = 0; void setup() { Serial.begin(9600); Serial.begin(115200); // Starts the serial commusication Serial.println("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); pinMode(relay1, OUTPUT); digitalWrite(relay1, HIGH); //Off pinMode(volt_sensor_pin, INPUT); //assigning the input port delay(2000); } void loop() { getVoltage(); getVoltageSensor(); //FROM DATABASE payload="%0%0%0%"; if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status WiFiClientSecure client; client.setInsecure(); HTTPClient https; //Declare an object of class HTTPClient https.begin(client,getUrl); //Specify request destination int httpCode = https.GET(); //Send the request if (httpCode > 0) { //Check the returning code String payload = https.getString(); //Get the request response payload String readString = payload; //Split del = readString.indexOf("%"); del_1 = readString.indexOf("%", del + 1); relay = readString.substring(del + 1, del_1).toInt(); //0 or 1 //%1%2%21:09:00 //DO TASK taskToDo(); } https.end(); //Close connection } delay(1000); } void taskToDo(){ if(relay==1){ digitalWrite(relay1, LOW);//On Serial.println("relay1 On"); }else{ digitalWrite(relay1, HIGH); //Off Serial.println("relay1 Off"); } } void getVoltage(){ Serial.print("Custom Address:"); Serial.println(pzem.readAddress(), HEX); // Read the data from the sensor voltage = pzem.voltage(); current = pzem.current(); power = pzem.power(); energy = pzem.energy(); frequency = pzem.frequency(); 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); } if(power>0){ //TO DATABASE if (WiFi.status() == WL_CONNECTED) { WiFiClientSecure client; client.setInsecure(); //https://phsolutions.tech/iomachine/save-data.php?id=1&r=120.42,34,2,5,23,4 ldrValue =String(voltage) + "," + String(current) + "," + String(power) + "," + String(energy,3) + "," + String(frequency, 1) + "," + String(pf) + "," + String(Vin) ; //Voltage,Current,Power,Energy,Frequency,PF HTTPClient https; String fullUrl = url + ldrValue ; Serial.println("Requesting " + fullUrl); if (https.begin(client, fullUrl)) { int httpCode = https.GET(); Serial.println("============== Response code: " + String(httpCode)); if (httpCode > 0) { Serial.println(https.getString()); } https.end(); } else { Serial.printf("[HTTPS] Unable to connect\n"); } } } Serial.println("=============================================="); } void getVoltageSensor(){ val = analogRead(volt_sensor_pin);//reads the analog input Vout = (val * 5.00) / 1024.00; // formula for calculating voltage out i.e. V+, here 5.00 Vin = Vout / (R2/(R1+R2)); // formula for calculating voltage in i.e. GND Serial.print("Voltage = "); Serial.println(Vin); }