Skip to content

14. Networking and communications

This week I experimented with simple networking.

Bluetooth

I decided to use an HC-05 Bluetooth module, and set it up so I can communicate with it using my phone.

AT Commands

In order to use the HC-05 module, it should first be configured using AT Commands, the list is below.

Command Function
AT+RMAAD Clears previously paired devices
AT+ROLE Sets it as slave 0 or master 1
AT+ADDR Gets the address of the module
AT+UART Sets the baud rate
AT+PSWD Changes the password of module

After each command add a “?” or to obtain/change the value

To actually enter the AT commands, I uploaded this program to the board, it will send the commands to the module (pins 10 and 11) and print its response back in the regular serial port. The pins were connected like this:

  • VCC (3.3V in Arduino) —> VCC

  • Using 5V will probably damage the module, but it worked fine for me…

  • GND —> GND (It should be disconnected when uploading the sketch)

  • RX —> PA2 (pin 5 in ATTiny412)
  • TX —> PA3 (pin 7 in ATTiny412)
  • EN —> PA7 (pin 3 in ATTiny412)
/*

AUTHOR: Hazim Bitar (techbitar)
DATE: Aug 29, 2013
LICENSE: Public domain (use at your own risk)
CONTACT: techbitar at gmail dot com (techbitar.com)

*/
#include <SoftwareSerial.h>

SoftwareSerial BTSerial(A2, A3); // RX | TX

void setup()
{
  pinMode(PA7, OUTPUT);  // this pin will pull the HC-05 pin 34 (key pin) HIGH to switch module to AT mode
  digitalWrite(PA7, HIGH);
  Serial.begin(9600);
  Serial.println("Enter AT commands:");
  BTSerial.begin(38400);  // HC-05 default speed in AT command more
}

void loop()
{
  // Keep reading from HC-05 and send to Arduino Serial Monitor
  if (BTSerial.available())
    Serial.write(BTSerial.read());

  // Keep reading from Arduino Serial Monitor and send to HC-05
  if (Serial.available())
    BTSerial.write(Serial.read());
}

Using the serial monitor, with Both NL & CR selected, I entered the commands and this was the result:

Main program

I then cross-connected the TX and RX pins of the board and HC-05 as before to communicate.

// Max GH 18.05.2022
#include <SoftwareSerial.h>

SoftwareSerial BTSerial(A2, A3);
String incomingString;   // for incoming serial data

void setup() {
        Serial.begin(38400);     // serial communication begins
        BTSerial.begin(38400);
}

void loop() {

        // send data only when you receive data:
        if (BTSerial.available() > 0) {
                // read the incoming byte
                incomingString = BTSerial.readString();

                // printing using regular Serial pins
                Serial.print("I received: ");
                Serial.println(incomingString);

                // sending it back to the app
                BTSerial.print("I received: ");
                BTSerial.println(incomingString);
        }
}

Sending/receiving data

I used Arduino bluetooth controler for android, which allows me to send data through bluetooth. The paired devices will show up in the main menu, selecting MAX_BT, I entered the Terminal mode, which essentially just sends strings.


I opened the Serial monitor in my computer, and then I send a short text from the phone terminal.


I just received some randome characters, this was because the baud rate was 9600 and not 38400 as I had specified in the program, I changed it.

And sent another message.

And it now made sense!

A good tutorial about HC-05

All AT commands


Last update: May 18, 2022