i need help with the basics, im new to raspberry pi's and rfid, i want to create a program or script that can read all the datablocks of a card and dump it to a text file. as well as a program/script that will allow me to write datablocks to the card
import nxppy
import time
mifare = nxppy.Mifare()
try:
while True:
try:
uid= mifare.select()
print(uid)
except nxppy.SelectError:
pass
time.sleep(0.2)
except KeyboardInterrupt:
pass
Related
I have a Spektrum Radio transmitter, along with its receiver. What I am currently trying to do is by using microPython and a Raspberry Pi Pico, I want to read the data coming into the receiver, and convert that into servo commands. All I know is that the protocol used by the transmitter/receiver is DSMX. How can I go about doing this? I only need to receive, I don't need to transfer any data back from the Raspberry Pi Pico.
I'm using Thonny, and all I've done is try to use the UART module and ustruct module and create a variable using that
uart = UART(1, baudrate = 115200)
data = uart.read()
header,id,data,error_checking,trailer = ustruct.unpack('>BBHHB',data)
When trying to run this, I get thrown the error
TypeError: object with buffer protocol required
I didn't expect anything, as I don't really know what I'm doing.
Any help would be really appreciated.
You're getting that TypeError exception because your call to uart.read() is returning None (meaning that there was no data available on the serial port). I don't know anything about the DSMX protocol, but to avoid that error in your code you probably want something like:
format = 'BBHHB'
required_size = ustruct.calcsize(format)
if uart.any() >= required_size:
data = uart.read(required_size)
header,id,data,error_checking,trailer = ustruct.unpack(f'>{format}',data)
...and the above probably needs to live in some sort of loop.
First time using Stack overflow so I'll do my best to explain.
TLDR - HOW DO I MAKE A PI PICO W CONNECT TO A NETWORK WITH WPS?
I'm making a thing, that basically checks whether the value of a particular stock has gone up or down and lights up green or red LED's based on whether the price is up or down. That's all well and good, but to do this it needs to connect to wifi, and being that the thing I'm making has no screen, or keyboard, all we have to accomplish this is the LED's (for the device to communicate with us) and whatever button I put on there.
My plan was to have the device look at a file called "your_wifi_details.txt" for the SSID and pass, and if it finds them, connect - if nothing is there, or if it cannot connect using those details, it is to enter "WPS mode" where the lights will flash, and the user presses the WPS button on their router then presses a button on the device.
Everything is wired up and working perfectly aside from I cannot work out how to get WPS to work on a Pi Pico W. I think I've gotten really close, but i'm getting the error "'CYW43' object has no attribute 'scan_wps'"
(The lines of code are:
# If the button is pressed, scan for WiFi networks that support WPS
if not button.value():
sta_if.scan_wps()
# Connect to the first WiFi network that supports WPS
sta_if.connect_wps()
)
Then it goes on to connect to the API's and such to get the stock price and from there everything is good.
Literally everything works, if I manually go into the "your_wifi_details.txt" and put in a correct SSID and password, we're golden, but I can't have users required to download thonny and connect to the device and edit files and yada yada... so I am really hoping this WPS thing can work.
This is the first 'proper' program i've written since starting to learn python so apologies if this is a stupid approach, it's taken a lot or time and confusion to get this far but it's so nearly working, I just need a little help over the finish line so if anyone has faced this problem or can help me toward a solution I'd be super grateful
Here's excerpts from the relevant bits of code that are connected to the problem. There's a lot of "print" debugging commands so I can see in the terminal in Thonny where the program is at and what it's doing, obviously in the final use case there won't be a screen or keyboard so all that stuff will be taken out once development is complete:
import urequests
import network
import time
import machine
#--------------(Stackoverflow comment) then further down
#stackoverflow comment: I don't know why this next line is in bold, it shouldn't be
# Set up the button (connected to pin 15)
button = machine.Pin(15, machine.Pin.IN, machine.Pin.PULL_UP)
print ('pin 15 set as button')
#--------------(Stackoverflow comment) then further down still
while True:
print ('enter while true loop')
# Connect to the WiFi network
sta_if = network.WLAN(network.STA_IF)
#print ('firstline passed, sleeping for 10 seconds')
#ignore this, unnecessary now
#for i in range (0, 20):
# time.sleep(0.55)
# print ('sleeping...')
#print ('nap over, trying to continue..')
sta_if.active(True)
print ('firing up connection')
# Read the WiFi SSID and password from the text file
with open("your_wifi_details.txt", "r") as f:
ssid = f.readline().strip()
password = f.readline().strip()
print ('read your_wifi_details.txt successfully')
# Try to connect to the WiFi network
sta_if.connect(ssid, password)
print ('attempting to connect to wifi')
# Wait until the device is connected to the WiFi network
while not sta_if.isconnected():
# Flash the LEDs to indicate that the device is unable to connect
print ('connection failed, attempting to flash LEDs')
green_led.on()
red_led.on()
time.sleep(0.5)
green_led.off()
red_led.off()
time.sleep(0.5)
# Set up the button (connected to pin 0)
print ('setting up button for WPS')
button = machine.Pin(15, machine.Pin.IN, machine.Pin.PULL_UP)
# If the button is pressed, scan for WiFi networks that support WPS
if not button.value():
sta_if.scan_wps()
# Connect to the first WiFi network that supports WPS
sta_if.connect_wps()
# Save the WiFi SSID and password to the "your_wifi_details.txt" file
with open("your_wifi_details.txt", "w") as f:
f.write(sta_if.config("essid") + "\n")
f.write(sta_if.config("password") + "\n")
print ('wifi details written to file')
# Print the WiFi connection status in the terminal so I can see if it worked
print("Connected to WiFi:", sta_if.isconnected())
I have looked everywhere on the internet, sadly i couldn't find anything. Is there anyway i could read data from my computer to the raspberry pi pico running micropython? Any help would be appreciated. I use pyserial to send and receive data on my computer.
I also had this issue and from this link I was able to figure it out. To read data from the usb port use "sys.stdin.buffer.read(8)".
Here is a short code that you can upload to the pico to do this.
import time
import sys
while True:
print("hello")
data = sys.stdin.buffer.read(8)
# to convert to string use data = sys.stdin.buffer.read(8).decode("utf-8")
print(data)
time.sleep(1)
With this code it will wait until 8 bytes come in, until then it will idle. I'm sure there is a way around it, just haven't tried yet.
I used the Arduino IDE serial monitor to send data. Upload this code in Thonny (save it as main.py so it starts on power up), then disconnect the pico, plug it back in, and open the Arduino serial monitor. Send something like '12345678' and it should spit it back out.
EDIT:
The above code will idle until you send it data. To have the pico continue running if no data is sent, use the following (source):
import time
import sys
import select
while True:
print("hello")
res = select.select([sys.stdin], [], [], 0)
data = ""
while res[0]:
data = data + sys.stdin.read(1)
res = select.select([sys.stdin], [], [], 0)
print(data, end = "")
time.sleep(1)
And you can just use Thonny to send the data, you don't need the Arduino IDE.
I'd like some help to debug the example code provided by adafruit. I have connected a potentiometer and an analog servo motor which when using the python2 script give me values just fine. I took from this site: https://learn.adafruit.com/raspberry-pi-analog-to-digital-converters/mcp3008
Since I have to upgrade to python3 I looked into new tutorials here: https://learn.adafruit.com/mcp3008-spi-adc/python-circuitpython
The problem I have is no matter what pin value I give in the board I always get 0 values. The pin I use for mcp3008 selector is BCM 22:
I've also provided an image of the connection here:
mcp3008 potentiometer raspberry pi zero w
import busio
import digitalio
import board
import adafruit_mcp3xxx.mcp3008 as MCP
from adafruit_mcp3xxx.analog_in import AnalogIn
spi = busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI)
cs = digitalio.DigitalInOut(board.D5)
mcp = MCP.MCP3008(spi, cs)
What am I doing wrong? Thanks a lot for your time.
It turns out the raspberry pi required restart which surprised me as I suspect it's because I installed the adafruit packages.
I would like to read a block of data from my Arduino Mega (and also from an Arduino Micro in another project) with my Raspberry Pi via I2C. The code has to be in Perl because it's sort of a plug-in for my Home-Automation-Server.
I'm using the Device::SMBus interface and the connection works, I'm able to write and read single Bytes. I can even use writeBlockData with register address 0x00. I randomly discovererd that this address works.
But when I want to readBlockData, no register-address seems to work.
Does anyone know the correct register-address, or is that not even the problem that causes errors?
Thanks in advance
First off, which register(s) are you wanting to read? Here's an example using my RPi::I2C software (it should be exceptionally similar with the distribution you're using), along with a sketch that has a bunch of pseudo-registers configured for reading/writing.
First, the Perl code. It reads two bytes (the output of an analogRead() of pin A0 which is set up as register 80), then bit-shifts the two bytes into a 16-bit integer to get the full 0-1023 value of the pin:
use warnings;
use strict;
use RPi::I2C;
my $arduino_addr = 0x04;
my $arduino = RPi::I2C->new($arduino_addr);
my #bytes = $arduino->read_block(2, 80);
my $a0_value = ($bytes[0] << 8) | $bytes[1];
print "$a0_value\n";
Here's a full-blown Arduino sketch you can review that sets up a half dozen or so pseudo-registers, and when each register is specified, the Arduino writes or reads the appropriate data. If no register is specified, it operates on 0x00 register.
The I2C on the Arduino always does an onReceive() call before it does the onRequest() (when using Wire), so I set up a global variable reg to hold the register value, which I populate in the onReceive() interrupt, which is then used in the onRequest() call to send you the data at the pseudo-register you've specified.
The sketch itself doesn't really do anything useful, I just presented it as an example. It's actually part of my automated unit test platform for my RPi::WiringPi distribution.