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