I am new to the micro-controller game and I ran into some issues trying to send data from one Pi Pico to another one using the SX1262 LoRa module by Waveshare.
Basically what I eventually want to do is make a sensor network and send all the data that I am going to gather from the sensors to a gateway and through there to a server for further development.
The first step is to send a simple string from one Raspberry Pi Pico to the other one, so I can understand the driver a little bit better and eventually make any modifications needed to create my project.
The driver that I found for the SX1262 is this one.
When I try to run the code on my Pico using the Thonny IDE, I get the following errors:
Traceback (most recent call last):
File "< stdin >", line 11, in <module>
File "sx1262.py", line 27, in begin
File "sx126x.py", line 115, in begin
File "sx126x.py", line 240, in reset
File "sx126x.py", line 389, in standby
File "sx126x.py", line 1270, in SPIwriteCommand
File "sx126x.py", line 1287, in SPItransfer
TypeError: object with buffer protocol required
Those errors happen both on the TX and TX (non blocking), which are in the examples folder.
Can someone help me with this and hopefully explain some things to me? Because as long as I get the driver running fine I can continue with my project.
TypeError: object with buffer protocol required means that you are trying to send something that's neither a string nor a byte array. Check your code, and convert your data to a proper type.
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.
My understanding is that circuit python will support bitbanging by default, but I might be mistaken...
For this example I am using a DHT11 temp and humidity sensor (I have a adafruit stemma/quic one on the way but shipping in Australia is awfully slow)
import board
import adafruit_dht
# Initialize the DHT11 sensor
dht = adafruit_dht.DHT11()
# Read the temperature and humidity
temperature = dht.temperature
humidity = dht.humidity
# Print the values
print("Temperature: {:.1f} C".format(temperature))
print("Humidity: {:.1f} %".format(humidity))
and the error I am being returned is:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "adafruit_dht.py", line 295, in __init__
File "adafruit_dht.py", line 82, in __init__
Exception: Bitbanging is not supported when using CircuitPython.
I have tried using the adafruit_bitbangio library, but I haven't been making much headway with that whatever docs I see are navigating me to either use this, or that is should just work without it.
Any help on this topic would be a huge help!
Thanks.
I am trying to drive a stepper motor with circuitpython 7.3.3 on a esp32 s2 with a A4988 stepper motor driver board. I have been fiddling for a while now. Since adafruit does not carry the A4988 boards there is no example code to start from on there pages, so I ended up trying to modify a piece of micropython code from TechToTinker.
https://techtotinker.blogspot.com/2021/05/036-micropython-technotes-stepper-motor.html%5B/url%5D
Unfortunately I can't seem to make the most basics of code to work and get the stepper to move.
import time
import board
from digitalio import DigitalInOut, Direction, Pull
dir_1 = DigitalInOut(board.IO1)
dir_1.direction = Direction.OUTPUT
step_1 = DigitalInOut(board.IO2)
step_1.direction = Direction.OUTPUT
def rotate(angle=0, rotation='cw'):
if rotation == 'cw':
dir_1.value(0)
for i in range(0, 200, 1):
step_1.value(1) # one step
time.sleep(0.5)
step_1.value(0)
time.sleep(0.5)
if rotation == 'ccw':
dir_1.value(1)
for i in range(200-1, -1, -1):
step_1.value(1)
time.sleep(0.5)
step_1.value(0)
time.sleep(0.5)
With the following lines of codes I should be able to test the stepper using the REPL:
rotate(360, 'cw')
rotate(360, 'ccw')
I checked the above code with mu and corrected all the found (style) error's, still when I run it I get the following error.
Traceback (most recent call last): File "<stdin>", line 1, in
<module> NameError: name 'rotate' is not defined.
A google search gave me an ansers like:
It looks like a variable named 'python' is used which is not present at the given scope during execution[/quote],
Unfortunately I can't do much with that, as far as I understand I am not doing that, so if I am, I have no idea how I am doing that, additionally I thought to have defined 'rotate'.
I tried the following to get some use out of the stepper:
I searched the adafruit site for relevant example code.
I searched the adafruit forum for similar issues but found only topics on arduino boards.
I searched the net for similar pieces of ciruit/micropython code to use with the A4988 board.
I finally tried to write, modify code myself, from relevant code from whichI tried to solve all errors.
I googled the final additional error but did not understand what I found.
I'd like to control a servo by a given angle.
I am using a RaspberryPi 4 Model B which is running Raspian.
The servos are connected to a Adafruit PCA9685 16-Channel Servo Driver.
The servo driver is connected to the RaspberryPi via i2c.
Python version 3.7.
I used the following tutorial: https://learn.adafruit.com/16-channel-pwm-servo-driver/python-circuitpython
I am able to properly control a LED (just like in the above tutorial) with the setup.
The LED is connected to the servo driver on channel 8, whereas the servo is connected at channel 2.
So here's my code (controlling the LED also included):
import board
import busio
import adafruit_pca9685
from adafruit_servokit import ServoKit
i2c = busio.I2C(board.SCL, board.SDA)
pca = adafruit_pca9685.PCA9685(i2c)
pca.frequency = 60
pca.channels[8].duty_cycle = 0x7FFF
kit = ServoKit(channels=16)
kit.servo[2].angle = 180
And here's the error code I get in return:
Traceback (most recent call last):
File "/home/pi/rover/Main.py", line 12, in <module>
kit.servo[2].angle = 180
File "/home/pi/.local/lib/python3.7/site-packages/adafruit_servokit.py", line 147, in __getitem__
servo = adafruit_motor.servo.Servo(self.kit._pca.channels[servo_channel])
File "/usr/local/lib/python3.7/dist-packages/adafruit_motor/servo.py", line 89, in __init__
super().__init__(pwm_out, min_pulse=min_pulse, max_pulse=max_pulse)
File "/usr/local/lib/python3.7/dist-packages/adafruit_motor/servo.py", line 29, in __init__
self.set_pulse_width_range(min_pulse, max_pulse)
File "/usr/local/lib/python3.7/dist-packages/adafruit_motor/servo.py", line 33, in set_pulse_width_range
self._min_duty = int((min_pulse * self._pwm_out.frequency) / 1000000 * 0xFFFF)
File "/home/pi/.local/lib/python3.7/site-packages/adafruit_pca9685.py", line 56, in frequency
return self._pca.frequency
File "/home/pi/.local/lib/python3.7/site-packages/adafruit_pca9685.py", line 134, in frequency
return self.reference_clock_speed / 4096 / self.prescale_reg
ZeroDivisionError: float division by zero
I solved the problem myself. Here's what I did:
I was confused by the difference between circuitpython and regular python.
As far as I understand this, circuitpython is a whole programming language with its environment. If that is even possible to install on the RaspberryPi, I am not sure. I checked on circuitpython's official website and it does not seem to be supported, check the download's page.
In case you have circuitpython installed, you can refer to this Github page: https://github.com/adafruit/Adafruit_CircuitPython_PCA9685
Anyway, what I am looking for is Adafruit's library for "regular" python. This can be pulled from here: https://github.com/adafruit/Adafruit_Python_PCA9685
Check out the readme.md for setup instructions.
With this, it worked for me. Controlling servos is now fairly easy.
I have a question regarding using the parameter_variation.py script provided on GitHub.
I'm using FMPy functions here (https://github.com/CATIA-Systems/FMPy) and have a specific error occurring only when I run a certain FMU, which is only slightly different from other FMU’s I’ve been using with a modified version of the parameter_variation.py example script provided.
Errors:
...
File "c:\parameter_variation.py", line 136, in simulate_fmu
fmu.terminate()
File "C:\AppData\Local\Continuum\anaconda3\lib\site-packages\fmpy\fmi2.py", line 231, in terminate
return self.fmi2Terminate(self.component)
File "C:\AppData\Local\Continuum\anaconda3\lib\site-packages\fmpy\fmi2.py", line 169, in w res = f(*args, **kwargs)
OSError: exception: access violation reading 0xFFFFFFFE1CD34660
End
I’m running 100 simulations for this FMU in 20 chunks, although the same FMU in the parameter_variation.py script appears to provide results if I run less than ~30 simulations in ~6 chunks.
Do you have any guesses why the access violation error may be occurring and how a solution can be forged? Let me know if this is enough information.
Thanks in advance.
In the title you mention multi-threading (multiple instances of the same FMU in the same process) which is not supported by many FMUs and can lead to unexpected side effects (e.g. through access to shared resources). If this is the case here you should be able to run your variation with a synchronized scheduler by setting the variable sync = True in parameter_variation.py (line 27).