kami
Feb 24, 2018 - 11:27
Hi,
i am trying to write a chat client with a arduino controller. For now i am connecting to JDY-08 and sending the command on\n in hex. This works and the Arduino answer with "LED_on" but how can i recieve this string in Python???
import cb
import struct
from pprint import pprint
class MyCentralManagerDelegate (object):
def __init__(self):
self.peripheral = None
def did_discover_peripheral(self, p):
print('+++ Discovered peripheral: %s (%s)' % (p.name, p.uuid))
if p.name and 'JDY-08' in p.name and not self.peripheral:
# Keep a reference to the peripheral, so it doesn't get garbage-collected:
self.peripheral = p
cb.connect_peripheral(self.peripheral)
def did_connect_peripheral(self, p):
print('*** Connected: %s' % p.name)
print('Discovering services...')
p.discover_services()
def did_fail_to_connect_peripheral(self, p, error):
print('Failed to connect')
def did_disconnect_peripheral(self, p, error):
print('Disconnected, error: %s' % (error,))
self.peripheral = None
def did_discover_services(self, p, error):
for s in p.services:
print s.uuid
if 'FE96' in s.uuid:
print('+++ Different Service found')
p.discover_characteristics(s)
elif 'FFE0' in s.uuid:
print('+++ Arduino found')
p.discover_characteristics(s)
def did_discover_characteristics(self, s, error):
if 'FFE0' in s.uuid:
pprint(s)
for c in s.characteristics:
pprint(c)
if 'FFE1' in c.uuid:
print('Enabling Arduino Cmd1...')
self.peripheral.write_characteristic_value(c, chr(0x6F), False)
self.peripheral.write_characteristic_value(c, chr(0x6E), False)
self.peripheral.write_characteristic_value(c, chr(0x0a), True)
elif 'AA01' in c.uuid:
print('Enabling temperature sensor notifications...')
self.peripheral.set_notify_value(c, True)
elif 'FFE3' in s.uuid:
print('Enabling notifications for Simple Key Service...')
def did_write_value(self, c, error):
print('Did enable Arduino 1')
def did_update_value(self, c, error):
print('update:')
pprint(c.value.encode('hex'))
if 'FFE1' == c.uuid:
print('Get value: %s' % c.value.encode('hex'))
else:
print('Nothing')
delegate = MyCentralManagerDelegate()
print('Scanning for peripherals...')
cb.set_central_delegate(delegate)
cb.scan_for_peripherals()
# Keep the connection alive until the 'Stop' button is pressed:
try:
while True: pass
except KeyboardInterrupt:
# Disconnect everything:
cb.reset()
Thanks a lot.
Cu kami