I am facing a problem when i tried to run the python code. The error is " Name error:global name 'receive' is not defined". May I know how can I solve the problem? I am using this code in my mobile phone to communicate with microcontroller to control ON/OFF ports on the board. Anyone can help me check what is going wrong? And if I delete the ‘def receive()' will it affect my whole program? Actually what is the purpose of the 'receive()'? If I just want to ON/OFF ports, is it necessary to have the 'receive()' in the code? Thank for helping me :$
import appuifw
# import the module socket
import socket
import e32
# 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()
print "Discovered: %s, %s"%(address,services)
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])
print "Connecting to "+str(target)
# connect to the serial port of the PC
sock.connect(target)
return sock
print "OK."
def recieve():
data=sock.recv(1)
if data=="1":
appuifw.note(u"LED on","info")
elif data=="0":
appuifw.note(u"LED off","info")
def recieve():
data=sock.recv(2)
if data=="1":
appuifw.note(u"Light on","info")
elif data=="0":
appuifw.note(u"Light off","info")
def bt_send_data1():
bt_connect()
sock.send("1")
receive()
def bt_send_data2():
bt_connect()
sock.send("0")
receive()
def bt_send_data3():
bt_connect()
sock.send("3")
receive()
def bt_send_data4():
bt_connect()
sock.send("4")
receive()
def quit():
print"socket closed"
sock.close()
app_lock.signal()
app_lock=e32.Ao_lock()
appuifw.app.menu=[(u"LED on",bt_send_data1),
(u"LED off",bt_send_data2),
(u"Light on",bt_send_data3),
(u"Light off",bt_send_data4),
(u"Exit", quit]
appuifw.app.exit_key_handler = quit
app_lock.wait()
def exit_key_handler():
script_lock.signal()
appuifw.app.set_exit()
appuifw.app.title = u"Smart home"
script_lock = e32.Ao_lock()
appuifw.app.exit_key_handler = exit_key_handler()
script_lock.wait()