Raspberry PI GPIO Example

HI Friends,
I have created an example on Raspberry PI as follows

  1. Separate Thread to read Input pin (GPIO) of raspberry PI
  2. Update the mainWindow Indicator (Bevel Button)
  3. Keep Count of the inputs
  4. Account for only rising edge of the inputs
  5. Update each count into SQLITE
    I would like to upload the example and can somebody please guide me how to upload the project file ?
    thanks

you could use your webspace, the google drive, ms cloud one drive
and link it here.

Reading Input in a “Loop” can use much CPU.

Hi MarkusR
Thanks for your response
You said reading Input in a Loop can cause much CPU. Thanks for the tip. Can you please tell me what is the alternative. I need to continually monitor the inputs for changes. Is there any way like events attached to inputs which get raised ? I just observed that it occupies 100% of the cpu
Thanks once again

Hello Sass,

The proper way in the electronic-world is to use interrupts. Unfortunately, Xojo is not compatible with interrupts.

A few alternatives are:

  1. Program the interrupt in another language, and send the data to the Xojo application
  2. program the interrupt on an Arduino and share the data over another connection (LAN, etc).
1 Like

Hi Eugene. Thanks for the prompt response.
I will prefer option 1 : Program in another language and send the data to Xojo application.
But I am trying to see how this can be done. Google shows that one particular method is to use IPCSocket. Is that the only way.
I am trying to do some serious stuff.I am just trying to evaluate if Xojo fits the requirements.
Also, right now I am using Xojo for raspberry Pi which is with a free license. I am not n able to deploy the app on to Rasperry other than the Run Remotely option.
Not sure again if there is another way
Thanks

There is an example of the pigpio library in Xojo (not supported) in Example 19-1. The same pigpio library that is used with Xojo also is supported by Python and there are many references to example code

  1. pigpio callback - Raspberry Pi Forums
  2. pigpio library
  3. How to use interrupts with Python on the Raspberry Pi and RPi.GPIO – part 3 – RasPi.TV

… and more :slight_smile:

1 Like

Thanks
Will Check that

thats a Python .py script which start a shell script.
Python can also handle tcp connections with a few rows, means it can speak with xojo.
also useful at raspberry pi is remote gpio via tcp because you can run your gui app elsewhere.
Ashampoo_Snap_Freitag, 9. April 2021_10_6_18_001_

Ok. Now I have to start learning and working with Python. This whole stuff is getting interesting.
Now a few points which I hope MarkusR and / or Eugene Dakin will answer :slight_smile:
a) I am guided to pigpio the GitHub - eugenedakin/pigpio-GPIO: Code for Xojo apps to access the Raspberry Pi GPIO pins through the pigpio library says that wiringpi is deprecated. I used the GitHub - xojo/GPIO: A module for accessing the Raspberry Pi GPIO port using the Xojo programming language. for my demo. It is working. My Raspberry Pi 3b+ has the latest OS downloaded Version 5.10 release date March 4 2021. Does it mean both pigpio and wiring pi are maintained in the OS ?
b) Is there any possibility of using the interrupts in Xojo itself instead of the IPCSocket . It seems a bit too long drawn and i am not an python expert. I come from the windows world
I vaguely recollect MarkusR asking this same question earlier but not sure .
Thanks once again
Thanks

Hi Eugene
I “discovered” that you are the author of the book " I Wish I Knew How to… Program Raspberry Pi 4B Electronics with Xojo" . I have a Raspberry Pi 3b+. Will this book help in to write programs for that or I need to buy the Rpi 4B ?

Probably my dumbest question even after your earlier answer “The pigpio has a method called gpioSetISRFunc … So can this be used to raise the interrupts or should I have to go with the earlier suggested answer of 2 programs communicating over ISOSocket ?”
I am trying to develop an application for the industrial environment, if I have to use arduino then I will rather go with a small PLC (with High speed counter ) and use Modbus (if a library is available in Xojo) to connect Pi with the PLC …
Else give up the RPi approach all together
Thanks and sorry for so many questions …

1 Like

If you don’t want to get into interrupts, callbacks, IPC, Python and so forth, you can also just check the state of your inputs at intervals using a timer, depending on what amount of latency you can tolerate. Checking at say 100ms intervals should leave plenty of time for the processor to handle other tasks like the UI. Not sure how short the polling interval can be made before things get bogged down.

1 Like

Yeah. That is what I have already done. But the latency I have given is 20ms. This i have kept in a separate thread.
But, as MarkusR said, the cpu used is 97 to 100%. Of course, there is not really much work apart from monitoring the gpio. The status of the GPIO is shown on the front GUI.
I am not facing much issue as far as response is concerned. But I am still concerned. I am planning to set up the electronics side to run the RPi for a few hours and test if there is any issue. This will be my deciding factor else I will go back to the toradex board which I have been using earlier. Issue is , it is quite costly and gets costlier as I scale it match the RPi
Thanks for your response.

what kind of input do you need to read frequently?

Hi MarkusR
This is digital input equivalent to a PLC’s High Speed Counter input.
As an example scenario : counting the products moving on a conveyor at a speed for 20 products per second
That is why I have to have a very low latency . As suggested by Eugene, Interrupts are the best way, but I am not so strong in python to write a program which communicates with Xojo. And I somehow do not feel comfortable with the idea of 2 programs for a single task.
Thanks for your response

You said your program loops, which would explain 100% CPU usage. If it’s not looping, I would not expect a 20ms timer that reads some ports and updates some controls to use 97-100% CPU. Are you sure your program is not looping? It should just sit idle until the timer fires.

Not sure that a thread is giving you any benefit here; Xojo only uses one CPU core no matter how many threads you run, unless you use Worker apps.

Hi
First of all, I am sorry. I did not get things straight. Somewhere my replies were misleading. Yes , my progam has a loop in a separate thread. I also use a thread.sleep for 20ms.
Ok , thanks for pointing out, I will try the 20ms timer.
BTB, I am from the Desktop (C#) world. You rightly brought the single core usage of Xojo to my attention. Seeing the threads example I got carried away.
I will also look into Worker apps.
And my thanks to all of you. Honestly, i have worked with various other s/w and forums, but the response in Xojo forum tops the list.

i have a idea to run the Python script in a shell and read the output data in xojo Shell.
this Python could count perfect the signals often and output it each second as sum with print.
https://documentation.xojo.com/api/os/shell.html
the python interpreter need to start with the name of the .py script.
as example the interpreter path is hidden in remark at the first line of a .py script #! /usr/bin/python

Hello Sass,

Here are answers to some of your questions:

WiringPi is deprecated, which means that the library is not being updated. I do not know when the library would stop working.

Yes, both libraries are installed in the Raspberry Pi OS, and only pigpio is being updated.

Here is the link where it is mentioned that the WiringPiISR is not safe with Xojo (contribution with @MarkusR ): gpio event at level change - #2 by Eugene_Dakin

@Julia_Truchsess is a professional and this is likely your best option, if you are to use Xojo with the Raspberry Pi.

Eugene,
I really appreciate your effort in replying . I have been trying to get your book online , but the payment is failing consistently. I will raise the issue with my credit card company and find if it is any way related to international transaction being blocked.
I will try again today.
Thanks a lot.

And, I am trying Julia’s suggestion. I am trying to use the timer event in the MainWindow itself. That part is ok, I am just trying to get the pigpio library to work . OF course, I am scouring some of the help on the internet finding how to use the library, which is a bit difficult, in the absence of your book (my purchase attempt failed multiple times)