for loop: connect/do something/disconnect

Ask, and ye shall receive.
papa
Posts: 2
Joined: 01:21, 04 Nov 2015

for loop: connect/do something/disconnect

Postby papa » 01:36, 04 Nov 2015

Hi,

I want to get data from several (generally unknown number) of rfduinos.
Found this thread on the forum: viewtopic.php?t=1473#p3838
According to the answer:
another point to note - you can only have a single device connected at any time; if you want to connect to both, you'll need to build some fancy logic to connect/do something/disconnect and move along to another device. great if you want to cycle through a few devices and collect information from them


I struggled for several days to make it work.
Results:
1. For some reason can only read data from one device most of the time;
2. If I have N devices on the list - the app reads data from the device N times (not one time from each device as I want to);
3. Sometimes the reading suddenly switches (I have the loop executed each second) to another device (this behaviour very rare - only saw once).
4. Getting connect error: "null" - this happens often.

Please help with functional code.

Details of my buggy code are below:

Code:

Code: Select all

for (var i = 0; i < app.activeDevices.length; i++)
      {      
         app.connect(app.activeDevices[i]);
         app.measure(app.device);                  
      }

  • activeDevices - stores names for devices to connect to - it populates correctly;
  • connect(deviceName) is function from rfduino LED On/Off example (includes rfduinoble.close() in the beginning)
  • measure(device) - is my function that makes graphical display of the data - works properly for single device.

Code: Select all

connect: function(deviceName)
   {
      rfduinoble.close();      
      //app.showMessage("Connecting...");
      setTimeout(function()
      {         
         rfduinoble.connect(
            deviceName,
            function(device)
            {
               //app.showMessage(deviceName + " connected");
               app.device = device;
            },
            function(errorCode)
            {
               app.showMessage("Connect error: " + errorCode);
            });
      },
      500);
   },

Fredrik
Site Admin
Posts: 196
Joined: 15:00, 18 Nov 2013

Re: for loop: connect/do something/disconnect

Postby Fredrik » 10:06, 04 Nov 2015

app.connect() is asynchronous, such that app.device will not have been set by the time app.measure is called.

Also, calling connect() several times in a for loop will cause lots of trouble, since a second/third/... successful connect will overwrite the app.device from the previous connect.

You will want to somehow wait until the measurement is complete before connecting to the next device. Bear in mind that app.measure() is also asynchronous.

papa
Posts: 2
Joined: 01:21, 04 Nov 2015

Re: for loop: connect/do something/disconnect

Postby papa » 23:13, 04 Nov 2015

Thank you for your response.
This makes sense to me.
I noticed that timing matters a lot in the script - adding delays in different locations changes behaviour of the program (getting more errors most of the times).
I also found that app.activeDevices[i] changes with i while app.device.name stays the same.

Could you recommend proper time to wait and where to put the delays? Or a way to find this out?

Fredrik
Site Admin
Posts: 196
Joined: 15:00, 18 Nov 2013

Re: for loop: connect/do something/disconnect

Postby Fredrik » 09:58, 06 Nov 2015

Delays are not the proper way to approach asynchronous programming. Never use setTimeout().

Instead, you must put your code inside the callbacks from functions like rfduinoble.connect().


Return to “Questions and answers”

Who is online

Users browsing this forum: No registered users and 17 guests