fg71
The lack of Client of different protocol in OpenPLC IDE have been there for many years. I do know that Arduino API need a Network object. This object could be Ethernet or WiFi. Many boards have it own API how this should be done instead of 1 solution that feat them all.
Let take @davenardella Settimino a S7-Communication library to be able to talk to Siemens device's or other that support it.
 
All communication API need a Network object.
 
ex.
 
#include <WiFi.h> to include API to use WiFi Network
 
OpenPLC IDE already have Modbus TCP support and it also give us the network object. It means if we like to use Settimino as Arduino extension. We understand quickly we do not need SSID, PASSWORD, WiFi.begin()... The reason is Modbus TCP already give us this information. If so many communication protocol for Arduino compatible board have the same approach why not use it in a clever way instead of ONLY for modbus protocol. It means we only need to setup a Modbus TCP with correct configuration and use other protocol. The code below is running on Arduino board with a dummy plc program that do nothing.
 
So it kind of give me the sense that Network object should be separate object, and from that object we include communication protocol. What I'm trying to say you do not need WiFi.h header if you include Modbus TCP. We could go so far to remove the Platform.h from the code below and it still work. My point is or what I like to share is. The communication tab in the upload dialog of OpenPLC could look different.
 
So there is 3 part of a communication: Network object, Interface, Protocol. It means the only thing that is different is the Protocol and its implementation.
It also means files that are part of compile state could be separate from each other and the maintaining part could be easier to debug. To embedded #ifdefine
statement to doing hacks to support a implementation do not need to be in the protocol but instead in the Interface. Its kind of way how Settimino is working
with the Platform.h header. You only need to update the header to support a board. The file I'm target here or concern is ModbusSlave.cpp I believe the structure
of that file could separate the interface from the protocol.
 
  1. Enable network (checkbox) with configuration
  2. Choose protocol interface type (Ethernet or WiFi)
  3. Enable Modbus
  4. Enable S7
It means that Modbus and S7 share the same Network object, the object that make sure your board could communicate with your Network.
This way it properly would be easier to support other protocol. Instead of have nested calls to same libraries or maybe better performance
in the compiling state.
 
The code will set the bit 0 of byte 0 to True or False every second.
 
To be able to run the code below you need a dummy plc program that include contact and coil and variable.
Add a Arduino extension, copy/paste the code below, add Modbus TCP configuration and upload to your board.
 
PS. You do need to download the Settimino library and install it on your Arduino environment.
 
//////////////////////////////// sketch section ////////////////////////////////
//#include <Platform.h>
#include <Settimino.h>

IPAddress PLC = "S7_SERVER_IP";

S7Client s7Client;

uint16_t db1 = 1;

bool state = false;
unsigned long previousMillis = 0;
const long interval = 1000;

void sketch_setup() {
    s7Client.ConnectTo(PLC, 0, 0);
}

void sketch_loop() {
    unsigned long currentMillis = millis();
    if (currentMillis - previousMillis >= interval) {
        previousMillis = currentMillis;
        
        if (state == false) {
            state = true;
        } else {
            state = false;
        }
    }

    if (s7Client.Connected) {
        s7Client.WriteBit(S7AreaDB, db1, 0, 0, state);
    }      
}
 
 
[203128%2F9302759%2Fmceclip0]
Reply 1 0
davenardella
Very impressive !! 👌
Reply 0 0
fg71
Thanks, I have find a good way to learn to ask AI-Assistant specially the Deep Seek that explain it very well. 😉
 
Lets try to understand how a Network object could be configure in OpenPLC. We make a new class OPLC_Network this class contains a interface (WiFI).
In this class we have a method to connect with parameters (ssid and password). The interface have class WiFi from the header WiFi.h.
The default connection is AP_Mode so we do not need to configure it. In other words other devices could connect to it.
 
Lets assume OpenPLC had a class OPLC_Network we could then ask in Arduino extension if its connected with method isConnected()
If we are connected we do protocol calls. OpenPLC do not have API to call classes or method that it configure in UI.
Lets assume we could inject C code in Function Block directly inside the application and we use a OpenPLC API.
 
// Part of Network object and its interface, its the interface that separate the Arduino compatible boards
#include <WiFi.h>

// Network object class
class OPLC_Network {
private:
    const char* ssid;
    const char* password;
public:
    OPLC_Network(const char* ssid, const char* password) : ssid(ssid), password(password) {}

    void connect() {
        WiFi.begin(ssid, password);
        Serial.print("Connecting to WiFi...");
        
        while (WiFi.status() != WL_CONNECTED) {
            delay(1000);
            Serial.print(".");
        }

        Serial.println("\nConnected!");
        Serial.print("IP Address: ");
        Serial.println(WiFi.localIP());
    }
};

// We initialize the object OPLC_Network and configure the SSID and PASSWORD
OPLC_Network myNetworkObject("SSID", "PASSWORD");

void setup() {
    Serial.begin(115200);
    // We connect to the Network
    myNetworkObject.connect();
}

void loop() {
    // Your network-related tasks here
}
Reply 1 0
fg71
Here is a prototype of how OpenPLC could work, this code is made for Arduino IDE and not for Arduino extension.
I try to explain what I'm trying to do.
 
The code start with macro and board definition for the Network interface WiFi for Arduino Nano ESP32. It include the header for protocol S7
and it include preprocessor directive OPLC_Network_WiFi
 
Inside the preprocessor conditional directive OPLC_Network_WiFI we make the class OPLC_Network and its methods.
 
The rest of the code is part of basic Arduino code there you have 2 functions setup() and loop(). The things we need to configure
and only run once we include in setup() function and the part that is specific to plc are running in loop() function. In other
words the data exchange I/O is made in loop() function and in this case WriteBit() function from Settimino.
The macro could be part of header, and the class and method could be part of interface.cpp file. The only purpose of the
interface of Network object is connect to the Network. Its the Client, Server or any protocol that are doing the rest and they
could belong to its own files. In this example I have choose to include everything in the same file to make it simple.
You could say this approach of thinking is a modular programming there difference things are separated from each other.
 


#ifdef ARDUINO_ARCH_ESP32
  #include <WiFi.h>
  #include <Settimino.h>
  #define OPLC_Network_WiFi
#endif

#ifdef OPLC_Network_WiFi // Setup the interface WiFi

  class OPLC_Network {
    private:
      const char* ssid;
      const char* password;
    public:
      OPLC_Network(const char* ssid, const char* password) : ssid(ssid), password(password) {}

    void connect() {
        WiFi.begin(ssid, password);
        Serial.print("Connecting to WiFi...");
        
        while (WiFi.status() != WL_CONNECTED) {
            delay(2000);
            Serial.print(".");
        }

        Serial.println("\nConnected!");
        Serial.print("IP Address: ");
        Serial.println(WiFi.localIP());
    }

    bool isConnected() {
        return WiFi.status() == WL_CONNECTED;
    }
  };

#endif

//Initialize the S7Client object
S7Client s7Client;

//Configure the PLC IP
IPAddress PLC(PLC_IP);
uint16_t db1 = 1;

void setup() {
    //Connect the board to the Network object
    OPLC_Network myNetwork("SSID", "PASSWORD");
    myNetwork.connect();
    
    //Connect to the protocol
    if (myNetwork.isConnected()) {
      s7Client.ConnectTo(PLC, 0, 0);
    }
}

bool state = false;
unsigned long previousMillis = 0;
const long interval = 1000;

void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;

    if (state == false) {
      state = true;
    } else {
      state = false;
    }
  }

  if (s7Client.Connected) {
    s7Client.WriteBit(S7AreaDB, db1, 0, 0, state);
  }

}
 
Reply 0 0
Reply