Tag: Firmata

If you managed to get to this part, you have seen how simple wired communication protocol between two Arduino’s is transformed into communication over Bluetooth using the Firmata protocol. Great!

Now we take a look, again, at a demo given at the Ignite 2015 in Australia. In this demo, an ultrasonic distance reader is used to generate a custom stream of measurement.

Although it’s a demo and it serves a purpose, the solution shown has an architectural flaw. If we look deeper into the Arduino code, we will find a ‘while (true) {}’. It is put inside a switch inside the ‘custom function handler’ sysexCallback().

Yes, this generates a constant stream of measurement but it also locks up the Firmata on the Arduino. No other call will de handle anymore because the endless loop is just blocking further communication.

In my previous blog, we investigated the usage of the standard Firmata protocol to communicate between UWP apps and an Arduino. I ended with directing to Channel 9 for more information about custom functions in Firmata.

But I think that’s not enough 🙂 First of all, that demo in the presentation failed. And next to that, it simply lacked any description on how to do it yourself.

I have put some time in figuring out this functionality. Again, the example below is based on the code of Part 4.

So we want to call some logic on the Arduino, using Firmata, and it has to be available for real-time usage. So we want to pass some parameters and we want to receive some response.

“Firmata is a protocol for communicating with microcontrollers from software on a computer (or smartphone/tablet, etc). The protocol can be implemented in firmware on any microcontroller architecture as well as software on any computer software package”

This sounds promising, doesn’t it?

Using Firmata, it should be possible to access the ports on the Arduino directly from a UWP app, without extra custom code needed on the Arduino.

Of course, we NEED code on the Arduino, but that’s just a standard sketch, which is available in the examples library inside the Arduino IDE.

I just downloaded the latest version (now 2.5.1) of the Firmata Arduino library from the GIT repository because it was not yet available in the IDE. That’s why it is just in another place in the menu. Select the StandardFirmata sketch:

The Firmata protocol supports a connection between the UWP app and the Arduino using USB, Bluetooth and Wifi. In this example, we will use Bluetooth. There is one caveat, the baud rate used to communicate can vary with the board you use (it’s all about quality). Initially, it’s 57600 baud. I changed it into 9600 baud, just to make sure the communication speed will not be an issue.

Upload the sketch. Plug in your Bluetooth module on port 0 and 1 (cross the RX and DX lines). That’s all we need to do on the Arduino.

Just to check if everything is working, Microsoft already provides a nice app. Download the Windows Remote Arduino Experience for free, just to test your Firmata sketch.

If your Arduino is connected to your PC using USB, first try to talk to it using the USB connection. Here I have selected the Bluetooth module (and I selected the correct baud rate, equal to the rate in the sketch):

Either way, USB or Bluetooth, the outcome should be the same. The (digital) pin layout should be shown:

Now activate output pin 13. This is the standard available LED op the Arduino board. This LED should be lit. And yes, this also works on my Windows Phone:

Note: I got this message “Pin configuration not received” the first times I tried to connect to my Arduino using Bluetooth:

If you get this message, just check the serial port connection (pin 0 and 1) and the baud rate in the sketch.

So now we will do the same communication in our own UWP app.

Start a new UWP template. Add the Bluetooth capability to the list of capabilities in the Package.appxmanifest.

To communicate to a Firmata device, we also need the NuGet package called “Windows-Remote-Arduino”:

First of all, we have to connect to the Bluetooth module. I added placeholder code for the ConnectionLost en ConnectionFailed events. Just so you can act on these circumstances (in case you get the name of the device wrong).

Note that the baud rate is set to 0 (zero). It just works 🙂

When the Bluetooth connection is up, the On button becomes available. Look at the code, we can write LOW or HIGH to digital ports. So we write to the output port 13. The LED on the board will be lit or unlit:

The Arduino class also supports reading and writing to analog ports. And I2C communication seems to be possible too.

Letting the LED blink several times, as in the previous blog post, is now trivial. It can be programmed just in C#.

The drawback of Firmata is that the real-time behavior of the Arduino is now limited by the speed of the calling UWP and the speed of the communication. And complex communication with exotic modules over several pins with specific timing will be hard or even impossible.

So if you need real-time interaction and still want to use Firmata, take a look at the Ignite presentation Windows IoT, UWP and the Remote Wiring API by Mitch Denny. He adds custom code to the Firmata sketch and executes it by name. Then he listens for the response which is received using an event handler. It acts just like a stored procedure in a database.

Update: The source code of the Windows Remote Arduino Experience app is available at GitHub.