#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
char auth[] = "WdPlYZ-hH2pj1FHrzuoDQgs0AANCDtJG"; // ใส่ Auth Token ของ Blynk ที่ได้รับจากแอปพลิเคชัน Blynk
char ssid[] = "Wokwi-GUEST"; // ใส่ชื่อ WiFi ที่ต้องการเชื่อมต่อ
char pass[] = ""; // ใส่รหัสผ่าน WiFi ที่ต้องการเชื่อมต่อ
int ACS712 = 34; // กำหนดขาเชื่อมต่อ ACS712
int ACS712_ = 35; // กำหนดขาเชื่อมต่อ ACS712
float VCC = 5.0; // แรงดันไฟฟ้า VCC ที่ใช้ในการส่งข้อมูล
float VREF = 2.5; // แรงดันไฟฟ้าอ้างอิง VREF
float Sensitivity = 0.66; // ความไวต่อการตรวจจับของ ACS712
float VCC2 = 5.0; // แรงดันไฟฟ้า VCC ที่ใช้ในการส่งข้อมูล
float VREF2 = 2.5; // แรงดันไฟฟ้าอ้างอิง VREF
float Sensitivity2 = 0.66; // ความไวต่อการตรวจจับของ
WidgetTerminal terminal(V1); // Declare terminal widget object
void setup() {
Serial.begin(9600); // เปิดการใช้งาน Serial Monitor
Blynk.begin(auth, ssid, pass);
// Initialize Blynk
Blynk.begin(auth, ssid, pass);
// Setup terminal widget
terminal.clear(); // Clear terminal content
terminal.println("Starting..."); // Print message to terminal
terminal.flush(); // Send to terminal
}
void loop() {
Blynk.run();
// Write to terminal widget
// terminal.println("Some data to display in terminal");
terminal.flush(); // Send to terminal
int sensorValue = analogRead(ACS712); // อ่านค่าแรงดันไฟฟ้าจาก ACS712
float voltage = (sensorValue / 1024.0) * VCC; // คำนวณแรงดันไฟฟ้า
float voltage_difference = voltage - VREF; // คำนวณความแตกต่างของแรงดันไฟฟ้า
float current = voltage_difference / Sensitivity; // คำนวณกระแสไฟฟ้า
float power = current * voltage; // คำนวณพลังงานไฟฟ้า
float energy = power * (1.0 / 3600.0); // คำนวณพลังงานไฟฟ้าที่ใช้ใน 1 ชั่วโมง
float unit = (power * 1 * 1 * 30) / 1000;
float Bath = unit * 4;
int sensorValue2 = analogRead(ACS712_); // อ่านค่าแรงดันไฟฟ้าจาก ACS712
float voltage2 = (sensorValue2 / 1024.0) * VCC2; // คำนวณแรงดันไฟฟ้า
float voltage_difference2 = voltage2 - VREF2; // คำนวณความแตกต่างของแรงดันไฟฟ้า
float current2 = voltage_difference2 / Sensitivity2; // คำนวณกระแสไฟฟ้า
float power2 = current2 * voltage2; // คำนวณพลังงานไฟฟ้า
float energy2 = power2 * (1.0 / 3600.0); // คำนวณพลังงานไฟฟ้าที่ใช้ใน 1 ชั่วโมง
float unit2 = (power2 * 1 * 1 * 30) / 1000;
/*หลอดไฟ ๑๐๐วัตติ์ จำนวน ๑หลอดเปิดวันละ ๑๐ชั่วโมง เวลา ๓๐วัน
(100*1*10*30)/1000 = 30หน่วย ถ้าค่าไฟฟ้า หน่วยละ ๔บาท
ต้องจ่าย 30*4 = ๑๒๐บาทเป็นต้น
*/
float Bath2 = unit2 * 4;
// แสดงผลค่าแรงดันไฟฟ้า กระแสไฟฟ้า และพลังงานไฟฟ้าบน Serial Monitor
terminal.print("Voltage 1: ");
terminal.print(voltage);
terminal.print("V | Current 1: ");
terminal.print(current);
terminal.print("A | Power 1: ");
terminal.print(power);
terminal.print("W | Energy 1: ");
terminal.print(energy);
terminal.print("Wh | Bath 1: ");
terminal.print(Bath);
terminal.println("B / month");
terminal.print("Voltage 2: ");
terminal.print(voltage2);
terminal.print("V | Current 2: ");
terminal.print(current2);
terminal.print("A | Power 2: ");
terminal.print(power2);
terminal.print("W | Energy 2: ");
terminal.print(energy2);
terminal.print("Wh | Bath 2: ");
terminal.print(Bath2);
terminal.println("B / month");
terminal.println("");
delay(2000); // หน่วงเวลา 1 วินาที
}