Reading Serial Data Using a Raspberry Pi Pico - micropython

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.

Related

How to use read data from computer using a pi pico running micropython

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.

Connecting serial port via MATLAB App Designer

I want to connect my Arduino to App Designer by using the "drop down" list. This is what my app looks like
First, I am looking for if there is any serial com. system. And I am writing them to Drop Down.
p = instrhwinfo('serial');
app.SerialPortsDropDown.Items = p.AvailableSerialPorts;
After this I have planned to read the serial port that is shown in the Drop Down and write it to serialport()
app.a = serialport(app.SerialPortsDropDown.value,9600);
Unfortunately these lines did not work. The error message I got:
Error using serialport (line 116)
Unable to connect to the serialport device at port 'COM9'. Verify that
a device is connected to the port, the port is not in use, and all
serialport input arguments and parameter values are supported by the
device.
So, the first two lines of code work. I am able to see COM9 (the com my arduino connected) in the drop-down list. This shows there is a serial port at COM9. But when it comes to reading it with app.a = serialport(app.SerialPortsDropDown.value,9600); it gives error.
How can I connect a serial port via the MATLAB App-designer?
app.a = serialport(app.SerialPortsDropDown.value,9600);
This is a wrong way of connecting Arduino to MATLAB. This declaration does not let us use Arduino functions such as 'writeDigitalPin, writePWMDutyCycle'.,
As I mentioned in the comments, it is still important to clear the port first and connect the serial port.
Lastly, the true way to declare Arduino to be able to use its functions as in the following:
app.a = arduino(app.SerialPortsDropDown.value, 'Tag of your arduino card');

Xojo detect input from 2 inputs with interrupts. (Raspberry Pi)

I have a raspberry pi 3 running a program made in Xojo.
My goal is to have two flow sensors which display the amount of water that flows though each sensor on the screen.
I have a program that works for one flow sensor, it uses the GPIO library and a custom module called 'InterruptModule'. I followed a tutorial to make this program.
Tutorial: https://einhugur.com/blog/index.php/xojo-gpio/connecting-button-with-gpio-and-using-interupts/#comment-14
This program works successfully for both flow sensors, but only one at a time. I.e if I change the input pin and run the program again it works.
HOWEVER, when I try combine the two it responds can't differentiate between the two inputs.
I have tried with two GPIO modules and two custom 'InterruptModule' modules but it still counts the inputs under whichever sensor is defined first.
See my attempt here.
Screenshot of Xojo code
One way to differentiate between the two interrupts is to create two separate callback method.
Example:
Const kPin = 14
If GPIO.WiringPiISR(kPin, GPIO.EDGE_RISING, Addressof InteruptModule.ButtonDownInterupt1) = -1 then
MsgBox "Could not register for Interupt1 on kPin14"
End If
Const kPin = 18
If GPIO.WiringPiISR(kPin, GPIO.EDGE_RISING, Addressof InteruptModule.ButtonDownInterupt2) = -1 then
MsgBox "Could not register for Interupt2 on kPin18"
End If
In this example, each pin interruption would have its own callback method with different code to work with each pin.

I2C: Raspberry Pi (Master) read Arduino (Slave)

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.

Bluetooth Low Energy: Reading Fitbit Zip Data from Raspberry Pi

I have a Fitbit Zip and I want to extract its data from Raspberry Pi using gatttool.
I can connect to it, figure out RX, TX handle etc. I understand I need to enable listen to the CCC handle like this
char-write-req 0x000c 0100 --listen
But before that, I need to enable reading the sensor (or series of action) through the TX handle, something like below.
char-write-cmd 0x000b
I am not sure how to figure out that or the series of action that needs to be done before I can listen. Can anyone please help?