This Sketch is a slight modification of that published as a companion to the original General Bluetooth Serial App.

 

Links

 

clip_image012_thumb1
The EtherTen (Uno) and the Freetronics Bluetooth Shield The EtherTen (Uno) and the Sparkfun Bluetooth Shield

 

Device Configuration

This sketch targets an Arduino UNO (actually a Freetronics EtherTen). Using pins 2 and 3, (RX, TX) it receives characters over Bluetooth (serial) and adds then to a string. Reception completes with a null char or when MAXSTRING characters are received, The string is then sent back (echoed) over the serial Bluetooth stream. IAs it uses Pins 2 and 3, the Arduino SoftwareSerial Library is used. For development, the Freetronics Bluetooth Shield was used but other Arduino Bluetooth devices connected to pins 2 and 3 could be used. (The Freetronics one has jumpers to simplify selection). Note using Pins 2 and 3 leaves the Pin0/1 serial port free for development purposes.
NB: Pins 2&3 are set to Bluetooth TX and RX respectively which are RX and TX respectively from the Arduino perspective.

 

Freetronics Bluetooth Shield configuration

The UWP  Generic Bluetooth Serial App is used to test the device. This can be run on a Windows 10 desktop, Windows 10 Phone or Windows 10 IoT-Core device. The Arduino device needs to be paired with test app device. When the app runs it lists all Bluetooth paired devices (not necessarily connected) that support the BT Serial Profile. The target Arduino device is selected by double clicking it in that list. The app can then be set to automatically receive text from the Arduino device and can be used to send text to it (to be echoed back).

The Arduino device needs to be paired with the UWP app host outside of the UWP app. (??Like to see some code for doing that though??). With the Freetronics BT Shield, the Passkey is 1234. Some devices use 0000. It can simplify things, if possible, to remove the Passkey security on the Arduino end.

 

Further: Pairing & Connectivity

NB: In some contexts it has been found that the device doesn’t need to to be actually connected (but is pared) when the terminal app runs. In other contexts, you have to manually connect the Arduino device before it will show in the list in the terminal app.

 

The Sketch

As per usual, the code consists of declarations, the setup() method and the loop() method. For the uninitiated, the setup() runs once post boot or reset and teh Sketch then continuously loops by calling loop()..

 

The SoftwareSerial library is used as it implements a serial port in software (as there is no hardware serial port on pins 2 & 3, unlike pins 0 & 1). This library needs to be added to the Arduino development UI :

  • Menu->Sketch->Include Library->Manage Libraries
  • Scroll down to SoftwareSerial  and click on it then select Install.

image

 

#include <SoftwareSerial.h>
SoftwareSerial bt(2,3); // RX, TX

#define  MAXSTRING 65
int thisByte = 32;
char msg[MAXSTRING];
int cntr=0;

The Declarations

The serial port is configured (as discussed previously) to use pins 2 & 3 as RX and TX from the code perspective. MAXSTRING sets msg the bailout point for large receptions. The receiver reads bytes from the serial port, read into thisByte .cntr counts the number of bytes received in the current string

 

void setup() {
  Serial.begin(9600);
  bt.begin(9600);
  bt.println("Starting"); }

The setup( ) method

Note that there is nothing Bluetooth specific in the code. From that perspective it is only a serial port.

 

void loop() {

  cntr=0;
  thisByte = 32; //!=0
  msg[cntr] = 0;
  while ( (bt.available()) && (cntr< MAXSTRING-1) && (thisByte !=0))
  {
    thisByte = bt.read();
    if ((thisByte>0) && (thisByte !=255))
    {   msg[cntr++] = thisByte;
        msg[cntr] = 0; //Null terminated string
    }
  }
  if (cntr > 0)
  {
    bt.write(msg);
    Serial.println(msg);
  }
}

The loop( ) method

The app assumes that it receives null terminated strings and so reads bytes until a zero (null termination character) is received.As it progressively places each received byte into the next location in the char array, it writes a zero in to the next byte in the array so that the array is always a null terminated string. Once string has ben received it is echoed back to the terminal app.

 

NB: If the msg bailout point is reached, a null terminated truncated string is sent back, but the remainder is sent back in the next string reception.