Could you please help me with MODBUS library GitHub - emelianov/modbus-esp8266: Most complete Modbus library for Arduino. A library that allows your Arduino board to communicate via Modbus protocol, acting as a master, slave or both. Supports network transport (Modbus TCP) and Serial line/RS-485 (Modbus RTU). Supports Modbus TCP Security for ESP8266/ESP32. - 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();
}