15. Networking and communications

Group assignment:

  • Send a message between two projects

Individual assignment:

  • Design, build, and connect wired or wireless node(s) with network or bus addresses

Group assignment

Our group assignment can be found here.

Individual assignment

Testing with Arduino Uno

Earlier on this course we have had one host and a project and TX and RX going in between them. This week in addition to host computer/board we need number of nodes. They are all connected the way presented in the next picture (from Neil’s lecture)

The host thinks it is just talking to TX and RX but the lines are shared. One TX goes out to each node but the RX is shared so there would be a “fight” if multiple devices try to talk at the same time. To fix this, each node is going to be initially in tri-state X (disconnected) which means it is listening to the pin, not talking to it.

Each node is going to have an ID hardcoded (it’s network address). The node waits to get a character and if maches its address, the listening mode pin is set as an output to put out a message and flash the LED and then put back to listening the pin.

Since I only have one Arduino board at home I have to use that for the assignment. The same principle can be used for multiple boards by giving them a different network address. I don’t have a FTDI-USB cable so I have to test my node with USB connection.

The Arduino hardware has built-in support for serial communication on pins 0 (RX) and 1 (TX) (which also goes to the computer via the USB connection). The native serial support happens via a piece of hardware (built into the chip) called a UART. This hardware allows the Atmega chip to receive serial communication even while working on other tasks, as long as there room in the 64 byte serial buffer.

I used Arduino Simple Software Serial tutorial and Jari’s page as a reference for designing my network.

Code for controlling the node is

#include <SoftwareSerial.h>
SoftwareSerial mySerial(0, 1); // RX, TX

const char node = '1'; // network address
const int ledPin =  13; // the number of the LED pin

int incomingByte;

void setup() {
  mySerial.begin(9600);
  pinMode(ledPin, OUTPUT);
  pinMode(1, INPUT);
}

void loop() {
  if (mySerial.available() > 0) {
    digitalWrite(ledPin, HIGH);
    delay(200);
    digitalWrite(ledPin, LOW);
    delay(200);
    incomingByte = mySerial.read();
    if (incomingByte == node) {
      digitalWrite(ledPin, HIGH);    
      pinMode(1, OUTPUT); // open line to write
      mySerial.print("node ");  
      mySerial.println(node);  
      pinMode(1, INPUT);
      delay(200);
      digitalWrite(ledPin, LOW);
    }
  }
}

What the code does?

#include <SoftwareSerial.h>

The SoftwareSerial library has been developed to allow serial communication on other digital pins of the Arduino, using software to replicate the functionality (hence the name “SoftwareSerial”). It is possible to have multiple software serial ports with speeds up to 115200 bps. A parameter enables inverted signaling for devices which require that protocol.

SoftwareSerial mySerial(0, 1); // RX, TX

RX and TX pins are 0 and 1.

const int ledPin = 13; // the number of the LED pin

In Arduino LED pin number is 13.

mySerial.begin(9600);

The bound rate is set to 9600.

const char node = '1'; // network address

I’m naming my Arduino’s network address as ‘1’. If I had more boards I would name them 2,3,… etc.

if (incomingByte == node) { digitalWrite(ledPin, HIGH);

This node address is taken into concideration in void loop(). The serial data is read into the variable incomingByte. If the read data equals to node address, the LED is set on.

pinMode(RX, INPUT);

By default all boards with RX are input.

pinMode(RX, OUTPUT); // open line to write
mySerial.print("node ");  
mySerial.println(node);  
pinMode(RX, INPUT);

For writing the response it is made output for a while.

I downloaded the code to my Arduino board in Arduino IDE to test if the network code works. I opened serial monitor and send “1”. The board responded by flashing the LED and printing “node 1”.

I also tested to change network address to ‘2’. I dowloaded the modified code to Arduino and send “2” from the serial monitor. The board responded by flashing the LED and printing “node 2”.

I’m amazed that I was able to make it work at a first try.

Making nodes using Arduino Nano and week 7 board

Next I applied the same principle for Arduino Nano board and two week 7 boards to build the bus.

I set Arduino Nano to be Node 1, and my two LED/Button boards to be Node 2 and Node 3. I uploaded codes to LED/Button boards using week 5 programmer.

I wired boards using Arduino bread board.

At first I used wrong pin numbers (physical pin numbers) but then after going through other Oulu students’ documentations I realized that I had made mistake there. I should use DIGITAL/ANALOG pin numbers 2 and 3 instead board pins 4 and 5 as in week 9. I modified Jari’s code by uncommenting following lines in void loop()

    //digitalWrite(ledPin, HIGH);
    //delay(200);
    //digitalWrite(ledPin, LOW);
    //delay(200);

since I didn’t want the boards to have the extra blink that indicates that they have received a signal.

I also increased the time that the LED is on when the incomingByte maches the node number up to 1000ms to make it easier to see.

After these modifications the system worked the way I wanted: when I type 1 in serial monitor, the Nano blinks and prints Node 1 to monitor and same with LED/Button boards when 2 or 3 is typed.

Reflection

I learned basic principles about how networking and communication works. I used pretty great amount of time finding explanation and examples how to apply networking and communication on one Arduino board. I wasn’t able to build a complete bus with just one Arduino board but I think this principle could be applied to several boards by giving them different addresses.

After figuring out what Jari’s code does and realizing that I can use it with small modifications (LED pin number) I was able to build my network without facing any problems.

When making the system with my week 7 boards and Arduino Nano I had first some troubles understanding the code since every board blinked when I typed 1, 2 or 3 in the serial monitor. I tried changing pin numbers and TX/RX wire orders in my system to check if they somehow get a wrong signal. After figuring out that they blink when they get any signal I was able to make them blink only when the incomingByte maches the pin number.

Files

Code for Arduino Nano - Node1

Code for LED/Button board 1 - Node2

Code for LED/Button board 2 - Node3