PZEM004T - Volt , Current ไม่แสดงสถานะ

ต้องแก้ไขอย่างไรคับ

ขอดูโค้ตที่ใช้ครับ

แล้ววนี้คือทำอะไรหรอครับ ส่งค่าไปที่เว็บ socket หรอ อธิบายหน่อยครับ

ใช่คับส่งค่ามาที่blynk คับ

:thinking:

1 Like

pzem

  • List item

เปิดในโทรศัพไม่ได้เดียวช่วงเย็นเดียวเอามาลงให้นะคับ

โอเคครับ

#include <ESP8266WiFi.h>
#include <PZEM004T.h>
#define BLYNK_PRINT Serial
#include <BlynkSimpleEsp8266.h>
#include <ThingspeakClient.h>
#include <EEPROM.h>

#define FIRST_ADDRESS 0
#define MAX_ATTEMPTS 10
#define min(a,b) ((a)<(b)?(a):(b))

PZEM004T pzem(4, 5); // RX,TX d2,d6
IPAddress ip(192, 168, 1, 1);

const char* ssid     = "tanawat";
const char* password = "Wat08076677433";
char auth[] = "xFBNngkpA5dvh5MTWhygWS1hnCbCZEUV";  //Blynk Token

WiFiClient client;
ThingspeakClient thingspeak;
const char* host = "api.thingspeak.com";
const String THINGSPEAK_CHANNEL_ID = "860804";
const String THINGSPEAK_API_READ_KEY = "2IXSQKX4AN4LDH0P";  //read key
const char* api   = "AQDT0NYNL3U1W7YI";  //write key

float v, i, p, e, b;
long milisec = 0;
float calBill(float Unit, float ft, bool over_150_Unit_per_month = false) ;
float FT = -15.9;
int firstState;

void Line_Notify(String message);
int token = 0;
String LINE_TOKEN[2]    = {"n1vN3fQAUMQRhGdMdijqQYUyiopmm3sYxfyCBeIsRkJ", "sUS5V77H0wOMyXjHudqGsdaUCwO3EFCPeGW9nJl8WhX"};  //line token
unsigned long currentMillis, previousMillis, previousMillis2, previousMillis3, readMillis = 0;

String message = "%20Volt%20";  // " Volt "
String message2 = "%20Current%20";  //  " Current "
String message3 = "%20Power%20Now%20";  //  " Power Now "
String message4 = "%20Energy%20";  //  " Energy "
String message5 = "%20Cost%20";  //  " Cost "
String message6 = "%20Warning%20overload%20";  //  "  Warning overload "
String message7 = "%20Power%20Total%20";  //  " Power Total "

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

  pzem.setAddress(ip);
  Blynk.begin(auth, ssid, password);

  EEPROM.begin(512);
  firstState = EEPROM.read(FIRST_ADDRESS);
  Serial.println(firstState);

  if (firstState == 5) {
    Serial.print("use again:");
    thingspeak.getLastChannelItem(THINGSPEAK_CHANNEL_ID, THINGSPEAK_API_READ_KEY);
    String f1 = (thingspeak.getFieldValue(3));
    String f2 = (thingspeak.getFieldValue(4));
    String f3 = (thingspeak.getFieldValue(5));
    e = f1.toFloat();
    b = f2.toFloat();
    FT = f3.toFloat();
  } else {
    Serial.print("use new:");
    Serial.println("first write eeprom");
    EEPROM.write(0, 5);
    EEPROM.commit();
  }

  ReadPzem();
  SendNormal();
  currentMillis = previousMillis = previousMillis2 = previousMillis3 = readMillis = millis();
  Serial.println(FT);
  Blynk.virtualWrite(V7, FT);
  b = calBill(e, FT, true);
}

void loop() {
  currentMillis = millis();
  Blynk.run();

  if (currentMillis - readMillis >= 1000) {
    ReadPzem();
    SetBlynk();
  }

  if (currentMillis - previousMillis3 >= 30000) UpdateThingspeak();
  if (currentMillis - previousMillis >= 900000) SendNormal();
  if (i >= 2.00 && currentMillis - previousMillis2 >= 60000) SendWarning();
}

void ReadPzem() {
  readMillis = millis();
  unsigned long time;

  v = getVoltage();
  i = getCurrent();
  p = v * i;
  if (p >= 1) {
    time += 1;
    e += (p * time) / (1000 * 3600);
    b = calBill(e, FT, true);
  } else {
    time = 0;
  }

  Serial.print("Volt: "); Serial.print(v); Serial.print("V\n");
  Serial.print("Current: "); Serial.print(i); Serial.print("A\n");
  Serial.print("power Now: "); Serial.print(p); Serial.print("W\n");
  Serial.print("Energy: "); Serial.print(e, 3); Serial.print("kWh\n");
  Serial.print("Cost: "); Serial.print(b, 3); Serial.print("฿\n");
  Serial.print("FT: "); Serial.println(FT);
  Serial.println("--------------------");

}

void SetBlynk() {
  Blynk.virtualWrite(V0, v);
  Blynk.virtualWrite(V1, i);
  Blynk.virtualWrite(V2, p);
  Blynk.virtualWrite(V4, e);
  Blynk.virtualWrite(V5, b);
  Blynk.virtualWrite(V6, FT);
}

void UpdateThingspeak() {
  previousMillis3 = millis();
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }

  String etemp = String(e, 3);
  String btemp = String(b, 3);

  // We now create a URI for the request
  String url = "/update?api_key=";
  url += api;
  url += "&field1=";
  url += v;
  url += "&field2=";
  url += i;
  url += "&field3=";
  url += p;
  url += "&field4=";
  url += etemp;
  url += "&field5=";
  url += btemp;
  url += "&field6=";
  url += FT;

  Serial.print("Requesting URL: ");
  Serial.println(url);
  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");
  delay(10);

  // Read all the lines of the reply from server and print them to Serial
  while (client.available()) {
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }

  Serial.println();
  Serial.println("closing connection");
}

void SendNormal() {
  previousMillis = millis();
  token = 0;
  Line_Notify(message + v + "V" + message2 + i + "A" + message3
              + p + "W" + message4 + e + "Wh" + message5 + b + "฿");
}

void SendWarning() {
  previousMillis2 = millis();
  token = 1;
  Line_Notify(message6 + i + "A");
}

BLYNK_WRITE(V7) {
  FT = param[0].asFloat();
}

BLYNK_WRITE(V8) {
  p = 0.0;
  e = 0.0;
  b = 0.0;
}

float calBill(float Unit, float ft, bool over_150_Unit_per_month) {
  float Service = over_150_Unit_per_month ? 38.22 : 8.19;

  float total = 0;

  if (!over_150_Unit_per_month) {
    float Rate15 = 2.3488;
    float Rate25 = 2.9882;
    float Rate35 = 3.2405;
    float Rate100 = 3.6237;
    float Rate150 = 3.7171;
    float Rate400 = 4.2218;
    float RateMore400 = 4.4217;

    if (Unit >= 6) total += min(Unit, 15) * Rate15;
    if (Unit >= 16) total += min(Unit - 15, 10) * Rate25;
    if (Unit >= 26) total += min(Unit - 25, 10) * Rate35;
    if (Unit >= 36) total += min(Unit - 35, 65) * Rate100;
    if (Unit >= 101) total += min(Unit - 100, 50) * Rate150;
    if (Unit >= 151) total += min(Unit - 150, 250) * Rate400;
    if (Unit >= 401) total += (Unit - 400) * RateMore400;
  } else {
    float Rate150 = 3.2484;
    float Rate400 = 4.2218;
    float RateMore400 = 4.4217;

    total += min(Unit, 150) * Rate150;
    if (Unit >= 151) total += min(Unit - 150, 250) * Rate400;
    if (Unit >= 401) total += (Unit - 400) * RateMore400;
  }

  total += Service;
  total += Unit * (ft / 100);
  total += total * 7 / 100;

  return total;
}

void Line_Notify(String message) {
  WiFiClientSecure client;

  if (!client.connect("notify-api.line.me", 443)) {
    Serial.println("connection failed");
    return;
  }

  String req = "";
  req += "POST /api/notify HTTP/1.1\r\n";
  req += "Host: notify-api.line.me\r\n";
  req += "Authorization: Bearer " + String(LINE_TOKEN[token]) + "\r\n";
  req += "Cache-Control: no-cache\r\n";
  req += "User-Agent: ESP8266\r\n";
  req += "Content-Type: application/x-www-form-urlencoded\r\n";
  req += "Content-Length: " + String(String("message=" + message).length()) + "\r\n";
  req += "\r\n";
  req += "message=" + message;

  client.print(req);
  delay(30);

  while (client.connected()) {
    String line = client.readStringUntil('\n');
    if (line == "\r") {
      break;
    }
  }
}

float getVoltage() {
  int i = 0;
  float r = -1.0;
  do {
    r = pzem.voltage(ip);
    wdt_reset();
    i++;
  } while ( i < MAX_ATTEMPTS && r < 0.0);
  return r;
}

float getCurrent() {
  int i = 0;
  float r = -1.0;
  do {
    r = pzem.current(ip);
    wdt_reset();
    i++;
  } while ( i < MAX_ATTEMPTS && r < 0.0);
  return r;
}

ค่าที่ print ออกมาจากส่วนนี้ได้เท่าไหร่ครับ

1 Like

-1คับ:expressionless:

หมายถึง Serial.print จาก Arduino IDE ครับ

1 Like

แปปคับ

จะเห็นได้ว่าค่า Volt และ Current ได้ -1 ทั้งคู่แสดงว่าที่ blynk และ websocket นั้นแสดงค่าถูกแล้ว ลองเช็ค Hardware ดูก่อนว่าได้ค่าจริงๆ ก่อนเข้าไปในบอร์ดเท่าไหร่ ครับ ตรงกันรึป่าว

image

1 Like

อีกคำถามคือต่อ Hardware แบบไหน?

ผมใช้ pzem-004t หลักร้อยอุปกรณ์เสียมีส่วนไมคับ

1 Like

PZEM-004T%20Current%20Voltage%20Power%20Energy

เช็คให้ดีครับ เอาอะไรวัดก่อนเข้าบอร์ดก็ได้ ยืนยันว่า Hardware เราไม่ได้ผิด ไม่งั้นเข้าไปงมโค้ตตายเลยทีนี้

ลองเช็คแล้วมาอัพเดทหน่อยนะครับ

free
hit counter