Embedded Networking and Communications

Week 15

Chossing a protocol

As most of the weeks, I was totally clueless after the global class. I could see what to do and all those differences between protocols and all these different ways to make to devices communicate. I'm not even sure I could see how useful this would be. This where I began my research on this subject and lot at different projects and daily object that need this kind of process and them it talked to me! I'd still need to choose a process to begin with. As I begin to be more and more comfortable with Arduino and it's coding language I thought I'd stay with them and they look at a way to make two or more Arduino communicate together. As for lots of things in Arduino world, it is very well documented and I could use that as a starting point. I discovered I2C protocol that generates a master/slave dynamic of communication between the two devices.

"I²C (Inter-Integrated Circuit), pronounced I-squared-C or I-two-C, is a multi-master, multi-slave, packet switched, single-ended, serial computer bus invented by Philips Semiconductor (now NXP Semiconductors). It is typically used for attaching lower-speed peripheral ICs to processors and microcontrollers in short-distance, intra-board communication. Alternatively I²C is spelled I2C (pronounced I-two-C) or IIC (pronounced I-I-C)." - Source : Wikipedia

This schematic and some other tutorials allowed me do understand from which pins the communication between the two processor would be done and I began to see it getting done in my mind. I was ready to make tests with Arduinos.

Make tests with arduino

As I said, I thought it'd a good idea to start with Arduino, as it is very well documented, and them try to export the code and the circuit design to two boards I'd desgin myself. Here the schematic I followed to do that :

One thing that is very important to understand there is that pin 4 is the data, or SDA, pin and 5 the clock, or SCL, pin. That information will get very useful getting to design a board with ATTinys. That's pretty much it, I tried to connect the board the same way (which is definitly simple) and them upload a code in: One for the master Arduino and another one for the slave Arduino. At first, I did'nt work until I made research with Raphaël and find somme little changes to make within the code as for connecting 5.5V(master) to Vin(slave) instead of 5.5V for this last one. Put some resistor between the pins and the VCC. At the end, the main goal of that code was to make the slave Arduino to send a comminication at some specifif frequency. Now it says FabAcamdey2017 :p! I'm sorry but I didn't take picture of this phase .. anyway it's just connect 4 wire doesn't it ? :p

Design the circuits, mill and solder them

I love designing circuits with Eagle! I don't know why but I just love it! Creating those little link between such small components that, at first seemed useless or even abstract in their composition and that at the end, make your prototype come to life! This is awesome! Again, I'm not going to explain the process in such detail considering that I did that in Electronics design and Electronics production weeks. The thing I had to remember was to find the SDA and SCL pin because they would be the main way to make those two processors communicate together. Also, I'd have, for the master board, to prepare a header that'll communicate with the FabISP, one that'll be using for the power supply also. On the slave one, it was very important that the two boards were grounded in the some GND so I did that and I had to put a LED on to. There are some pictures of it:

The master board

The slave board

Before to end that section, it seems very important to me to say that to be able to design those board, I had to refer myself to the Hello board for the ISP connection header but more important yet, to that Arduino/ATTiny conversion map. This one as been very useful since the beginning of the program! There it goes again!

There you go ! The board are gone !!

Prepare and upload the code

It is now time to code the whole thing ! The code is pretty basic I guess but there's some important thing to understand in it as :

After this, and some example, and a little help, I've been able to upload this code to the boards :

The master board

#include <.Wire.h> //include this specific library
void setup() {
Wire.begin();        // join i2c bus
Serial.begin(9600);  // start serial for output
}
void loop()
{
	Wire.requestFrom(7, 9);    // request 6 bytes from slave device #8
while (Wire.available()) { // slave may send less than requested
	char c = Wire.read(); // receive a byte as character
 Serial.print(c);         // print the character
}
delay(500);
}
												

The slave board

#include <.Wire.h>
void setup() {
Wire.begin(8);                // join i2c bus with address #8
Wire.onRequest(requestEvent); // register event
}
void loop() {
delay(100);
}
// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent() {
Wire.write("FabAcademy2017 "); // respond with message of 6 bytes
// as expected by master
}
}
												

There you go with the code ! It is always nice to see texts like that comme to life in one object !

Final result

Then, I realized that I forgot to put the header to connect it to the ISP. Danm me ! Just before testing the whole thing ! I felt like a real stupid f*&%. Also, the wire library doesn't work well with ATTiny. Another thing I sould have looked first. Damn again !!

With Raphaël we changed the protocol, we'll do serial protocol that does the same thing then with I2C. The main objective was to make a slave board react to a master board command. In this case, it'll send a message to the computer.

In this case, as I was lacking one board, I chose to program the first Hello board I made as the slave board. It was basically a question of programming the right pins so it does that new job instead of it's first one. As for the code, I used the softserial library for the communication with the computer and between the board. I used the pins dedicated to be connected to the ISP for the master board and the helloboard to make them communicate between each other. As they already were connected to specific pins, I could use those to make the communication and then use jumpers to make the links. On the master board, I used pin 6 and 4 to communicate with the other board. For the communication with the computer, I used pin 5. For the slave board, I used pins 0 and 1 to communicate with the master. Finally, instead of adding a serial port to the board, I used the 6 pos header that was already connected to the right pins. It was easier to connect the boards using jumpers on the headers than to redesign et resolder the whole thing. Then, following Raphaël advice, I came out with those codes:

Here's also a nice definition that explain the serial communication process (source:Wikipedia):

"In telecommunication and data transmission, serial communication is the process of sending data one bit at a time, sequentially, over a communication channel or computer bus. This is in contrast to parallel communication, where several bits are sent as a whole, on a link with several parallel channels."

Here's the master's code:

#include <.SoftwareSerial.h> // The dot is there because it doesn'T show otherwise
int rx=6;
int tx=4;
int tx2=5;
SoftwareSerial mySerial(rx, tx);
SoftwareSerial mySerial2(-1, tx2);
void setup()
{
mySerial.begin(4800);
mySerial2.begin(4800);
}
void loop()
{
mySerial.write(1); //Send 1 byte as a trigger to Slave on SoftSerial #1
delay(100); //allow some time for the slave to send message back
while(mySerial.available()>0){
mySerial2.write(mySerial.read()); //Send message recieved from slave to Computer through softSerial #2
}
delay(1000);
}
												

Here's the slave's code:

#include <.SoftwareSerial.h> // The dot is there because it doesn'T show otherwise
int rx = 0;
int tx = 1;
SoftwareSerial mySerial(rx, tx);
void setup() {
  mySerial.begin(4800);
  pinMode(8, OUTPUT);
}
void loop() {
  while (mySerial.available() > 0) {
    mySerial.read(); //Read the value to remove it from the buffer
    mySerial.println("Fab Academy 2017");
    digitalWrite(8, HIGH);
    delay(10);
    digitalWrite(8, LOW);
  }
}
												

And it worked !! Here's the video of it !

What's nice about that mystake, is that I got to learn two kind of protocols I guess. Also, as for the recuperation of a very big mystake, I guess it is pretty good to ! Thanks to Raph !!!