Week 14. Networking and Communications

14. Networking and Communications

Assignment

Group assignment: “Send a message between two projects (assignments made by different students)”
Individual assignment: “design, build, and connect wired or wireless node(s) with network or bus addresses”

Files

Arduino serial test code
Arduino serial Bluetooth communication code
Group assingment Node code
Group assignment Bridge code

Software

Arduino (IDE to write code for the ATTiny)
AVR-Dude (software that allows us to talk to the ATTiny board via the USBTiny)

Hardware

Texas Instruments Bluetooth 4.2 chip CC2541 provided via HM-10 board
Our ATTiny microcontroller board from week 7
USBTinyISP boarf of week 5
A UART interface/ Serial to USB convert

Group assignment “Send a message between two projects over Serial”

As a group we have several different microcontroller boards.
From week 7 we have 3 working boards with a LED and a button. And Rutger has a input board from week 11 that uses a Ultrasonic distance sensor.
We decided to connect the ultrasonic board to the other boards.
The ultrasonic board will be the master board, the others the slave.
We will use the distance from the ultrasonic sensor to determine which board needs to burn its LED:

  • if the distance is close we will burn the LED on node 1
  • if the distance is normal we will burn the LED on node 2
  • if the distance is far we will burn the LED on node 3 Each slave will listend for its own ID and turn its LED on when it receives its ID, and turn its LED off when it receives a 0.

How can multiple devices communicate over Serial?

To explain how we can have multiple devices communicate over Serial, we must first dive into what Serial is.
When I talk about Serial, I mean the RS-232 point-to-point communication standard.
It is a telecommunications protocol develop in the 60’s of last century, therefor it has a long history, and usage an specification changed a little bit over the years. For example, there were many connector types that used/or could be used with the RS-232 standard.
But since we are focussing on how to use Serial with microcontrollers, I will try to explain the most important basics we need to know to use it for our application.
Serial uses a transmit line (TX) and a receive line (RX) per device, further the devices need to share a common ground and communication needs to happen at the same voltage. (There are actually more dedicated ports as DTR and RTS, but for this example we do not really need them and for simplicity reasons we will not discuss them in this example.)
By default with Arduino’s we use 5V.
To connect two devices to eachother, we need to connect the TX of device 1 to the RX of device 2, and the TX of device 2 to the RX of device 1.
Basically the mouth of device 1 is connected to the ear of device 2, and the mouth of device 2 is connected to the ear of device 1. image showing the transmit and recieve lines connected to eachother(image by sebalabs.blogspot.com)

Now the devices can send data to eachother, but what data, and how do they know they have received everything?
We start by what they send. Over serial we send bits, ones and zeros by setting the line hig and low. Further to know when a byte is send, we start by sending a start bit, changing from High to low, then send our 8 bits that make a byte, and end with a parity bit and a end bit (low to high). When we send bytes, we start first with sending its Least Significant Bit, followed by its Most Significant Bit. (To be honest, this format is a convention and can differ per device, but typically this order is used.) More details over the serial communication protocol can be read here..

Last, to make sure each bit is registered, both devices need to transmit and receive at the same frequency. This is called baudrate, and it determines how many bits per second are send over the lines. Both devices need to communicate at the same baudrate, otherwhise, they will not be able to properly determine what is send.

How about 3 microcontrollers

Now when we have connected our two devices to eachother, set them at the same baudrate, they can send and receive data to eachother using the Serial communication lib from Arduino or the SoftwareSerial lib.
But how about 3 or more devices?
For this we need to slightly modify our structure and instroduce a new structure in the data itself.
First I would advice to make one microcontroller the bridge node, this node talks to all other nodes and receives data from all other nodes.
The other nodes are the slaves from this head node.
Connect the TX of the bridge node to the RX of all the slave nodes, and the RX of the bridge to the TX of all the slave nodes.

Schematic overview of connecting multiple Arduino's over serial (credits http://cool-emerald.blogspot.com)

Now when the bridge sends some data all the slave nodes receive this data, en when a slave node sends data the bridge node receives this data.
Tip with connecting the devices, connect them all with the FTDI connection cable in the same way, but in the bridge node switch the TX and RX pins using the software serial library. The example of this can be seen in the code provided below.

Also in this code you find a new data structure. To be able to distinquis per slave if a message is intended for this slave, we give each slave a number we call SALVEID.
In the slave Arduino code we listen on the Serial if the specific SALVEID is send by the bridge. If so we do something, like truning an LED on. If we reveive a SLAVEID of a different slave we do nothing. Important is that the slave does not continously sends data on the transmit line, since all slaves are connected on the transmit line. We can prevent this by for example alowing the slave to only transmit when receiving a datarequest from the bridge, or transmit a random time, or only transmit when it receives its id. If you want to you can further expand this structure, by implementing a full data protocol with identifiers, but for this example just knowing that a slave has received a value and identified the value was enough for our first test.
[In week 17 I actually expend on these identifiers and define a protocol allowing the bridge to controll 20 servo’s using 4 different Arduino pro mini’s as slave.] (http://fab.academany.org/2019/labs/waag/students/josephus-vanderbie//week17.html)

For this group test we implemented as a group this concept of bridge with slaves with different slave ID’s. We succesfully connected a bridge to two nodes.
We had to improvice a special FTDI cable that allowed us to connect all the devices together.
The bridge sends first a 0, allowing the nodes to turn off their LEDS.
Next it sends a 1, turning on node 1.
After the 1 it sends a 2, turning on node 2.
Then is restarts. In between each send it waits 2 seconds.
When we look at the video we see this behavior by the nodes.
The first node is programmed by Micky and Anne, the second node by me and Rutger.

My example code of my slave node with SLAVEID 1:

/*
*   Group Sofware Serial slave LED code
*   This code reads incomming data and sends it back over the Software serial line (if needed).
*   Next it turns the internal LED on when the received data is its ID.
*   It turns the LED off when the received data is 0.
*   This code is based on the work of Cody Bradly at http://www.ernstc.dk/arduino/tinycom.html
*   and on the SoftwareSerialExample Tom Igoe
*   
*   Date 2019-04-25
*   Author Joey van der Bie
*   
*/
#include <SoftwareSerial.h>
const int RX = 0;
const int TX = 1;
const int SLAVEID = 1;

SoftwareSerial mySerial(RX, TX);

int received = 0;

const int LED_PIN = 7;
void setup() {
  pinMode(RX, INPUT);
  pinMode(TX, OUTPUT);
  mySerial.begin(9600);

  // declare the LED pin as an OUTPUT:
  pinMode(LED_PIN, OUTPUT);
}

void loop() {
  if ( mySerial.available() ) {
    received =  mySerial.parseInt();
//    mySerial.println(received);
    
    if(received == SLAVEID){
      //turn LED on
        digitalWrite(LED_PIN, HIGH);
    }
    
    if(received == 0){
      // turn LED off
        digitalWrite(LED_PIN, LOW);
    }
  } 
}

Our master code looks like this

/*
    Group Sofware Serial Bridge LED code
    Crosses RX and TX compared to nodes.
    Starts by resetting all IDS by sending 0.
    Sends the ID over the serial port, waits two seconds, second the second ID.
    This code is based on the work of Cody Bradly at http://www.ernstc.dk/arduino/tinycom.html
    and on the SoftwareSerialExample Tom Igoe

    Date 2019-04-25
    Author Joey van der Bie

*/
#include <SoftwareSerial.h>
const int RX = 1;
const int TX = 0;

SoftwareSerial mySerial(RX, TX);

int received = 0;

const int LED_PIN = 7;
void setup() {
  pinMode(RX, INPUT);
  pinMode(TX, OUTPUT);
  mySerial.begin(9600);

  // declare the LED pin as an OUTPUT:
  pinMode(LED_PIN, OUTPUT);
}

void loop() {
  //reset all lights
  digitalWrite(LED_PIN, HIGH);
  delay(50);
  digitalWrite(LED_PIN, LOW);
  mySerial.print(0);
  delay(2000);
  
  //turn on light 1
  digitalWrite(LED_PIN, HIGH);
  delay(50);
  digitalWrite(LED_PIN, LOW);
  mySerial.print(1);
  delay(2000);

  //turn on light 2
  digitalWrite(LED_PIN, HIGH);
  delay(50);
  digitalWrite(LED_PIN, LOW);
  mySerial.print(2);
  delay(2000);
}

Using Bluetooth to controll a microcontroller board

In this exercise I am going to connect a Bluetooth board to my ATTiny and controll the LED on the ATTiny via the Bluetooth board using my smartphone.

What is Bluetooth

Bluetooth is a wireless data protocol desinged to be used on short distances like for a Personal Area Network.
It works on the 2,4 GHz bandwave and has a datarate of up to 2.1 Mbit/s.
It is very energy efficient and can connect to multiple devices at the same time.
With the introduction of Bluetooth 4/LE/Smart we have a large library of defined services that products can utilize. These services are presented in the Generic Attribute Profile (GATT). For example a heart rate sensor has its own GATT profile. By predefining all these services your smart device can connect to this device and directly use it without having to install special software.
Bluetooth is currently widely available in our devices as smartphones, laptops and tablets making it also a cheap and versatile product to use.

The Bluetooth board

For this assignment we are going to use the Texas Instruments Bluetooth 4.2 chip CC2541.
The datasheet of the chip can be found here
Our chip is fixed on a board that provides an easy pin layout for Serial communication and allows us to connect it using 5V as voltage. The board design is called a HM-10 board.. There is a special guide for this type of board made clearly describing different commands that can be used. As we can read in the datasheet the Serial communication is provided in the chip:

"The debug interface implements a proprietary two-wire serial interface that is used for in-circuit debugging.
Through this debug interface, it is possible to erase or program the entire flash memory, control which oscillators
are enabled, stop and start execution of the user program, execute instructions on the 8051 core, set code
breakpoints, and single-step through instructions in the code. Using these techniques, it is possible to perform incircuit debugging and external flash programming elegantly."

Testing if the board works

To test if the Bluetooth board works, we are going to connected it to the computer over Serial and to the smartphone over Bluetooth and trie to send data from the computer to smartphone and from the smartphone to the computer.
The break-out bord attached to the Bluetooth chip provides a nice layout to connect our Serial interface and provide up to 6V power. However the comunication over Serial happens on 3.3V.
Connected the Bluetooth board to the computer using the Serial to USB converter. Connect RX of the board to the TX of the Serial converter, and the TX of the board to the RX of the Serial converter.
If your Serial converter does not support 3.3V over the RX and TX pins use a voltage converter over at least the TX of your Serial converter.

A voltage converter converting 5V to 3.3V

Connect RX of the board to the TX of the Serial converter, and the TX of the board to the RX of the Serial converter.

After connecting the Bluetooth module to the computer, install a Bluetooth terminal app on the smartphone.
There are multiple available, like this one for Android, I used this one for my Iphone . These Bluetooth terminal apps allow you to connect to your Bluetooth device, scan for its services and send and receive data from the device.
Pair your phone/app with the Bluetooth board and selected the service FFE1.
Start the Arduino IDE, selected the UART device in Tools-> Port, and launch the Serial Monitor.
Type something in the Serial Monitor and send it over the UART device, the Bluetooth module will send it to the phone and the app will display the data.
In the image we see the text “Goede morgen” translated to 8029109330519617351 in the app.
It also works the other way around, when you type someting in the terminal app and send it to the Bluetooth module, it is send to the Serial monitor.

Serial communication shown in the iPhone Bluetooth terminal iPhone app
Serial communincation shown in the Serial Minitor on my computer

Bluetooth board and microcontroller

Having confirmed that the Bluetooth board works, we can do something more interesting with it: connect the board to a ATTiny board.
Before connecting the Bluetooth board, create code for the ATTiny to interact with the board first.
Start by creating a basic Software Serial communication program to test if the serial communication on the board works.

/*
*   Sofware Serial test code
*   This code reads incomming data and sends it back over the Software serial line.
*/
#include <SoftwareSerial.h>
const int RX = 0;
const int TX = 1;

SoftwareSerial mySerial(RX, TX);

void setup() {
  pinMode(RX, INPUT);
  pinMode(TX, OUTPUT);
  mySerial.begin(9600);
}

void loop() {
  if ( mySerial.available() ) {
    mySerial.print( mySerial.read() );
  } else {
    mySerial.println( "0" );
  }
  delay(1000);
}

Somehow the Serial communication on my ATTiny 3.3V board with vibration motor does not work.
I switched the board for my ATTiny 5V board and powered the board using the 3.3V of my UART device.
Using the 3.3V as input power converts my 5V board to a 3.3V board, alsom making the TX pin having a maximu voltage of 3.3V. When testing the ATTiny board the Serial communication test now works perfectly.
Since I eventually am going to create a new board anyway, I let the problems with my ATTiny vibration motor board rest for now.

Now modify the testcode to turn the LED on the ATTiny board on when it receives a 1, and off when it receives some other data.

/*
*   Sofware Serial test code for Bluetooth module
*   This code reads incomming data and sends it back over the Software serial line.
*   Next it turns the internal LED on when the received data is 1.
*   It turns the LED off when the received data is not 1.
*   This code is based on the work of Cody Bradly at http://www.ernstc.dk/arduino/tinycom.html
*   and on the SoftwareSerialExample Tom Igoe
*   
*   Date 2019-04-24
*   Author Joey van der Bie
*   
*/
#include <SoftwareSerial.h>
const int RX = 0;
const int TX = 1;

SoftwareSerial mySerial(RX, TX);

int received = 0;

const int LED_PIN = 7;
void setup() {
  pinMode(RX, INPUT);
  pinMode(TX, OUTPUT);
  mySerial.begin(9600);

  // declare the LED pin as an OUTPUT:
  pinMode(LED_PIN, OUTPUT);
}

void loop() {
  if ( mySerial.available() ) {
    received =  mySerial.parseInt();
    mySerial.println(received);
    
    if(received == 1){
      //turn LED on
        digitalWrite(LED_PIN, HIGH);
    }else {
      // turn LED off
        digitalWrite(LED_PIN, LOW);
    }
  } 
}

Test this on your computer using the Serial monitor. And confirm it works. Next connected the Serial interface of the Bluetooth board to the ATTiny board, as you would normally connect your board to the UART device.

connect the Serial interface of the Bluetooth board to the ATTiny

Connect the Bluetooth board via the smartphone, selected the FFE1 service and send a 1 in ASCII.
Check if the LED turns on. In my case the LED turned briefly on and then off.
I also received some data back on my phone.
Not only the 1 as a response, but also a different number.

A 1 and line break as response

Apparently the app I used also sends a Line Break after the message is send.
I turned off the Line Break option in the settings and tried again. This worked!
Having confirmed that when we send a 1 the LED turns on and when we send something else the LED turns off,
we can now conclude we can controll our microcrontoller over Bluetooth using our smarpthone.

A 1 as response

LED turned ON at ATTiny with Bluetooth module

Fails

No RX at my 3.3V ATTiny board

Before connecting the Bluetooth board, I created code for the ATTiny 3.3V to interact with the board first.
I started by creating a basic Software Serial communication program to test if the serial communication on the board works.

/*
*   Sofware Serial test code
*   This code reads incomming data and sends it back over the Software serial line.
*/
#include <SoftwareSerial.h>
const int RX = 0;
const int TX = 1;

SoftwareSerial mySerial(RX, TX);

void setup() {
  pinMode(RX, INPUT);
  pinMode(TX, OUTPUT);
  mySerial.begin(9600);
}

void loop() {
  if ( mySerial.available() ) {
    mySerial.print( mySerial.read() );
  } else {
    mySerial.println( "0" );
  }
  delay(1000);
}

Somehow the Serial communication on my ATTiny 3.3V board with vibration motor does not work.
I switched the board for my ATTiny 5V board and powered the board using the 3.3V of my UART device.
Using the 3.3V as input power converts my 5V board to a 3.3V board, alsom making the TX pin having a maximu voltage of 3.3V. When testing the ATTiny board the Serial communication test now works perfectly.
Since I eventually am going to create a new board anyway, I let the problems with my ATTiny vibration motor board rest for now.

Destroyed Rutgers FTDI header

For the group assignment we had to improvice a special FTDI cable that allowed us to connect all the devices together.
The connecters weren’t perfect for the devices, resulting in me breaking off Rutger his connectors and coper traces.
I had to solder in new connectors on a different location and bridge the GND pin with a wire.

Reflection




Share this story