PlatformIO Community

Library Download Modbus


Hi guys,
i’m new in PlatformIO forum.
My order was: “ESP32: include a Modbus TCP server application”

My Platform for Arduino “ESP32-EVB” is: PlatFormIO ( newest: Home 2.0.0 ), and VisualStudioCode
platform = espressif32
board = esp32-evb
framework = arduino
board_build.partitions = default.csv

What i’m doing (after reading some examples) inside PlatFormIO:

  1. append the new library “ArduinoModbus”
    … compiling: he said missing “ArduinoRS485” …

  2. so i append the new library “ArduinoRS485”

  3. i’m appending “lib_ldf_mode = deep+” in my, because installed libs are in:

My source modification is only 2 includes:

#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
#include <ArduinoModbus.h>

then the compiler said:

D:\orcad\spb_data.platformio\lib\ArduinoRS485_ID5814\src\RS485.cpp: In member function ‘virtual void RS485Class::begin(long unsigned int)’:
D:\orcad\spb_data.platformio\lib\ArduinoRS485_ID5814\src\RS485.cpp:33:29: warning: large integer implicitly truncated to unsigned type [-Woverflow]
begin(baudrate, SERIAL_8N1);
D:\orcad\spb_data.platformio\lib\ArduinoRS485_ID5814\src\RS485.cpp: At global scope:
D:\orcad\spb_data.platformio\lib\ArduinoRS485_ID5814\src\RS485.cpp:169:18: error: ‘SERIAL_PORT_HARDWARE’ was not declared in this scope
In file included from D:\orcad\spb_data.platformio\lib\ArduinoRS485_ID5814\src\RS485.cpp:20:0:
D:\orcad\spb_data.platformio\lib\ArduinoRS485_ID5814\src\RS485.h:35:30: error: ‘A6’ was not declared in this scope
#define RS845_DEFAULT_DE_PIN A6
D:\orcad\spb_data.platformio\lib\ArduinoRS485_ID5814\src\RS485.cpp:169:62: note: in expansion of macro ‘RS845_DEFAULT_DE_PIN’
D:\orcad\spb_data.platformio\lib\ArduinoRS485_ID5814\src\RS485.h:36:30: error: ‘A5’ was not declared in this scope
#define RS845_DEFAULT_RE_PIN A5
D:\orcad\spb_data.platformio\lib\ArduinoRS485_ID5814\src\RS485.cpp:169:84: note: in expansion of macro ‘RS845_DEFAULT_RE_PIN’

Searching google for ‘SERIAL_PORT_HARDWARE’, found a: #define SERIAL_PORT_HARDWARE Serial1
but that makes no sense !!

Is there any way to solve this ???
Please help


Yes it does, it’s the HardwareSerial object to which your RS485 transceiver is connected to. Check the pin mapping of your ESP32 board to see which pins are correct for you. (e.g. here)

These pin definitions seem to be for a Arduino Uno/Mega type where it’s Analog Pin 5. Change that to the pin where you have the receiver enable / transmitter enable etc.

Can e.g. be done by defining the macro via build_flags = -D RS845_DEFAULT_DE_PIN=12 or whatever in the platformio.ini.


Hallo Max,

thanks for your quick reply…

It seems there is a small misunderstanding !!

  1. I NEVER NEED the RS485 for my project
    the missing definitions f.e. ‘SERIAL_PORT_HARDWARE’ and ‘A6’ …
    are coming out after the automatic installation of the two libs inside PlatformIO:

  2. it seems i need another lib for the default RS485 parts ??
    I think: “…take original Arduino libs, they must still be compatibel …” or ?

  3. i think:
    #include <ArduinoRS485.h>
    is not enough. There must be a deeper includ before ?

Greetings Wolfgang


Oh okay you’re trying to do Modbus via TCP/IP not via an actual RS485 transceiver. Seems you have the wrong library for that? Does the ArduinoModbus library say it can do TCP/IP Modbus?


Hallo Max,

no problem.

  1. YES they do:
    scr are always with xx.cpp and xxx.h
    ModbusClient ModbusRTUClient ModbusRTUServer ModbusTCPClient ModbusTCPServer

  2. but they discribe the pysical layer is always RS485 based.
    Thatsway we need RS485 ?!?!?

  3. maybe you have another solution, with other PlatformIO libraries ?

Greetings Wolfgang


Uh seems like you want or Found via First one only implements a client.


Hallo Max,
OK i see it also before installing my libs …
But “sunnyboy” example looks not so good like “ArduinoModbus” examples…

But i will try it tomorrow, and let you know wkat happens.

Greetings Wolfgang


Hi Max,

i am a little bit confiused …

everytime i’m installing another lib inside platformio
i will get different errors.
I think there are some libs missmatched in the deep ?

First try: i always append only the include files to see what happens.
// TEST2
#include <SPI.h>
#include <Ethernet.h>
#include <Modbus.h>
#include <ModbusIP.h>

So after i included again a “Ethernet LIB” Ethernet by Paul Stoffregen
i have only one compile error:
In file included from src\Modbus_KE.cpp:58:0:
…/.platformio/lib/ModbusIP_ID1725/ModbusIP.h:20:24: error: cannot declare field ‘ModbusIP::_server’ to be of abstract type ‘EthernetServer’
EthernetServer _server;

This libs i have installed from PIO Home -> libraries

Greetings Wolfgang


Hi Max,

Test3: i install your example: “5902/esp32ModbusTCP”
and again, only including the 3 #include files out of the example in my “Modbus_KE.cpp”:
#include <Arduino.h>
#include <WiFi.h>
#include <ModbusTCP.h>
=> error “ModbusTCP.h” not found ( and really it is NOT on my PC ! )

by the way:
I need a ModbusServer example that uses also Ethernet and WiFi on a ESP32-EVB board
( hardware: ESP32-WROOM-32 ).
It reads inside ESP32 board 10 Hz CAN-values and transmit it over Ethernet and/or Wifi to
an external PC.

Greetings Wolfgang


This one would probably never work, as it clearly states on the Library Registry it is for the Teensy platform, not the ESP32! :open_mouth:

For the esp32ModbusTCP library (id:5902), it looks like the author of that library has been making changes to the library and neglected to update the the example. Change the include and the object initialisation to the following (i.e esp32ModbusTCP instead of ModbusTCP).

#include <esp32ModbusTCP.h>
esp32ModbusTCP sunnyboy(3, {192, 168, 123, 123}, 502);

It’ll then start to compile, but then hit an 'esp32ModbusInternals' has not been declared error. I’m not sure why that is happening, as it appears esp32ModbusInternals is declared in Helper.h which is included by esp32ModbusTCP.cpp . Anyway, this is my platformio.ini and compile output.


platform = espressif32
board = esp32-evb
framework = arduino
lib_deps = 


#include <Arduino.h>
#include <WiFi.h>
#include <esp32ModbusTCP.h>

const char* ssid = "xxxx";
const char* pass = "xxxx";
bool WiFiConnected = false;

esp32ModbusTCP sunnyboy(3, {192, 168, 123, 123}, 502);

void setup() {

    WiFi.disconnect(true);  // delete old config

    sunnyboy.onAnswer([](bool succes, MB_ADU mb_adu) {
      if (succes) {
        uint32_t deviceType = 0;
        uint8_t* value = mb_adu.value;
        deviceType = value[0];
        deviceType = deviceType << 8 | value[1];
        deviceType = deviceType << 8 | value[2];
        deviceType = deviceType << 8 | value[3];
        Serial.printf("Sunny Boy device type: %i\n", deviceType);
      } else {
        Serial.println("modbus error");


    WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info) {
      Serial.print("WiFi connected. IP: ");
      WiFiConnected = true;
    }, WiFiEvent_t::SYSTEM_EVENT_STA_GOT_IP);
    WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info){
        Serial.print("WiFi lost connection. Reason: ");
        WiFiConnected = false;

    WiFi.begin(ssid, pass);

    Serial.println("Connecting to WiFi... ");

void loop() {
  static uint32_t lastMillis = 0;
  if ((millis() - lastMillis > 60000 &&
      WiFiConnected) ||
      lastMillis == 0) {
    lastMillis = millis();
    if (sunnyboy.request(READ_HOLD_REGISTER, 30053, 2)) {
      Serial.print("Requesting data\n");
    } else {
      Serial.print("Request not succeeded\n");

Compile log:

Processing esp32-evb (platform: espressif32; board: esp32-evb; framework: arduino)
Verbose mode can be enabled via `-v, --verbose` option
HARDWARE: ESP32 240MHz 320KB RAM (4MB Flash)
DEBUG: CURRENT(esp-prog) EXTERNAL(esp-prog, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa)
Library Dependency Finder ->
Collected 28 compatible libraries
Scanning dependencies...
Dependency Graph
|-- <esp32ModbusTCP> 0.0.1
|   |-- <AsyncTCP> 1.0.3
|   |-- <ESP32 BLE Arduino> 1.0.1
|-- <WiFi> 1.0
Compiling .pioenvs/esp32-evb/src/main.cpp.o
Generating partitions .pioenvs/esp32-evb/partitions.bin
Compiling .pioenvs/esp32-evb/lib288/AsyncTCP_ID1826/AsyncTCP.cpp.o
Compiling .pioenvs/esp32-evb/libbc4/BLE/BLE2902.cpp.o
Compiling .pioenvs/esp32-evb/libbc4/BLE/BLE2904.cpp.o
Compiling .pioenvs/esp32-evb/libbc4/BLE/BLEAddress.cpp.o
Compiling .pioenvs/esp32-evb/libbc4/BLE/BLEAdvertisedDevice.cpp.o
Compiling .pioenvs/esp32-evb/libbc4/BLE/BLEAdvertising.cpp.o
Archiving .pioenvs/esp32-evb/lib288/libAsyncTCP_ID1826.a
Compiling .pioenvs/esp32-evb/libbc4/BLE/BLEBeacon.cpp.o
Indexing .pioenvs/esp32-evb/lib288/libAsyncTCP_ID1826.a
Compiling .pioenvs/esp32-evb/libbc4/BLE/BLECharacteristic.cpp.o
Compiling .pioenvs/esp32-evb/libbc4/BLE/BLECharacteristicMap.cpp.o
Compiling .pioenvs/esp32-evb/libbc4/BLE/BLEClient.cpp.o
Compiling .pioenvs/esp32-evb/libbc4/BLE/BLEDescriptor.cpp.o
/home/pfeerick/.platformio/packages/framework-arduinoespressif32/libraries/BLE/src/BLECharacteristic.cpp: In member function 'void BLECharacteristic::setValue(float&)':
/home/pfeerick/.platformio/packages/framework-arduinoespressif32/libraries/BLE/src/BLECharacteristic.cpp:680:16: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
*((float*)temp) = data32;
/home/pfeerick/.platformio/packages/framework-arduinoespressif32/libraries/BLE/src/BLECharacteristic.cpp: In member function 'void BLECharacteristic::setValue(double&)':
/home/pfeerick/.platformio/packages/framework-arduinoespressif32/libraries/BLE/src/BLECharacteristic.cpp:686:17: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
*((double*)temp) = data64;
Compiling .pioenvs/esp32-evb/libbc4/BLE/BLEDescriptorMap.cpp.o
Compiling .pioenvs/esp32-evb/libbc4/BLE/BLEDevice.cpp.o
Compiling .pioenvs/esp32-evb/libbc4/BLE/BLEEddystoneTLM.cpp.o
Compiling .pioenvs/esp32-evb/libbc4/BLE/BLEEddystoneURL.cpp.o
Compiling .pioenvs/esp32-evb/libbc4/BLE/BLEExceptions.cpp.o
Compiling .pioenvs/esp32-evb/libbc4/BLE/BLEHIDDevice.cpp.o
Compiling .pioenvs/esp32-evb/libbc4/BLE/BLERemoteCharacteristic.cpp.o
Compiling .pioenvs/esp32-evb/libbc4/BLE/BLERemoteDescriptor.cpp.o
Compiling .pioenvs/esp32-evb/libbc4/BLE/BLERemoteService.cpp.o
Compiling .pioenvs/esp32-evb/libbc4/BLE/BLEScan.cpp.o
Compiling .pioenvs/esp32-evb/libbc4/BLE/BLESecurity.cpp.o
Compiling .pioenvs/esp32-evb/libbc4/BLE/BLEServer.cpp.o
Compiling .pioenvs/esp32-evb/libbc4/BLE/BLEService.cpp.o
Compiling .pioenvs/esp32-evb/libbc4/BLE/BLEServiceMap.cpp.o
Compiling .pioenvs/esp32-evb/libbc4/BLE/BLEUUID.cpp.o
Compiling .pioenvs/esp32-evb/libbc4/BLE/BLEUtils.cpp.o
Compiling .pioenvs/esp32-evb/libbc4/BLE/BLEValue.cpp.o
Compiling .pioenvs/esp32-evb/libbc4/BLE/FreeRTOS.cpp.o
Compiling .pioenvs/esp32-evb/libbc4/BLE/GeneralUtils.cpp.o
Compiling .pioenvs/esp32-evb/lib27d/esp32ModbusTCP_ID5902/esp32ModbusTCP.cpp.o
Compiling .pioenvs/esp32-evb/lib114/WiFi/ETH.cpp.o
.piolibdeps/esp32ModbusTCP_ID5902/src/esp32ModbusTCP.cpp: In member function 'void esp32ModbusTCP::_send()':
.piolibdeps/esp32ModbusTCP_ID5902/src/esp32ModbusTCP.cpp:138:25: error: 'esp32ModbusInternals' has not been declared
memset(&_TXBuff[0], esp32ModbusInternals::high(, 1);
.piolibdeps/esp32ModbusTCP_ID5902/src/esp32ModbusTCP.cpp:139:25: error: 'esp32ModbusInternals' has not been declared
memset(&_TXBuff[1], esp32ModbusInternals::low(, 1);
Compiling .pioenvs/esp32-evb/lib114/WiFi/WiFi.cpp.o
/home/pfeerick/.platformio/packages/framework-arduinoespressif32/libraries/BLE/src/GeneralUtils.cpp: In static member function 'static void GeneralUtils::dumpInfo()':
/home/pfeerick/.platformio/packages/framework-arduinoespressif32/libraries/BLE/src/GeneralUtils.cpp:115:9: warning: unused variable 'freeHeap' [-Wunused-variable]
size_t freeHeap = heap_caps_get_free_size(MALLOC_CAP_8BIT);
*** [.pioenvs/esp32-evb/lib27d/esp32ModbusTCP_ID5902/esp32ModbusTCP.cpp.o] Error 1
=========================================================== [ERROR] Took 11.65 seconds ===========================================================


Hi, sorry for the long delay.

I give the ModbusTCP a chance, and actual it works for me.
Why: Because the examples are working and i need a Server/Slave mode.
My hardware is SLAVE: modbusTCPServer.begin() and PC is Master/Client
I’m filtering out all files i need, and exclude everything with ModbusRTU = RS485, because of “RS485Class RS485(SERIAL_PORT_HARDWARE)”


C and h nearly identical

Modbus Library with ID5816 is working for me.
I can define 20 InputRegisters and can read them in 10 Hz from the PC.
Initialisation: …ETH.begin(), and wifiServer.begin(), so switching is possible about flag “eth is connected”

Thanks a lot
Greetings Wolfgang