@@ -20,46 +20,19 @@ def send_command(string):
20
20
except RuntimeError as error :
21
21
raise RuntimeError ("AT command failure: " + repr (error ))
22
22
23
- def command_check_OK (string ):
23
+ def command_check_OK (string , delay = 0.0 ):
24
24
ret = send_command (string )
25
+ time .sleep (delay )
25
26
if not ret or not ret [- 4 :]:
26
27
raise RuntimeError ("Not OK" )
27
28
if ret [- 4 :] != b'OK\r \n ' :
28
29
raise RuntimeError ("Not OK" )
29
30
if ret [:- 4 ]:
30
31
return str (ret [:- 4 ], 'utf-8' )
31
32
32
- def uarttx (string ):
33
- try :
34
- msgtype , msgid , rsp = bluefruit .uarttx (string )
35
- if msgtype == MsgType .ERROR :
36
- raise RuntimeError ("Error (id:{0})" .format (hex (msgid )))
37
- except RuntimeError as error :
38
- raise RuntimeError ("UARTTX command failure: " + repr (error ))
39
- if not rsp or not rsp [- 4 :]:
40
- raise RuntimeError ("Not OK" )
41
- if rsp [- 4 :] != b'OK\r \n ' :
42
- raise RuntimeError ("Not OK" )
43
- if rsp [:- 4 ]:
44
- return str (ret [:- 4 ], 'utf-8' )
45
-
46
- def uartrx ():
47
- try :
48
- msgtype , msgid , rsp = bluefruit .uartrx ()
49
- if msgtype == MsgType .ERROR :
50
- raise RuntimeError ("Error (id:{0})" .format (hex (msgid )))
51
- except RuntimeError as error :
52
- raise RuntimeError ("UARTRX command failure: " + repr (error ))
53
- if not rsp or not rsp [- 4 :]:
54
- raise RuntimeError ("Not OK" )
55
- if rsp [- 4 :] != b'OK\r \n ' :
56
- raise RuntimeError ("Not OK" )
57
- if rsp [:- 4 ]:
58
- return str (ret [:- 4 ], 'utf-8' )
59
-
60
- # Send the ATI command
61
- print (command_check_OK ("AT+FACTORYRESET" ))
62
- time .sleep (1 )
33
+ # Initialize the device
34
+ bluefruit .init ()
35
+ print (command_check_OK ("AT+FACTORYRESET" , 1.0 ))
63
36
print (command_check_OK ("ATI" ))
64
37
#print(command_check_OK("AT+GAPDEVNAME=ColorLamp"))
65
38
@@ -80,6 +53,10 @@ def uartrx():
80
53
# Yay!
81
54
print ("\n Connected!" )
82
55
while connected :
83
- uarttx ("1" )
84
- connected = int (command_check_OK ("AT+GAPGETCONN" )) == 1
56
+ command_check_OK ("AT+BLEUARTTX=*" )
57
+ resp = command_check_OK ("AT+BLEUARTRX" )
58
+ if resp :
59
+ print (resp )
60
+ # Check connection status with a 1s delay
61
+ connected = int (command_check_OK ("AT+GAPGETCONN" , 0.5 )) == 1
85
62
print ("Connection lost." )
0 commit comments