Simplified Bluetooth and Serial Communication Example Sketch


#1

Hi folks,

I digged a bit into the way, MeBluetooth can be used to communicate with the Makeblock kits. Using the example sketch “SlaveBluetoothBySoftSerialTest.ino” which is part of the Makeblock library Package, I found the code complicated, as it relies on the good old C tradition of handling serial communication by reading them into char arrays. Nothing wrong with C syntac, but personally I am much more influenced by the modern scripting languages than by C, so I prefer to use the Arduino “String” functionality, instead.

Here is my solution. It is actually derived from the makeblock example sketch, so you can easily compare the two and decide what you prefer.


/**
 * \par Copyright (C), 2016: This Arduino sketch is provided under GPL-V3
 * @file    BT_Serial_Test_Using_String.ino
 * @author  Stefan Rothe
 * @version V1.0.0
 * @date    2016/02/12
 * @brief   Description: this sketch demonstrates the usage of MeBluetooth
 *          and SoftwareSerial with using the Arduino "String" functionality.
 *          Using the String functionality avoids the need of using arrays
 *          to read data, which may be easier for most not-so-experienced programmers.
 *          To test the functionality, upload the sketch, open a Bluetooth Monitor 
 *          on a mobile device, and Serial Monitor within the Arduino IDE. 
 *          Then type some nice words in either device and send them abroad.
 *          
 *
 * \par History:
 * <pre>
 * <Author>     <Time>        <Version>      <Descr>
 * Stefan Rothe 2016/02/12    1.0.0          BT_to_serial_comms using String instead of char[]
 */
 
#include "MeOrion.h"
#include <SoftwareSerial.h>

MeBluetooth bluetooth(PORT_5);

String btIn = "";

void setup()
{
  Serial.begin(115200);
  bluetooth.begin(115200);    //The factory default baud rate is 115200
  Serial.println("Bluetooth Start!");
}

void loop()
{ 
  int readBt = 0,i = 0,count = 0;
  String btOut;
  if (bluetooth.available())
  {
    while((readBt = bluetooth.read()) != (int)-1) //while there are characters to read...
    {
      btIn.concat(char(readBt)); //append them to the String btIn
      delay(2); //to avoid hickups
    }
    Serial.println(btIn);  //print the string to Arduino IDE Serial Monitor
    btIn = "";             //and clear the variable
  }
  if(Serial.available() )
  {
    btOut = Serial.read(); //read from serial
    bluetooth.println(btOut); //and write to Bluetooth
  }
}

The sketch provides two-way comms between the MeBluetooth module and the serial monitor of the Arduino IDE. In a real sketch you may want to log data through a BT monitor on your mobile device, or send commands from your mobile to the bot to trigger action.

Hope this makes it easier to deal with self-made BT comms. Any comments welcome.

Cheers, Stefan


#2

Hi Stefan,

thanks for this Sketch. Works well after:

  • I’ve removed the ME Bluetooth module from ORION board !!!

Connecting via bluetoth to Arduino IDE serial monitor.
Connecting via USB to terminal (coolterm).

Ever tried connecting from ios (iPhone) ???

Regards
Marcel


#3