Python or Javascript? and disconnect handle tutorial for python

Hello,

After I developed a python script for my project and I faced a lot of problem with connections. I cannot find any solution to reconnect in Python. do you guys have any tutorial? I have 3 questions...

  1. Is Javascript SDK better than Python using Warble? If so, Can I develop on windows machine?

  2. If not, how to handle disconnection on Python? e.g. I connected the sensor and let it run for 10 minutes streaming acc data. but after 6-7 minutes passed it's stopped streaming value and when I forced it to disconnect, it shows error "write_without_resp_async". Is there any callback function on when it's disconnected during streaming, so I can reconnect again?

  3. Is the sensor capable of turning on and stream for over 8-10 hours a day? If not how can I improvise it?

Thank you in advance,
Puckapao

Comments

  • edited January 27
    1. Is Javascript SDK better than Python using Warble? If so, Can I develop on windows machine?
      Javascript has a better and more reliable bluetooth library (unfortunately we do not write the ble libraries our API used (they are from 3rd parties) and the Python ble libs are not reliable - if you find bugs, please send the ble lib owners pull requests or file a bug).
      Yes, we have windows support (see our C# apis).
    2. If not, how to handle disconnection on Python? e.g. I connected the sensor and let it run for 10 minutes streaming acc data. but after 6-7 minutes passed it's stopped streaming value and when I forced it to disconnect, it shows error "write_without_resp_async". Is there any callback function on when it's disconnected during streaming, so I can reconnect again?
      You need to automatically reconnect if a conn fails or drops (at minimum).
      You can reconnect and start the stream again. You can also use our macro system (see tutorials).
    3. Is the sensor capable of turning on and stream for over 8-10 hours a day? If not how can I improvise it?
      Yes, the sensor can stream for 8 hours a day. Just use our streaming APIs and make sure the receive device is always on (for example, if your computer goes to sleep during the 8 hours, it could corrupt the stream link)
  • You need to automatically reconnect if a conn fails or drops (at minimum).
    You can reconnect and start the stream again. You can also use our macro system (see tutorials).

    Thank you for your answers. By the way I have looked into disconnect event in c++ api docs and found this and trying to implement in Python...
    libmetawear.mbl_mw_settings_get_disconnect_event(s.device.board)

    and I put callback in Sensor Class
    self.on_disconnect = FnVoid_VoidP_VoidP_Int(self.disconnect_handler)

    to call this function
    def disconnect_handler(self, ctx, value): self.disconnect = "in" self.test1 = self self.test2 = ctx self.test3 = parse_value(data)

    but it doesn't work. I tried finding tutorials or examples in Python but there's none.
    btw how to use markdown for code? I don't see it's working

  • Are you saying that you are not getting a disconnect callback?

  • yes, my function to check if it's called or not by changing self.disconnect to "in" but it's not changing.

  • Can you change your code to just print "disconnect" to the terminal on a disconnect event to see if it even gets the callback in the first place.

  • edited January 29

    I moved the sensor far away from PC over 10m and it showed this error instead of calling my on_disconnect callback. I tried another time, it stop the scripts instead of calling callback too.

    Code when I set sensor parameters
    libmetawear.mbl_mw_settings_set_connection_parameters(s.device.board, 7.5, 7.5, 0, 15000) libmetawear.mbl_mw_settings_get_disconnect_event(s.device.board)

    Code for my Sensor Class and callback
    `class Sensor:
    def init(self, device):
    self.device = device
    self.samples = 0
    self.voltage = 0.0
    self.charge = 0.0
    self.charging = 0
    self.callback = FnVoid_VoidP_DataP(self.data_handler)
    self.battery_callback = FnVoid_VoidP_DataP(self.battery_handler)
    self.charging_callback = FnVoid_VoidP_VoidP_Int(self.charging_handler)
    self.on_disconnect = FnVoid_VoidP_VoidP_Int(self.disconnect_handler)
    self.magnitude = 0.0
    self.disconnect = "on"

    def disconnect_handler(self, ctx, value):
        print("disconnect")`
    
  • edited January 30

    A few things:

    1. Does your script work on Python2.7?

    2. You did get a disconnect event because it shows "has been disconnected" in your terminal (try to find out where it comes from).

    3. Some relevant docs: https://mbientlab.com/cppdocs/latest/mblmwevent.html
      https://mbientlab.com/cppdocs/latest/settings.html?highlight=disconnect#handling-disconnects
      https://mbientlab.com/cppdocs/latest/btlecommunication.html?highlight=disconnect#disconnect-handler

  • @Laura said:
    A few things:

    1. Does your script work on Python2.7?

    2. You did get a disconnect event because it shows "has been disconnected" in your terminal (try to find out where it comes from).

    3. Some relevant docs: https://mbientlab.com/cppdocs/latest/mblmwevent.html
      https://mbientlab.com/cppdocs/latest/settings.html?highlight=disconnect#handling-disconnects
      https://mbientlab.com/cppdocs/latest/btlecommunication.html?highlight=disconnect#disconnect-handler

    1. Haven't tried with python2.7

    2. that "has been disconnect" is from this line of code.
      libmetawear.mbl_mw_debug_disconnect(s.device.board) print(d.address + " has been disconnected")
      What I want is to detect when the device is timeout disconnect from this line of code...

    libmetawear.mbl_mw_settings_set_connection_parameters(s.device.board, 7.5, 7.5, 0, 15000)

    1. I've looked into btlecommunication disconnect but I cannot do as api said. nothing coming in my on_disconnect event
  • Try Python 2.7
    Set a callback on libmetawear.mbl_mw_debug_disconnect

  • edited January 31

    but that won't be called when the board is lost connection? will it?

    https://mbientlab.com/documents/metawear/cpp/0/settings_8h.html#a1cf3cae052fe7981c26124340a41d66d

    mbl_mw_settings_set_connection_parameters()

    timeout Max time between data exchanges until the connection is considered to be lost, between [10, 32000]ms

  • edited February 1

    It's the same when I tried with python 2.7.14. When I moved sensor far away from Bluetooth until it's lost and it shows error like in the image instead of calling on_disconnect()

    What I want is to call on_disconnect() when the MetaMotionR connection is 'Lost', not disconnected by code.

    you can try my code here. https://drive.google.com/drive/folders/1dkmSVWHo7Js5sMTZVP81O8Obt3mirKaQ?usp=sharing

  • You won't get a disconnect event from the sensor if it's "suddenly" disconnected. Instead you will have to go up a layer into the BLE libraries to get the disconnect event from them.

  • edited February 3

    Thank you for information,
    By the way do you have examples for that? or how to catch that error?

Sign In or Register to comment.