Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define BLYNK_PRINT Serial
- #define BLYNK_TEMPLATE_ID "TMPL-YozzxnU"
- #define BLYNK_DEVICE_NAME "Quickstart Template"
- #define BLYNK_AUTH_TOKEN "kcbRdG124Fv61Jg-r5fx8WfYXn-rR4_u"
- #include <WiFi.h>
- #include <WiFiClient.h>
- #include <BlynkSimpleEsp32.h>
- // Your WiFi credentials.
- // Set password to "" for open networks.
- char ssid[] = "MIKROAVR.COM";
- char pass[] = "Admin@357";
- #include <ModbusMaster.h>
- ModbusMaster node;
- static uint8_t pzemSlaveAddr = 1;
- #define MAX485_DE 2
- #define MAX485_RE_NEG 2
- float voltage, current, power, energy, freq, pf;
- #define RXD_MASTER 17
- #define TXD_MASTER 16
- #define pzemSerial Serial1
- unsigned long cur_time, old_time;
- const byte pin_relay = 32;
- void preTransmission()
- {
- digitalWrite(MAX485_RE_NEG, 1);
- digitalWrite(MAX485_DE, 1);
- }
- void postTransmission()
- {
- digitalWrite(MAX485_RE_NEG, 0);
- digitalWrite(MAX485_DE, 0);
- }
- BLYNK_WRITE(V0)
- {
- int value = param.asInt(); // Get value as integer
- Serial.print("rel: ");
- Serial.println(value);
- digitalWrite(pin_relay,value);
- }
- BLYNK_WRITE(V10)
- {
- int value = param.asInt(); // Get value as integer
- Serial.print("v10: ");
- Serial.println(value);
- if(value == true){
- Serial.println("reset energy");
- //resetEnergy(pzemSlaveAddr);
- }
- }
- void setup() {
- pinMode(pin_relay, OUTPUT);
- pinMode(MAX485_RE_NEG, OUTPUT);
- pinMode(MAX485_DE, OUTPUT);
- // Init in receive mode
- digitalWrite(MAX485_RE_NEG, 0);
- digitalWrite(MAX485_DE, 0);
- pzemSerial.begin(9600, SERIAL_8N1, RXD_MASTER, TXD_MASTER);
- Serial.begin(115200);
- node.begin(pzemSlaveAddr, pzemSerial);
- node.preTransmission(preTransmission);
- node.postTransmission(postTransmission);
- Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
- }
- void loop() {
- Blynk.run();
- cur_time = millis();
- if (cur_time - old_time >= 3000) {
- //baca_pzem();
- //push_blynk();
- old_time = cur_time;
- }
- }
- void push_blynk(){
- Blynk.virtualWrite(V4, voltage);
- Blynk.virtualWrite(V5, current);
- Blynk.virtualWrite(V6, freq);
- Blynk.virtualWrite(V7, pf);
- Blynk.virtualWrite(V8, power);
- Blynk.virtualWrite(V9, energy);
- }
- void baca_pzem() {
- uint8_t result;
- result = node.readInputRegisters(0, 9); //read the 9 registers of the PZEM-014 / 016
- if (result == node.ku8MBSuccess)
- {
- Serial.println("sukses modbus");
- voltage = node.getResponseBuffer(0) / 10.0;
- uint32_t tempdouble = 0x00000000;
- tempdouble = node.getResponseBuffer(1); //LowByte
- tempdouble |= node.getResponseBuffer(2) << 8; //highByte
- current = tempdouble / 1000.0;
- tempdouble |= node.getResponseBuffer(3); //LowByte
- tempdouble |= node.getResponseBuffer(4) << 8; //highByte
- power = tempdouble / 10.0;
- tempdouble = node.getResponseBuffer(5); //LowByte
- tempdouble |= node.getResponseBuffer(6) << 8; //highByte
- energy = tempdouble;
- tempdouble = node.getResponseBuffer(7);
- freq = tempdouble / 10.0;
- tempdouble = node.getResponseBuffer(8);
- pf = tempdouble / 10.0;
- print_data();
- } else {
- Serial.println("Failed to read modbus");
- }
- }
- void resetEnergy(uint8_t slaveAddr) {
- //The command to reset the slave's energy is (total 4 bytes):
- //Slave address + 0x42 + CRC check high byte + CRC check low byte.
- uint16_t u16CRC = 0xFFFF;
- static uint8_t resetCommand = 0x42;
- u16CRC = crc16_update(u16CRC, slaveAddr);
- u16CRC = crc16_update(u16CRC, resetCommand);
- Serial.println("Resetting Energy");
- delay(10);//ditambah
- pzemSerial.write(slaveAddr);
- pzemSerial.write(resetCommand);
- pzemSerial.write(lowByte(u16CRC));
- pzemSerial.write(highByte(u16CRC));
- delay(10);//ditambah
- Serial.print("respon: ");
- while (pzemSerial.available() > 0) {
- Serial.print(pzemSerial.read(), HEX);
- Serial.print(",");
- }
- Serial.println();
- delay(1000);
- }
- void print_data() {
- Serial.print(voltage);
- Serial.print("V ");
- Serial.print(current);
- Serial.print("A ");
- Serial.print(freq);
- Serial.print("Hz ");
- Serial.print(pf);
- Serial.print("pf ");
- Serial.print(" ");
- Serial.print(power);
- Serial.print("W ");
- Serial.print(energy);
- Serial.print("Wh ");
- Serial.println();
- }
Add Comment
Please, Sign In to add comment