Page 1 of 2
Read DI pin with timer
Posted: 20 Nov 2019, 15:56
by Ibra
Hello,
I just bought a RevPI Connect with a RevPi DI and DIO and I'm trying to interrogate a DI pin, for example every 100 ms or 10 seconds to accurately read the status of the pin.
You have a tutorial with timer and PIN code reader in python or something that could help me.
Thank you,
Re: Read DI pin with timer
Posted: 21 Nov 2019, 07:41
by dirk
Hi, have a look at this code snippet
Code: Select all
#!/usr/bin/env python3
# BitMirror.py
# Mirror DIO channel 1 input to DIO channel 1 output
import revpimodio2
import time
rpi = revpimodio2.RevPiModIO(autorefresh=True)
while True:
rpi.io.O_1.value = rpi.io.I_1.value
time.sleep(0.02)
Re: Read DI pin with timer
Posted: 21 Nov 2019, 11:26
by MartynSudworth
Make sure you have all green LEDs. If you have some red LEDs on your DI or DIO module, then your power is not wired correctly.
The example above, also assumed that Pictory has setup the tags like in the example - on my module it chose different tags. This will generate exceptions in the python code.
Also make sure you call the python routine using python3 (ie "python3 filename.py" and not "python filename.py" the latter example, on my revpi, chooses python2).
Re: Read DI pin with timer
Posted: 21 Nov 2019, 14:27
by Ibra
Hello thanks for your replay, but it's I do timer. sleep is the processor going to be really stuck or not ?
And i have anthor question, Is there a way to use an input to detect rising edge and falling edge at the same time ?
thanks,
Re: Read DI pin with timer
Posted: 26 Nov 2019, 06:42
by dirk
Hi thank you MartynSudworth for the helpful comments. It is also good to know that there are by default cycle times applied by the RevPiModIO library which you may modify via '.cycletime',
see here. The time sleep command is useful so that you do not peek your CPUs. The cycle time of the PiBridge is > 2ms so without the sleep you would simply waste CPU power. There is unfortunately just counter rising edge or counter falling edge possible. But you could use also direct value with debouncing. I.e. InputMode => Direct, InputDebounce => 3 m -
Have a look here.
Re: Read DI pin with timer
Posted: 05 Dec 2019, 13:42
by michris
Hi Dirk,
I have more or less the same problem.
For one or other reason I can not communicate to the DIO module in python.
I get the following error when try to read or write to the IO
Installed the following :
sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get update
sudo apt-get install python3-revpimodio2
sudo apt-get dist-upgrade
sudo apt-get install revpipyload
sudo pip3 install revpimodio2
sudo apt-get install mosquitto-clients
Python code :
import revpimodio2
revpi = revpimodio2.RevPiModIO(autorefresh=True)
revpi.io.O_1.value=True
Error :
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3/dist-packages/revpimodio2/io.py", line 79, in __getattr__
raise AttributeError("can not find io '{0}'".format(key))
AttributeError: can not find io 'O_1'
Any idea what can cause this ?
Many thanks in advance
Regards,
Christophe
Re: Read DI pin with timer
Posted: 05 Dec 2019, 16:27
by dirk
Hi Christophe, start the script with python3 and verify the PiCtory configuration. What is the output of "piTest -d" and "python --version". You have to run your script using "python3 myscript.py" instead of "python myscript.py". Maybe
this page helps with python/python3. Have a look at the page
How to Configure RevPi DIO by Using PiCtory.
Re: Read DI pin with timer
Posted: 13 Dec 2019, 11:57
by michris
Hi Dirk,
Thanks for your reply
The output of piTest -d displayes the following ...
pi@RevPi20977:~ $ piTest -d
Found 2 devices:
Address: 0 module type: 95 (0x5f) RevPi Core V1.2
Module is present
input offset: 0 length: 6
output offset: 6 length: 5
Address: 32 module type: 96 (0x60) RevPi DIO V1.4
Module is present, but NOT CONFIGURED!!!
input offset: 11 length: 70
output offset: 81 length: 18
Aparently the DIO is not configured ?
I added it on the PiCtory interface ... but how do I make it effective ? How can you save it ?
Thanks
Regards,
Christophe
Re: Read DI pin with timer
Posted: 16 Dec 2019, 08:46
by dirk
Hi Christophe, yes this is the right way. In case "piTest -d shows" devices "present but not configured".
Go to PiCtory
- Change the configuration
- Save the configuration via "File -> Save as startup configuration"
- Reset the driver via "Tools -> Reset driver"
You can execute "piTest -x" alternatively to the last step.
Then "piTest -d" should not display the "present but not configured" warning any more.
Or repeat until your hardware configuration matches...
Re: Read DI pin with timer
Posted: 16 Dec 2019, 16:59
by michris
It works now.
Thanks for your support!
Regards,
Christophe