user118216
Member since Sep 2020 • Last active Jan 2021Most recent activity
-
- 31 comments
- 5,923 views
-
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!
-
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); }
-
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. -
Thanks Gordon,
I have tried your suggestion but bleak throws an error, which I will show below.
My single quotes show jason.dump etc as before. This is also below.
However I am confused, as per conversation with fanoush, I thought my problem is for my function to convert the bytearray back to a string using javascript. ( Icant find a way to do this yet) but any clarity on this matter would help.
With the followingcommand = "Y("+json.dumps(dict)+")\n"
gives an error. Note I am not convinced that this is due to the command line as bleak seems a bit unstable.
File "/home/pi/.local/lib/python3.7/site-packages/bleak/backends/bluezdbus/client.py", line 146, in connect raise BleakError(str(e)) bleak.exc.BleakError: org.bluez.Error.Failed: Software caused connection abort
With this
command = "Y('+json.dumps(dict)+')\n" ``Console gives
Connecting...
Connected
Writing command
RX> bytearray(b"Y(\'+json.dumps(dict)")
RX> bytearray(b"Y(\'+json.dumps(dict)")
RX> bytearray(b"+\')")
RX> bytearray(b"+\')")
RX> bytearray(b'\r\n')
RX> bytearray(b'\r\n')
Waiting for data
RX> bytearray(b'=undefined\r\n>')
RX> bytearray(b'=undefined\r\n>')
Done!The watch displays
+json.dumps(dict)+
``` -
Sorry console didnt post right
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 ReferenceEr') RX> bytearray(b'Uncaught ReferenceEr') RX> bytearray(b'ror: "nY" is not def') RX> bytearray(b'ror: "nY" is not def') RX> bytearray(b'ined\r\n at line 1 col') RX> bytearray(b'ined\r\n at line 1 col') RX> bytearray(b" 25\r\nY(\'+json.dumps(") RX> bytearray(b" 25\r\nY(\'+json.dumps(") RX> bytearray(b'dict)+\')/nY({"Code":') RX> bytearray(b'dict)+\')/nY({"Code":') RX> bytearray(b' "10"})/nY({"Code": ') RX> bytearray(b' "10"})/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!
-
Thanks got you.
I now get output on the watch. Not what I want but progress.
Watch shows+json.dumps(dict)+
Console shows
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 ReferenceEr') RX> bytearray(b'Uncaught ReferenceEr') RX> bytearray(b'ror: "nY" is not def') RX> bytearray(b'ror: "nY" is not def') RX> bytearray(b'ined\r\n at line 1 col') RX> bytearray(b'ined\r\n at line 1 col') RX> bytearray(b" 25\r\nY(\'+json.dumps(") RX> bytearray(b" 25\r\nY(\'+json.dumps(") RX> bytearray(b'dict)+\')/nY({"Code":') RX> bytearray(b'dict)+\')/nY({"Code":') RX> bytearray(b' "10"})/nY({"Code": ') RX> bytearray(b' "10"})/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!
`
I will try the acii next.
Thanks -
-
Hi fanoush, thanks for your patience.
So to go back I made the following changes but I stiil get no display on the watch. Just to note the function has changed to Y rather than X.
This is the console output.Connecting... Connected Writing command Waiting for data RX> bytearray(b'Y({"Code": "10"})/n') RX> bytearray(b'Y({"Code": "10"})/n') Done!
Putting the command back to what is was
command = "Y("+json.dumps(dict)+")/n"
and my app function is now
E.showMessage("Hello Code"); function Y(dict) { E.showMessage(dict); }
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