Example code for bluetooth


Hi everyone,
I was wondering if there are any examples of using XBEE bluetooth or other bluetooth shields with M2?
I am trying to compile some sample code, but they require SoftwareSerial library which doesn’t seem to be compatible with SAM architecture.



If your using the Macchina BT module it needs to be programmed. Once programmed you connect to it via the standard Serial interface. The XBee port I believe is Serial2 so you would talk to it the same as you would any other serial port. I have the ESP’s AT library installed on mine and I am able to talk to it via AT commands so I am not sure what your asking?

Please note that at this time the M2RET firmware does not take advantage of any port other than the USB port. At this time use of anything not supported directly my M2RET requires custom programming.


This is the sketch i’m using. I’m not getting any replies back from the bluetooth module.

String command = “”;
void setup ()
delay (3000);

Serial2.begin (9600);
delay (3000);
SerialUSB.println (“Type AT commands”);

void loop ()

if (Serial2.available ()) // receive data if available.
while (Serial2.available ()) // “keep receiving”.
delay (10); // Delay added to make thing stable
char c = Serial2.read (); // Conduct serial read
command += c; // Build the string.
SerialUSB.println (command);
command = “”; // No repeats
if (SerialUSB.available ())
delay (10);
char c = SerialUSB.read (); // Conduct serial read
command += c; // Build the string.

Serial2.println (command);
SerialUSB.print("sending command:");
SerialUSB.println (command);
command = ""; // No repeats



what do you mean? the BT module itself needs to be programmed or the M2 needs to be programmed to talk to BT module?


The ESP WiFi/BT module that macchina is selling has no firmware on it and needs to be programmed unless their release model has it.

I found it easier to use esp’s software package but you need a simple sketch to bridge the computer to esp stuff. There is a thread on here on how to get the ESP WiFi/BT module up and running.


I’m not sure if I’m following. I bought this https://www.amazon.com/gp/product/B073YJRSHS/ref=oh_aui_detailpage_o01_s00?ie=UTF8&psc=1
from amazon and put it as the picture shows on my M2:

now I’m sending AT commands (using the sketch above) to it and get nothing in return.
what am I missing?


You need to contact the manufacturer for more information. If it does not line up with the Macchina’s UART pins then the Serial2 stuff won’t work with it and there likely is nothing currently written to talk to it. First step is to find out what communications methods are available for it.

@josh or @ecsharpe may be able to also share information about the Macchina module that has been created for use with the M2 that might be a better solution. This one has both BT and WiFi if you can not figure out your module. I notice it is not listed in the store yet but I have one here along with my beta module.

As far as I am aware (and I have not done much with mine yet) the only currently supported (in software) connection is via the UART serial connection.

If it does SPI then there are libraries for that but would need custom software to make it work.

I will be taking on a major project this summer to try and make the M2 firmware more modular but I don’t have access to this hardware module. When I am done, adding your module to the supported software would be a simple matter assuming you can sort out the method it uses.



I just got my M2 bluetooth module, put it on my M2, powered it out and I can’t see it on my android to pair with. is there any documentation on the module? any code samples?


Check out the link I showed above. Unless Macchina has started including firmware in the chip you need to install what you want onto that module. There are threads on here on how to get them up and running. I have a Beta one here running now and will be trying to get the production one running that I also have in a couple weeks when I am done with Finals.

I believe everything you should need is here:

But there may be another thread if you don’t get it done with that thread.

Basically you need to install a passthrough sketch on the M2, grab some software from ESP to install firmware on the module and once done you are off and running. They have AT commands for everything on the library I pointed to above. It takes a little work to get up and running but once done there are MANY things it can do.

Hopefully Macchina will start installing the AT library above by default on them. Once it is up and running it really is as simple as sending an AT to the module to get it to respond.

Please note that this module will be one of the first things I will be working with to insure my new software will not care how it is connected. There are Bluetooth serial drivers out there as well as Ethernet serial drivers out there so I will likely be using one of those to test the unit with the firmware. In theory you should be able to connect to the M2 via USB, BT, Ethernet or any other way and be able to access it the same way. By end of summer you should be able to use SavvyCAN connected to your M2 in a variety of ways.