Using serial ports over bluetooth with micro bit - python

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.

Related

LoRa communication between RPI and PICO with Waveshare hats

I'm trying to make a Raspberry PICO with the Waveshare SX1262 LoRa hat to send LoRa messages to a Raspberry Pi also using a Waveshare sx1262 LoRa Hat.
The radio configuration is different in both devices and I'm unable to see any message sent by the PICO in the RPI.
Communication between 2 PICOs work, and also between 2 RPI using LoRa protocol, but not between different devices.
Initialisation options are very different and I'm wondering which options I should use...
Raspberry PICO configuration (using https://github.com/ehong-tl/micropySX126X code)
sx.begin(freq=923, bw=500.0, sf=12, cr=8, syncWord=0x12,
power=-5, currentLimit=60.0, preambleLength=8,
implicit=False, implicitLen=0xFF,
crcOn=True, txIq=False, rxIq=False,
tcxoVoltage=1.7, useRegulatorLDO=False, blocking=True)
Raspberry Pi configuration (using https://www.waveshare.net/w/upload/6/68/SX126X_LoRa_HAT_Code.zip code)
node = sx126x.sx126x(serial_num = "/dev/ttyS0",freq=923,addr=101,power=22,rssi=True)
AIR_SPEED = 2400
PACKAGE_SIZE = 240 Bytes
Even though the chip is the same, the way to configure them is very different.
Any idea on how to make the 2 devices communicate?
Thanks in advance!
That's because the RPi firmware is weird and the Ebyte E22 chip is even weirder. These managed chips they've been churning out are not sending LoRa packets, and are incompatible with regular LoRa modules.
Look at the initialization code:
node = sx126x.sx126x(serial_num = "/dev/ttyS0", freq=868, addr=0, power=22, rssi=True, air_speed=2400, relay=False)
The air_speed=2400 parameter is definitely not a LoRa parameter (and on the other hand LoRa SF and BW parameters are absent), because the chip is in FSK/OOK mode, instead of LoRa mode. It's the RegBitrateMsb/RegBitrateLsb registers, 0x02/0x03. So the RPi hat can definitely not communicate with a normal LoRa module, unless you bypass the UART management, and rewire it for SPI.

How to use Python to communicate with an device connect to a different computer

I have an instrument setup composed of a command-and-control computer, connected in a network with other computers which have a specific device attached to it (by device I mean something like, for instance, a laser). The communication to these instruments is done via a Serial cable (RS-232) connected to the local computer.
[C&C Computer] -- [local computer] -- [device]
My intention is to directly control the instrument from the command-and-control. I can connect locally to the laser fine, by making use of the PySerial module. My only doubt is on how to access the instrument from the C&C Computer.

Arduino and pyserial and ble

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.

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

Serial communication emulation in Windows

I want to emulate an Arduino serial communication in Windows. I wrote this pySerial script to represent the connection:
from serial import Serial
from time import sleep
serial_conn = Serial(<some port>)
serial_conn.baudrate = 9600
for i in range(1,10):
serial_conn.write(<dummy data>)
sleep(1)
The problem is the following: I tried to use available serial ports (like COM1 or COM3 for example) but I can't sniff the port with a serial monitor tool. It's because I need hardware to open the port? Or maybe the problem are the tested ports? (maybe Windows uses the COM1 for comm as Linux uses the first serial too). Should I try with a virtual serial ports tool? If that's the point, can you recomend me someone and the usage?
In Windows hardware and virtual serial ports have the same enumeration scheme, so they will be COM.
The problem is that only one program at time (theorically) can use a Serial port, so if the port is used by your Python program, it won't be available to the terminal.
You should setup a fake COM, and this mean a custom driver... too much difficult.
Socket, file and standard input can be read/written one byte a time, so you can test your parser using them.

Categories

Resources