Bluetooth >>> Picaxe

julianE

Senior Member
I took a plunge and managed to solder the tiny bluetooth board. Discovery process went without a hitch. I'm using a 20X2 picaxe and made some changes to the sample program and the led lights. Running on 2 batteries, voltage 3.2V.

I do have an issue that must be something terribly simple but I just can't figure it out.

To simplify I pared down the code to this,

main:
Serin b.6,T9600_8,b1
debug b1
Goto main

I'm using Hyperterminal setup as 9600 8-N-1. Emulation is Auto Detect and I used the settings suggested by Slimplynth for ASCII setup.

The values I'm getting in b1 variable in the debug window when I send the 'a' key are 225 and 97. 97 the correct value appears 2 out of 3 tries but it does vary.

I tried other keys and they are all consistent i.e. if i press 'b' i'll get 226 or 98.

Of course i can compensate in software but I'm kind of curious why it's happening. probably something wrong with my Hyperterminal setup.
 

hippy

Ex-Staff (retired)
The values I'm getting in b1 variable in the debug window when I send the 'a' key are 225 and 97. 97 the correct value appears 2 out of 3 tries but it does vary.

I tried other keys and they are all consistent i.e. if i press 'b' i'll get 226 or 98.
Have you determined the correlation between 225 and 97, between 226 and 98 ?

These are characters sent via serial as a bit-stream, so consider them as Hexadecimal or better still binary ...

225 = $E1 = %11100001 / 97 = $61 = %01100001
226 = $E2 = %11100010 / 98 = $62 = %01100010

RS232-style serial is sent lsb first and it is the msb which is being incorrectly set, the last bit received. This is most likely a baud rate issue. You can either take a brute force approach and simply mask off the msb or, better, use CALIBFREQ to adjust the PICAXE operating speed.

Ideally put an oscilloscope on the signal to see how accurate the baud rate is for the data to start with.

An alternative to SERIN would be to use high-speed serial background receive.
 

julianE

Senior Member
An alternative to SERIN would be to use high-speed serial background receive.
Hippy you are a genius!!!

When I chose B.6 for my serial port it was with intention of eventually learning the HSERIN command, your suggestion forced me to try it earlier.
Truth be told the HSERIN is a mystery to me, the details on it in the manual leave me wanting for more.
Here is the code that works, I added the LED flashing for additional fun. The debug window shows the right number every time.

hsersetup B9600_8,%00 'setup hserin port B.6 for 9600 baud

main:

hserin 0,1
ptr=0
get 0,b1

debug b1

Select b1

Case = 97 '"a" key
High B.1
Pause 5000
Low B.1

Case = 98 '"b" key
High B.2
Pause 5000
Low B.2

Else Goto main

Endselect

Goto main


Thanks for all the help and I'd be grateful for any additional info on HSERIN command. I did a search on the forum and found enough to get this done but feel there is a lot more to learn.
 

dsvilko

Senior Member
I am also for the first time experimenting with a background receive and it's working wonderfully. Here a part of my code:

Code:
hsersetup B9600_64, %01
...
WaitSerial:	
	for i=1 to 255
		pause 200
		if hserinflag=1 then gosub ProcSerial
	next i
	goto WaitSerial
...
ProcSerial:
	gosub LCD_clear
	do while ptr<>hserptr
		byt=@ptrinc : gosub LCD_char   ' output a character to the LCD
	loop
	ptr=0
	hserptr=0
	hserinflag=0
	i=1
	return
I have also written a simple bluetooth terminal application for the Symbian phones (in Py60 - even though I know absolutely nothing about Python). Now I can send messages wirelessly from my phone to a LCD :) I also get current temperature and the battery voltage echoed back to my phone every few seconds. These bluetooth modules are very cool! :)
 

julianE

Senior Member
I will try your code first chance I get. I think I'm starting to understand the hserin command better.
I have a bluetooth on my blackberry bold, not sure where to find a terminal for it. The blackberry recognizes the board but not sure if there is anything i can do with it, I don't think termnal software comes with it.

Also, I ordered this,

http://www.mdfly.com/index.php?main_page=product_info&products_id=428

Should have it soon, will make things easier, i am amazed I soldered the first one, it was super tight.

Maybe you should post your Python code for your phone, could be something that can be adapted for other phones.

Happy New Year to all.
 

dsvilko

Senior Member
Should have it soon, will make things easier, i am amazed I soldered the first one, it was super tight.
Breakout looks nice. Very convenient - just stick it in the breadboard. I didn't have that much trouble soldering mine, though.

Maybe you should post your Python code for your phone, could be something that can be adapted for other phones.
Ok, here it is:

Code:
# basend on a script from Jurgen Scheible
import appuifw
import socket
import e32
import sys

# function that handles the bluetooth connection:
def bt_connect():
    global sock
    # create a bluetooth socket
    sock=socket.socket(socket.AF_BT,socket.SOCK_STREAM)
    target=''# here you can give the bt address of the other mobile if you know it
    if not target:
        # scan for bluetooth devices
        address,services=socket.bt_discover()
        if len(services)>1:
            choices=services.keys()
            choices.sort()
            # bring up a popup menu and show the available bt devices for selection
            choice=appuifw.popup_menu([unicode(services[x])+": "+x
                                        for x in choices],u'Choose port:')
            target=(address,services[choices[choice]])
        else:
            target=(address,services.values()[0])
    sock.connect(target)
    t.color=(0,150,0)
    t.add(u"Connected\n")
    t.color=(0,0,255)
    
        
# define the textinput function
def bt_send():
	global sock
	test = appuifw.query(u"String:", "text", u"")
	sock.send(test)
	t.color=(255,0,0)
	t.add(unicode(test))
	t.add(u"\n")
	t.color=(0,0,255)
        
def bt_rec():
	global sock
	#sys.stdout.write(sock.recv(1024))
	try:
		t.add(unicode(sock.recv(1024)))
	except:
		t.add(u"???")
  
def exit_key_handler():
	global running
	running=0

appuifw.app.exit_key_handler = exit_key_handler

# call the function that handles the bluetooth connection
appuifw.app.menu = [(u"send line", bt_send)]
appuifw.app.screen='large'
t=appuifw.Text()
appuifw.app.body=t
t.color=(0,0,255)
bt_connect()
running=1
while running:
  bt_rec()
  e32.ao_yield()
It's heavily based on a few examples I found on the net as I am just starting to learn Py60. I think it will be only useful for the Symbian phones.

[OT rant]I have bought my Nokia 6124c for something like $60 (second hand but completely new) and even though it's quite an old model it's amazing how much more 'smartphone' the OS feels when compared to the latest Android/iPhone phones. Heck, I could write programs directly on my 10 year old 64MHz Palm, with full API support in a very nice scripting language (Lua) but I can't do anything similar either on an Android or a iPhone. It seems to me the more fancy the UI has become and the more feature-packed the hardware, the 'stupider' and more limited the smartphones we got. [/rant]
 

slimplynth

Senior Member
Nice work Vilko... I haven't touched the bluetooth for a while now but thanks for posting that python code.

Will keep my eyes open for the same Nokia, I have Blackberry 8900, which is ok but to use the BT part of the API you have to pay RIM around $20 for the privilege - I know it's not much, less than a Nokia but today it's $20 for Bluetooth, tomorrow it will be $600 for Redtooth® :)
 

julianE

Senior Member

slimplynth

Senior Member
Hi Julian

With the Blackberry I set up the IDE and did the Hello world program on the physical phone (i do like the virtual phone simulator - Android has the same)

I don't think you have to install anything as such on the Blackberry to activate bluetooth in programs. My understanding was that you compile the project so it will appear as a downloaded app on the blackberry.. you pay the money to have the security key - needed during the compiling stage, like a digital signature.

The thing that was swinging me to android was the shear volume of example projects/programs available. Hackaday has a very good tutorial series too. Shame T-Mobile's computer said 'no' to an HTC Desire... I am a bad lad though :)
 

dsvilko

Senior Member
I had no idea about android phones being a step backward when it comes to scripting languages.
OT alert:
I could be wrong. I know there is a possibility of scripting on android using multiple scripting languages but I think that the functionality is limited (meant for task automation more than anything else). With PyS60 (Python for Symbian) you get a full API support. Don't know if the new S^3 Nokias support it, though. It seems that the one and the only thing a smartphone has to have nowadays is a fancy GUI (and a high Mpixel camera). Everything else is optional.
My ancient Palm, when you consider the ease of use and the number, the quality and often the incredible complexity of the available apps feels extremely advanced compared to a modern smartphones. How many modern apps do you know that require a >300 page manuals to describe all the features? There were quite a few for Palm. The most advanced applications I have on my Nokia are actually Palm applications run in an Palm emulator! On Palm, not only could I use Lua to write full-featured applications (no PC required) but I could also use a simpler Basic or even write a program in Fortran or C and actually compile it on-board! Now it sounds like some impossibly futuristic technology. Today we are less likely to get such features than cool things as a full-body-gesture-recognition (roll on the floor one way to advance a song, play dead to stop playing...). Depressing, really.
 

Haku

Senior Member
I thought I'd have a bash at getting one of these modules working beyong the PC host simply 'seeing' the module, but when I run hyperterminal and select the outgoing BT serial port it comes back with "Unable to open COM9. Please check your port setings".
The same happens on two different WinXP machines using the same USB BT adaptor.

I'm using one of those miniature half-moon shaped USB BT adaptors, with the "Serial port(SPP) 'Dev B'" selected in the properties of the BT device. Pressing the Configure button in hyperterminal does nothing, in the system device properties of the BT adaptor it's set to 9600-8-n-1.

What am I doing wrong this time?
 
Last edited:

slimplynth

Senior Member
So in 'My Bluetooth Places' it actually shows as being connected? From memory the serial port icon gets a little green line to indicate it's active. (WinXP)
 

hippy

Ex-Staff (retired)
I thought I'd have a bash at getting one of these modules working beyong the PC host simply 'seeing' the module, but when I run hyperterminal and select the outgoing BT serial port it comes back with "Unable to open COM9. Please check your port setings".
I can't really help as although I picked up a USB Bluetooth dongle for a PC I don't have any other Bluetooth devices to connect to ! I also only installed the Device Drivers rather the application suite which came with it. That created a number of COM ports, some which could be connected to, some which gave 'cannot open COMx' type errors.

I would guess it depends on the type of adapter, what it connects to and whether there's anything paired-up with it or not.

With the adapter installed I was getting long delays with Programming Editor and anything which scans available serial ports, presumably as each Bluetooth port checks it has something at the other end before deciding to report back if that port is available, can or cannot be used. If there's a timeout within the app and it doesn't respond quickly enough it may be that the port is then reported as unavailable, if it's there but cannot be used ( nothing at the other end responding ) it may be reported as unavailable. It's also possible that a port may report unavailable if an attempt is made to open it with an incompatible baud rate, handshaking, parity etc. It all depends on how the VSP / Device Driver handles things.

There's some tips on Bluetooth and XP here, but I don't know how useful they will be ...

http://support.microsoft.com/?kbid=883258
 

julianE

Senior Member
Has anyone managed to use a pair of these with 2 picaxe chips, not sure how the discovery process would work.
 
Top