-
• #27
Thanks Gordon,
So I now have double quotes back in and I have tested the connection and it has worked ok so far in terms of not giving the connection error.However the console output looks werid and the watch display is blank.
So I will post the complete code.``
#!/usr/bin/env python3import asyncio
import array
from bleak import discover
from bleak import BleakClient
import json#create dictionary for the data to be sent
dict = {'Code': '10'}address = "D8:AB:E0:B7:F5:AE"
UUID_NORDIC_TX = "6e400002-b5a3-f393-e0a9-e50e24dcca9e"
UUID_NORDIC_RX = "6e400003-b5a3-f393-e0a9-e50e24dcca9e"
#command = "X("+json.dumps(dict)+")\n"
#command = b"E.showMessage('Hello','A Title')/n"
command = "Y("+json.dumps(dict)+")\n"def uart_data_received(sender, data):
print("RX> {0}".format(data))
You can scan for devices with:
async def run():
devices = await discover() for d in devices: print(d)
print("Connecting...")
async def run(address, loop):async with BleakClient(address, loop=loop) as client: print("Connected") await client.start_notify(UUID_NORDIC_RX, uart_data_received) print("Writing command") c=command while len(c)>0: await client.write_gatt_char(UUID_NORDIC_TX,bytearray((c[0:20]),'utf-8') , True) c = c[20:] print("Waiting for data") await asyncio.sleep(1.0, loop=loop) # wait for a response print("Done!")
loop = asyncio.get_event_loop()
loop.run_until_complete(run(address, loop))My app. Note function is now Y.
E.showMessage("Hello Code");
function Y(dict) {
E.showMessage(dict);}
and the console
Connecting...
Connected
Writing command
Waiting for data
RX> bytearray(b'Y({"Code": "10"})\r\n')
RX> bytearray(b'Y({"Code": "10"})\r\n')
RX> bytearray(b'Uncaught Error: Func')
RX> bytearray(b'Uncaught Error: Func')
RX> bytearray(b'tion "split" not fou')
RX> bytearray(b'tion "split" not fou')
RX> bytearray(b'nd!\r\n at line 1 col ')
RX> bytearray(b'nd!\r\n at line 1 col ')
RX> bytearray(b'250\r\n...ect(c/2-b,44')
RX> bytearray(b'250\r\n...ect(c/2-b,44')
RX> bytearray(b',c/2+b,45)}b=e.split')
RX> bytearray(b',c/2+b,45)}b=e.split')
RX> bytearray(b'("\n");var h=(f-16*b')
RX> bytearray(b'("\n");var h=(f-16*b')
RX> bytearray(b'.length)...\r\n ')
RX> bytearray(b'.length)...\r\n ')
RX> bytearray(b' ')
RX> bytearray(b' ')
RX> bytearray(b' ^\r\nin function ca')
RX> bytearray(b' ^\r\nin function ca')
RX> bytearray(b'lled from line 1 col')
RX> bytearray(b'lled from line 1 col')
RX> bytearray(b' 19\r\nE.showMessage(d')
RX> bytearray(b' 19\r\nE.showMessage(d')
RX> bytearray(b'ict);\r\n ')
RX> bytearray(b'ict);\r\n ')
RX> bytearray(b' ^\r\nat line 1 co')
RX> bytearray(b' ^\r\nat line 1 co')
RX> bytearray(b'l 19\r\nE.showMessage(')
RX> bytearray(b'l 19\r\nE.showMessage(')
RX> bytearray(b'dict);\r\n ')
RX> bytearray(b'dict);\r\n ')
RX> bytearray(b' ^\r\nin function')
RX> bytearray(b' ^\r\nin function')
RX> bytearray(b' "Y" called from lin')
RX> bytearray(b' "Y" called from lin')
RX> bytearray(b'e 1 col 17\r\nY({"Code')
RX> bytearray(b'e 1 col 17\r\nY({"Code')
RX> bytearray(b'": "10"})\r\n ')
RX> bytearray(b'": "10"})\r\n ')
RX> bytearray(b' ^\r\n>')
RX> bytearray(b' ^\r\n>')
Done!```
Any help most appreciated. -
• #28
Looks like my code didnt post well, I will try again.
bleak code
#!/usr/bin/env python3 import asyncio import array from bleak import discover from bleak import BleakClient import json [#create](https://forum.espruino.com/search/?q=%23create) dictionary for the data to be sent dict = {'Code': '10'} address = "D8:AB:E0:B7:F5:AE" UUID_NORDIC_TX = "6e400002-b5a3-f393-e0a9-e50e24dcca9e" UUID_NORDIC_RX = "6e400003-b5a3-f393-e0a9-e50e24dcca9e" command = "Y('+json.dumps(dict)+')\n" def uart_data_received(sender, data): print("RX> {0}".format(data)) async def run(): devices = await discover() for d in devices: print(d) print("Connecting...") async def run(address, loop): async with BleakClient(address, loop=loop) as client: print("Connected") await client.start_notify(UUID_NORDIC_RX, uart_data_received) print("Writing command") c=command while len(c)>0: await client.write_gatt_char(UUID_NORDIC_TX,bytearray((c[0:20]),'utf-8') , True) c = c[20:] print("Waiting for data") await asyncio.sleep(1.0, loop=loop) # wait for a response print("Done!") loop = asyncio.get_event_loop() loop.run_until_complete(run(address, loop))
Console
Connecting... Connected Writing command Waiting for data RX> bytearray(b'Y({"Code": "10"})\r\n') RX> bytearray(b'Y({"Code": "10"})\r\n') RX> bytearray(b'Uncaught Error: Func') RX> bytearray(b'Uncaught Error: Func') RX> bytearray(b'tion "split" not fou') RX> bytearray(b'tion "split" not fou') RX> bytearray(b'nd!\r\n at line 1 col ') RX> bytearray(b'nd!\r\n at line 1 col ') RX> bytearray(b'250\r\n...ect(c/2-b,44') RX> bytearray(b'250\r\n...ect(c/2-b,44') RX> bytearray(b',c/2+b,45)}b=e.split') RX> bytearray(b',c/2+b,45)}b=e.split') RX> bytearray(b'("\\n");var h=(f-16*b') RX> bytearray(b'("\\n");var h=(f-16*b') RX> bytearray(b'.length)...\r\n ') RX> bytearray(b'.length)...\r\n ') RX> bytearray(b' ') RX> bytearray(b' ') RX> bytearray(b' ^\r\nin function ca') RX> bytearray(b' ^\r\nin function ca') RX> bytearray(b'lled from line 1 col') RX> bytearray(b'lled from line 1 col') RX> bytearray(b' 19\r\nE.showMessage(d') RX> bytearray(b' 19\r\nE.showMessage(d') RX> bytearray(b'ict);\r\n ') RX> bytearray(b'ict);\r\n ') RX> bytearray(b' ^\r\nat line 1 co') RX> bytearray(b' ^\r\nat line 1 co') RX> bytearray(b'l 19\r\nE.showMessage(') RX> bytearray(b'l 19\r\nE.showMessage(') RX> bytearray(b'dict);\r\n ') RX> bytearray(b'dict);\r\n ') RX> bytearray(b' ^\r\nin function') RX> bytearray(b' ^\r\nin function') RX> bytearray(b' "Y" called from lin') RX> bytearray(b' "Y" called from lin') RX> bytearray(b'e 1 col 17\r\nY({"Code') RX> bytearray(b'e 1 col 17\r\nY({"Code') RX> bytearray(b'": "10"})\r\n ') RX> bytearray(b'": "10"})\r\n ') RX> bytearray(b' ^\r\n>') RX> bytearray(b' ^\r\n>')
Code on watch
E.showMessage("Hello Code"); function Y(dict) { E.showMessage(dict); }
-
• #29
The quoting in your listing when composing Y() call still looks a bit strange but in your real code you have it right since it is passing data to the watch. From the error 'e.split not found' looks like the E.showMessage expects string parameter and you are passing object
{'Code': '10'}
such object does not have method called split. so make string out of it e.g. by passingdict.Code
to it, then it should print '10' -
• #30
Hi fanoush,
Thanks.
I presumed you meant I should add your suggestion to my code app.
I did that and still no result yet.
Code on watchE.showMessage("Hello Code"); function Y(dict) { E.showMessage(dict.Code); }
Console
Connecting... Connected Writing command RX> bytearray(b"Y(\'+json.dumps(dict)") RX> bytearray(b"Y(\'+json.dumps(dict)") RX> bytearray(b"+\')\r\n") RX> bytearray(b"+\')\r\n") Waiting for data RX> bytearray(b'Uncaught Error: Cann') RX> bytearray(b'Uncaught Error: Cann') RX> bytearray(b"ot read property \'sp") RX> bytearray(b"ot read property \'sp") RX> bytearray(b"lit\' of undefined\r\n ") RX> bytearray(b"lit\' of undefined\r\n ") RX> bytearray(b'at line 1 col 249\r\n.') RX> bytearray(b'at line 1 col 249\r\n.') RX> bytearray(b'..Rect(c/2-b,44,c/2+') RX> bytearray(b'..Rect(c/2-b,44,c/2+') RX> bytearray(b'b,45)}b=e.split("\\n"') RX> bytearray(b'b,45)}b=e.split("\\n"') RX> bytearray(b');var h=(f-16*b.leng') RX> bytearray(b');var h=(f-16*b.leng') RX> bytearray(b'th...\r\n ') RX> bytearray(b'th...\r\n ') RX> bytearray(b' ^\r\n') RX> bytearray(b' ^\r\n') RX> bytearray(b'in function called f') RX> bytearray(b'in function called f') RX> bytearray(b'rom line 1 col 24\r\nE') RX> bytearray(b'rom line 1 col 24\r\nE') RX> bytearray(b'.showMessage(dict.Co') RX> bytearray(b'.showMessage(dict.Co') RX> bytearray(b'de);\r\n ') RX> bytearray(b'de);\r\n ') RX> bytearray(b' ^\r\nat line ') RX> bytearray(b' ^\r\nat line ') RX> bytearray(b'1 col 24\r\nE.showMess') RX> bytearray(b'1 col 24\r\nE.showMess') RX> bytearray(b'age(dict.Code);\r\n ') RX> bytearray(b'age(dict.Code);\r\n ') RX> bytearray(b' ') RX> bytearray(b' ') RX> bytearray(b'^\r\nin function "Y" c') RX> bytearray(b'^\r\nin function "Y" c') RX> bytearray(b'alled from line 1 co') RX> bytearray(b'alled from line 1 co') RX> bytearray(b"l 23\r\nY(\'+json.dumps") RX> bytearray(b"l 23\r\nY(\'+json.dumps") RX> bytearray(b"(dict)+\')\r\n ") RX> bytearray(b"(dict)+\')\r\n ") RX> bytearray(b' ^\r\n>') RX> bytearray(b' ^\r\n>') Done!
-
• #31
Sorry fanoush correction. I used a modified version of bleak.
Original version worked!! at last thanks to you and Gordon.
I have plenty more to do with this but I hope I wont have to bother you too soon.
For completeness
Watch No change from above.Console
Connecting... Connected Writing command Waiting for data RX> bytearray(b'Y({"Code": "10"})\r\n') RX> bytearray(b'Y({"Code": "10"})\r\n') RX> bytearray(b'=undefined\r\n>') RX> bytearray(b'=undefined\r\n>') Done!
Hmm - when you get
Software caused connection abort
did you try it more than once just in case it was a glitch?And how big is the JSON you're sending? Maybe it's huge and that causes a problem? You could just try something really basic.
Yes, that's what you'd expect - because as above you're sending the actual text, but not
dict
at all.Maybe you could post up your complete code for us to take a look at?