Usage Example for Arduino-Bluetooth Serial Port-V2.0

Last revision 2025/12/29

This article offers a comprehensive guide to utilizing Arduino's Bluetooth serial port capabilities via the SIM800C Shield, detailing necessary hardware and software setup and providing code examples for smooth wireless communication.

Hardware Preparation

Software Preparation

Other Preparation

  • Preparation: The mobile terminal needs to download BluetoothSPP (only support Android, iOS system has not provided Bluetooth interface of below 4.0)
  • The jumper is connected to D2&D3 side, and the soft serial port is used for Bluetooth communication this time.

Code

 /***************************************************
     * SIM800C GPRS/GSM  Shield V2.0
     *
     ***************************************************
     * This example show you how to use SIM800C GPRS/GSM  Shield V2.0 BT-SPP.
     *
     * @author Dongzi
     * @version  V2.0
     * @date  2016-9-1
     * All above must be included in any redistribution
     * This code is tested on SIM800C GPRS/GSM Shield V2.0.
     ****************************************************/
    #include <SoftwareSerial.h>
    SoftwareSerial bt_cmd(2,3);

    void blue_spp();
    void splitString_t(char *data);
    #define power_key 12

    String cmd[6]={
      "AT","AT+BTHOST?","AT+BTPOWER=1","AT+BTPAIR=1,1","AT+BTACPT=1","AT+BTSPPSEND"};
    char buffer[50],power_mark=0;    // Serialbuffer and power flag

    void setup()
    {
      pinMode(power_key,OUTPUT);
      Serial.begin(9600);
      bt_cmd.begin(9600);
      blue_spp();
      delay(800);
      bt_cmd.println(cmd[4]);
      delay(800);
      bt_cmd.println(cmd[3]);
      delay(800);
    }
    void loop()
    {
      data_R1();
      data_R();
    }
    void data_R1()  {
      if(bt_cmd.available() > 0)
      {
        int index = 0;
        delay(100);             //wait data end
        int numChar = bt_cmd.available();
        if(numChar >50)
        {
          numChar =50;
        }
        while(numChar--)
        {
          buffer[index++] = bt_cmd.read();
        }
        splitString(buffer);
      }
    }
    void blue_spp()
    {
      bt_cmd.println(cmd[0]);
      delay(20);
      data_R1();
      delay(20);
      bt_cmd.println(cmd[2]);
      delay(200);
      bt_cmd.println(cmd[1]);
      data_R1();
      if(bt_cmd.find("ERROR"))
      {
        bt_cmd.println(cmd[1]);
        Serial.println("start again");
      }
      else
        Serial.println("bt_power_on");

    }

    void splitString(char *data)
    {
      Serial.print("Data entered:");
      Serial.println(data);
      for(int x = 0; x < 50; x++)
      {
        buffer[x] = '\0';
      }
    }
    void data_R()  {
      if(Serial.available() > 0)
      {
        int index = 0;
        delay(100);
        int numChar = Serial.available();
        if(numChar >50)
        {
          numChar =50;
        }
        while(numChar--)
        {
          buffer[index++] = Serial.read();
        }
        splitString_t(buffer);
      }
    }
    void splitString_t(char *data)
    {
      bt_cmd.println(cmd[5]);
      delay(5);
      bt_cmd.print(data);
      bt_cmd.write(0x1a);
      delay(5);
      for(int x = 0; x < 50; x++)
      {
        buffer[x] = '\0';
      }
    }

Result

  • This routine uses the soft serial port of arduino, and No. 2 and No. 3 digital pins are used as RX\TX of the serial port. After the program is burnt, “spp” Bluetooth serial port of the mobile terminal can realize wireless communication with serial assistant of the computer terminal through Bluetooth of the module.

Bluetooth1

Bluetooth2

Was this article helpful?

ON THIS PAGE

TOP