Modbus RTU - ESP8266

Could you please help me with MODBUS library GitHub - emelianov/modbus-esp8266: Most complete Modbus library for Arduino. A library that allows - how to send command to the slave and If i got everyting wired properly? Thank you. I'm figthing with this all day ;/


#include <ModbusRTU.h>
#define SLAVE_ID 2
#if defined(ESP8266)
 #include <SoftwareSerial.h>
 // SoftwareSerial S(D1, D2, false, 256);

 // receivePin, transmitPin, inverse_logic, bufSize, isrBufSize
 // connect RX to D2 (GPIO4, Arduino pin 4), TX to D1 (GPIO5, Arduino pin 4)
 SoftwareSerial S(3, 1); //     3 - RX,     1 - TX       Wemos D1 Mini
#endif

int DE_RE = 5; // For MAX485 chip
ModbusRTU mb;

bool cbWrite(Modbus::ResultCode event, uint16_t transactionId, void* data) {
#ifdef ESP8266
  Serial.printf_P("Request result: 0x%02X, Mem: %d\n", event, ESP.getFreeHeap());
#else
  Serial.print("Request result: 0x");
  Serial.print(event, HEX);
#endif
  return true;
}

void setup() {
  Serial.begin(115200);
 #if defined(ESP8266)
  S.begin(9600, SWSERIAL_8N1);
  mb.begin(&S, DE_RE);
  mb.master();
#endif
}


void loop() {
  if (!mb.slave()) {
    mb.writeHreg(SLAVE_ID, 0x0001 ,0x0100 , cbWrite);
  }
  mb.task();
  yield();
}

Your post was MOVED to its current location as it is more suitable.

Fighting with what? You didn't say what is wrong.

I just wanna ask you guys if everyting is wired properly. Because I burnt nodemcu because I powered it with 12V -> VIN, lol. I read that lm1117 will handle it, but......

In your diagram, the NodeMCU is powered from 3.3V, not 12V.

Yes, but previously I got NodeMCU v3. Now I got wemos d1 mini and I would like to power it with 3,3V.

Sure, why not? Go ahead and power it from 3.3V.

Sorry, I meant, in the diagram, the D1 mini is powered from 3.3V. What is your actual problem? If you burned something with the diagram above, you probably made a wiring error.

Please supply a clickable link to the relay board documentation.

Sadly when I want to turn more relays It only turns only the first one and stops.

#include <ModbusRTU.h>
#define SLAVE_ID 2
#if defined(ESP8266)
 #include <SoftwareSerial.h>
 // SoftwareSerial S(D1, D2, false, 256);

 // receivePin, transmitPin, inverse_logic, bufSize, isrBufSize
 // connect RX to D2 (GPIO4, Arduino pin 4), TX to D1 (GPIO5, Arduino pin 4)
 SoftwareSerial S(3, 1); //     3 - RX,     1 - TX       Wemos D1 Mini
#endif

int DE_RE = 5; //D4  (02) For MAX485 chip
ModbusRTU mb;

bool cbWrite(Modbus::ResultCode event, uint16_t transactionId, void* data) {
#ifdef ESP8266
  Serial.printf_P("Request result: 0x%02X, Mem: %d\n", event, ESP.getFreeHeap());
#else
  Serial.print("Request result: 0x");
  Serial.print(event, HEX);
#endif
  return true;
}

void setup() {
  Serial.begin(115200);
 #if defined(ESP8266)
  S.begin(9600, SWSERIAL_8N1);
  mb.begin(&S, DE_RE);
  mb.master();
#endif
}


void loop() {
  if (!mb.slave()) {
    mb.writeHreg(SLAVE_ID, 0x0001 ,0x0100 , cbWrite); // 0x001 - relay 1, 0x002 - relay 2 and so on
    mb.writeHreg(SLAVE_ID, 0x0002 ,0x0100 , cbWrite); // 0x0100 - turn on relay, 0x0200 turn off relay 
    mb.writeHreg(SLAVE_ID, 0x0003 ,0x0100 , cbWrite);
    mb.writeHreg(SLAVE_ID, 0x0004 ,0x0100 , cbWrite);
    mb.writeHreg(SLAVE_ID, 0x0005 ,0x0100 , cbWrite);

    mb.writeHreg(SLAVE_ID, 0x0001 ,0x0200 , cbWrite);
    mb.writeHreg(SLAVE_ID, 0x0002 ,0x0200 , cbWrite);
    mb.writeHreg(SLAVE_ID, 0x0003 ,0x0200 , cbWrite);
    mb.writeHreg(SLAVE_ID, 0x0004 ,0x0200 , cbWrite);
    mb.writeHreg(SLAVE_ID, 0x0005 ,0x0200 , cbWrite);
  }
  mb.task();
  yield();
}

Please supply a clickable link to the relay board documentation.

Well, they did say, " When calling readCoil/readHreg/writeHreg/etc multiple times only first of them executed"

in the library documentation.