I have managed to receive data from a bluetooth connected device by implementing exactly this solution:
https://scribles.net/setting-up-bluetooth-serial-port-profile-on-raspberry-pi-using-d-bus-api/
This works fine using the minicom, but I need to manipulate the data which arrives in a python program. How on earth do I go about getting the data into a python program instead of receiving it in the minicom???
Also: as I am an absolute beginner with python, bluetooth and github this is the only solution that I was able to get working. I have tried several approaches over the last couple of weeks and I am sure that there are many other which are better and easier and I would appreciate any suggestions of better/easier solutions.
Python 3.7
RPi3 (1.3)
Connection status in bluetoothclt->info:
Device 00:07:80:E0:A4:FC (public)
Name: WT11i-A
Alias: WT11i-A
Class: 0x00001f00
Paired: yes
Trusted: yes
Blocked: no
Connected: yes
LegacyPairing: no
UUID: Serial Port (00001101-0000-1000-8000-00805f9b34fb)
UUID: PnP Information (00001200-0000-1000-8000-00805f9b34fb)
Modalias: bluetooth:v0047pF000d0400
sudo rfcomm -a show hci0:
rfcomm0: B8:27:EB:A6:D7:4F -> 00:07:80:E0:A4:FC channel 1 connected [reuse-dlc release-on-hup tty-attached]
With the amazing assistance of ukBaz I finally got it working.
The trick is: ditch RFCOMM ... not much of a surprise apparently.
I disconnected the RFCOMM connection and did not connect using the 'Blueman manager'. By instead just running the Bluedot default client script it worked!
Related
hello I work with an Rpi 4 and an Rpi Zero W and I want to activate the internet connection by a communication network using SIM7020E. I manage to establish the contact by checking the basic commands on Minicom but the connection is not activated. I must provide at the end transfer data from my Rpi by Email but without Wifi or Ethernet Cable.
i program with python
please can someone help me?
I would recommend checking out the 1NCE Developer Hub.
In the recipes section are examples of using the SIM7000G, which works very much in the same way and can be compared. You find all the recipes here: https://help.1nce.com/dev-hub/recipes and look for SIM7000G.
The main things you need to do is to configure the SIM7020E via the serial interface and send the correct commands from your python application.
Do the overall network registration of the module as shown here: https://help.1nce.com/dev-hub/recipes/sim7000g-network-registration
From there, you can already start sending data. Unfortunately, there is not a direct example for sending mail but as I expect you are using some mail delivery service that also has an API, you can follow the following guide for that: https://help.1nce.com/dev-hub/recipes/sim7000g-http-post
It will show you what commands are required for an HTTP POST call.
I have found the correct Bluetooth address of the device I want to connect to. When I run the code below, it prints "Connecting to device..." but then hangs and never prints "Connected" or finishes running. No errors are thrown.
import asyncio
from bleak import BleakClient
address = "24:71:89:cc:09:05" # Replaced with actual bluetooth address
async def main(address):
print("Connecting to device...")
async with BleakClient(address) as client:
print("Connected")
asyncio.run(main(address))
Is this issue related to my PC software/drivers? I am on a Lenovo laptop running Windows 11.
Or, do some devices not respond to simple "connection requests"? The device I am connecting to is a Tesla Model 3, and I know there is a particular set of data I should be sending to authenticate. If this is the case, how do I send data without connecting with a BleakClient in this way?
Update: I should mention that scanning / discovering devices works just fine via Bleak. And, I tried connecting to other devices via Bleak and the same issue occurred. Bluetooth through the Windows Settings app works fine though.
I was also having issues using any bleak commands beyond discover(). I downgraded from 0.14.3 to 0.14.0 and that resolved my problem.
When I looked up "python tesla bluetooth api" I found my way to this site, which documents the API which seems great for what you are doing. That lets you do lots of things like unlocking, opening trunk, etc.
It sounds like the main problem is that you can't connect at all in order to do stuff. The way that site is written glosses over that part, just saying "send it over".
I would ensure that you have the right address, you should use this scanning code from the Bleak doc. Also, make sure that you can use bluetooth normally, like by connecting to a wireless speaker.
Sadly, this error is still unsolved in Bleak, in the project they claim: still waiting for someone to capture bluetooth packets.
Trying to connect, you get either an endless hang with no response or if the device is paired upfront:
Connection error: [WinError -2147483629] The object has been closed
It seems the Library isn't very mature for windows:
https://github.com/hbldh/bleak/issues?q=label%3A%22Backend%3A+WinRT%22+
I'm trying to connect my Arduino Uno to my computer and writing code in python using PyVisa. I have installed PyVisa correctly, since it has worked with other devices. For some reason the Arduino Uno never returns a response. I'm using the NI Visa package, because I was hoping the pyvisa-py was the problem. Is there something I'm missing?
The code I am using to connect is:
import pyvisa_py as pv
port = "ASRL4::INSTR"
rm = pv.ResourceManager()
device = rm.open_resource(port)
The ResourceManager finds the correct port, and I don't get an error making the device. When I try to use a query however, like
print(device.query("*IBN?"))
It gives the following error:
pyvisa.errors.VisaIOError: VI_ERROR_TMO (-1073807339): Timeout expired before operation completed.
I haven't tried to use an Arduino with PyVisa in a long time. I always use the PySerial library instead. Here is a personal note I wrote about four years ago (in 2017) for a Python module interfacing with an Arduino.
This driver does not use the VISA layer to communicate with the device. Instead, it uses the more low-level and less general PySerial library. The reason for that is that the Arduino reboots ("resets itself") whenever the serial port is opened through VISA. This is a feature, not a bug, so that you don't have to manually reset it every time you flash a new firmware version from the Arduino IDE. As a consequence, though, the controller will not respond to requests for about two seconds after opening its serial port. The only way to avoid the reboot is making sure that the serial port's DTR line is not toggled when opening the resource. VISA, however, does so by default, and there seems to be no way to disable this disruptive behavior. In fact, VISA attributes cannot be set until after the resource has been opened. The PySerial library, on the other hand, does not have this limitation.
As the note is four years old, take it with a grain of salt. Things may have changed. But using PySerial instead of PyVisa is still your best bet. See my answer here for a few more details. And maybe do a web search with the keywords "Arduino" and "DTR" to see if there have been any recent developments.
I am using a telit he910g card. it is connected to my PC directly using a miniPCI slot.
I am using it for 3G internet connection and A-GPS/GPS services.
My system is running linux mint 17.1, the 3G connection is handled using the network manager APP and works great. The 3G connection is started and handled using a module that is part of a program I am writing.
The code I am using in order to connect to the serial port is this:
def _connect_to_device(self):
""" Connect to a serial port """
try:
self._device = serial.Serial(self._filename, baudrate=self._baud_rate)
except StandardError, e:
raise StandardError("Couldn't connect to GPS device. Error: %s" % str(e))
When I use the python program alone it works great. But when I try and use it while the 3G is on i cant connect to the serial device. The wierd thing is that if I try to connect to it using a program like "minicom" while 3G is turned on it DOES work.
So my question is: how can I make both run and work together? since now they are mutually exclusive.
thanks to all who help. :)
Glad you found a way round your problem. Just for completeness:
Normally, serial ports can be opened by multiple processes.
If one of them does ioctl(,TIOCEXCL) on the open file then further opens will return EBUSY until everyone closes the device. Only root can get past this and open the device at all times.
If root opens the device and does an ioctl(,TIOCNXCL), then other processes can open the device too.
In python, TIOCNXCL isnt defined anywhere, but you can do the ioctl (eg on stdin) with:
import fcntl
TIOCEXCL = 0x540c # from /usr/lib64/perl5/asm-generic/ioctls.ph
TIOCNXCL = 0x540d
print fcntl.ioctl(0, TIOCNXCL)
Ok, so it is solved.
the issue was that the telit module has 2 ports /dev/ttyACM0 (high speed) and /dev/ttyACM3 (lower speed).
I tried to connect to the high speed one, but apparently the 3G uses that one and it causes contentions.
So moving to use the lower speed port in my script solved the issue.
I'm using twisted.internet.serialport to have my program be continuously connected to a device on a serial port.
Unfortunately my serial port is just a usb device, which means it can be disconnected or reset by the OS at any time (port 2 disabled by hub (EMI?), re-enabling... ). I see that pyserial has support for this for a few weeks and raises a SerialException.
What I would love to do is try to reconnect to the serial port that just disappeared every few seconds.
So, is there any way how I can tell twisted to notify me about a disconnect? Or should I go ahead and write a threaded wrapper for pyserial?
Thanks
It seems the only relevant change in branched version is a call to connectionLost() in the protocol.
Until it's fixed in the trunk I use a:
class fixedSerialPort(SerialPort):
def connectionLost(self, reason):
SerialPort.connectionLost(self, reason)
self.protocol.connectionLost(reason)
I tested it with Twisted 10.1 (on ubuntu) and 8.1 (on my trusty debian). Both works fine. No idea about other OSs though.
http://twistedmatrix.com/trac/ticket/3690 may be related.
The ticket appears blocked on proper Windows support. I'm not sure if this kind of disconnect event will trigger Twisted's internal connection lost detection code, but I would expect it to (even without a recent version of pyserial).
You could probably try out the branch linked from that ticket pretty easily to see if it does what you want, at least. And if so, perhaps you could help get the ticket actually resolved (the 10.2 release is coming up pretty soon).