ESP32 - PZEM004Tv30 read data

#include <Arduino.h>
#include <PZEM004Tv30.h>
#include <SPIFFS.h>
#include <FS.h>
#include <WiFi.h>
#include <WebServer.h>

#define RX_PIN 16   // āļāļģāļŦāļ™āļ”āļžāļīāļ™ Rx āļŠāļģāļŦāļĢāļąāļšāļāļēāļĢāđ€āļŠāļ·āđˆāļ­āļĄāļ•āđˆāļ­āļāļąāļš PZEM
#define TX_PIN 17   // āļāļģāļŦāļ™āļ”āļžāļīāļ™ Tx āļŠāļģāļŦāļĢāļąāļšāļāļēāļĢāđ€āļŠāļ·āđˆāļ­āļĄāļ•āđˆāļ­āļāļąāļš PZEM
#define INTERVAL 10000 // āđ€āļāđ‡āļšāļ‚āđ‰āļ­āļĄāļđāļĨāļ—āļļāļ 5 āļ§āļīāļ™āļēāļ—āļĩ

// PZEM instance
PZEM004Tv30 pzem(Serial2, RX_PIN, TX_PIN);

// WiFi Hotspot credentials
const char *ssid = "ESP32-AP";  // āļ•āļąāđ‰āļ‡āļ„āđˆāļē SSID āļŠāļģāļŦāļĢāļąāļš Hotspot
const char *password = "123456789";  // āļ•āļąāđ‰āļ‡āļ„āđˆāļēāļĢāļŦāļąāļŠāļœāđˆāļēāļ™

WebServer server(80);  // Web server āļšāļ™āļžāļ­āļĢāđŒāļ• 80

unsigned long previousMillis = 0;

void handleDownload() {
  File file = SPIFFS.open("/data.csv", FILE_READ);
  if(file){
    server.sendHeader("Content-Type", "text/csv");
    server.sendHeader("Content-Disposition", "attachment; filename=data.csv");
    server.streamFile(file, "application/octet-stream");
    file.close();
  } else {
    server.send(404, "text/plain", "File not found");
  }
}

void setup() {
  Serial.begin(115200);

  // āļ•āļąāđ‰āļ‡āļ„āđˆāļē Hotspot (Access Point)
  WiFi.softAP(ssid, password);
  Serial.println("Access Point Started");
  Serial.print("IP Address: ");
  Serial.println(WiFi.softAPIP());

  // āđ€āļĢāļīāđˆāļĄāļ•āđ‰āļ™ SPIFFS
  if(!SPIFFS.begin(true)){
    Serial.println("SPIFFS Mount Failed");
    return;
  }

  // āļŠāļĢāđ‰āļēāļ‡āđ„āļŸāļĨāđŒ CSV āļŦāļēāļāļĒāļąāļ‡āđ„āļĄāđˆāļĄāļĩ
  if(!SPIFFS.exists("/data.csv")){
    File file = SPIFFS.open("/data.csv", FILE_WRITE);
    if(file){
      file.println("Time,Voltage,Current,Power,Energy"); // āļŦāļąāļ§āļ•āļēāļĢāļēāļ‡ CSV
      file.close();
    }
  }
  
  // āļ•āļąāđ‰āļ‡āļ„āđˆāļēāļāļēāļĢāļŠāđˆāļ‡āļ­āļ­āļāđ„āļŸāļĨāđŒ CSV āļœāđˆāļēāļ™āđ€āļ§āđ‡āļšāđ€āļ‹āļīāļĢāđŒāļŸāđ€āļ§āļ­āļĢāđŒ
  server.on("/download", handleDownload);
  server.begin();
  Serial.println("HTTP server started");

  Serial.println("Setup complete");
}

void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= INTERVAL) {
    previousMillis = currentMillis;

    // āļ­āđˆāļēāļ™āļ„āđˆāļēāļˆāļēāļ PZEM
    float voltage = pzem.voltage();
    float current = pzem.current();
    float power = pzem.power();
    float energy = pzem.energy();

    // āđāļŠāļ”āļ‡āļ„āđˆāļēāļ—āļĩāđˆāļ­āđˆāļēāļ™āđ„āļ”āđ‰āļœāđˆāļēāļ™ Serial
    Serial.print("Voltage: "); Serial.print(voltage); Serial.print("V, ");
    Serial.print("Current: "); Serial.print(current); Serial.print("A, ");
    Serial.print("Power: "); Serial.print(power); Serial.print("W, ");
    Serial.print("Energy: "); Serial.println(energy);

    // āđ€āļ‚āļĩāļĒāļ™āļ‚āđ‰āļ­āļĄāļđāļĨāļĨāļ‡āđƒāļ™āđ„āļŸāļĨāđŒ CSV
    File file = SPIFFS.open("/data.csv", FILE_APPEND);
    if(file){
      String data = String(millis()/1000) + "," + String(voltage) + "," + String(current) + "," + String(power) + "," + String(energy);
      file.println(data);
      file.close();
    } else {
      Serial.println("Failed to open file for appending");
    }
  }

  // āļˆāļąāļ”āļāļēāļĢāļāļąāļšāđ€āļ§āđ‡āļšāđ€āļ‹āļīāļĢāđŒāļŸāđ€āļ§āļ­āļĢāđŒ
  server.handleClient();

  // āļˆāļąāļ”āļāļēāļĢāļ„āļģāļŠāļąāđˆāļ‡āļœāđˆāļēāļ™ Serial
  if(Serial.available()){
    String command = Serial.readStringUntil('\n');
    command.trim();
    
    if(command == "delete csv"){
      SPIFFS.remove("/data.csv");
      Serial.println("CSV file deleted");
    }
    else if(command == "get csv"){
      File file = SPIFFS.open("/data.csv", FILE_READ);
      if(file){
        while(file.available()){
          Serial.write(file.read());
        }
        file.close();
      } else {
        Serial.println("Failed to open CSV file");
      }
    }
  }
}

Last updated