omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    CB recieve RX-String from Arduino Serial

    Pythonista
    2
    3
    2264
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • kami
      kami last edited by

      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

      1 Reply Last reply Reply Quote 0
      • kami
        kami last edited by

        Hi,

        anyone here how can help me with the recieving problem? Thanks a lot.

        Cu kami

        1 Reply Last reply Reply Quote 0
        • brumm
          brumm last edited by

          I‘m not sure, but maybe you first need a

          Peripheral.read_characteristic_value(characteristic)
          

          „When the value has been read, your central delegate will receive a did_update_value callback, at which point you can access the value with the characteristics’s value attribute. “

          1 Reply Last reply Reply Quote 0
          • First post
            Last post
          Powered by NodeBB Forums | Contributors