Skip to content

13. Networking and communications

So, since our assignment this week was networking and communications, and for my Final project I needed a Bluetooth module, I decided to use Bluetooth to better understand the device in the future. So, I started the week by studying Neil’s lecture in detail, then I found Adrian’s assignment, he also used Bluetooth. And it was very usefull.

Bluetooth setup

So, to use Bluetooth, you first need to determine the code name as well as the bitrate. I tried ATtiny first but couldn’t get any result so I decided to do this part using Arduino.

The first 9 step is to upload the code to the Arduino and if it works, a “Start” message will appear on the serial monitor. The code did not work correctly because I used a different bitrate, as it turned out the previous user changed the bitrate. I set 38400 to 9600 and it worked correctly

After that you need to connect the Bluetooth module to the Arduino by holding down the button.

Here is a diagram of connecting Arduino to Bluetooth

If everything is done correctly, an OK message will appear on the serial monitor.

After that with these commands I request information

  • AT+NAME? - Name
  • AT+PSWD? - Password
  • AT+UART? - Communication speed.

And then you can change the name bitrate and password for your bluetooth I didn’t need this, I just changed the name for the test.

  • AT+NAME= your name - Name
  • AT+PSWD=”your code” - Password
  • AT+UART= Communication speed.

Code for setting up bluetooth using arduino.

#include <SoftwareSerial.h>

const int arduino_rx = 5;
const int arduino_tx = 6;
SoftwareSerial mySerial (arduino_rx, arduino_tx);

void setup()  {
  pinMode( arduino_rx,INPUT); pinMode( arduino_tx, OUTPUT);
  Serial.begin(9600);       //in the port monitor select NL&CR, Arduino speed with computer 9600
  mySerial.begin(9600);    //By default, the speed of the Bluetooth module with Arduino in AT command mode is 38400, sometimes 9600
  Serial.println("<<<Start!>>>");
  mySerial.println("AT");    
}

void loop() {
  if (mySerial.available()) {
    char c = mySerial.read();      // read Serial-po
    Serial.print(c);   }           // write Serial-port
  if (Serial.available()) {
    char c = Serial.read();        // read Serial-port
    mySerial.write(c); }           // write Serial-port
}

Control via mobile app

After setting the parameters, I tested what Adrian wrote, since this was my first acquaintance with the Bluetooth module. In this code you can see the bitrate that we read using setup code and wrote here so that the program works correctly Code for LED control.

//Fab Academy 2021 - Fab Lab León
//Bluetooth                          
//Adrianino
//ATtiny1614

const int led = 8; // LED pin

int option;

void setup(){
  Serial.begin(9600);  //speed of the communications
  pinMode(led, OUTPUT); 
}

void loop(){
  //if there is pending information
  if (Serial.available()>0){
    //let's read the option
    char option = Serial.read();
    //if the option is between '1' and '9'
    if (option >= '1' && option <= '9')
    {
      //we subtract the value '0' to obtain the number sent
      option -= '0';
      for(int i=0;i<option;i++){
         digitalWrite(led, HIGH);
         delay(100);
         digitalWrite(led, LOW);
         delay(200);
      }
    }
  }
}    
For this I used the board I designed in input week with an ATtiny 1614.

Connection is very simple. Gnd to Gnd, VCC to VCC, TX to RX and RX connect to TX .

Programmed using rp2040 with UPDI adapter .

I also downloaded a program for Bluetooth on my Android. I used 2 programs and both work here you can find links to APPs.

Arduino Bluetooth Controller

Bluetooth Terminal

And so After downloading the program, we connect to the bluetooth whose name was set on the Arduino and also enter the password.

And as you can see when I send a message 1 my slender LED flashes 1 time when 3 - 3 times when 6 - 6 times and so on from 1 to 9.

Message on LCD display

So based on Adrian’s code I modified the code for the LCD screen.

The most important difference in this code is that Adrian used a Char intended for a single character; whole words were not needed and therefore I rewrote the code a lot, but subsequently I wrote an array, with which I first created a buffer for characters and, using the command of the resulting line, read the line

Code for sending messages from phone to LCD via Bluetooth.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd( 0x27, 16 , 2 );
void setup(){
Serial.begin(9600);  //speed of the communications
pinMode(led, OUTPUT); 
  lcd.init();
lcd.setBacklight(255); // turn on backlight
}

void loop() {
  // Checking the presence of data on the Bluetooth serial port
  if (Serial.available()) {
       // Create a buffer to store the string
    char receivedString[32]; 

    int i = 0;
    while (Serial.available() ) {
      receivedString[i] = Serial.read(); // Reading the character
      i++;
    }
    receivedString[i] = '\0'; // Add a terminating null character


    lcd.clear(); 
    lcd.setCursor(0, 0); 
    lcd.print(receivedString); 

  }
}

Group Assignment

Conclusion

And this time I didn’t document my failures, there were some, but I forgot to take a photo. At first I wanted to use my ATtiny board to set up Bluetooth, but it didn’t work. I also had difficulties sending messages from my mobile phone to the LSD screen.I tried many different codes to read words since this is the first time. But in general, everything worked out