Read data in Raspberry Pi from an SPI connection with spidev library in python - python-3.x

I want to transfer data from an external device to my Raspberry Pi 3 model B. I'm using SPI protocool to do this task. I'm using spidev library on python. I have already created the next program:
#/usr/bin/env python3
import spidev
import time
spi = spidev.SpiDev(0,0)
spi.max_speed_hz= 122000
while True:
print(spi.readbytes(1))
time.sleep(2)
When I run the program I get always the same result: ([0]).
I don't know if this is the correct method to read data on spidev. I'm new with spidev library and I would be so grateful to hear any suggestions of how to do this task.

Related

Pyftdi issue with multiprocessing

I'm currently using a FT232H Breakout board (USB GPIO) from Adafruit that I am controlling with Pyftdi in Windows, using the libusbK 3.0.7 driver installed through ZADIG. It's working just fine in every aspect, but for this particular project I need to use the multiprocessing module.
However, I can't get Pyftdi to work with it. To replicate my issue, you can just run this bit of code.
import multiprocessing as mp
import board
def func():
print('This will crash')
p1 = mp.Process(target=func)
p1.start()
p1.join()
p1.terminate
From what I can gather, the problem is that when instanciating a new process, Python will once again import the board module, required to run the FT232H, and will attempt to claim it's USB interface which is already claimed, throwing this error:
pyftdi.ftdi.FtdiError: UsbError: [Errno None] b'libusb0-dll:err [claim_interface] could not claim interface 0, win error: Cannot create a file when that file already exists.
However, if I write around this so that the board module is not imported a second time for the new process, any FT232H commands ran in the new process will not work.
Anyone has any ideas on how I can tackle this somehow?

Sending Serial Data to Arduino Using Python Script

I am facing a peculiar issue while writing characters to Arduino using python serial communication on macOS 10.14.
The Arduino is programmed to read a string, parse it and take PWM action to run a car.
Ardiuno's serial communication channel is configured to receive the strings in the format < A, B, C, D > where ABCD are numbers which denote car direction, speed, steering direction and steering position.
The problem is, when I send a string from the serial monitor or through the Python Development environment the string is received, parsed properly and command executed successfully.
However if I write a simple program in a file write.py and execute it from the command line, nothing happens.
import serial
ser = serial.Serial('/dev/cu.usbmodem14301', 9600)
data = '<1,150,0,0>'
ser.write(data.encode())
If I run this script from the macOS terminal using the command:
python write.py
nothing happens. What am I missing here?
A new USB connection with ser=serial.Serial('/dev/cu.usbmodem14301',9600) resets the Arduino. The data sent right after connection are lost because the Arduino boots.
It may be that the port is in text mode and will not send the data until a newline is sent:
data = '<1,150,0,0>\n'
ser.write(data.encode())
or flush() is called.
data = '<1,150,0,0>'
ser.write(data.encode())
ser.flush()
The most probably thing happening here is that the data is not being sent to the serial port.
There is a simple method to check this.
Connect the Arduino to your laptop (I suspect it to be a mac), and start the serial monitor on the Arduino IDE.
In the serial monitor type in <1,150,0,0> and press send.
The tx LED on the Arduino will blink. Now that you know how the pattern looks, repeat the same experiment with the Python code.
If the LED does not blink in the same manner you have a Serial port access issue, which you can fix using the instructions in the following link
Access USB serial ports using Python and pyserial
If not, I am stumped.

rfid-rc522 Tag is not read

I am using the instructions of https://pimylifeup.com/raspberry-pi-rfid-rc522/ to learn how to read my rfid-rc522.
I installed all the things needed, and cloned from git all the files. I connected accordingly and double checked the wire.
The code in Write.py is
#!/usr/bin/env python
import RPi.GPIO as GPIO
import SimpleMFRC522
reader = SimpleMFRC522.SimpleMFRC522()
try:
text = raw_input('New data:')
print("Now place your tag to write")
reader.write(text)
print("Written")
finally:
GPIO.cleanup()
When I run it- The result stops after "Now place your tag to write" and nothing happens when I place my tag. Any help please? What's wrong here? The module gives red light meaning it is connected. Is it really connected? How do I know? Please help.
Edit: My title and tags were wrong in this question due to already saved data. I edited. :)
It can be multiple reasons why your reader is not retrieving any data:
The RFID cards that you are providing to the reader are not supported by the ISO 14443: Mifare Classic, 4K. (Some DesFire, etc...) are the cards you want to try. Source: https://en.wikipedia.org/wiki/ISO/IEC_14443
The MFRC522 is not receiving enough input voltage. Try to increase from 3.3V to 4V. I have tried with 5V months and the reader is still running perfectly, but be cautious. The safe range in NXP is from 2.5V to 3.6V. Source: https://www.nxp.com/docs/en/data-sheet/MFRC522.pdf
The library you are using does not support IRQ (Interruption Request). This means the process of reading UIDs is high CPU consuming and low in performance. Try to use this popular library which supports interruptions: https://github.com/ondryaso/pi-rc522 . You will need to connect another wire from the IRQ pin in the MFRC522 to one GPIO pin in the Raspberry that is free and supports reading/writing operations.
Check if SPI interfaces are enable in Raspbian. Open a terminal and run:
ls -l /dev/spi
Lastly, it could be that your reader is broken. Some chinese versions do not work as they should do. Maybe you should buy another one and try more luck.
I suppose you have connected all cables in the correct way between the MFRC522 and the Raspberry Pi. Check that again.

How to use a MCP23017 with MCP3008 for I2C voltage sensor with Raspberry Pi?

I would like to know if is possible to use an MCP23017 16 bit I/O expander with a MCP3008 ADC and read the voltage with a Raspberry Pi 2. I want to use the ADC as an I2C device. I would like to do this so I don't have to run the program a 'root', so I'm thinking that running the ADC as an I2C device will fix this problem. I'm looking for help with how to wire the system as well as programming it. I'm using the Python 3 editor. The existing program I have will be used to plot a sine wave generated by a AD9850 DDS module who's signal is amplified and fed into a device. I want to measure this voltage. I know how to use a voltage divider, but am having trouble coming up with a way to read it. The measured voltage value needs to be stored as global variable that can be passed around the program. Right now I'm mainly concerned with not running the program as a root, turning the ADC into an i2c device, and storing the voltage as a global variable to be passed around in an existing program.
I have not worked with any kind of I2C TO SPI converter. Still, you can use some I2C to SPI bridges if they work, I just googled it, but that can cause wiring problems.
I can suggest you the same ADC MCP series with I2C interface.Thus, the further I2C connections with MCP23017 expander and then the Raspberry pi would be easy.You can go through various analog to digital converters that can be I2C interfaced with their codes in python or java for pi like MCP3425, MCP3426, MCP3427, MCP3428. You can easily find them or also check control everything as that would be easy to interface using I2C cables and adapters preventing connection or wiring problems.For codes: https://github.com/ControlEverythingCommunity?utf8=%E2%9C%93&query=MCP34
The following codes for MCP_23017 can also help you code the way you want easily with expander being connected to pi:https://github.com/ControlEverythingCommunity/MCP23017_16-Channel.
I think this would solve your problem!!
Thanks.

How to poll for GPIO interrupts in python?

I need to poll for interrupts on a GPIO pin using python 2.7, and I wonder if there are any native functions or libraries to help me do that?
When searching for anything like this I find a lot about Raspberry Pi, but that is not what I need.
Is this doable?
It will be greate if you post what you already tried so we can help.
I did it like this
while 1:
events = po.poll(30000)
if not events:
print ('TIMEOUT!')
for fd, flag in events:
# check what's been done with os.lseek and os.read
This code is based on How do I wait for interrupts in different languages? question.
Whole code was done as a simple game for emebeded system on Raspberry PI and is available here

Resources