how to send print from Arduino to client app thru BLE?

Ask, and ye shall receive.
internetizen
Posts: 14
Joined: 12:00, 15 Oct 2014

how to send print from Arduino to client app thru BLE?

Postby internetizen » 09:36, 25 Oct 2014

Hi All,

Sorry for asking a very newbie question...

I could successfully connect my Arduino BLE to my client app and tested out the examples.

My arduino sketch can do Serial.print to serial monitor. How can I print my output to my client app through BLE? Also, how can I refresh my output on the same line?

Thanks

internetizen
Posts: 14
Joined: 12:00, 15 Oct 2014

Re: how to send print from Arduino to client app thru BLE?

Postby internetizen » 05:57, 26 Oct 2014

Is there any examples out that? Thanks!

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

Re: how to send print from Arduino to client app thru BLE?

Postby Fredrik » 11:25, 26 Oct 2014

The BLE Arduino library we use have functions ble_read() and ble_write_bytes(). See the example "arduinoble".

If you know a bit of C++, you could wrap those in a new class that inherits from Print, thus allowing you to call something like "ble.println()".

internetizen
Posts: 14
Joined: 12:00, 15 Oct 2014

Re: how to send print from Arduino to client app thru BLE?

Postby internetizen » 01:44, 27 Oct 2014

unfortunately, I am not familiar with java and C++... I think I can modify some codes, but I really don't know how to create a new one from scratch... I hope some experts out there can show me one or two examples how to do the javascript and how to call it in the index.html. Thanks!

internetizen
Posts: 14
Joined: 12:00, 15 Oct 2014

Re: how to send print from Arduino to client app thru BLE?

Postby internetizen » 07:24, 28 Oct 2014

I can successfully use ble_write_bytes() to pass my Arduino data. How can I pass this data to my Javascript function such that I can call this function from my index.html?

User avatar
micke
Posts: 256
Joined: 20:49, 18 Nov 2013

Re: how to send print from Arduino to client app thru BLE?

Postby micke » 19:13, 28 Oct 2014

Hi,

It is a bit complex to pass data back from Arduino to JavaScript.

To learn more I suggest you run and explore the example "Arduino BLE":

http://evothings.com/doc/examples/arduino-ble.html

It has code for doing this. Then you can adopt the example to work with your own code.

We plan doing a high-level JavaScript library that will make this much easier.

Let me know how it goes!

Best regards, Mikael

internetizen
Posts: 14
Joined: 12:00, 15 Oct 2014

Re: how to send print from Arduino to client app thru BLE?

Postby internetizen » 20:34, 28 Oct 2014

Hi Mick,

In fact, I am using the the example "Arduino BLE" and trying to modify it.
JavaScript is totally new for me... I know C, but don't know anything about JavaScript and C++.
My added ble_write_bytes() is totally messed with the one for "canvas." I am trying to add one more data coming from Arduino below the canvas in the index.html.

Sorry for asking this kind of basis thing...

Can you please tell me which lines in your app.js are doing data passing from Arduino to index.html?
I have been studying for 5 days, still can't figure it out...

Thanks a lot!

User avatar
micke
Posts: 256
Joined: 20:49, 18 Nov 2013

Re: how to send print from Arduino to client app thru BLE?

Postby micke » 10:30, 29 Oct 2014

Hi,

The actual data is sent from the Arduino code, in the arduino.ble file.

Look in the loop function, here are the lines that send data to the mobile phone:

Code: Select all

   // Read the analog input pin and send the data over BLE.
   short i = analogRead(INPUT_PIN);
   ble_write_bytes((byte*)&i, 2);


In app.js the startReading function enables a notification that will read the data sent from the Arduino code. Here is the code that defines the function called when data is received:

Code: Select all

      // Start reading notifications.
      evothings.ble.enableNotification(
         deviceHandle,
         app.characteristicRead,
         function(data)
         {
            app.drawLines([new DataView(data).getUint16(0, true)]);
         },
         function(errorCode)
         {
            console.log('enableNotification error: ' + errorCode);
         });


The evothings.ble.enableNotification takes four parameters:

  • A device handle (this is the Arduino)
  • A handle to the characteristic to enable
  • A function called repeatedly as soon as new data is sent from the Arduino
  • An error function called if something goes wrong

The functions passed to evothings.ble.enableNotification are not evaluated at the time of the actual call to evothings.ble.enableNotification, but later, when data is available or there is an error. Such functions are called "callback functions". Notice that in this case, the callback functions have not name, they are "anonymous functions" also known as "closures". This is a very common programming technique in JavaScript.

It takes some time to learn all this, so keep on learning and reading. The Arduino BLE is a fairly complex example, so you should not be surprised that it takes time to understand what is going on there.

Best, Mikael

juan92
Posts: 2
Joined: 23:46, 15 Nov 2014

Re: how to send print from Arduino to client app thru BLE?

Postby juan92 » 01:14, 16 Nov 2014

micke wrote:Hi,

The functions passed to evothings.ble.enableNotification are not evaluated at the time of the actual call to evothings.ble.enableNotification, but later, when data is available or there is an error. Such functions are called "callback functions". Notice that in this case, the callback functions have not name, they are "anonymous functions" also known as "closures". This is a very common programming technique in JavaScript.

It takes some time to learn all this, so keep on learning and reading. The Arduino BLE is a fairly complex example, so you should not be surprised that it takes time to understand what is going on there.

Best, Mikael


Is there any way to process the data instantaneously? I mean if DataArray!="value" do something. I tried it and I modified the example but it takes 7 seconds since the value changes in Arduino Serial Monitor.

Thanks!
By the way, You did and you are doing a great job!!

User avatar
micke
Posts: 256
Joined: 20:49, 18 Nov 2013

Re: how to send print from Arduino to client app thru BLE?

Postby micke » 13:42, 21 Nov 2014

Hi Juan, thanks for the feedback!

I wonder what causes the 7 second delay?

Is your setup similar to that of the Arduino BLE example? How is performance of the Arduino BLE example on your device? Would be interesting to see if there is a difference.

Regards, Mikael


Return to “Questions and answers”

Who is online

Users browsing this forum: No registered users and 1 guest