14. Networking and Communications

Group Assignment

To view the group assignment click here.

Individual Assignments

In this assignment, I will control the LED of two of ATtiny44 PCB by the serial monitor in Arduino IDE at the same time.

ESP8266 or ESP-12E, is a Wi-Fi microchip it can connect to an existing WiFi network and act as a Web Server, and it can also set up a network of its own, allowing other devices to connect directly to it. Also in this assignment, I will connect it once with an existing wifi, and also with its own.

Communication Protocols

I was searching about communication protocol implement and interpretation to broaden my perceptions and to understand more about networking and Communications, and I found that it is a wide field that has no limits and is also indispensable.

Communication Protocols Definition

A communication protocol is a set of rules and regulations that allow two electronic devices to connect to transmit information with one and another.

Between the sender and receiver, the message consists of granular and discrete units, where each data packet has its own header, core information, and routing trajectory. All of this has to be synchronized and choreographed in detailed ways, and that’s where communications protocols have such a powerful reach and such an important role.

Communication protocols are implemented in hardware and software. There are thousands of communications protocols that are used everywhere in analog and digital communications. Computer networks cannot exist without them.

Communication Protocols Systems
Communication protocols are divided into Inter System protocols and Intra System protocols.

Inter System protocols
The inter-system protocol using to communicate the two different devices. For example the “UART”.

  • UART
    A universal asynchronous receiver/transmitter “UART”, is a block of circuitry responsible for implementing serial communication. UART acts as an intermediary between parallel and serial interfaces. On one end of the UART is a bus of eight-or-so data lines (plus some control pins), on the other is the two serial wires “RX and TX”.

The Intra system protocol
The Intra system protocol is used to communicate the two devices within the circuit board.

  • I2C
    The Inter-integrated Circuit “I2C” Protocol is a protocol intended to allow multiple “slave” digital integrated circuits “chips” to communicate with one or more “master” chips. It is like the Serial Peripheral Interface “SPI”, only intended for short-distance communications within a single device. Also, like Asynchronous Serial Interfaces “such as RS-232 or UARTs”, it only requires two signal wires to exchange information.

  • SPI The Serial Peripheral Interface Bus is a communication standard, used mainly for the transfer of information between integrated circuits in electronic equipment, it is a standard for controlling almost any digital electronic device that accepts a serial bit stream regulated by a clock “synchronous communication”.

Other Popular Protocols

  • File Transfer Protocol (FTP) - - protocol to transfer computer files from a server to a client and vice versa.
  • Transmission Control Protocol (TCP) - - used for data transmission
  • User Datagram Protocol (UDP) - - used by programs to send short datagram messages
  • Hypertext Transfer Protocol (HTTP) - - application protocol that uses hyperlinks between nodes containing text
  • Post Office Protocol (POP) - - used by local email clients to retrieve email from a remote server over TCP
  • Internet Message Access Protocol (IMAP) - - a communication protocol used by email clients to retrieve messages from a mail server over TCP

Sources

Control LED of two ATtiny44 boards

Serial data transfer is when we transfer data one after the other. Information can be passed back and forth between the computer and Arduino, also can just send data.

I am going to connect two ATtiny44 PCB I had produced before to the Arduino UNO board, so the Arduino UNO itself communicates with the circuit through RX and TX connected in series with each other. The Arduino will communicate with the laptop throw the “USB to Serial” chip on it.

As a result of the research that I did about the type of protocol that I will use in this part, I came to the following from the broad source, Arduino Uno Wikipedia. One of the thousands of communications protocols is the “STK500” protocol, the one Arduino UNO communicates using it. Arduino UNO uses the Atmega16U2 “Atmega8U2 up to version R2” programmed as a “USB-to-serial converter”, it differs from all preceding boards in that it does not use the FTDI USB-to-serial driver chip.

Steps

First, I will upload the code for each ATtiny44 board independently. Here I uploaded the code to one of the board via FabISP. The code is as follows, when I send the number “1” in the serial monitor, the board LED will light up, the LED pin in Arduino is “3”. The code is bellow.

#include <SoftwareSerial.h>
SoftwareSerial mySerial(PA1,PA0); //RX, TX
int v=3;
int nodeid=1;

void setup() {
  mySerial.begin(9600);
  pinMode(3, OUTPUT); // led
}

void loop() {
  digitalWrite(3,LOW);
  v = 3;
  while (mySerial.available () == 0 ) {}
  v = mySerial.parseInt();
  mySerial.println(v);
  if(v == nodeid)
  {
    digitalWrite(3,HIGH);
    delay(1000);
  }
  else
  {
    digitalWrite(3,LOW);
   }
}       

Secondly, in the same way, I uploaded the code to the second board via FabISP. This time, when I send the number “2” in the serial monitor, the board LED will light up, the LED pin in Arduino is “8”. The code is below.

#include <SoftwareSerial.h>
SoftwareSerial mySerial(PA1,PA0); //RX, TX
int v=3;
int nodeid=2;

void setup() {
  mySerial.begin(9600);
  pinMode(8, OUTPUT); // led
}

void loop() {
  digitalWrite(8,LOW);
  v = 3;
  while (mySerial.available () == 0 ) {}
  v = mySerial.parseInt();
  mySerial.println(v);
  if(v == nodeid)
  {
    digitalWrite(8,HIGH);
    delay(1000);
  }
  else
  {
    digitalWrite(8,LOW);
   }
}       

Now and finally, I have connected the “RX” of all of the both ATtiny44 PCB and the Arduino RX in one node, as well as for the “TX” to be able to transfer data. I connected the two PCB also to the ground and VCC to Arduino to power it.

And connect Arduino via USB cable to the laptop, so that the Arduino would be the mediator transfer between the PCBs and the laptop, “the serial monitor.”

Here is a picture showing how I connected the “RX”, “TX”, “GND” and “VCC” in series to each other.

Here, whenever I send the number “1” the first board LED light, and whenever I send the number “2” the second board LED lights.

I have actually used the serial communications, that is how we send data to the Arduino. When we compile/verify what we are really doing is turning the sketch into binary data “ones and zeros”. When we upload it to the Arduino, the bits are shoved out one at a time through the USB cable to the Arduino where they are stored in the main chip.

A nice note, when we upload a sketch, look at the two LEDs near the USB connector, they’ll blink when data is being transmitted. One blinks when the Arduino is receiving data “RX” and one blinks when the Arduino is transmitting data “TX”.

Files

ESP8266 with ATtiny44


Here, I will connect the “ESP-12E” to the wifi, in order to be able to control the LED of this “ESP-12E” as well as the LED of the ATtiny44 PCB from the webserver. Also, I will link the LEDs with each other.

Source Code

In this part of the code I sets SSID (Name of the network I were in) and password to it, to connect it to an existing WiFi network and act as a Web Server. I was in Fablab Bahrain.

const char* ssid     = "Zain Broadband 2.4G";
const char* password = "fabacademy@2020";   

ESP8266 Code

/*********
  Rui Santos
  Complete project details at http://randomnerdtutorials.com  
*********/

// Load Wi-Fi library
#include <ESP8266WiFi.h>


// Replace with your network credentials
const char* ssid     = "Zain Broadband 2.4G";
const char* password = "fabacademy@2020";
int v=0;
//int nodeid=1;

// Set web server port number to 80
WiFiServer server(80);

// Variable to store the HTTP request
String header;

// Auxiliar variables to store the current output state
String output5State = "off";
String output4State = "off";

// Assign output variables to GPIO pins
const int output5 = 5;
const int output4 = 4;

// Current time
unsigned long currentTime = millis();
// Previous time
unsigned long previousTime = 0;
// Define timeout time in milliseconds (example: 2000ms = 2s)
const long timeoutTime = 2000;

void setup() {
  //Serial.begin(115200);
  Serial.begin(9600);
  // Initialize the output variables as outputs
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(output4, OUTPUT);
  // Set outputs to LOW
  digitalWrite(LED_BUILTIN, LOW);
  digitalWrite(output4, LOW);

  // Connect to Wi-Fi network with SSID and password
  //Serial.print("Connecting to ");
  //Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    //Serial.print(".");
  }
  // Print local IP address and start web server
  //Serial.println("");
  //Serial.println("WiFi connected.");
  //Serial.println("IP address: ");
  //Serial.println(WiFi.localIP());
  server.begin();
}

void loop(){
  WiFiClient client = server.available();   // Listen for incoming clients

  if (client) {                             // If a new client connects,
    //Serial.println("New Client.");          // print a message out in the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    currentTime = millis();
    previousTime = currentTime;
    while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected
      currentTime = millis();         
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
       // Serial.write(c);                    // print it out the serial monitor
        header += c;
        if (c == '\n') {                    // if the byte is a newline character
          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println("Connection: close");
            client.println();

            // turns the GPIOs on and off
            if (header.indexOf("GET /5/on") >= 0) {
            //  Serial.println("GPIO 5 on");
              output5State = "on";
              digitalWrite(LED_BUILTIN, HIGH);
            } else if (header.indexOf("GET /5/off") >= 0) {
             // Serial.println("GPIO 5 off");
              output5State = "off";
              digitalWrite(LED_BUILTIN, LOW);
            } else if (header.indexOf("GET /4/on") >= 0) {
            //  Serial.println("GPIO 4 on");
               v=1;
              Serial.write(v);
              output4State = "on";

             // digitalWrite(output4, HIGH);

            } else if (header.indexOf("GET /4/off") >= 0) {
            //  Serial.println("GPIO 4 off");
              v=0;
              Serial.write(v);
              output4State = "off";
              //digitalWrite(output4, LOW);
            }

            // Display the HTML web page
            client.println("<!DOCTYPE html><html>");
            client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
            client.println("<link rel=\"icon\" href=\"data:,\">");
            // CSS to style the on/off buttons
            // Feel free to change the background-color and font-size attributes to fit your preferences
            client.println("<style>html { font-family: Helvetica; background-color: pink; display: inline-block; margin: 0px auto; text-align: center;}");
            client.println(".button { background-color: #195B6A; border: none; color: white; padding: 16px 40px;");
            client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
            client.println(".button2 {background-color: #77878A;}</style></head>");

            // Web Page Heading
             client.println("<title>Page Title</title>");

            client.println("<body><h1>ESP8266 Web Server</h1>");
             client.print(" <img src=");
             client.print("https://icon-library.com/images/mouse-click-icon-png/mouse-click-icon-png-8.jpg");
             client.println(">");
            // Display current state, and ON/OFF buttons for GPIO 5  
            client.println("<p>GPIO 5 - State " + output5State + "</p>");
            // If the output5State is off, it displays the ON button       
            if (output5State=="off") {
              client.println("<p><a href=\"/5/on\"><button class=\"button\">ON</button></a></p>");
            } else {
              client.println("<p><a href=\"/5/off\"><button class=\"button button2\">OFF</button></a></p>");
            }

            // Display current state, and ON/OFF buttons for GPIO 4  
            client.println("<p>GPIO 4 - State " + output4State + "</p>");
            // If the output4State is off, it displays the ON button       
            if (output4State=="off") {
              client.println("<p><a href=\"/4/on\"><button class=\"button\">ON</button></a></p>");
            } else {
              client.println("<p><a href=\"/4/off\"><button class=\"button button2\">OFF</button></a></p>");
            }
            client.println("</body></html>");

            // The HTTP response ends with another blank line
            client.println();
            // Break out of the while loop
            break;
          } else { // if you got a newline, then clear currentLine
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }
      }
    }
    // Clear the header variable
    header = "";
    // Close the connection
    client.stop();
   // Serial.println("Client disconnected.");
    // Serial.println("");
  }
}     

ATtiny44

(PA1, PA0) are the ATtiny44 pins for the RX and TX respectively, and the (PA3) is the ATtiny44 pin that I have connected it with the LED. I determined them by referring to Electronics Design Week.

#include <SoftwareSerial.h>
SoftwareSerial mySerial(PA1,PA0); //RX, TX
int v=0;
int nodeid=1;

void setup() {
  mySerial.begin(9600);
  pinMode(PA3, OUTPUT); // led
}

void loop() {
  while (mySerial.available () == 0 ) {}
  v = mySerial.parseInt();
  mySerial.println(v);
  if(v == nodeid)
{
  digitalWrite(PA3,HIGH);
}
else
{
  digitalWrite(PA3,LOW);
 }
}     

WIFI Communication with Webserver

In this part, I am trying to control the ESP-12E and ATtiny44 PCB LEDs. When I access the IP URL in the browser, the browser then sends an HTTP request to the ESP-12E to handle this request. When ESP-12E reads this request, it knows when I want to turn the LED ON or OFF and so it turns it and sends a dynamic webpage to a browser showing LED status. Follow these steps to know how to get the IP:

Before Uplodeing the Code

  • You need to install the ESP8266 add-on for the Arduino IDE. For that, go to “File” > “Preferences” and enter into the “Additional Board Manager URLs” field:
http://arduino.esp8266.com/stable/package_esp8266com_index.json   
  • Go to “Tools” > “Board” > “Boards Manager” and install “esp8266 by ESP8266 Community”.

  • Plug the ESP-12E to the PC via micro-B USB cable and select this serial port under the “Tools” > “Port” > “COMB”.

  • Make sure that the “board” is selected properly in Arduino, “NodeMCU 0.9 (ESP-12 Module)” option. After that, upload the code to it.

  • After uploading the code, open the “Serial Monitor” at a rate of “115200 baud”, and press the RESET button on ESP-12E and it will show the IP address. With this IP address, it can deliver web pages to all connected devices under its own network.

//Serial.begin(115200);
Serial.begin(9600);   

Now I can control the ESP-12E and ATtiny44 PCB LEDs by accessing the IP URL http://192.168.1.112/ in the browser, the browser then sends an HTTP request to the ESP-12E to handle this request. When ESP-12E reads this request, it knows when I want to turn the LED ON or OFF and so it turns it and sends a dynamic webpage to a browser showing LED status.

By accessing the IP URL http://192.168.1.112/ in the browser, here is the web server. The upper button is for controlling the LED to the ESP-12E, and the lower is for controlling the ATtiny44 LED, up of them showing LED status.

Here is a video showing how to turn OFF and ON the ESP-12E LED via wifi.

Here is a screenshot of the webserver when I press the button to turn OFF and ON the LED of the ESP-12E, as there is the LED status above the button.

Serial Communication

I used the “SoftwareSerial” library in Arduino to communicate between the boards. Communication on a UART is done by using two data lines: a Transmission (TX) and a Receiving (RX) lines, as well as a Ground connection. So serial devices should have two serial pins, the receiver, RX, and the transmitter, TX.

I connected the ATtiny44 ISP to the Arduino as in Embedded Programming Week, and I uploaded the ATtiny Code after I uploaded “Arduino as ISP” code. Then I connected the ATtiny44 FTDI to the ESP-12E, from the “GND”, “TX” and ” RX “. Connection method:
- GND - - GND
- RX - - TX
- TX - - RX

Make sure that you connect “3.3V” to the ATtiny from the Arduino, so that it does not reach the ESP-12E volt that exceeds its tolerance, which leads to its burning.

Here, to the serial communication, I used a different code than the ESP-12E code because I no longer need a WiFi. As it is written in the code, when sending the number “1” the LED turns ON.

int v=0;
int nodeid=1;

void setup() {
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT); // white led
}

void loop() {
  while (Serial.available () == 0 ) {}
  v = Serial.parseInt();
  Serial.println(v);
  if(v == nodeid)
{
  digitalWrite(LED_BUILTIN,HIGH);
  delay(500);
  digitalWrite(LED_BUILTIN,LOW);
  delay(500);
  digitalWrite(LED_BUILTIN,HIGH);
  delay(500);
  digitalWrite(LED_BUILTIN,LOW);
}
else
{
  digitalWrite(LED_BUILTIN,LOW);
 }
}

int nodeid=1;
if(v == nodeid)
{
 digitalWrite(LED_BUILTIN,HIGH);
 delay(500);
 digitalWrite(LED_BUILTIN,LOW);
 delay(500);
 digitalWrite(LED_BUILTIN,HIGH);
 delay(500);
 digitalWrite(LED_BUILTIN,LOW);
}

This upper code part written in the code, meaning that if the number “1” was sent in the serial monitor, the LED would turn ON. This is called serial communication, when the ESP-12E and ATtiny are connected, and I send the number “1”, the LEDs for each turn ON.

Failed WIFI and Serial attempt

I connected the ATtiny44 PCB to the ESP-12E, and with it, I can control the ESP-12E LED from the webserver, but I cannot control the ATtiny44 LED though it, and still I didn’t know the reason despite my attempts.

Files

Alarm System

Through this assignment, an alarm system will be designed and implemented that helps hospitals track the patient’s condition. That is, the nursing department is notified if the patient’s temperature rises, through a temperature sensor connected to a screen to inform the patient’s temperature of the nursing department, and when the lighting is activated it alerts if the patient’s temperature rises above 38. Also, this system can be connected to the WIFI which transmits the temperature to the nursing staff (The sensor used to measure the temperature of the air instead of a medical sensor, but that doesn’t change the idea).

Hardware Components and Connections

  • ESP8266
  • BMP180
  • LCD
  • LED
  • 220 OHM RESISTOR
  • Jumper Wires
  • Bread-board

I connected the component to the bread-board, then connected ESP8266 piece to the laptop for programming by USB.

Source Code

I uploaded the code from Arduino to ESP8266, where the code works as follows: When the temperature is sensed by the sensor BMP180, this temperature appears on the LCD screen, and when the temperature rises above 38, the yellow LED lights up. Thus, the circuit operates once it is connected to power.

Modify the following two variables with your network credentials before uploading the code, so that your ESP8266 can establish a connection with your router.

// Replace with your network credentials
const char* ssid = "";
const char* password = "";      

ESP-12E Code

#include <ESP8266WiFi.h>
#include <Adafruit_BMP085.h>
#include <Wire.h>
#include <LiquidCrystal.h>
#include <ESP8266WebServer.h>

// network  
IPAddress local_ip(10,0,0,1);
IPAddress gateway(10,0,0,1);
IPAddress subnet(255,255,255,0);

const char* ssid = "NursingStation";  // Enter SSID here
const char* password = "00000000";  //Enter Password here

// temp sensor
Adafruit_BMP085 bmp;





// mapping of pins with LCD
const int rs = D7, en = D8, d4 = D6 , d5 = D5, d6 = D4, d7 = D3;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

float Temperature;


// esp web server at port 80
ESP8266WebServer server(80);
void setup() {

  // set up wifi
WiFi.softAP(ssid, password);
WiFi.softAPConfig(local_ip, gateway, subnet);
delay(100);

pinMode(D0, OUTPUT);
// setup sensor temprature
 if (!bmp.begin())
  {
    Serial.println("Could not find BMP180 or BMP085 sensor at 0x77");
    while (1) {}
  }

server.on("/", mainP);      //This is display page
server.onNotFound(NotFound);
server.begin();


Serial.begin(115200);
Serial.println("HTTP server started");

// this message will not move

lcd.print("Temprature: ");
}

void mainP() {
 server.send(200, "text/html", s(Temperature));
}


void NotFound(){
  server.send(404, "text/plain", "Not Available");
}

void loop() {


// 1- read temprature from bmp sensor
Temperature = bmp.readTemperature();

// 2- set cursor to display temprature beside the word "temprature"
lcd.setCursor(11,0);
lcd.print(Temperature);

  Serial.print("Temperature = ");
  Serial.print(bmp.readTemperature());
  Serial.println(" Celsius");

// 3- led connected to port D0 will go high if temp above 38 (alert)
if(Temperature > 38) {
  digitalWrite(D0, HIGH);
}else {
  digitalWrite(D0, LOW);

 }


server.handleClient();


}

String s(float t){
  String p = "<!DOCTYPE html> <html>\n";

  p ="<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n <title>Nursing Station</title>\n";
  p += "<meta http-equiv=\"refresh\" content=\"1\">";
  p +="</head>\n";
  p +="<body>\n";
  p +="<div id=\"temp\">\n";
  p +="<h2>Body Temprature: ";
  p += float(t);
  p +="°C</h2>\n\n ";
  p +="</div>\n";
  p +="</html>\n";
 return p;
}   

Testing the system

I tested the board’s work, and this is an image of the LED at room temperature.

After that, I exposed the sensor to a source of heat, and when the temperature rises and reaches 38.10 degrees, the LED lights up.

Connecting to the WIFI

As written in the code:

// network  
IPAddress local_ip(10,0,0,1);
IPAddress gateway(10,0,0,1);
IPAddress subnet(255,255,255,0);

const char* ssid = "NursingStation";  // Enter SSID here
const char* password = "00000000";  //Enter Password here

I connected the phone to the Wi-Fi for the microcontroller “NursingStation” with the Password “00000000”.

Open the web page and write the following link: Http://10.0.0.1/, to show the measured temperature.

Video

ESP8266 wifi in an alarm system

ATtiny44 Hello board with Arduino UNO

I want to send a message between Arduino UNO and Attiny44; by pressing the button in ATtiny44 board, Arduino IDE serial monitor will print the message (“Hi I’m Attiny”).

Connection

I tried to connect the Arduino board with two ATtiny44 Hello board.
ATtiny RX pin 0 – Arduino TX Pin 1
ATtiny TX pin 1 – Arduino RX Pin 0
ATtiny GND – Arduino GND
ATtiny VCC – Arduino VCC

My Attiny44 Board:

Abdullah fabacademy Attiny44 Board:

Libraries

Sketch > Include Library > Manage Libraries > SoftwareSerial.h

Codes

Arduino Code

String sentence;  // define a string variable
#define led 13  //define led pin


void setup() {

Serial.begin(9600); // begin serial
pinMode(led, OUTPUT); // set led pin as output pin

}

void loop() {

    if(Serial.available()){  //When any thing reach on serial pins

  sentence=Serial.readStringUntil('\n'); //read the reached string until new line
  Serial.println(sentence); // prints the string that you read
  digitalWrite(led,HIGH); //turn led on
  delay(1000); //wait for 1 second

    }
}

Attiny44 Code

#include <SoftwareSerial.h> //include SoftwareSerial library

SoftwareSerial myAttinySerial(0,1);// Define pins 0 and on as serial pins 0 is RX ans 1 is TX

#define BUTTON 3 //Define on Attiny board button pin 3
#define LED 2   //define on Attiny board LED
int state =0; //set variable to monitor the state of the button

 void setup() {

    myAttinySerial.begin(9600); //begin my defined SoftwareSerial
    pinMode(BUTTON,INPUT); // set BUTTON as an OUTPUT pin
    pinMode(LED,OUTPUT); // set LED as an INPUT pin
 }

void loop (){

    state = digitalRead(BUTTON); //Read the BUTTON  and save it's value on status variable
    if(state == HIGH){  // If BUTTON is pressed
     myAttinySerial.println("Hi I'm Attiny"); // Send this string to SoftwareSerial
     digitalWrite(LED,HIGH); //lights the LED
     delay(1000); //wai for 1 second
    }

    digitalWrite(LED,LOW); // Turn LED off
    delay(1000);
    //}
    }

Result

I don’t know what is wrong with uploading the code.

After the several programming attempt that I did, I knew that the mistake behind the failure of this attempt was the connection of the ISP headers to the Arduino for programming.

Source