Bleak (python) does not respond on connect - python

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+

Related

how to connect the Raspberrypi to internet with Sim7020E using 1NCE IoT FLAT

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.

How to get data from (bluetooth) serial port into python program?

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!

How do you connect PyVisa to an Arduino Uno?

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.

communciation with Fluke endurance pyrometer (profinet/RS485) via python

I have a Fluke Endurance pyrometer (and a pretty flimsy user guide) which has output via an ethernet cable which I'm then connecting to my computer via a USB3.0 to Gigabit ethernet adaptor (windows control panel tells me it's working). According to the guide, communication should be possible via RS485 and profinet. But it's unclear to me if both are possible through the same cable.
My initial plan was to try to use snap7 to try profinet communication, but when I try:
import snap7
plc = snap7.client.Client()
plc.connect("192.168.42.132",0,1)
I get
snap7.snap7exceptions.Snap7Exception: b' TCP : Unreachable peer'
I can ping the device at that IP address.
So my questions are:
any ideas why I can ping, but snap7 seems to have problems connecting?
if I can't get profinet communication to work, any chance I can get RS485 communication somehow through the ethernet cable? Everything I looked up on that talks about having hardware that does the conversion, so I'm assuming it's more than just looking for the right signal format on the right wires. I can't find anything in the manual that turns on/off one form of communication, and it looks like from the menu they are both active, if that's possible.
Better yet, if anyone has communicated with this device in python, any suggestions would be greatly appreciated!
Thanks,
Matthew
Update:
In case anyone else has a similar problem, I solved this by using socket and ASCII commands like this:
import socket
pyrometerIP = "192.168.42.132"
pyrometerPORT = 6363
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(3) # 3 second timeout on commands
s.connect((pyrometerIP, pyrometerPORT))
MESSAGE=bytes('U=C\r', 'ascii')
s.send(MESSAGE)
There are couple of reasons why, some of the reason are listed below:
The rack and slot available to over TIA portal.
You cannot reach the PLC on that IP
You do not have the two DLL file for snap7 module which can be found on https://sourceforge.net/projects/snap7/files/1.4.2/

how can i connect and send commands to a serial port while another program is using it?

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.

Categories

Resources