GithubHelp home page GithubHelp logo

bertmelis / esp32modbustcp Goto Github PK

View Code? Open in Web Editor NEW
81.0 81.0 32.0 581 KB

Modbus client for ESP32

License: MIT License

Batchfile 0.29% C++ 99.71%
arduino async esp32 modbus-client modbus-master modbus-tcp

esp32modbustcp's People

Contributors

bertmelis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

esp32modbustcp's Issues

Bad message response

Hi, I'm testing your new V1 code, but I get same data payload in any address I query.

After check the changes I see this in esp32ModbusMessage.cpp:

uint8_t* ModbusResponse::getData() {
return &_buffer[6];
}

If I change the buffer index to 9 I start to get the correct payload again.

uint8_t* ModbusResponse::getData() {
return &_buffer[9];
}

Thanks for you work.

onData/onError callbacks are not called in the example code.

Hello Bert,

I'm trying to run the example code given with my device/WiFi setup. I'm able to connect to the WiFi, and when readHoldingRegisters() is called I get a positive packetId back (as expected). However, the onError() and onData() callbacks are never called.

Do you have any idea what the problem might be?

Regards,
Thanakrit Lee

requirements for this library to work?

I am super new to wanting to work with modbus so really sorry if I am just not getting something. Is any external hardware requried to talk between 2 esp32s using modbus protocol or can we simply use tx rx ect or other pits to output rs485..somehow im not getting it. I know its not an issue with the library but would love clarification

modbus sunny boy + LCD I2C 20x4

Hello. The first of all, thanks for sharing your work.
I am trying to create a simple proyect. My intention is read values for inverter Sunny Boy 5.0 and shows the values on the LCD 20x4.
Then, it's more simple see the values of my photovoltaic.
The values read correctly from the inverter but When I added the code for LCD I2C 20x4, the sketch is unestable.
The LCD shows caracters wrongs and even, the Connection TCP with the inverter hang out.
Please can you review the sketch?
SMAv2.1a.zip

Disconnecting or closing a link

Hi bertmelis

Thanks for your efforts with the original ESP32ModbusTCP library.

My setup is a Sunny Boy and Sunny Island. Using your original library (not v1) I am able to read the holdingregisters I need using an ESP32 by creating two Modbus server instances. In the development environment I need to tweak code and upload, which on occasion causes Modbus comms errors to occur. If I kill the ESP32 and wait 3 minutes I get the Modbus back up and running correctly without errors. The .onerror function is reporting the errors but the link rarely recovers on its own. I’m polling 4 registers on the SI and one register on the SB every 1s.

To regain a ModbusTCP connection to the SMA without errors I either need to timeout the link (brute force) or use a library function to close the link and reopen it after a short delay(preferable as it’s more elegant).

I have scanned the ESP32ModbusTCP and AsyncTCP libraries for relevant .close() or similar .disconnect() commands, but can’t find any that seem to work they way I would have thought they worked. For example I want to close and reinstate the link ESP32ModbusTCP sunnyisland(3,{IP},502) by issuing a sunnyisland.close() on an error and then reinstate the link with another ESP32ModbusTCP sunnyisland(3,{IP},502) after a short delay.

Did you implement a TCP link disconnection in your original library? If so could you please share the syntax?

Many thanks
Carl

SMA.ino with a SMA inverter - getting error e4 for each register read

Hey Bert,

First, thank you so much for this project! This is exactly what I was trying to achieve. I'm running exactly this same setup as you ESP32 board + SMA inverter on a LAN.

I have succes to access the registers of the SunnyBoy SMA inverter manually from a PC-based modbus client on the same LAN, but I systematically get errors while using SMA.ino.

  • It successfully connects to my SSID and LAN.
  • It tries to read registers, and gets error E4
  • it then crashes and reboot:

Connecting to WiFi...
WiFi connected. IP: 192.168.10.151
reading registers
Error packet 1: e4
Error packet 2: e4
Error packet 3: e4
Error packet 4: e4
Error packet 5: e0
Error packet 6: e0
Guru Meditation Error: Core 0 panic'ed (LoadProhibited). Exception was unhandled.
Core 0 register dump:
PC : 0x4013b7ab PS : 0x00060f30 A0 : 0x800d2c84 A1 : 0x3ffcf5d0
A2 : 0x3ffcf5f8 A3 : 0x0000000c A4 : 0x0000000c A5 : 0x00000000


I tries to change and explore a few different register addresses with the same result. But they all work ok with a PC-client.

Have you ever experienced this?

Thanks Again,
Pascal

Is it running on ESP8266 ?

Hi,

I've an SMA inverter and several Wemos D1 ESP8266 boards.

So, is it possible to adapt this library to be compatible with ESP8266 ???

Thanks in advance.

Debug problem

Hello Bert,
I'm triying to debug your example code. My modbus server is a Eastron ESP 2341 modbus TCP/RTU gateway.
Debugging is hanging in this part of code. I can't find/do not understand why.

Do you have any idea?

Regards,
Joop Hilverink

ModbusTCP

Load prohibited on tcp timeout

Hi, sometimes I have an issue when tcp timeout occurs,


14:27:31.340 -> [V][esp32ModbusTCP.cpp:171] _tryToSend(): send id 593
14:27:31.807 -> [V][esp32ModbusTCP.cpp:270] _onData(): data: len 13
14:27:32.043 ->   requested 40079
14:27:32.327 -> [V][esp32ModbusTCP.cpp:171] _tryToSend(): send id 594
14:27:32.990 -> [V][esp32ModbusTCP.cpp:270] _onData(): data: len 13
14:27:33.037 ->   requested 40071
14:27:33.320 -> [V][esp32ModbusTCP.cpp:171] _tryToSend(): send id 595
14:27:33.976 -> [V][esp32ModbusTCP.cpp:270] _onData(): data: len 13
14:27:34.023 ->   requested 499
14:27:34.355 -> [V][esp32ModbusTCP.cpp:171] _tryToSend(): send id 596
14:27:35.060 -> [V][esp32ModbusTCP.cpp:270] _onData(): data: len 21
14:27:35.532 ->   requested 40265
14:27:35.813 -> [V][esp32ModbusTCP.cpp:171] _tryToSend(): send id 597
14:27:39.298 -> [V][esp32ModbusTCP.cpp:270] _onData(): data: len 17
14:27:39.536 ->   requested 40282
14:27:39.824 -> [V][esp32ModbusTCP.cpp:171] _tryToSend(): send id 598
14:27:40.728 -> [V][esp32ModbusTCP.cpp:270] _onData(): data: len 15
14:27:40.822 -> [W][AsyncTCP.cpp:962] _poll(): ack timeout 4
14:27:41.013 ->   requested 40302
14:27:41.341 -> [V][esp32ModbusTCP.cpp:171] _tryToSend(): send id 599
14:27:46.389 -> [W][AsyncTCP.cpp:962] _poll(): ack timeout 4
14:27:46.389 -> [V][esp32ModbusTCP.cpp:263] _onTimeout(): TCP timeout
14:27:46.389 -> Guru Meditation Error: Core  1 panic'ed (LoadProhibited). Exception was unhandled.
14:27:46.389 -> Core 1 register dump:
14:27:46.389 -> PC      : 0x40177b8f  PS      : 0x00060a30  A0      : 0x800ee051  A1      : 0x3ffdf740  
14:27:46.389 -> A2      : 0x00000000  A3      : 0x00000000  A4      : 0x3ffd893c  A5      : 0x00000000  
14:27:46.389 -> A6      : 0x3ffd88f4  A7      : 0xffffffc8  A8      : 0x8008922e  A9      : 0x3ffdf700  
14:27:46.389 -> A10     : 0x00000000  A11     : 0x00000000  A12     : 0x00000001  A13     : 0x00000001  
14:27:46.436 -> A14     : 0x00060a20  A15     : 0x00000000  SAR     : 0x00000004  EXCCAUSE: 0x0000001c  
14:27:46.436 -> EXCVADDR: 0x00000010  LBEG    : 0x400014fd  LEND    : 0x4000150d  LCOUNT  : 0xfffffffd  
14:27:46.436 -> 
14:27:46.436 -> Backtrace: 0x40177b8f:0x3ffdf740 0x400ee04e:0x3ffdf760 0x400ee2a8:0x3ffdf780 0x40177abb:0x3ffdf7a0 0x400e58c9:0x3ffdf7c0 0x400e5931:0x3ffdf7f0 0x400e5a67:0x3ffdf810 0x40089479:0x3ffdf840


14:52:21.093 -> [V][esp32ModbusTCP.cpp:171] _tryToSend(): send id 1329
14:52:21.602 -> [V][esp32ModbusTCP.cpp:270] _onData(): data: len 13
14:52:21.602 ->   requested 40071
14:52:22.115 -> [V][esp32ModbusTCP.cpp:171] _tryToSend(): send id 1330
14:52:22.678 -> [V][esp32ModbusTCP.cpp:270] _onData(): data: len 13
14:52:23.101 ->   requested 499
14:52:23.616 -> [V][esp32ModbusTCP.cpp:171] _tryToSend(): send id 1331
14:52:28.667 -> [W][AsyncTCP.cpp:962] _poll(): ack timeout 4
14:52:28.667 -> [V][esp32ModbusTCP.cpp:263] _onTimeout(): TCP timeout
14:52:28.667 -> Guru Meditation Error: Core  1 panic'ed (LoadProhibited). Exception was unhandled.
14:52:28.667 -> Core 1 register dump:
14:52:28.667 -> PC      : 0x40177b8f  PS      : 0x00060430  A0      : 0x800ee051  A1      : 0x3ffdf7e0  
14:52:28.667 -> A2      : 0x00000000  A3      : 0x00000000  A4      : 0x3ffd89a4  A5      : 0x00000000  
14:52:28.667 -> A6      : 0x3ffd895c  A7      : 0xffffffc8  A8      : 0x8008922e  A9      : 0x3ffdf7a0  
14:52:28.667 -> A10     : 0x00000000  A11     : 0x00000000  A12     : 0x00000001  A13     : 0x00000001  
14:52:28.667 -> A14     : 0x00060420  A15     : 0x00000000  SAR     : 0x00000004  EXCCAUSE: 0x0000001c  
14:52:28.667 -> EXCVADDR: 0x00000010  LBEG    : 0x400014fd  LEND    : 0x4000150d  LCOUNT  : 0xfffffffd  
14:52:28.667 -> 
14:52:28.667 -> Backtrace: 0x40177b8f:0x3ffdf7e0 0x400ee04e:0x3ffdf800 0x400ee2a8:0x3ffdf820 0x40177abb:0x3ffdf840 0x400e58c9:0x3ffdf860 0x400e5931:0x3ffdf890 0x400e5a67:0x3ffdf8b0 0x40089479:0x3ffdf8e0

14:57:49.701 -> [V][esp32ModbusTCP.cpp:171] _tryToSend(): send id 132
14:57:50.214 -> [V][esp32ModbusTCP.cpp:270] _onData(): data: len 13
14:57:50.308 ->   requested 40071
14:57:50.688 -> [V][esp32ModbusTCP.cpp:171] _tryToSend(): send id 133
14:57:55.231 -> [V][esp32ModbusTCP.cpp:270] _onData(): data: len 13
14:57:55.327 ->   requested 499
14:57:55.703 -> [V][esp32ModbusTCP.cpp:171] _tryToSend(): send id 134
14:57:56.036 -> [V][esp32ModbusTCP.cpp:270] _onData(): data: len 21
14:57:56.322 ->   requested 40265
14:57:56.697 -> [V][esp32ModbusTCP.cpp:171] _tryToSend(): send id 135
14:57:57.266 -> [V][esp32ModbusTCP.cpp:270] _onData(): data: len 17
14:57:57.314 ->   requested 40282
14:57:57.693 -> [V][esp32ModbusTCP.cpp:171] _tryToSend(): send id 136
14:58:02.733 -> [W][AsyncTCP.cpp:962] _poll(): ack timeout 4
14:58:02.733 -> [V][esp32ModbusTCP.cpp:263] _onTimeout(): TCP timeout
14:58:02.733 -> Guru Meditation Error: Core  1 panic'ed (LoadProhibited). Exception was unhandled.
14:58:02.733 -> Core 1 register dump:
14:58:02.733 -> PC      : 0x40177b8f  PS      : 0x00060830  A0      : 0x800ee051  A1      : 0x3ffdf670  
14:58:02.733 -> A2      : 0x00000000  A3      : 0x00000000  A4      : 0x3ffd906c  A5      : 0x00000000  
14:58:02.733 -> A6      : 0x3ffd9024  A7      : 0xffffffc8  A8      : 0x8008922e  A9      : 0x3ffdf630  
14:58:02.733 -> A10     : 0x00000000  A11     : 0x00000000  A12     : 0x00000001  A13     : 0x00000001  
14:58:02.780 -> A14     : 0x00060820  A15     : 0x00000000  SAR     : 0x00000004  EXCCAUSE: 0x0000001c  
14:58:02.780 -> EXCVADDR: 0x00000010  LBEG    : 0x400014fd  LEND    : 0x4000150d  LCOUNT  : 0xfffffffd  
14:58:02.780 -> 
14:58:02.780 -> Backtrace: 0x40177b8f:0x3ffdf670 0x400ee04e:0x3ffdf690 0x400ee2a8:0x3ffdf6b0 0x40177abb:0x3ffdf6d0 0x400e58c9:0x3ffdf6f0 0x400e5931:0x3ffdf720 0x400e5a67:0x3ffdf740 0x40089479:0x3ffdf770

and the exception decoder output:

PC: 0x40177b8f: ModbusRequest::getId() const at lib\esp32ModbusTCP\src\esp32ModbusMessage.cpp line 92
EXCVADDR: 0x00000010

Decoding stack results
0x40177b8f: ModbusRequest::getId() const at lib\esp32ModbusTCP\src\esp32ModbusMessage.cpp line 92
0x400ee04e: esp32ModbusTCP::_clearQueue(esp32Modbus::Error) at lib\esp32ModbusTCP\src\esp32ModbusTCP.cpp line 197
0x400ee2a8: esp32ModbusTCP::_onTimeout(void*, AsyncClient*, unsigned int) at lib\esp32ModbusTCP\src\esp32ModbusTCP.cpp line 265
0x40177abb: std::_Function_handler ::_M_invoke(std::_Any_data const&, void*&&, AsyncClient*&&, unsigned int&&) at c:\.platformio\packages\toolchain-xtensa32\xtensa-esp32-elf\include\c++\5.2.0/functional line 1871
0x400e58c9: AsyncClient::_poll(tcp_pcb*) at c:\.platformio\packages\toolchain-xtensa32\xtensa-esp32-elf\include\c++\5.2.0/functional line 2271
0x400e5931: AsyncClient::_s_poll(void*, tcp_pcb*) at lib\AsyncTCP\src\AsyncTCP.cpp line 1206
0x400e5a67: _async_service_task(void*) at lib\AsyncTCP\src\AsyncTCP.cpp line 171
0x40089479: vPortTaskWrapper at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/port.c line 143

´´´´

Can you take a look?, thanks.

Problems with connection and comunication!

Good morning dear Bert.
I am writing to you first of all to congratulate you for all the excellent work you do!
I am writing to you because I have an exigency!
I am designing an electronic board to which 2 Ds18b20 sensors, an alternating current sensor, digital inputs and outputs are connected.
I have to transmit the temperature, inputs, current, digital i / o data to a remote supervisor via the company wifi network.
All this I need for my company!
I tried to use your library but it always gives me communication errors!
I am using as master on the PC a library for C # called EasyModbusTCP .NET Package V5.5.
I can't open the connection nor send data!

Could you help me, even for a fee?

Thanks a lot by Davide

Writing data in to a holding register.

Hi Bertmelis, I'm so iterested on this library, it worked very well for me, but i don't now if it has implemented the write holding regiters yet. I really hope if you can help me with this. I'm working on a water well system that needs to send data via modbus tcp and store it on a database, so, the data acquirer is a ESP32 board. Thank you very much for your work, it was a great advace for my proyect.

Suggestion on how to read from two Slaves at the same time

Hello,
i have an inverter (fronius) and a smart meter.
The smart meter itself is read through the inverter with no problem.
Sometimes, though, i have a AsyncTCP kernel panic fault (i assume it is from Async lib)
My "read" function is the following

for (uint8_t i = 0; i < _numberfroniusRegisters; i++) {
        uint16_t packetId = _fronius.readHoldingRegisters(_froniusRegisters[i].address, _froniusRegisters[i].length);
        if (packetId > 0) {
            _froniusRegisters[i].packetId = packetId;
        } else {
            Serial.print("reading Primo error\n");
        }
    }
    delay(500); // test
for (uint8_t i = 0; i < _numbersmartMeterRegisters; i++) {
        uint16_t packetId = _smartMeter.readHoldingRegisters(_smartMeterRegisters[i].address, _smartMeterRegisters[i].length);
        if (packetId > 0) {
            _smartMeterRegisters[i].packetId = packetId;
        } else {
            Serial.print("reading Smart Meter error\n");
        }
    }

the onData() and onError() definition are inside the class init and are something like this

_fronius.onData([&](uint16_t packet, uint8_t slave, esp32Modbus::FunctionCode fc, uint8_t* data, uint16_t len) {
        for (uint8_t i = 0; i < _numberfroniusRegisters; ++i) {
            if (_froniusRegisters[i].packetId == packet) {
                _froniusRegisters[i].packetId = 0;
                if (_froniusRegisters[i].address == 40091) {
                    union u_tag {
                        uint32_t b;
                        float fval;
                    } u;
                    u.b = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | (data[3]);
                    _fronius_power = u.fval;
                    if (!isnan(_fronius_power)) {
                        _fronius_power = u.fval;
                        _produced_power = u.fval;
                        _average_fronius_power = (_average_fronius_power * idx + u.fval) / (idx + 1);
                        Serial.printf("_produced_power: %.2f\n", _produced_power);
                        Serial.printf("_average_fronius_power: %.2f\n", _average_fronius_power);
                    }
                }
            }
        }
    });

Do you believe that calling them sequentially like that can be the reason i have these crashes?
I will soon provide you an example of a crash

Moreover in the initialization list of my main class i have the following

 , _fronius(1, { 192, 168, 188, 62 }, 502)
 , _smartMeter(240, { 192, 168, 188, 62 }, 502)

is there a simpler way in order to avoid declaring two "esp32ModbusTCP" and only using one that can access two slave ids?

Can i extend the API of

uint16_t esp32ModbusTCP::readHoldingRegisters(uint16_t address, uint16_t numberRegisters) {
  esp32ModbusTCPInternals::ModbusRequest* request =
    new esp32ModbusTCPInternals::ModbusRequest03(_serverID, address, numberRegisters);
  return _addToQueue(request);
}

to take in also the different _serverID ? But how do i change the callback in order to understand which ID the information is from?

ERROR

Hi I am trying to install ESP32 circuit this SMA Modbus TCP application but I just get an error message that the "fatal error: AsyncTCP.h: No such file or directory" a library is missing but I can not find it anywhere. I would be interested in this application.
Regards Rauno Finland

========================================================
ERROR Message:
In file included from C:\Users\RR\AppData\Local\Temp\arduino_modified_sketch_453095\SMA.ino:42:0:

D:\Program Files\Arduino\Ohjelmat\libraries\esp32ModbusTCP-master\src/esp32ModbusTCP.h:30:22: fatal error: AsyncTCP.h: No such file or directory

compilation terminated.

exit status 1
Error compiling for board WiFi Kit 32.

Sample run error

Hello, I have a problem, looking for your help.
When using "esp32modbustcp-v1.0", esp32modbustcp-1\ SMA.ino "There was an error in the sample run. Can you show me the size?

Rebooting...
␇���H␒�6U␕␅�DKH�␒␝�
Connecting to WiFi...
WiFi connected. IP: 192.168.1.20
reading registers:
[E][esp32ModbusMessage.cpp:132] ModbusRequest(): couldn't obtain semaphore
error request status
status: 2097200
reading registers:
Guru Meditation Error: Core 1 panic'ed (LoadProhibited). Exception was unhandled.
Core 1 register dump:
PC : 0x4000c344 PS : 0x00060a33 A0 : 0x80088336 A1 : 0x3ffb1ea0
A2 : 0x00000000 A3 : 0x00000000 A4 : 0x3ffbd4c8 A5 : 0x00000000
A6 : 0x00000000 A7 : 0x03ffbd4c A8 : 0x80000000 A9 : 0x80000020
A10 : 0x00000008 A11 : 0x00000002 A12 : 0x00000005 A13 : 0x00000020
A14 : 0x00000020 A15 : 0x00000000 SAR : 0x00000010 EXCCAUSE: 0x0000001c
EXCVADDR: 0x00000000 LBEG : 0x400014fd LEND : 0x4000150d LCOUNT : 0xfffffffb
Backtrace: 0x4000c344:0x3ffb1ea0 0x40088333:0x3ffb1eb0 0x400888c5:0x3ffb1ed0 0x400d293e:0x3ffb1f10 0x400d29e7:0x3ffb1f30 0x400d20d1:0x3ffb1f50 0x400d210a:0x3ffb1f70 0x400d120b:0x3ffb1f90 0x400d3cad:0x3ffb1fb0 0x40088b7d:0x3ffb1fd0

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.