Arduino and pyserial and ble - python

Actually i can send data from pyhton(pyserial) to arduino through cable,but i want to do that with hm-10, normally bluetooth module which is the hc-05 ,hc-06 u can find port,
With wire or wirelles(hc-05) com7 port:
arduino = serial.Serial("COM7", baudrate=9600,timeout=.1)"
But hm-10 is ble.
what i need to write instead of com7?
or how can i solve that problem?
(windows 10)

The pySerial library will not work with the HM-10 module because the module uses Bluetooth Low Energy (rather than Bluetooth Classic).
This means you will have to create a GATT Client to interact with a BLE GATT Server. Typically you would have the Arduino in the Peripheral role (server) and the Windows PC in the Central role (Client).
A quick internet search suggests that the GATT service on HM-10 module has the following values:
Service UUID: 0000FFE0-0000-1000-8000-00805F9B34FB
Characteristic UUID: 0000FFE1-0000-1000-8000-00805F9B34FB
You can check this using a generic Bluetooth Low Energy scanning and exploration tool such as nRF Connect
There are a limited number of Python BLE Central libraries with Bleak probably being the pick of the bunch and has good examples of how to use it.

Related

Connect to a BLE (bluetooth low energy device) and send/receive data using python

My aim is to create a python script that connects to a blue tooth low energy digital vernier caliper and receive its reading.
So far i used BLEAK module and am able to discover the said device address but have no clue about connecting or receiving reading.
Previously tried Pybluez and pybluez2 but they weren't even discovering the device.
The documentations aren't helping either.
I am using a windows 10 PC with Python 3.10.2 64bit interpreter.
My python code for discovering BLE devices is
import asyncio
from bleak import BleakScanner
async def main():
devices = await BleakScanner.discover()
for d in devices:
print(d)
asyncio.run(main())
this successfully found and displayed all of the available BLE devices,But not sure how to connect with the particular device

Using serial ports over bluetooth with micro bit

I have correctly got a microbit working with serial communication via COM port USB.
My aim is to use COM over bluetooth to do the same.
Steps I have taken:
(on windows 10) bluetooth settings -> more bluetooth settings -> COM ports -> add -> incoming
in device manager changed the baud rate to match that of the microbit (115,200)
paired and connected to the microbit
tried to write to both the serial and uart bluetooth connection from the microbit to the PC (using a flashed python script)
using Tera Term, setup -> serial port... -> COM(number - in my case 4), with all necessary values (including 115,200 baud rate)
After doing all of these, I see no incoming message on Tera Term. Have I missed anything?
This is not directly possible via BLE UART communication because it uses different protocols (as mentioned above by ukBaz).
You are able to, however, communicate via custom BLE libraries such as bleak.
Bleak has some good examples on its github repo of how to scan GATT characteristics and services to find the TX and RX characteristics of your BLE device.
From there you're able to connect to the microbit directly over bluetooth and read and write to it's GATT table and not using the proprietary BLE protocols etc.
I'll make a tutorial at some point and link it back here when it's done.

Serial Communication through Bluetooth ble

I am trying to communicate with my battery that has a ble Bluetooth module on it. the goal is to be able to connect to it and then communicate via serial communication by sending bytes back and forth
for example, I have a read and write uuid for it and some byte lines to send to it that it should send back bytes that I can interpret to read voltage temperature and current. like if I send the battery this EA D1 01 04 FF 02 F9 F5 in bytes it should send a byte line very similar back that tells me what the voltage is for the 4 different cells in the battery.
the things that I have are a service uuid and those bytes line commands.
I have tried using bleak as I have had some experience in this but for whatever reason it would not connect, I would get timeout errors or an os error.
I haven't been able to manually connect the Bluetooth with my Bluetooth settings on the desktop, so I haven't been able to setup a com port
I have been able to connect to it with a Arduino nano 33 but haven't been able to send and receive serial communication
I have been able to do this successfully with a Serial Bluetooth terminal on my android device and it works very nicely on that, but I would like to get it on my desktop as it would help me out a lot to automate getting that information and using it.
any help is greatly appreciated and i look forward to possibly communicating with some of you. feel free to ask for any addition information as maybe i was unclear with some things
Welcome to Stack Overflow. You want to become familiar with Bluetooth and need to understand that Bluetooth classic and Bluetooth LE are different standards that are incompatible to each other.
So while the Serial Bluetooth terminal probably uses the classic standard with SPP, other things you've tried seem to work with Bluetooth LE. The battery, for example, seems to work with Bluetooth LE and the GATT service, as indicated by the service UUID.
All in all, it looks to me as if you should first familiarise yourself with the technology before you start writing your program.

BLE security mechanism implementation

I am currently developing a BLE communication system where an app on the phone will communicate with a peripheral device. I am working on both the BLE client on the phone and the BLE server on the peripheral
For the app part, I'm using react-native-ble-manager and for the ble server part, I based it on examples from the official BLE docs using dbus and Python.
I want to add some security to the pairing process, by implementing the PIN security mechanism or just saving the same private key and verifying the connection is trustworthy but after spending some time researching how this should be implemented I did not find any good examples. Where could I find some resources or guidelines on how to implement these security mechanisms?

python communication MODBUS TCP/IP raspberry pi and HMI - write value

I'm French student and i need your help in Python for my program.
I've got a program in my rapsberry in Python which acquire datas from temperature and hygrometry sensors.
I need to communicate these values to an Human Machine Interface supervisor by the MODBUS TCP/IP PROTOCOL to display and made some graphics of these values in my HMI
The IP adress of the raspberry : 172.16.0.2
The IP adress of the HMI : 172.16.0.10
I think I need to use package like pyModbusTCP or things like that but I don't understand how to use it.
Could you help me to understand how I made the communication between my Rpi and my HMI and for example how I could write the integer value 100 at the address index 1?
Thanks for all!
Antoine
MODBUS is a single-master protocol, meaning that there can only be one master, the rest of attached devices are slaves ( http://www.ni.com/white-paper/52134/en/ , similar to USB protocol, called host and device there ). In addition in MODBUS protocol a slave never starts a communications, slaves only answer to requests. Consequentially the machine on that your HMI runs has to be the MODBUS master /client ( this naming convention is a bit senseless )
For a quick general overview read this https://www.reddit.com/r/PLC/comments/7bqppu/using_raspberry_pi_as_modbus_slave_and/ and http://www.simplymodbus.ca/TCP.htm
pymodbus client / master on HMI machine
From this master / client you can send requests to the RPi ( MODBUS slave / server ) with its sensors using the following code ( if one of the sensors stores its data in a register that is presented to the bus as coil 1 by the pymodbus server that runs on the RPi, see below ). This is only an example there are other data blocks in MODBUS, namely Coils, Discrete Inputs, Input Registers, Holding Registers which of them you use depends on how you configure the MODBUS server on the RPi, normally Discrete Inputs and Input Registers are rarely used :
client = ModbusTcpClient('172.16.0.2')
client.write_coil(1, True)
result = client.read_coils(1,1)
print(result.bits[0])
client.close()
https://github.com/riptideio/pymodbus
pymodbus server / slave on Rpi
For this to work on the RPi has to run software ( pymodbus server ) that enables it as MODBUS slave / server and the sensors have to write their values into specific memory locations that are presented to MODBUS as coils / registers. How this can be done is in https://www.youtube.com/watch?v=p3Dgd0PDjnU and https://jacekhryniewicz.wixsite.com/website/raspberry-pi-with-modbus-tcp ( is a bit outdated )
In https://github.com/riptideio/pymodbus/blob/master/examples/common/asynchronous_server.py is a working example of a MODBUS server that has to run onthe RPi ( read the comments, especially the lines following # initialize your data store )
The word coils has its origin in the past of MODBUS protocol which was developed when electromechanical relays with coils were used in automation technology

Categories

Resources