-
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); }
-
Thanks fanoush.
Re my understanding yes you are probably right.
To clarify I thought this line converted the python dictionary to a string:-command = "X('+json.dumps(dict)+')/n"
and this line converted the json string to bytes:-
await client.write_gatt_char(UUID_NORDIC_TX,bytearray((c[0:20]),'utf-8') , True) c = c[20:]
I thought this would then be sent and my function would decode the bytes back to a json string:-
E.showMessage("Hello Code"); function X(dict) { msg = json.loads(dict.decode('utf-8')); E.showMessage(msg); }
I dont now get the previous error, but the bleak library seems a little unreliable and gives a connection failure after a few attempts. However I am getting no response on the watch so I cant say its working yet.
I will try ascii and see if there's a difference,but I really need a more reliable bluetooth tranfer code. Hopefully the code comes out OK.
Many thanks for your help.
mike -
thanks for that
I have amended the code.command = "X('+json.dumps(dict).encode('utf-8')+')/n"
and
await client.write_gatt_char(UUID_NORDIC_TX,bytearray((c[0:20]),'utf-8') , True) c = c[20:]
Not sure if I need both utf-8's, however I am assuming that passing this to my function I will need to decode it.
-
-
-
Hi Gordon,
Many thanks for the response.
I decided to make an app for this to make things a bit more visible.
However I get an error with json.dumps, which isTraceback (most recent call last): File "/home/pi/bleak-test.py", line 15, in <module> command = b"X("+json.dumps(dict)+")\n" TypeError: can't concat str to bytes
I added this snippet to bleak to creates the variable.
import json [#create](https://forum.espruino.com/search/?q=%23create) dictionary for the data to be sent dict = {'Code': '10'}
And for completeness my app does this
E.showMessage("Hello Code"); function X(dict) { E.showMessage(dict); }
So do I have to convert the json to bytes?
Thanks for your patience
mike -
Hi Guys,
Thanks for your replies.
Fanoush, your approach is a little beyond my experience at this point but thank you.
Gordan , are (some, data) variables passed to the functon?
Does myapp.boot.js run in the background or do i need to to write an info file as described in your tutorials.
Sorry for the basic level of my response. -
-
Hi ,Thanks for that.
It displayed on the watch so, so far so good.
This is the shell output
Connecting...
Connected
Writing command
RX> bytearray(b"E.showMessage(\'Hello")
RX> bytearray(b"\',\'A")
RX> bytearray(b" Title\')\r\n")
Waiting for data
RX> bytearray(b'Uncaught ReferenceEr')
RX> bytearray(b'ror: "nE" is not def')
RX> bytearray(b'ined\r\n at line 1 col')
RX> bytearray(b" 36\r\n...ssage(\'Hello")
RX> bytearray(b"\',\'A Title\')/nE.show")
RX> bytearray(b"Message(\'Hello\',\'A T")
RX> bytearray(b"itle\')...\r\n ")
RX> bytearray(b' ')
RX> bytearray(b' ^\r\n>')
Done!
It doesnt look to clean. Is that down to how much data it can send?
Also do you now anything re UUIDNORDIC TX and RX?
Many Thanks -
As a follow on from a previous post( Espruino Hub and BLE) I am trying to connect a raspberrypi to the bangle and send from the pi a short message or two digit code.
I have researched the espruino ref https://www.espruino.com/Interfacing#bluetooth-le, and in so doing came across a ble wrapper called bleak.
This seems suited to my purpose but whilst it will connect and appear to send data it the data does not show on the bangle.
I also do not know what ref I would need for these to entries (UUID_NORDIC_TX = ""
UUID_NORDIC_RX = ""
If it helps this is the shell outputConnecting...
Connected
Writing command
RX> bytearray(b"E.showMessage(\'Hello")
RX> bytearray(b"\',\'A")
RX> bytearray(b" Title\')/n")
Waiting for data
Done!and this is the python code
e#!/usr/bin/env python3 import asyncio import array from bleak import discover from bleak import BleakClient address = "D8:AB:E0:B7:F5:AE" UUID_NORDIC_TX = "6e400002-b5a3-f393-e0a9-e50e24dcca9e" UUID_NORDIC_RX = "6e400003-b5a3-f393-e0a9-e50e24dcca9e" command = b"E.showMessage('Hello','A Title')/n" def uart_data_received(sender, data): print("RX> {0}".format(data)) # You can scan for devices with: [#async](https://forum.espruino.com/search/?q=%23async) 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]), True) c = c[20:] print("Waiting for data") await asyncio.sleep(1.0, loop=loop) # wait for a response print("Done!")
Any guidance appreciated.
-
-
I would like to transfer data from a raspberrypi TO my Banglejs.
The data would be a series of probable two digit codes which would describe various status indicators from an app running on the Pi.
The reason for using the watch is an easy way to receive alerts etc as it will be used outdoors in the countryside connecting to the pi over a local connection.
I am wondering ( having read the git posting) if mqtt is a method of achieving this and if there are any examples of this being done.
many thanks for any suggestions. -
-
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