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.
Other
534 stars 188 forks source link

[TCP] Too many connections? #193

Closed stritti closed 2 years ago

stritti commented 2 years ago

Hi @emelianov

I use your library to create an e-paper device to monitor solar power converter: https://github.com/stritti/kostal-pv-monitor

The device will go to deep sleep for 1min and queries via modbus data of converter to update the e-paper. Now I have to see, that after some days the device does not get correct data any more. It looks like there are too many connections open or something like this.

I know, this is very device specific, but probably I implemented your lib in wrong way. It would be awesome If you could spend a little time to review my example. Every suggestion is welcome.

emelianov commented 2 years ago

Hello, I don't think the issue is library related. For the sketch and the library every wake event looks just like starting from by power on. Basing on ESP32 documentation the only one suggestion:

Try to use esp_wifi_stop() before go to deep sleep:

Before entering deep sleep or light sleep modes, applications must disable WiFi and BT using appropriate calls (esp_bluedroid_disable(), esp_bt_controller_disable(), esp_wifi_stop()).

stritti commented 2 years ago

Hello @emelianov

that is a good suggestion. I will try this in a week (not at home next week).

Thank you very much for your investigation and time invest.

stritti commented 2 years ago

Thanks for the hint.

Following lines seem to solve the issue (running since 7 days now):

  mb.disconnect(remote);            // Close Modbus connection and
  mb.dropTransactions();            // cancel all waiting transactions
  while (mb.isConnected(remote)) {  // Check if connection to Modbus Slave is established and wait until it is closed
    mb.task();
    delay(10);
  }

  WiFi.disconnect(); // disconnect WiFi now