Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <PubSubClient.h>
- #include <Wire.h>
- #include <PZEM004Tv30.h>
- #include <SoftwareSerial.h>
- const char* ssid = "Almer";
- const char* password = "tidakada";
- const char* mqtt_server = "broker.hivemq.com";
- const char* mqtt_user = "";
- const char* mqtt_pass= "";
- #define tegangan "0"
- #define arus "0"
- #define daya "0"
- #define energi "0"
- #define keterangan ""
- WiFiClient espClient;
- PubSubClient client(espClient);
- /* Use software serial for the PZEM
- * Pin 12 Rx (Connects to the Tx pin on the PZEM)
- * Pin 13 Tx (Connects to the Rx pin on the PZEM)
- */
- //#if !defined(PZEM_RX_PIN) && !defined(PZEM_TX_PIN)
- //#define PZEM_RX_PIN 12
- //#define PZEM_TX_PIN 13
- //#endif
- //SoftwareSerial pzemSWSerial(PZEM_RX_PIN, PZEM_TX_PIN);
- PZEM004Tv30 pzem(13,12);
- void setup_wifi() {
- // Connecting to a WiFi network
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("WiFi Terhubung");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- }
- void reconnect() {
- // Ulangi koneksi sampai terhubung
- Serial.println("Menghubungkan Ulang...");
- while (!client.connected()) {
- Serial.print("Melakukan koneksi ke MQTT Broker...");
- // Attempt to connect
- if (client.connect("Arduino_ikhsan", mqtt_user, mqtt_pass)) {
- Serial.println("connected");
- } else {
- Serial.print("failed, rc=");
- Serial.print(client.state());
- Serial.println(" Reconect dalam 5 detik");
- delay(5000);
- }
- }
- }
- void setup() {
- /* Debugging serial */
- Serial.begin(115200);
- setup_wifi();
- client.setServer(mqtt_server, 1883);
- }
- void loop() {
- // Serial.print("Custom Address:");
- // Serial.println(pzem.readAddress(), HEX);
- // Read the data from the sensor
- float voltage = pzem.voltage();
- float current = pzem.current();
- float power = pzem.power();
- float energy = pzem.energy();
- float frequency = pzem.frequency();
- float 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);
- }
- Serial.println();
- if (!client.connected()) {
- reconnect();
- }
- client.loop();
- client.publish("tegangan", String(voltage).c_str(), true);
- client.publish("arus", String(current).c_str(), true);
- client.publish("daya", String(power).c_str(), true);
- client.publish("energi", String(energy,3).c_str(), true);
- client.publish("keterangan", String(keterangan).c_str(), true);
- client.publish("keterangan", "Light Off");
- delay(2000);
- }
Advertisement
Add Comment
Please, Sign In to add comment