Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- esp8266
- 14 / D5 - Rx
- 12 / D6 - Tx
- Elektromer Schneider iEM3155-M (38400) MODBUS
- RS485 to TTL module
- */
- #include <ModbusMaster.h>
- #include <SoftwareSerial.h>
- uint8_t result;
- uint16_t data[1024];
- uint16_t data_test1;
- uint16_t data_test2;
- uint16_t V1;
- uint16_t V2;
- uint16_t V3;
- uint16_t VAvg;
- uint16_t A1;
- uint16_t A2;
- uint16_t A3;
- uint16_t AAvg;
- uint16_t PwrFactor;
- uint16_t Freq;
- uint16_t Pwr1;
- uint16_t Pwr2;
- uint16_t Pwr3;
- uint16_t PwrTotal;
- bool modbusError;
- SoftwareSerial mySerial(14, 12); // RX, TX
- #define MAX485_DE 3
- #define MAX485_RE_NEG 2
- ModbusMaster node;
- void preTransmission()
- {
- digitalWrite(MAX485_RE_NEG, 1);
- digitalWrite(MAX485_DE, 1);
- }
- void postTransmission()
- {
- digitalWrite(MAX485_RE_NEG, 0);
- digitalWrite(MAX485_DE, 0);
- }
- void setup()
- {
- pinMode(MAX485_RE_NEG, OUTPUT);
- pinMode(MAX485_DE, OUTPUT);
- digitalWrite(MAX485_RE_NEG, 0);
- digitalWrite(MAX485_DE, 0);
- Serial.begin(38400);
- mySerial.begin(38400);
- node.begin(1, mySerial);
- node.preTransmission(preTransmission);
- node.postTransmission(postTransmission);
- }
- uint8_t readHoldingRegisters(uint16_t regpos, uint16_t size) {
- uint8_t result;
- result = node.readHoldingRegisters(regpos, size);
- if (result == node.ku8MBSuccess)
- {
- for (int j = 0; j < size; j++)
- {
- data[j] = node.getResponseBuffer(j);
- }
- } else {
- modbusError = true;
- }
- return result;
- }
- void loop() {
- delay(1000);
- Serial.println();
- // vycteni napeti faze 1 V1
- delay(5);
- readHoldingRegisters(0x0BD3, 1);
- Serial.print("V1: ");
- for(int i = 0; i < 1; i++)
- {
- Serial.println(data[i], HEX);
- V1 = data[i];
- }
- // vycteni napeti faze 2 V2
- delay(5);
- readHoldingRegisters(0x0BD5, 1);
- Serial.print("V2: ");
- for(int i = 0; i < 1; i++)
- {
- Serial.println(data[i], HEX);
- V2 = data[i];
- }
- // vycteni napeti faze 3 V3
- delay(5);
- readHoldingRegisters(0x0BD7, 1);
- Serial.print("V3: ");
- for(int i = 0; i < 1; i++)
- {
- V3 = data[i];
- Serial.println(data[i], HEX);
- }
- // vycteni napeti prumer VAvg
- delay(5);
- readHoldingRegisters(0x0BDB, 1);
- Serial.print("VAvg: ");
- for(int i = 0; i < 1; i++)
- {
- Serial.println(data[i], HEX);
- VAvg = data[i];
- }
- // vycteni proudu faze 1 A1
- delay(5);
- readHoldingRegisters(0x0BB7, 1);
- Serial.print("A1: ");
- for(int i = 0; i < 1; i++)
- {
- Serial.println(data[i], HEX);
- A1 = data[i];
- }
- // vycteni proudu faze 2 A2
- delay(5);
- readHoldingRegisters(0x0BB9, 1);
- Serial.print("A2: ");
- for(int i = 0; i < 1; i++)
- {
- Serial.println(data[i], HEX);
- A2 = data[i];
- }
- // vycteni proudu faze 3 A3
- delay(5);
- readHoldingRegisters(0x0BBB, 1);
- Serial.print("A3: ");
- for(int i = 0; i < 1; i++)
- {
- Serial.println(data[i], HEX);
- A3 = data[i];
- }
- // vycteni proudu prumer AAvg
- delay(5);
- readHoldingRegisters(0x0BC1, 1);
- Serial.print("AAvg: ");
- for(int i = 0; i < 1; i++)
- {
- Serial.println(data[i], HEX);
- AAvg = data[i];
- }
- // vycteni vykon faze 1 Pwr1
- delay(5);
- readHoldingRegisters(0x0BED, 1);
- Serial.print("Pwr1: ");
- for(int i = 0; i < 1; i++)
- {
- Serial.println(data[i], HEX);
- Pwr1 = data[i];
- }
- // vycteni vykon faze 2 Pwr2
- delay(5);
- readHoldingRegisters(0x0BEF, 1);
- Serial.print("Pwr2: ");
- for(int i = 0; i < 1; i++)
- {
- Serial.println(data[i], HEX);
- Pwr2 = data[i];
- }
- // vycteni vykon faze 3 Pwr3
- delay(5);
- readHoldingRegisters(0x0BF1, 1);
- Serial.print("Pwr3: ");
- for(int i = 0; i < 1; i++)
- {
- Serial.println(data[i], HEX);
- Pwr3 = data[i];
- }
- // vycteni vykon prumer PwrTotal
- delay(5);
- readHoldingRegisters(0x0BF3, 1);
- Serial.print("PwrTotal: ");
- for(int i = 0; i < 1; i++)
- {
- Serial.println(data[i], HEX);
- PwrTotal = data[i];
- }
- // vycteni uciniku PwrFactor
- delay(5);
- readHoldingRegisters(0x0C0B, 1);
- Serial.print("PwrFactor: ");
- for(int i = 0; i < 1; i++)
- {
- Serial.println(data[i], HEX);
- PwrFactor = data[i];
- }
- // vycteni frekvence site Freq
- delay(5);
- readHoldingRegisters(0x0C25, 1);
- Serial.print("Freq: ");
- for(int i = 0; i < 1; i++)
- {
- Serial.println(data[i], HEX);
- Freq = data[i];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement