logo elektroda
logo elektroda
X
logo elektroda

[Solved] ESP32 communication with Siemens PLC - Ethernet connection using ENC28J60

madiz08 7032 15
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 18302167
    madiz08
    Level 14  
    The topic is probably a bit off topic for this section, but probably in the Automation section, no one will be able to help me in this topic.
    Some time ago someone wrote the Settimino.h library for communication between Arduino and Siemens PLC. With an ethernet connection, I use UNO or Mega with the W5100 ethernet module, if I use ESP32, I connect via WiFi and everything is fine.
    I would like to use a LAN cable connection, but using ESP32, which has no comparison at all with UNO or MEGA in terms of speed, multifunctionality, etc.
    https://www.elektroda.pl/rtvforum/topic3634381.html
    In the link above, I have already received help on how to configure the ESP32 + enc28j60 ethernet module together. I assembled a board for this purpose:
    ESP32 communication with Siemens PLC - Ethernet connection using ENC28J60
    At the beginning, I uploaded the following program to check if a connection to the network and then to the PLC will be established
    Code: C / C++
    Log in, to see the code


    Watching the serial monitor, I noticed that the microcontroller establishes a connection to the network, connects to the assigned IP, but when trying to establish a connection with the PLC, unfortunately, the program resets again and again.
    There is an important thing in the settimino library, namely the file platform.h
    Code: C / C++
    Log in, to see the code


    Depending on the microcontroller you are using, you should select the appropriate hardware there
    Code: C / C++
    Log in, to see the code

    Which as you can see below in the library affects the choice and "EthernetClient.h" or and "WiFiClient.h"
    I changed the ESP32 selection to:
    Code: C / C++
    Log in, to see the code

    Compilation errors
    I tried another way:
    Code: C / C++
    Log in, to see the code

    I was getting the same compile errors
    Code: C / C++
    Log in, to see the code
    https://www.elektroda.pl/rtvforum/posting.php?mode=newtopic&f=428#
    Could any of your colleagues spare some time to analyze this problem?
    Do you have a problem with Arduino? Ask question. Visit our forum Arduino.
  • ADVERTISEMENT
  • #2 18302610
    madiz08
    Level 14  
    error: 'EthernetClass Ethernet' redeclared as different kind of symbol extern EthernetClass Ethernet;

    Added after 7 [minutes]:

    If I comment out two lines:
    //#include "Settimino.h"
    //S7Client Client;//
    and get rid of the connect() function, then everything is ok. This ENC28J6 module uses and the settimino library refers to . Two libraries used together cause errors and whether it could be somehow unified
  • Helpful post
    #3 18302935
    khoam
    Level 42  
    I was able to compile the program with the following fix:
    Code: C / C++
    Log in, to see the code
    Code: Bash
    Log in, to see the code

    An additional problem is that you have the library installed in the library manager of the Arduino IDE Ethernet which actually conflicts with the library EtherCard .

    Added after 48 [minutes]:

    An alternative and probably better solution is to use a library UIPEthernet instead of EtherCard. UIPEthernet also supports ENC28J60 and additionally emulates class of the "standard" Ethernet library, i.e. Ethernet , EthernetClient . Unfortunately, you'll have to "rewrite" your program quite a bit. Whichever you use, you need to uninstall the Ethernet library from the Arduino IDE - UIPEthernet interferes with it too.
    https://github.com/UIPEthernet/UIPEthernet

    Unfortunately, the SETTIMINO library is written in such a way that it requires standard classes and methods, as in the Ethernet library.
  • #4 18303455
    madiz08
    Level 14  
    ESP32 communication with Siemens PLC - Ethernet connection using ENC28J60
    In fact, I managed to compile after your corrections, but when IP was assigned and ESP32 started to establish connection with PLC via Settimino.h, then the microcontroller restarted as in the attached picture after entering the Connect() function.

    Added after 1 [minutes]:

    After removing Ethernet.h, information about the lack of Ethernet.h popped up during compilation. Thanks for the advice, I'll try the other library again
  • ADVERTISEMENT
  • Helpful post
    #5 18303507
    khoam
    Level 42  
    madiz08 wrote:
    I'll try the other library

    This will probably be the only sensible solution. An alternative is to "patch" the SETTIMINO library :)
    When using UIPEthernet, you also need to remember to rename the header files in Platfform.h according to the scheme:
    Ethernet.h => UIPEthernet.h
    EthernetClient.h => UIPClient.h
    You do not have to, and even must not, remove the S7WIRED definition in the configuration for ESP32.
  • #6 18303537
    madiz08
    Level 14  
    So maybe it's worth staying with the current library? and try to change Settimino along with platform.h. Unfortunately, I'm bad at these libraries, but it seems to me that establishing a connection with the PLC is done via TCP client. I didn't find anything like that in EtherCard.
    The first thing in settimino is:
    void EthernetInit(uint8_t *mac, IPAddress ip);
    In settimino.cpp there is an extension of this function:
    Code: C / C++
    Log in, to see the code

    and below is a link to the client
    Code: C / C++
    Log in, to see the code

    and the Ethernet.Client file is black magic for me
  • Helpful post
    #7 18303566
    khoam
    Level 42  
    madiz08 wrote:
    So maybe it's worth staying with the current library? and try to change Settimino along with platform.h.

    The Settimino library has a lot of functionality that you are unlikely to write yourself from scratch.
    Don't worry about Ethernet.init() calls, the UIPEthernet library emulates the Ethernet or EtherneClient classes and they are "compatible" with the standard Ethernet library, so it should work. In fact, you need to make some minor changes to Platform.h, which I wrote about above, and some more changes to your program ;)
  • ADVERTISEMENT
  • #8 18304402
    madiz08
    Level 14  
    I downloaded the library and already when checking the examples there are problems with compiling for ESP32. I'm checking it on my laptop at work. At home, I also remember some problems with compiling ethernet connections for ESP32, but I managed to solve this problem somehow, so when I come back after work, I will check it right away.

    Added after 5 [hours] 43 [minutes]:

    Ok, everything is compiling, first I will try to look for some examples using enc28j60 and connecting to the network, and if I succeed, I will try to make changes in settimino

    Added after 37 [minutes]:

    I played the TCP_Server example to start with
    Code: C / C++
    Log in, to see the code

    At the beginning of the library there is information that it is an adaptation for Enc28J60, so I played it without any changes. First I tried to ping it after connecting to the network and unfortunately no response every time, also no connection to the local network.
    I started to wonder if the connection is the same as before. The UIPEthernet library in the hardware folder contains pictures of the connections for individual boards, but no ESP32. In the case of ESP8266, the connection looks like this:
    ESP32 communication with Siemens PLC - Ethernet connection using ENC28J60
    It looks like it should be the same, but it doesn't work. The connection on the board is 100% ok, because using the EtherCard library I can ping, i.e. the device is seen in the network
  • #9 18305121
    khoam
    Level 42  
    Connections to pins in ESP32 should be:
    C.S. = 5
    MOSI = 23
    MISO = 19
    SCK = 18
    SS is used as CS unless you call Ethernet.init(CS_PIN) and then select a different pin as CS in ESP32.
  • ADVERTISEMENT
  • #10 18305445
    madiz08
    Level 14  
    ESP32 communication with Siemens PLC - Ethernet connection using ENC28J60
    At the beginning also 0 responses, but when I added SPI.h to the TCPServer.ino example, it's ok, also positive for now :) second and third ping correct
  • #11 18305459
    khoam
    Level 42  
    madiz08 wrote:
    but as I added SPI.h

    Without SPI support, the ESP32 is unlikely to communicate with the ENC28J60 :)
  • #12 18305516
    madiz08
    Level 14  
    And could you guide me where to change these SPI pins to 12, 13, 14, 15 as in the case of EtherCard? If it's not advisable, then of course I'll stay with it. I just mean that now, I connect the second ESP32 with wires to the ethernet module, and if I had the same pins as in EtherCard, I could use my board, where I already have milled paths. But of course, if it's inadvisable, it stays what it is. In SPI.cpp I see either the default choice or just 12, 13, 14, 15, which is probably not here

    Added after 1 [minutes]:

    Code: C / C++
    Log in, to see the code

    The above is in SPI.cpp
  • #13 18305743
    khoam
    Level 42  
    madiz08 wrote:
    And could you guide me where to change these SPI pins to 12, 13, 14, 15 as in the case of EtherCard? J

    To begin with, try making a slight change to the utility/Enc28J60Network.cpp file in the UIPEthernet library. The code snippet below is at the beginning of this file. It's about modifying one line.
    [syntax=c]#if ENC28J60_USE_SPILIB
    #if defined(ARDUINO)
    #if defined(STM32F2)
    #include
    #elif !defined(STM32F3) && !defined(__STM32F4__)
    #include
    extern SPIClass SPI;
    //#elif defined(ARDUINO_ARCH_AMEBA)
    //SPIClass SPI((void *)(&spi_obj), 11, 12, 13, 10);
    //SPI _spi(SPI_MOSI,SPI_MISO,SPI_SCK,ENC28J60ControlCS);
    #else
    #include "HardwareSPI.h"
    extern HardwareSPI SPI(VSPI); //
  • #14 18306314
    madiz08
    Level 14  
    Unfortunately, it didn't help. I also scanned the entire computer and there is no HardwareSPI.h file
  • #15 18307439
    khoam
    Level 42  
    madiz08 wrote:
    I also scanned the entire computer and there is no HardwareSPI.h file

    In fact, you're right. I overlooked it. It seems that you have to "force" change the pin assignment for SPI. This can be done by modifying the Enc28J60Network.h file. At the very end you need to put additional lines of code, as below:
    Code: C / C++
    Log in, to see the code

    The previous modification in the Enc28J60Network.cpp file has no effect when working with ESP32, so it can be restored to its original form.
  • #16 18326706
    madiz08
    Level 14  
    Sorry for not answering for more than a week, but really only now I was able to get back to the topic.
    Regarding the attempt to modify the library, due to the change of the SPI pins, the modification did not help here, but it is irrelevant. I made a second board with pins for the enc28j60 pins, according to the pattern you presented to me earlier, i.e.:
    C.S. = 5
    MOSI = 23
    MISO = 19
    SCK = 18
    Then in platform.h for ESP32 I set S7WIRED and replaced it according to the guidelines:
    Ethernet.h => UIPEthernet.h
    EthernetClient.h => UIPClient.h
    Everything works as it should :) It is true that it probably works on the principle of connect - send - disconnect, where you need to spend 500ms on it, but still I can easily exchange data with the PLC in both directions using ESP32.
    Once again thank you very much for your help.

Topic summary

The discussion revolves around establishing an Ethernet connection between an ESP32 and a Siemens PLC using the ENC28J60 Ethernet module. The user initially faced compilation errors due to conflicts between the Settimino library and Ethernet libraries. Solutions included modifying library includes and using the UIPEthernet library, which is compatible with the ENC28J60. The user successfully compiled the code after adjusting the SPI pin assignments and library configurations. Ultimately, the ESP32 was able to communicate with the PLC, albeit with a connection method that required a delay for data exchange.
Summary generated by the language model.
ADVERTISEMENT