Paja

Icon

Rapid prototyping for music, art and design work

S60 Python connected to Sparkfun BlueSMiRF Silver bluetooth module

This demo is implemented with S60 Python connected to BASIC Stamp 2 via Sparkfun BlueSMiRF Silver bluetooth module.

schematic iconSchematic Plan

Data SheetBlueSMiRF Silver AT command manual

cart_iconMain components

  1. S60 Python for Nokia S60 phone (e.g. Nokia E51)
  2. BASIC Stamp Board of Education (USB version)
  3. Sparkfun BlueSMiRF Silver bluetooth module (Siver is discontinued. Gold should be available instead.)
  4. Ping))) Ultra sonic sensor

note_iconNote
BlueSMiRF Silver is discontinued on Sparkfun online shop but they sell Gold instead. The AT command manual is only for Siver. Gold has another set of |AT commands.

PBasic codePBasic Code

'{$STAMP BS2}
'{$PBASIC 2.5}
' 2008 mlab.taik.fi/paja
' Sending data to a Serial port
' ---[ I/O Definitions ]---
pRX		PIN	10
pTX		PIN	11
pPing		PIN	7
' ---[ Variables ]---
wTime		VAR	WORD
' ---[ Constants ]---
cTrigger     CON     5		' trigger pulse = 10 uS for BS2
' ---[ Initialization ]---
DEBUG CLS, "--- PING))) Ultrasonic Sensor", CR
' ---[ Main Code ]---
Main:
	DO
		GOSUB ReadPing
		wTime = wTime/10
		SEROUT pTX, 84, [DEC5 wTime]
		DEBUG DEC wTime, CR
		PAUSE 100
	LOOP
END
' ---[ Subroutines ]---
ReadPing:
	' PULSOUT Pin, Duration
	' pin: Pin number (0-15)
	' Duration: specifies the duration of the pulse. (BS2: a unit = 2us)
	PULSOUT pPing, cTrigger
	' PULSIN Pin, State, Variable
	' Pin: Pin number (0 - 15)
	' State: Specifies whether the puse to be mesured is low(0) or high(1)
	' Variable: the measured pulse duration will be stored.
 	PULSIN pPing, 1, wTime
RETURN

PYTHON CODE

import appuifw, e32, key_codes, graphics
import socket

BACKGROUND = (0, 0, 0)
TEXT_COLOR = (200, 200, 200)
TEXT_LOC = (10, 30)
TEXT = u""
COLOR = (0,0,0)
DEFAULT_COLOR = (200, 255, 100)
LOOP = 1

def bt_connect():
    global sock
    sock=socket.socket(socket.AF_BT,socket.SOCK_STREAM)
    target=''
    if not target:
        address,services=socket.bt_discover()
        print "Discovered: %s, %s"%(address,services)
        if len(services)>1:
            import appuifw
            choices=services.keys()
            choices.sort()
            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])
    print "Connecting to "+str(target)
    sock.connect(target)

def bt_receive_data():
    global sock
    buffer = []
    buffer = sock.recv(5)
    return buffer

def draw_text():
    img.text(TEXT_LOC, u"" + TEXT, fill = TEXT_COLOR)

def draw_background():
    img.clear(COLOR)
    handle_redraw(None)

def handle_redraw(rect):
    if img:
        canvas.blit(img)

def handle_event(event):
    handle_redraw(None)

def quit():
    global LOOP
    LOOP = 0
    print "Quit!"
    app_lock.signal()

img = None
canvas = appuifw.Canvas(redraw_callback = handle_redraw, event_callback = handle_event)

appuifw.app.body = canvas
appuifw.app.screen = "full"

app_lock = e32.Ao_lock()
appuifw.app.exit_key_handler = quit

w, h = canvas.size
img = graphics.Image.new((w, h))
img.clear(BACKGROUND)

bt_connect()

while LOOP:
    #e32.ao_sleep(0.1)

    data = bt_receive_data()

    number = int(data)
    if number <= 255:
        COLOR = (200, number, 100)
    else:
        COLOR = DEFAULT_COLOR
    TEXT = str(number)

    img.clear(0)

    draw_background()
    draw_text()

    canvas.blit(img)

#application waits for events
app_lock.wait()
Facebook Twitter Email

Category: Snippets

Tagged:

Social links powered by Ecreative Internet Marketing