Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * modif by mikroavr
- * jimmi sitepu
- * 31-05-2022
- */
- #include <ModbusMaster.h>
- ModbusMaster node;
- static uint8_t pzemSlaveAddr = 1;
- #define EN 2
- #define pzemSerial Serial1
- float voltage, current, power, energy, freq, pf;
- void preTransmission()
- {
- digitalWrite(EN, 1);
- }
- void postTransmission()
- {
- digitalWrite(EN, 0);
- }
- void setup() {
- pinMode(EN, OUTPUT);
- digitalWrite(EN, 0);
- Serial.begin(115200);
- pzemSerial.begin(9600, SERIAL_8N1, 17, 16);
- node.begin(pzemSlaveAddr, pzemSerial);
- node.preTransmission(preTransmission);
- node.postTransmission(postTransmission);
- }
- void loop() {
- 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");
- }
- delay(2000);
- }
- 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();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement