fg71
 
I'm looking at raspberrypi.cpp and was thinking to use #define macro to include both pigpio and wiringpi in the same .cpp
I could then use compile flag -D if the I like to use pigpio ex. -DUSE_PIGPIO
This way I believe the only thing a user need to do is to update compile_program.sh to be able to use any of whose 2 libraries.
 
What do you think of it?....
 
Here is early copy of raspberrypi.cpp
 
//-----------------------------------------------------------------------------
// Copyright 2015 Thiago Alves
//
// Based on the LDmicro software by Jonathan Westhues
// This file is part of the OpenPLC Software Stack.
//
// OpenPLC is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenPLC is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with OpenPLC.  If not, see <http://www.gnu.org/licenses/>.
//------
//
// This file is the hardware layer for the OpenPLC. If you change the platform
// where it is running, you may only need to change this file. All the I/O
// related stuff is here. Basically it provides functions to read and write
// to the OpenPLC internal buffers in order to update I/O state.
// Thiago Alves, Dec 2015
//-----------------------------------------------------------------------------

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#ifdef USE_PIGPIO
    #include <pigpio.h>
#else
    #include <wiringPi.h>
    #include <wiringSerial.h>
#endif

#include <pthread.h>

#include "ladder.h"

#if !defined(ARRAY_SIZE)
    #define ARRAY_SIZE(x) (sizeof((x)) / sizeof((x)[0]))
#endif

#ifdef USE_PIGPIO
    // Using PiGPIO
    #define MAX_INPUT     14
    #define MAX_OUTPUT     11
    #define MAX_ANALOG_OUT  1

//========================[I/O PINS CONFIGURATION]========================
// A good source for RaspberryPi I/O pins information is: http://pinout.xyz
// The buffers below works as an internal mask, so that the
// OpenPLC can access each pin sequentially
//===================================================================

//inBufferPinMask: pin mask for each input, which
//means what pin is mapped to that OpenPLC input
    int inBufferPinMask[MAX_INPUT] = \
        { 2, 3, 4, 17, 27, 22, 10, 9, 11, 5, 6, 13, 19, 26 };

//outBufferPinMask: pin mask for each output, which
//means what pin is mapped to that OpenPLC output
    int outBufferPinMask[MAX_OUTPUT] =  \
        { 14, 15, 23, 24, 25, 8, 7, 12, 16, 20, 21 };

//analogOutBufferPinMask: pin mask for the analog PWM
//output of the RaspberryPi
    int analogOutBufferPinMask[MAX_ANALOG_OUT] = \
        { 18 };

//-----------------------------------------------------------------------------
// This function is called by the main OpenPLC routine when it is initializing.
// Hardware initialization procedures should be here.
//-----------------------------------------------------------------------------
    void initializeHardware() {
        gpioInitialise();
        //piHiPri(99);

        //set pins as input
        for (int i = 0; i < MAX_INPUT; i++) {
            gpioSetMode(inBufferPinMask[i], PI_INPUT);
                //pull down can't be enabled on the first two pins
                if (i != 0 && i != 1) {
                    gpioSetPullUpDown(inBufferPinMask[i], PI_PUD_DOWN); //pull down enabled
                }
        }

        //set pins as output
        for (int i = 0; i < MAX_OUTPUT; i++) {
            gpioSetMode(outBufferPinMask[i], PI_OUTPUT);
        }

        //set PWM pins as output
        for (int i = 0; i < MAX_ANALOG_OUT; i++) {
            gpioSetMode(analogOutBufferPinMask[i], PI_ALT5);
            gpioSetPWMrange(analogOutBufferPinMask[i], 1024);
        }
    }

//-----------------------------------------------------------------------------
// This function is called by the main OpenPLC routine when it is finalizing.
// Resource clearing procedures should be here.
//-----------------------------------------------------------------------------
    void finalizeHardware() {
        gpioTerminate();
    }

//-----------------------------------------------------------------------------
// This function is called by the OpenPLC in a loop. Here the internal buffers
// must be updated to reflect the actual state of the input pins. The mutex buffer_lock
// must be used to protect access to the buffers on a threaded environment.
//-----------------------------------------------------------------------------
    void updateBuffersIn() {
        pthread_mutex_lock(&bufferLock); //lock mutex
            //INPUT
            for (int i = 0; i < MAX_INPUT; i++) {
                if (bool_input[i/8][i%8] != NULL) *bool_input[i/8][i%8] = gpioRead(inBufferPinMask[i]);
            }
        pthread_mutex_unlock(&bufferLock); //unlock mutex
    }

//-----------------------------------------------------------------------------
// This function is called by the OpenPLC in a loop. Here the internal buffers
// must be updated to reflect the actual state of the output pins. The mutex buffer_lock
// must be used to protect access to the buffers on a threaded environment.
//-----------------------------------------------------------------------------
    void updateBuffersOut() {
        pthread_mutex_lock(&bufferLock); //lock mutex
            //OUTPUT
            for (int i = 0; i < MAX_OUTPUT; i++) {
                if (bool_output[i/8][i%8] != NULL) gpioWrite(outBufferPinMask[i], *bool_output[i/8][i%8]);
            }

            //ANALOG OUT (PWM)
            for (int i = 0; i < MAX_ANALOG_OUT; i++) {
                if (int_output[i] != NULL) gpioPWM(analogOutBufferPinMask[i], (*int_output[i] / 64));
            }
        pthread_mutex_unlock(&bufferLock); //unlock mutex
    }
#else
    //Using WiringPi
    #define MAX_INPUT     <INPUT_TO_DO>
    #define MAX_OUTPUT     <INPUT_TO_DO>
    #define MAX_ANALOG_OUT  <INPUT_TO_DO>

    int inBufferPinMask[MAX_INPUT] = {<INPUT_TO_DO>};
    int outBufferPinMask[MAX_OUTPUT] =  {<INPUT_TO_DO>};
    int analogOutBufferPinMask[MAX_ANALOG_OUT] = {<INPUT_TO_DO>};
    
    void initializeHardware() {
        //wiringSetup();          // WiringPi pin mode
        //wiringPiSetupGpio();    // BCM/GPIO pin mode
        wiringPiSetupPhys();      // Physical pin mode

        for (int i = 0; i < MAX_INPUT; i++) {
            pinMode(inBufferPinMask[i], INPUT);
                if (i != 0 && i != 1) {
                    pullUpDnControl(inBufferPinMask[i], PUD_DOWN);
                }
        }

        for (int i = 0; i < MAX_OUTPUT; i++) {
            pinMode(outBufferPinMask[i], OUTPUT);
        }
    
        for (int i = 0; i < MAX_ANALOG_OUT; i++) {
            pinMode(analogOutBufferPinMask[i], PWM_OUTPUT);
        }
    }
    
    void finalizeHardware() {
    }
    
    void updateBuffersIn() {
        pthread_mutex_lock(&bufferLock);
        for (int i = 0; i < MAX_INPUT; i++) {
            if (bool_input[i/8][i%8] != NULL) *bool_input[i/8][i%8] = digitalRead(inBufferPinMask[i]);
        }
        pthread_mutex_unlock(&bufferLock);
    }
    
    void updateBuffersOut() {
        pthread_mutex_lock(&bufferLock);
            for (int i = 0; i < MAX_OUTPUT; i++) {
                if (bool_output[i/8][i%8] != NULL) digitalWrite(outBufferPinMask[i], *bool_output[i/8][i%8]);
            }
    
            for (int i = 0; i < MAX_ANALOG_OUT; i++) {
                if (int_output[i] != NULL) pwmWrite(analogOutBufferPinMask[i], (*int_output[i] / 64));
            }
        pthread_mutex_unlock(&bufferLock);
    }
#endif
 
fg71
Smile.... you are right the error I got made me to think something else.
 
Lets show my configuration, start with LOGO
[203128%2F9251937%2Fmceclip0]
 
The LOGO is S7 Client connected to Raspberry Pi 5, here we need to map the local and remote connection, you could see below.
 
[203128%2F9251940%2Fmceclip0]
 
Here is the mapping to communicate to Raspberry Pi from LOGO.
 
[203128%2F9251944%2Fmceclip0]
 
And here we use %QX0.0 that is WiringPi 15 or (Physical pin 8)
Reply 0 0
thiagoralves
We already know that S7 works. I think it would be wiser if you focus on checking only if the Raspberry Pi 5 GPIO pins are working with the new WiringPi to make sure this Pull Request is good to go. Adding other variables to the mix, like S7 Protocol, Modbus or other things may obfuscate problems with the actual thing we want to check, which is the new WiringPi library.
Reply 0 0
fg71
It looks like I find a good guide how to make ssl-key for github...
I got it to update my fork....
 
What do I do now ??
Reply 0 0
thiagoralves
Could you send me the link for your fork please?
Reply 0 0
fg71
Reply 0 0
fg71
I have little issue with the current OpenPLC_v3 in Docker.
 
This error is happen when I click the Settings in left side of the UI.
This error ONLY happen in Docker running on RPi5, any clue what could be wrong ??
 
[2025-03-26 12:58:39,573] ERROR in app: Exception on /settings [GET]
Traceback (most recent call last):
  File "/workdir/.venv/lib/python3.11/site-packages/flask/app.py", line 2190, in wsgi_app
    response = self.full_dispatch_request()
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/workdir/.venv/lib/python3.11/site-packages/flask/app.py", line 1486, in full_dispatch_request
    rv = self.handle_user_exception(e)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/workdir/.venv/lib/python3.11/site-packages/flask/app.py", line 1484, in full_dispatch_request
    rv = self.dispatch_request()
         ^^^^^^^^^^^^^^^^^^^^^^^
  File "/workdir/.venv/lib/python3.11/site-packages/flask/app.py", line 1469, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/workdir/webserver/webserver.py", line 2224, in settings
    if (start_snap7 == 'false'):
        ^^^^^^^^^^^
UnboundLocalError: cannot access local variable 'start_snap7' where it is not associated with a value
192.168.0.177 - - [26/Mar/2025 12:58:39] "GET /settings HTTP/1.1" 500 -
Reply 0 0
thiagoralves
This seems to be related to the new addition of the snap7 library. Maybe it is missing snap7 files, or the database hasn't been updated to include the snap7 settings.
 
By the way, I got your PR, thanks. I left a comment there that I'm repeating here for referece: the background_installer.sh seems to be missing the correct wiringpi installation function. It seems that you're using the old one that was already there, which installs an old version of the library that doesn't support Pi5.
Reply 0 0
fg71
I checked my fork on Github and it include
WIRINGPI_VERSION="3.14" # Support RPi 1..5, CM5, CM5(L), Pi500, GCLK (Generic Clock) for RPi5 is not supported.
 
That is the 4th line of background_installer.sh
 
That is the latest WiringPi version, and it was the that version my system installed and it did work with RPi5.
My reference was the function from line 59 - 74
function install_wiringpi {
    echo "[WIRINGPI]"
    echo "Trying distribution package..."
    sudo apt-get install -y wiringpi && return 0

    echo "Falling back to direct download..."
    local FILE="wiringpi_${WIRINGPI_VERSION}_arm64.deb"
    local URL="https://github.com/WiringPi/WiringPi/releases/download/$WIRINGPI_VERSION/$FILE"
    (
        set -e
        wget -c -O "$OPENPLC_DIR/$FILE" "$URL"
        sudo dpkg -i "$OPENPLC_DIR/$FILE"
        sudo apt install -f
        rm -f "$OPENPLC_DIR/$FILE"
    ) || fail "Failed to install wiringpi."
}
 
Reply 0 0
fg71
Maybe it wouldn't be a good advice to have this line sudo apt-get install -y wiringpi && return 0 before checking at GitHub.
WiringPi do not upload to any REPO they release version on GitHub.
 
Thats why I said Raspberry Pi OS do not have WiringPi in the default REPO they use for package.
And that is the version I have been used.
 
So maybe its better to ONLY have a direct download instead of thinking a REPO has the latest version.
Reply 0 0
thiagoralves
Yes, that's what I was concerned. I haven't checked this function in a good while. I didn't even know if it was pointing to the correct repository. Perhaps the best approach would be to see if there is a way to just download the latest version instead of sticking to a version in a variable define. I think GitHub has a link with latest in it that will download whatever is the most recent. If there is such a thing, we should use that.
Reply 0 0
fg71
This seems to be related to the new addition of the snap7 library. Maybe it is missing snap7 files, or the database hasn't been updated to include the snap7 settings.

The reason I had problem with Docker was not the Dockerfile I though first. Its was volume conflict in Docker so you are right something with database was wrong. So I remove all volumes I had and create a new one that I call openplc_data, now it works 🙂

Reply 0 0
davenardella
This seems to be related to the new addition of the snap7 library. Maybe it is missing snap7 files, or the database hasn't been updated to include the snap7 settings.
 
By the way, I got your PR, thanks. I left a comment there that I'm repeating here for referece: the background_installer.sh seems to be missing the correct wiringpi installation function. It seems that you're using the old one that was already there, which installs an old version of the library that doesn't support Pi5.
As you know I'm not very expert in Python (in fact not at all 😂) but there's something that doesn't convince me.
"This error is happen when I click the Settings in left side of the UI."
 
openplc.db is "global", i.e. is the same for all the platforms,; both /thiagoralves and /gustaf71 contain the snap7 param
sqlite.png
 
the error reported at line 2224 (cannot access local variable 'start_snap7' where it is not associated with a value) it's very strange since at row 2198 there is:
 
elif (row[0] == "snap7"):
start_snap7 = str(row[1])
in fact it works fine for Win/Linux/Rpi5(no docker).
 
I don't think it could be due to some missing file, otherwise openplc (the runtime) would not have been successfully compiled.
Honestly it's beyond my understanding (probably because I'm not an expert in python)
 
 
@fg71
I modified also the checkTableSettings function, try to delete openplc.db, it should be rebuild
Reply 0 0
thiagoralves

The reason I had problem with Docker was not the Dockerfile I though first. Its was volume conflict in Docker so you are right something with database was wrong. So I remove all volumes I had and create a new one that I call openplc_data, now it works 🙂

So does that mean that you can revert the changes you made to the dockerfile? I don't use OpenPLC with docker at all, but I know that many people do. I'm worried that the changes could break things for them.
Reply 0 0
fg71
Your Dockerfile do not install WiringPi that means you only could use it with communication protocol and not with GPIO.
That is the reason I change it.
 
I also do not use your ./install docker
 
Inside OpenPLC_v3 I do
I use docker build -t <docker_hub_account_name>/<docker_image_name>:<tag> ./
 
People who have never used Docker before will have problem, because it always take time to learn it. (include myself)
In that regard its not 1 command and everything works.
The benefit to use Portainer for Docker it become easier. (I have included in this forum how to install Portainer).
 
Also the Dockerfile in my fork is also the same file I use to push OpenPLC_v3 to Docker Hub.
 
So I could confirm that Dockerfile do work for Raspberry Pi 5 with WiringPi 3.14
 
Tell me what you like me to do, I do it ?
Reply 0 0
thiagoralves
99% of OpenPLC users that use docker, don't use it on a Raspberry Pi. OpenPLC on docker is mainly used in servers and other containerized environments where you need some form of isolation between the OpenPLC runtime and the rest of the system. In that regard, WiringPi on docker is totally irrelevant. In fact, forcing the installation of WiringPi on the dockerfile will be problematic for the 99% of users that won't run it on a Raspberry Pi.
Reply 0 0
Reply