KingoftheRoad
Hello all,
 
I am new to OpenPLC and am currently making my first attempts. Currently I have a problem with establishing the Modbus connection and can't get any further.
 
My setup:
The OpenPLC Runtime is running on a Raspberry Pi 4. In each room of my flat I want to measure the temperature and humidity with a D1 Board NodeMCU ESP8266MOD-12F and a DHT11. The whole thing is to be visualised with ScadeBR. For the connection of the boards to the Raspberry Pi I have set the WLAN module so that it opens an independent WLAN.
 
My problem:
Since the DHT11 cannot be read directly with OpenPLC and I still want to connect an LCD display to a D1 board, among other things, I want to program these via the Arduino IDE.
I have downloaded the library for Modbus communication from the Internet (https://github.com/emelianov/modbus-esp8266). Via Modbus, I want to transmit not only the inputs and outputs of the board, but all possible signals.
Accordingly, I have added a Generic Modbus TCP Device in the OpenPLC Runtime.

When I now start the runtime, I get the following message:
 

OpenPLC Runtime starting...

Interactive Server: Listening on port 43628

Device ESP8266 1 is disconnected. Attempting to reconnect...

Warning: Persistent Storage file not found

Connection failed on MB device ESP8266 1: Operation now in progress

Device ESP8266 1 is disconnected. Attempting to reconnect...

Issued start_modbus() command to start on port: 502

Server: Listening on port 502

Server: waiting for new client...

Issued start_dnp3() command to start on port: 20000

DNP3 ID manager: Starting thread (0)

DNP3 ID DNP3_Server: Listening on: 0.0.0.0:20000

Issued start_enip() command to start on port: 44818

Server: Listening on port 44818

Server: waiting for new client...

Issued stop_pstorage() command

Connection failed on MB device ESP8266 1: Operation now in progress

Device ESP8266 1 is disconnected. Attempting to reconnect...

Connection failed on MB device ESP8266 1: Operation now in progress

Device ESP8266 1 is disconnected. Attempting to reconnect...

Connection failed on MB device ESP8266 1: Operation now in progress

Device ESP8266 1 is disconnected. Attempting to reconnect...

Connection failed on MB device ESP8266 1: Operation now in progress

Device ESP8266 1 is disconnected. Attempting to reconnect...

Connection failed on MB device ESP8266 1: Operation now in progress

Device ESP8266 1 is disconnected. Attempting to reconnect...

Connection failed on MB device ESP8266 1: Operation now in progress

Device ESP8266 1 is disconnected. Attempting to reconnect...

Connection failed on MB device ESP8266 1: Operation now in progress

Device ESP8266 1 is disconnected. Attempting to reconnect...

Connection failed on MB device ESP8266 1: Operation now in progress

Device ESP8266 1 is disconnected. Attempting to reconnect...

Connection failed on MB device ESP8266 1: Connection refused

Device ESP8266 1 is disconnected. Attempting to reconnect...

Connection failed on MB device ESP8266 1: Connection refused

Server: Client accepted! Creating thread for the new client ID: 9...

Server: waiting for new client...

Server: Thread created for client ID: 9

Device ESP8266 1 is disconnected. Attempting to reconnect...

Connection failed on MB device ESP8266 1: Connection refused

Device ESP8266 1 is disconnected. Attempting to reconnect...

Connection failed on MB device ESP8266 1: Connection refused

...

 

From the D1 board I get the feedback that the Modbus connection has been established:

Bild1.jpg

Here is also the slave setting:

Bild2.jpg

And the Code on the D1 Board:

/*
  Modbus-Arduino Example - Master Modbus IP Client (ESP8266/ESP32)
  Read Holding Register from Server device

  (c)2018 Alexander Emelianov (a.m.emelianov@gmail.com)
   https://github.com/emelianov/modbus-esp8266
*/

#ifdef ESP8266
 #include <ESP8266WiFi.h>
#else
 #include <WiFi.h>
#endif
#include <ModbusIP_ESP8266.h>

const int Testsignal = 800;               // Modbus Offset
IPAddress remote(192, 168, 222, 1);  // Address of Modbus Slave device

ModbusIP mb;  //ModbusIP object

void setup() {
  Serial.begin(115200);
 
  WiFi.begin("raspberry-wlan", "az-delivery");
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
 
  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  mb.client();
  
  //mb.Coil(Testsignal);
}

void loop() {
  if (mb.isConnected(remote)) {   // Check if connection to Modbus Slave is established
    Serial.println("Modbus verbunden");
  } else {
    mb.connect(remote);           // Try to connect if no connection
    Serial.print(".");
  }
  
  mb.task();                      // Common local Modbus task

  //Serial.println(mb.Coil(Testsignal));

  //delay(100);
}
 
I would be happy if someone can help me with my problem.
P.S.: I come from the field of Siemens programming in particular. I learned the basics of text programming during my studies, but I'm not very good at it.
 
Thanks Steffen
Reply 0 0
thiagoralves
You are building a custom firmware for the ESP 8266, so there are several points of failure there. I'm not familiar with the library you're using (which could be one of the problems), but your code looks very barebones. It doesn't appear to have any Modbus functionality at all. I highly recommend you try uploading an OpenPLC firmware (like the blank program example showed in the documentation section of the OpenPLC website) first to the ESP 8266 board, and then try the Modbus slave communication. Make sure that it works properly. Then you can go and replace the OpenPLC firmware if you want with your own custom firmware. Perhaps a good idea would be to start with examples from that custom library you got.
Reply 0 0
KingoftheRoad
Hello Thiago,
 
Thank you very much for your feedback. I have not yet dared to adapt your firmware because, as I said, I am not very familiar with text programming.
 
I have adapted my programme with the Modbus Library and tested this with Modbus Poll. That went without a problem. With Modbus Poll, I could switch the LED on the board on and off. I also tested the OpenPLC communication again with Modbus Poll. Here, too, I was able to change the state of an output via Modbus.
 
However, when I now start the OpenPLC Runtime, I get the following feedback:
 

OpenPLC Runtime starting...

Interactive Server: Listening on port 43628

Device ESP8266 is disconnected. Attempting to reconnect...

Warning: Persistent Storage file not found

Connected to MB device ESP8266

Modbus Write Coils failed on MB device ESP8266: Illegal data address

Device ESP8266 is disconnected. Attempting to reconnect...

Connected to MB device ESP8266

Modbus Write Coils failed on MB device ESP8266: Illegal data address

Device ESP8266 is disconnected. Attempting to reconnect...

Connected to MB device ESP8266
 
...
 
I don't know exactly where this error is coming from now. Do you have an explanation for it? In Modbus Poll, I also get the response illegal data address for addresses that I do not use. Not via OpenPLC.
 
Modbus Library.jpgModbus Pull.jpgSlave device.jpg
Reply 0 0
thiagoralves
If you’re getting illegal data address it means you are not implementing on your firmware all coils you’re trying to read. From the screenshot it looks like you’re trying to read 8 coils. Make sure your firmware implements all 8
Reply 0 0
KingoftheRoad
I have now tried everything possible but I still get the message "Illegal Data Address". In any case, all the addresses set in the runtime were also created in the firmware. I don't know...
 
If I set up the ESP8266 with "Blank Projekt.zip" via the OpenPLC Editor, I can access the inputs and outputs. That worked.
 
As mentioned, I also want to read in sensors like the DHT11. Earlier I downloaded the firmware "OpenPLC_esp8266.zip" (https://openplc.discussion.community/post/openplc-now-supports-esp8266-8176870) and tried it out.
Unfortunately without success. The firmware reports that a connection exists, but in the runtime I get the message "Connection failed on MB device ESP8266 1: Connection refused. Device ESP8266 1 is disconnected. Attempting to reconnect...". Nothing is displayed in the monitoring either. Is there more to be adjusted here than the IP address and the WLAN access?
Unbenannt 1.jpg
Reply 0 0
thiagoralves
I believe you might be setting the wrong addresses on your esp8266 firmware

With the OpenPLC firmware, if nothing is displayed after that first error then probably it is working. It is normal for the device to lose the first packets until the connection is established.
Reply 0 0
richw
Steffen, If your using the program example you listed above, it is being used as a Master(client) and needs to be setup as a Slave(server) if you want to use it with the OpenPLC runtime. I also was confused by the change in names from Master/Slave to Client/Server. I am using this very library  on an ESP32 as a slave and have no problems. There are many examples supplied with library, look for the TCP-ESP server example. There are 4 basic examples  you can try. I would recommend starting with one coil at first to make sure its working.. 
Reply 0 0
KingoftheRoad
Thank you for your feedback.
Since the website says that OpenPLC has a Modbus server, I initially thought that I had to set up a Modbus client. After this did not work with Modbus Poll, I uploaded the example from the library for the Modbus server and adapted it.
As I said, this works without any problems with Modbus Poll. However, I get the above-mentioned message in OpenPLC. I also varied the number of coils in OpenPLC and in my programme. But the message remains the same, regardless of whether I have set fewer, more or the same number of coils in my programme.
Here is my current programme again. Do you notice anything?
 
/*
  Modbus-Arduino Example - Test Led (Modbus IP ESP8266)
  Control a Led on GPIO0 pin using Write Single Coil Modbus Function 
  Original library
  Copyright by André Sarmento Barbosa
   http://github.com/andresarmento/modbus-arduino

  Current version
  (c)2017 Alexander Emelianov (a.m.emelianov@gmail.com)
   https://github.com/emelianov/modbus-esp8266
*/

#ifdef ESP8266
 #include <ESP8266WiFi.h>
#else //ESP32
 #include <WiFi.h>
#endif
#include <ModbusIP_ESP8266.h>


//Modbus Registers Offsets

const int LED_COIL0 = 800;

int Pin4 = 4;

//Used Pins
//const int ledPin = 4; //GPIO0

//ModbusIP object
ModbusIP mb;
  
void setup() {
  Serial.begin(115200);
 
  WiFi.begin("raspberry-wlan", "az-delivery");
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
 
  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  mb.server();

  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(Pin4, INPUT);
  
  mb.addCoil(LED_COIL0);
}
 
void loop() {
   //Call once inside loop() - all magic here
   mb.task();

   mb.Coil(LED_COIL0,digitalRead(Pin4));
   
   //Attach ledPin to LED_COIL register
   digitalWrite(LED_BUILTIN, mb.Coil(LED_COIL0));
   //delay(10);
}
 
Reply 0 0
thiagoralves
Yup, your address is wrong. It shouldn’t be 800, but 0 (zero) instead
Reply 0 0
thiagoralves
Also, I believe you should remove your "mb.Coil(LED_COIL0,digitalRead(Pin4));" line. Modbus tasks are executed when the mb.task(); is called. If you keep that line in there, what it will essentially do is make your coil read only. No matter what you ask that coil to be over Modbus, it will always have the state of your Pin4. The last line is just enforcing the state of the pin that was just read. What you are doing is (logically) exactly as this:
digitalWrite(LED_BUILTIN, digitalRead(Pin4));
 
Reply 0 0
KingoftheRoad
Yup, your address is wrong. It shouldn’t be 800, but 0 (zero) instead
I have now tried it again with the 0 and now it works.
But then I don't understand the address table. Why is %QX100.0 assigned to address 0? According to the table, this is address 800. Can you please explain this again.
 
Unbenannt 2.jpg
 
I ask myself the same question when I have inputs and coils. Then I have %QX100.0 and %IX100.0, both of which have the same Modbus address. What is the distinction then?
Reply 0 0
thiagoralves
These addresses are from the master's perspective!! You are programming the slave, hence your address is zero for the slave. The zero address on the slave is mapped at the %QX100.0 on the master (modbus address 800). Therefore, you can access the same pin querying the slave at address zero, or querying the master at address 800. It goes exactly to the same spot, except that querying the master you will have one extra hop.
Reply 0 0
KingoftheRoad
Thank you for the clarification. Now it works 👍
Reply 0 0
Reply