Week 13 : Networking & Communication

☛ Individual Assignment

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

☛ Group Assignment

☐ 2. Send a message between two projects


☛ Connecting two boards through I2C Connection


As a group assignment we sent a message from Ambroise's board, with a Distance sensor, to Quentin's board, with a LCD display. They are communicating through I2C connection.To do so, a board's role is defined as a Teacher, and the second board is defined as the student, we have to connect the SDA and and SCL pin from the master board, to the SDA and SCL pins from the student board.
In the codes, we will use the Wire Library, the Teacher board, in this case the Distance sensor one, will transmit the values he is sensing to the Student board. The second board is linked by an address to its Teacher, his role will be to wait for data to come from the other board, and to print them on the LCD display.


Sample.img
Sample.img

Here is the video of the setup :




☛ Testing 433Mhz RF Modules : RX480E-4 & TX118SA-4


I bought these RF transmitter/receiver modules and I found this tutorial which explains the basics to use them.


Initially I thought I could use these modules in order to "pair" the controller from my final project to control the drawing car with it. After completing this tutorial, I don't think I can. A learning button is used to pair the emitter with a receiver that is accessible, the learning button has to be pressed 8 times to wipe out the memory from the transmitter, its led should blink 7 seven times. After this step it is possible to pair it with a receiver. These modules have 3 modes of operation that can be choosen using the learning/pairing button on the transmitter :

When using LEDs to test these modules, the momentary mode allows a momentary connection, the LED is High when the corresponding button is pressed, then low as soon as the button is unpressed. When using the Toggle mode the LED stays high after one push, and has to be triggered again to become low. The last mode allows one "channel" at a time to be open, if Led 1 button's is pushed, the led becomes high, then if Led 2 button's is pressed, Led 1 is low and Led 2 switches on. To replicate the circuit from the tutorial I needed :


Sample.img
Sample.img

Here are the modules and their respective circuits, I used arduino's board to power the modules with 5V. I took those images from the tutorial linked.


Sample.img
Sample.img

Here is a video resulting from the tutorial linked above, I am testing the momentary mode at a short range distance. I don't understand why two Leds are switching on at the same time, plus I had to modify the Leds order on the breadboard as they are not in the right order on the pcb.
Without using the antennas from these modules, I could use the transmitter up to 6 meters away from the receiver.




☛ Testing WiFi Module : the ESP8266-01


Another module I bought some time ago because "it could be useful one day", the EPS8266-01 module. It is a WiFi module that is cheap and it can be used with an arduino, or be programmed itself. It seems to be a very useful module and I chose to follow a tutorial to get to know him better - this week is really complicated because I really don't know how to proceed. So here is ESP8266 WiFi Module for Dummies tutorial.



▸ Circuit Setup


It took me time to make this circuit because the ESP8266-01 should not be supplied in 5V in order not to damage the module, there was various options to power supply it. I chose one I had never tried before : This is the first time I am programming a board by using a FTDI programmer (I bought this one years ago because a friend told me I would need it someday, this day is today), and this is the first time I use a breadboard power supply module. This power supply module is called the HC-131, and so far it is very handy as it can supply 3V or 5V one each side of the breadboard separately, or both, it means that it could supply 5V and 3V on the same breadboard. To follow the tutorial I used those parts :


Sample.img
Sample.img

The GPIO 0 pin has to be linked to GND while the board is being programmed, and it has to be unplugged when using the ESP without the FTDI.
The CH_PD pin has to be linked to the VCC.
The reset pin is linked to a switch button and a 10k pull-up resistor, it has to be pressed to reset the ESP before programming it.
Here is the block diagram I made for this circuit :


Sample.img

When the circuit is power supplied in 3V and connected properly, the ESP red led switches on, and the blue led blinks one time.



▸ Arduino IDE Setup


When the circuit is ready, the Arduino IDE we have to import a new board library, and several libraries for the ESP8266, most importantly the ESP8266WiFi.h that is used in the code from the tutorial that we will use.




▸ ESP8266 Arduino Code by TonesB


Here is a link to downlad the zipped arduino code ESP8266WiFi.zip.
In order to make it work as it is, the SSID and the Password have to be modified with ours.
Also, to make the programming part possible I had to reverse the Tx and Rx pins, when it worked this message appeared in the serial monitor.
When the code is uploading, the blue LED is blinking, as the board is programmed, the GPIO 0 pin can be unplugged from the GND.


Sample.img

I opened the URL provided and I could control the LED from the web page.




☛ Testing the ESP8266-01 2


Next I tried to reproduce this tutorial to control 2 servo motors on a browser with the ESP 826601 and an Arduino Uno.


Sample.img

The tutorial was only using the power supply from the arduino, I was a little bit suspicious about that after my readings in order to use this module, but as I'm not mastering all these things I decided to follow the tutorial "letter by letter". It didn't work so I rework the circuit in order to only have to plug the servo and RX, TX pins for the serial communication. This way, I show some kind of living form in the serial monitor, but nothing happened next. As I resetted and restared everything, nothing more would happen.


Sample.img
Sample.img

To be continued when I will find more time and patience. I linked the tutorial above If you want to give it a try.



☛ Testing the Bluetooth module : Student Communication


I tested the HC05 Bluetooth module, I followed a tutorial throuh which I learned to control a servo motor using bluetooth through an application made on MIT App Inventor. The great instructables tutorial is right here.


▸ Circuit Setup


The components needed are :

Some important notes before continuing are :


Here is the block diagram circuit for this project :


Sample.img

Sample.img

▸ Arduino code


I used the code provided by the author of the tutorial :


Code written by mertarduinotech // TX RX software library for bluetooth #include <SoftwareSerial.h> #include <Servo.h> // servo library Servo myservo; // servo name int bluetoothTx = 10; // bluetooth tx to 10 pin int bluetoothRx = 11; // bluetooth rx to 11 pin SoftwareSerial bluetooth(bluetoothTx, bluetoothRx); void setup() { // attach servo signal wire to pin 9 myservo.attach(9); //Setup usb serial connection to computer Serial.begin(9600); //Setup Bluetooth serial //connection to android bluetooth.begin(9600); } void loop() { //Read from bluetooth and write to usb serial // receive number from bluetooth if(bluetooth.available()> 0 ) { // save the received number to servopos int servopos = bluetooth.read(); // serial print servopos current //number received from bluetooth Serial.println(servopos); // roate the servo the angle received //from the android app myservo.write(servopos); } }

▸ Making an application on MIT inventor App


Here I am using the MIT App Inventor to conceive an interface to control the servo motor using the bluetooth connection established with the HC05 module. The app is composed of two main parts, the Designer mode, and the Blocks mode. In the first mode, we have to select the visible and invisible elements composing our app, we manage how they are linked by block coding in the Blocks mode.


In the images below, I select User interface modules and layouts to create the visual structure of the application, and I add a BluetoothClient, this component will allow my phone to connect to other bluetooth devices. It uses the Serial Port Profile for communication.

Sample.img
Sample.img

In the following images, I am first adding a Clock component, this is another invisible component (like the BluetoothClient) that uses the internal clock of the phone to provide "timing".
The second image shows the Blocks mode and the blocks responsible for the bluetooth connection between the phone and the HC05 module.


Sample.img
Sample.img

The first image here shows the messages sent to the bluetooth module when the slider is moving, or when buttons are clicked.


Sample.img
Sample.img

Finally, when the design and the blocks of code are set, we can built a .APK file and install it on an Android device. (An application from MIT App Inventor exists to test application without this APK installation, but it requires the phone to be connected to WiFi and I can't do that - internet issues - too bad, it seemed easier.)
In order to install the .APK file "installation from unknown sources" has to be accepted. And here is a screenshot of the application on my phone.


Sample.img

▸ Result


It didn't work as soon as everything was switched on. First, I didn't notice I was using a continuous Servo Motor, and it behaved "savagely". The second servo I used was always moving but on one side only. The serial communication shows 180 values only, while other commands are sent. I tried several things in the code, like mapping the values, but it didn't solve the issue. I tried with anoter servo, and it worked fine with it. I added a decoupling capacitor between the GND and VCC of the servo.


Sample.img
Sample.img

As I was using my phone to have acces to the App, I filmed this from my computer : I am first using the slider to change the servo's values, then the buttons, and the slider again.



☛ Testing the HC-05 Bluetooth module : Student Communication (Failed attempt)


It was late, it was working just before, so I thought that I could replicate this process using my code and my previous "Drawing Breadboard" by adapting it with the one used before to establish the bluetooth.
I used the template of the first app and added buttons to control the servo motor and the DC motors.


Sample.img
Sample.img

Here is the app looks like. I could establish the conection, as indicates the "Connected" on the screenshot below, but none of the commands worked. I couldn't recieve the serial communication (I need help on this part !) and so I don't know what the communication was like. I will go back to this later, I think the issue is in my code (DrawingCar-bluetooth.zip).


Sample.img

☛ Testing the HC-05 Bluetooth module : Teacher-Student Communication


So far I thought the HC-05 bluetooth module could only act as a Student, but I discovered that it has actually 3 operation modes, being a student is its default setup.This link (in french) explains how to configure a HC-05 bluetooth module. This second link (also in french) was more complete regarding a few important aspects of the configuration process for a Teacher-Student communication.
First I will show how these tutorials helped me to configure two HC-05 modules to enable the communication between them.



▸ Circuit Setup


In order to be configured, the module needs to recieve AT commands, to do so we need to link the module in its configuration mode : the EN or KEY pin of the HC-05 module has to be in a high state to enter this mode. In the communication mode, this pin should not be connected. To reproduce the circuit the components needed are :


To configure only one module, only one of each is required. Here is the HC-05 Configuration circuit that I reproduced :


Sample.img
Sample.img


▸ Arduino Code for AT Configuration


Once the circuit is ready we will upload the following code from here, that I commented, in the Arduino IDE :


// Virtual serial communication enabled #include <SoftwareSerial.h> // Virtual serial communication on TX, RX SoftwareSerial mySerial(10, 11); void setup() { Serial.begin(9600); // Connected to KEY on EN on HC-05, //allows configuration mode pinMode(9,OUTPUT); digitalWrite(9,HIGH); Serial.println("Enter AT commands:"); // Baud rate for the serial communication //with the bluetooth module mySerial.begin(38400); } void loop() { //If the serial on the bluetooth side //is available if (mySerial.available()) Serial.write(mySerial.read()); //If the serial is available from //the interface side if (Serial.available()) mySerial.write(Serial.read()); }

After uploading the code in the Arduino, the module's LED should be blinking gently, like on the second image below. If the LED is blinking faster it means that the module is not in configuration mode, you need to reset the arduino, or unplug and replug the USB, or unplug and replug the module itself. I don't know which of these did work, but it did work at the end.


Sample.img
Sample.img

When the module is in configuration mode, we can send AT commands through the serial monitor. AT Commands are also called "Hayes commands", there are commands developped to communicate with modems initially, and we use those to get informations about the module. Each command begins with "AT", in which the "A" means "Answer".
In the first image following I typed those commands :



In the second image, I am configuring my second HC-05 module, here are the commands sent :


Sample.img
Sample.img

In the following images, I took the first module and decided it would be the Teacher one. In my first attempt, I simply typed the command "AT+BIND=98D3,71,F5B7D3" in the serial monitor, it returned "OK", but when I typed "AT+ROLE" to see if that valued was modified, it wasn't. Here is how to configure a HC-05 module into a Teacher role :


  1. Type "AT+ADDR" on the module that will be the Student to get its address, it should return something like that : "+ADDR:98D3:71:F5B7D3"
  2. In the serial monitor of the Teacher-to-be, type : "AT+ROLE=1" (1 is for Teacher mode)
  3. Type "AT+ROLE" to verify if the command was applied
  4. Type "AT+BIND=98D3,71,F5B7D3", it is the address of the student, we need to replace the ":" by "," when sending the command.
Sample.img
Sample.img

When the modules are connected we can see that the LEDs are blinking at the same pace.


Sample.img

To differentiate my modules I taped them, black tape is for Sensei, and yellow tape is for Yellow Belt.


Sample.img

▸ Arduino Code for HC-05 Teacher/STudent Communication


In the next image, I am using the second tutorial linked above, to make the modules communicate together. In the tutorial the person is able to open two serial monitors at the same time, it doesn't work for me so I am using the Arduino IDE on Windows, and on a virtual machine on Ubuntu. This way I can have the serial monitor for each module.


Sample.img

Here is the code for the Teacher module (I only modiied Master/Slave to Teacher/Student, the original source is linked here.) :


#include <SoftwareSerial.h> SoftwareSerial ArduinoStud(2,3); String answer; String msg; void setup(){ Serial.begin(9600); Serial.println("ENTER Commands:"); ArduinoStud.begin(9600); } void loop(){ //Read command from monitor readSerialPort(); //Read answer from student while (ArduinoStud.available()) { delay(10); if (ArduinoStud.available() >0) { //gets one byte from serial buffer char c = ArduinoStud.read(); //makes the string readString answer += c; } } //Send data to student if(msg!=""){ Serial.print("Teacher sent : "); Serial.println(msg); ArduinoStud.print(msg); msg=""; } //Send answer to monitor if(answer!=""){ Serial.print("Stud recieved : "); Serial.println(answer); answer=""; } } void readSerialPort(){ while (Serial.available()) { delay(10); if (Serial.available() >0) { //gets one byte from serial buffer char c = Serial.read(); //makes the string readString msg += c; } } Serial.flush(); }

Here is the code for the Student module :


#include <SoftwareSerial.h> SoftwareSerial ArduinoTeacher(2,3); String msg; void setup(){ Serial.begin(9600); ArduinoTeacher.begin(9600); } void loop(){ readSerialPort(); // Send answer to master if(msg!=""){ Serial.print("Teacher sent : " ); Serial.println(msg); ArduinoTeacher.print(msg); msg=""; } } void readSerialPort(){ while (ArduinoTeacher.available()) { delay(10); if (ArduinoTeacher.available() >0) { //gets one byte from serial buffer char c = ArduinoTeacher.read(); msg += c; //makes the string readString } } ArduinoTeacher.flush(); }

▸ DC motors remote control using HC-05 bluetooth modules


In the following part I used several tutorials, and this one mainly in order to use this connection between the module to control motors with a joystick.
There will be a controller board with a joystick and the HC-05 Teacher, and another board with a motor driver and the HC-05 Student.


Sample.img

The Teacher board is composed of :



The Student board is composed of :



Sample.img

▸ Arduino codes


These codes were provided on this great tuorial.


Here is the Arduino code used for the Teacher board : HC05-Joystick-Teacher-code.zip


Here is the Arduino code used for the Student board : HC05-Car-Stud-code.zip



Here is a video of this set up in action, almost. Here the Teacher board is powered using USB, at 3V. The Student board has to power supply, the arduino is power supplying the bluetooth module, while a 6V alimentation is powering the motor driver. It needs more power but I can't provide more safely at home. But some good indicators are that, in one direction, it is clearly moving, and in the others, I can hear discrete motor noises. Communication seems to be happening.



I used the serial monitors to visualize the communication between the modules, it appeared that they are talking, but the commands are lost in the translation. While the Teacher modules is sending the joystick position's values, the Student module prints not expected values.


Sample.img
Sample.img

I tested several things in the code, what solved my issue was that even though I documented above that the baudrate for this module is 9600, the code I was using was set at a 38400 baudrate. The following images show that at the right baudrate communication is better, while the Teacher is sending the "Serial.println("4");, the receiver prints "10", "52", "13", which means "10 : New line", "52: 4", and "13: carriage return".
I used this ASCII table to translate these datas.


Sample.img
Sample.img

The next video shows that it is working better : the motors are moving in the expected direction, still, the motor board is under supply, and the result is not super reactive.



Here are the codes I wrote, using tutorial linked before, and my code from week 12, the Teacher code :


int xAxis = 0; int yAxis = 0; void setup() { // Default communication rate of the Bluetooth module Serial.begin(9600); } void loop() { // Read Joystick axis xAxis = analogRead(A0); yAxis = analogRead(A1); xAxis = map(xAxis, 0, 1023, 0, 1023); yAxis = map(yAxis, 0, 1023, 0, 1023); if(xAxis >= 612) { //Serial.write("A"); Serial.print("1"); delay(10); } if(xAxis <= 412) { //Serial.write("B"); Serial.print("2"); delay(10); } if(yAxis >= 612) { //Serial.write("C"); Serial.print("3"); delay(10); } if(yAxis <= 412) { //Serial.write("D"); Serial.print("4"); delay(10); } //else //{ // Serial.print("0"); // delay(10); //} }

The Student code :


#define enA 9 #define in1 4 #define in2 5 #define enB 10 #define in3 6 #define in4 7 int command; void setup() { pinMode(enA, OUTPUT); pinMode(enB, OUTPUT); pinMode(in1, OUTPUT); pinMode(in2, OUTPUT); pinMode(in3, OUTPUT); pinMode(in4, OUTPUT); Serial.begin(9600); // Default communication rate of the Bluetooth module } void loop() { // Read the incoming data from the Teacher module while (Serial.available() >= 2) { command = Serial.read(); delay(10); Serial.println("Command received :"); Serial.println(command); if (command=='1') { Forward(); } if (command=='2') { Backward(); } if (command=='3') { Right(); } if (command=='4') { Left(); } else { Stop(); } } } void Forward() { // Set motors to maximum speed // For PWM maximum possible values are 0 to 255 analogWrite(enA, 255); analogWrite(enB, 255); // Turn on motor A & B digitalWrite(in1, HIGH); digitalWrite(in2, LOW); digitalWrite(in3, HIGH); digitalWrite(in4, LOW); delay(10); } void Backward() { // Set motors to maximum speed // For PWM maximum possible values are 0 to 255 analogWrite(enA, 255); analogWrite(enB, 255); // Turn on motor A & B digitalWrite(in1, LOW); digitalWrite(in2, HIGH); digitalWrite(in3, LOW); digitalWrite(in4, HIGH); delay(10); } void Right() { // Set motors to maximum speed // For PWM maximum possible values are 0 to 255 analogWrite(enA, 255); analogWrite(enB, 255); // Turn on motor A & B digitalWrite(in1, HIGH); digitalWrite(in2, LOW); digitalWrite(in3, LOW); digitalWrite(in4, HIGH); delay(10); } void Left() { // Set motors to maximum speed // For PWM maximum possible values are 0 to 255 analogWrite(enA, 255); analogWrite(enB, 255); // Turn on motor A & B digitalWrite(in1, LOW); digitalWrite(in2, HIGH); digitalWrite(in3, HIGH); digitalWrite(in4, LOW); delay(10); } void Stop() { // Set motors to maximum speed // For PWM maximum possible values are 0 to 255 analogWrite(enA, 255); analogWrite(enB, 255); // Turn on motor A & B digitalWrite(in1, LOW); digitalWrite(in2, LOW); digitalWrite(in3, LOW); digitalWrite(in4, LOW); delay(10); }

☛ Using my boards


This is an update published on june the 1st.



▸ Circuit Setup


Finally, I tested the programs with my boards. I am using an Arduino as an ISP, Arduinos to power the board, and batteries to power the L293D from the Drawing Car.


Sample.img

I used the HC-05 Bluetooth modules I used before, and it they connected together. Here are the codes I used they are similar to the ones displayed above, but I made modifications to light up the LED. Also, for now I can't controle the servo motor. I didn't find why yet. This part will be updated in the Final project.


Sample.img

Here is the video of the first test. As the pins were changed but not the code (when I filmed this, the code zipped above is modified to the right pins). It is working, but there is still a delay between the command sent and the command applied.




‹ Previous : Output Devices   •   Next : Interface & Application programming ›