User avatar
marciokoko
Posts: 325
Joined: Sat Aug 27, 2016 4:33 pm

How to communicate with serial uart bouetooth module on RPi2

Fri Sep 02, 2016 10:30 pm

Can I communicate with a bluetooth module connected to the GPIO pins of my RPi2 using a program like screen or anything else from the linux terminal?

So far I may have been confusing about what I want. Ive connected my ble module to the GPIO pins (3.3v, GND. Tx & Rx). The module's blue LED is solid which should indicate it is connected to a device, but it shouldnt be connected to anything because I havent been able to communicate with it in order to connect to anything.

User avatar
topguy
Posts: 7243
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: How to communicate with serial uart bouetooth module on

Sat Sep 03, 2016 8:12 pm

Do you know what kind of protocol you have to use over the serial port to talk to this module ?


User avatar
topguy
Posts: 7243
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: How to communicate with serial uart bouetooth module on

Sat Sep 03, 2016 8:49 pm

Well "UART" is just another name for "serialport" in my book.
A universal asynchronous receiver/transmitter, abbreviated UART /ˈjuːɑːrt/, is a computer hardware device that translates data between characters (usually bytes) in a computer and an asynchronous serial communication format that encapsulates those characters between start bits and stop bits. UARTs are commonly used in conjunction with communication standards such as TIA (formerly EIA) RS-232, RS-422 or RS-485. The universal designation indicates that the data format and transmission speeds are configurable. The electric signaling levels and methods (such as differential signaling etc.) are handled by a driver circuit external to the UART.
Just a physical interface and pretty useless unless you know which bytes to send and what you expect to receive. Stuff you usually find in the documentation of the hardware or it might be hidden in a driver and the driver provides a more highlevel interface.

User avatar
Douglas6
Posts: 5048
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: How to communicate with serial uart bouetooth module on

Sat Sep 03, 2016 8:59 pm

AT commands, similar to what modems used to take for configuration. 'AT' to start with, which should return 'OK'. I've been trying to help marciokoko without much luck, I guess.

User avatar
topguy
Posts: 7243
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: How to communicate with serial uart bouetooth module on

Sat Sep 03, 2016 9:07 pm

Ahh.. this is still about the HM-10 module.

Didnt he say that he had used it on an Arduino ? If that was done with some C-program on the Arduino why not try to reuse that code on Raspberry Pi.
But the first step would be to get the rx/tx 3.3V vs 5V stuff sorted out so one can be sure that you actually have communication.

User avatar
marciokoko
Posts: 325
Joined: Sat Aug 27, 2016 4:33 pm

Re: How to communicate with serial uart bouetooth module on

Sat Sep 03, 2016 9:22 pm

Douglas, you have helped. I think I understand a bit better what im trying to do. Yes I did use the hm10 on an arduino and managed to get it connected to another hm10 on a bt relay board. I dont know much python but the code i was looking for, after piecing together a few samples found on the web was:

Code: Select all

#!/usr/bin/env python

import serial

ser = serial.Serial(
        port='/dev/serial0',
        baudrate=9600,
        parity=serial.PARITY_NONE,
        stopbits=serial.STOPBITS_ONE,
        bytesize=serial.EIGHTBITS,
        timeout=1
)

print "Serial is open: " + str(ser.isOpen())

print "Now Writing"
ser.write("AT")

print "Did write, now read"
x = ser.readline()
print "got '" + x + "'"

ser.close()
The first thing that confused me was a bluez tutorial, then a bluepy tutorial. Finally I got around to understand that I just needed to communicate with the hm10 as another serial device.

The second issue was that there was some confusing documentation on RPi2/3 about the ttyS0, ttyAMA0 and serial0. I have a RPi2 but with a post-March-2016 raspbian jessie. I ended up adding the enable_uart=1 line and eliminating the console code.

Finally was the hm10 not responding. I tried a few different sketches and had no luck. Today I decided to mess with my hm10 and check its settings using pl2032-usb adapter. I started documenting what settings were on it. My first settings were:

IMME = 0 (respond to at commands until AT+START/CON/CONL)
ROLE = 1
TCON = 0
TYPE = 0

So I set out to change each one. I got it working on the first try (talk about dumb luck!). I set IMME = 1 and I tried my sketch and it worked. So now I can go back and try to send commands from my RPi2-hm10 to the other relay-hm10 from the python script.

I was left with the curiosity of why I cant send those AT commands from a terminal program to the RPi2-hm10. Why does it have to be from a python script?

Thanks again to all. (gotta go close/answer all those posts in G+, stack and others :-)

User avatar
topguy
Posts: 7243
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: How to communicate with serial uart bouetooth module on

Sat Sep 03, 2016 9:37 pm

marciokoko wrote: I was left with the curiosity of why I cant send those AT commands from a terminal program to the RPi2-hm10. Why does it have to be from a python script?
It nothing stopping you from trying to use a terminalprogram, but if the module has very specific timing requirements, like was mentioned in one of the other threads then you will get inconsistent results. If you dont have exact documentation it will be troublesome to debug the module in general.
You dont have to use Python, most programming languages can read and write from a serial device.

User avatar
Douglas6
Posts: 5048
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: How to communicate with serial uart bouetooth module on

Sat Sep 03, 2016 9:46 pm

marciokoko wrote:I was left with the curiosity of why I cant send those AT commands from a terminal program to the RPi2-hm10. Why does it have to be from a python script?
I tried to explain this once; I'll try again. When the HM-10 interprets the AT command, it's not looking for an EOL to terminate it (notice that you sent 'AT', not 'AT\n') . It stops parsing the command at the first hardware timeout it sees, and I believe that timeout is on the order of 100 ms. Since terminal programs send each character as you type it, you would need to type about 10 characters per second, or the HM-10 will timeout and terminate the command. Python (or virtually any programming language) can keep up where your fingers can't.

Glad you got it working.

User avatar
marciokoko
Posts: 325
Joined: Sat Aug 27, 2016 4:33 pm

Re: How to communicate with serial uart bouetooth module on

Tue Jun 06, 2017 12:57 am

My rpi2 is connected to a ble device via its hm10.

I can ssh into my rpi2.

What can I run to see the device/mac address of the device the rpi2 is connected to?

User avatar
Douglas6
Posts: 5048
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: How to communicate with serial uart bouetooth module on

Tue Jun 06, 2017 1:51 pm

Possibly the 'Query Last Connected Device Address' command, AT+RADD?. See the datasheet for a full list of commands.


User avatar
marciokoko
Posts: 325
Joined: Sat Aug 27, 2016 4:33 pm

Re: How to communicate with serial uart bouetooth module on

Thu Jun 08, 2017 12:27 am

Ok Im running this script to get the last address and noticed that it disconnects the hm10 whenever I send this to the serial:

Code: Select all

#!/usr/bin/env python
import serial
ser = serial.Serial(
        port='/dev/serial0',
        baudrate=9600,
        parity=serial.PARITY_NONE,
        stopbits=serial.STOPBITS_ONE,
        bytesize=serial.EIGHTBITS,
        timeout=1
)
print "Serial is open: " + str(ser.isOpen())
print "Now getting RADD"
ser.write("AT+RADD")
print "Did write, now read"
x = ser.readline()
print "got '" + x + "'"
ser.close()
It even disconnects it when I send it AT+OK.

But even after it disconnects, I run AT+RADD and just get AT+LOST.

Any ideas why?

User avatar
Douglas6
Posts: 5048
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: How to communicate with serial uart bouetooth module on

Thu Jun 08, 2017 12:37 am

My understanding is that the trailing question mark is significant. What happens if you

Code: Select all

ser.write("AT+RADD?")
It's been awhile since I've dealt with these serial interface Bluetooth devices; again, the best information is in the datasheet.

Another issue is possibly that if you have not connected, there will be no last connection.

User avatar
marciokoko
Posts: 325
Joined: Sat Aug 27, 2016 4:33 pm

Re: How to communicate with serial uart bouetooth module on

Thu Jun 08, 2017 1:40 am

Well all of a sudden it worked. I sent AT, which does disconnect my hm10 from my ble device. Then after a while I ran AT+RADD? and it worked.

Thanks

User avatar
Douglas6
Posts: 5048
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: How to communicate with serial uart bouetooth module on

Thu Jun 08, 2017 1:54 am

'All of a sudden' is often the consequence of doing it right.

User avatar
marciokoko
Posts: 325
Joined: Sat Aug 27, 2016 4:33 pm

Re: How to communicate with serial uart bouetooth module on

Thu Jun 08, 2017 1:53 pm

Yeah! I guess the hm10 needs some time to compose itself after certain changes. This is what I remember:
0. AT+RADD? failed, so I sent AT expecting OK
1. AT = responded AT+LOST and got disconnected betweem hm10 and ble-device
2. AT+RADD? = got AT+LOST
3. AT+RADD?= got AT+LOST
4. AT+RADD=? got AT+LOST
5. FRUSTRATION
6. AT+CONNL because i needed to be connected while I researched the issue
7. POST
8. Found hm10 datasheet, confirmed it was indeed AT+RADD?
9. Also found AT+LOST meant disconnection after sending AT command
0. So I sent AT in order to disconnect, got AT+LOST
1. Waited a while (10 seconds), then ran AT+RADD? = got the mac!

It was either the 10 second wait or the fact that I may have also run AT+DISC before that. But I dont think I did.

Anyway, I noticed that after running AT+DISC and finding both the ble-device-relayboard and a new ble-blunoNano I need to add, so I wrote both mac addresses down in order to write a script and toggle between connections, when I tried AT+CONNL in order to connect back to the relayboard device (the only one Ive ever connected to from that hm10 module/rpi, I got AT+CONNN, which means No address, as if the hm10 lost the previous address by just running AT+DISC. So I tried running:

AT+CONNmacaddressofrelayboard = AT+CONNN
AT+CONN[macaddressofrelayboard]=AT+CONNN
AT+CONN2 because of its id on DISC=AT+CONNN

I waited for a while in frustration, more than 10 seconds, more like a minute, then ran AT+CONNL again and it worked. So Im not really sure what did it. I should really try it again to see if it has anything to do with the wait time.

Ever heard of that wait time?

User avatar
Douglas6
Posts: 5048
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: How to communicate with serial uart bouetooth module on

Thu Jun 08, 2017 5:49 pm

My experience, in normal situations, is that Bluetooth devices will connect in about one second, probably less for BLE devices. Lots of variables, though.

Return to “Beginners”