Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ModbusMaster.h>
- ModbusMaster node;
- #define MAX485_RXD 17
- #define MAX485_TXD 16
- #define led 23
- bool stateLed = 0;
- byte addr_pzem = 1;
- unsigned long cur_time, old_time;
- float voltage, current, power, energy, freq, pf;
- int waktu = 30;
- void setup() {
- // put your setup code here, to run once:
- delay(100);
- pinMode(led, OUTPUT);
- Serial.begin(115200);
- Serial1.begin(9600, SERIAL_8N1, MAX485_RXD, MAX485_TXD);
- node.begin(addr_pzem, Serial1);
- }
- void loop() {
- // put your main code here, to run repeatedly:
- cur_time = millis();
- if (cur_time - old_time >= 250) {
- baca_pzem();
- stateLed = !stateLed;
- digitalWrite(led, stateLed);
- old_time = cur_time;
- }
- }
- 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 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