13.Networking & Communications:

01-05-2021 | Jai Hanani

1. Objectives:

2.Group Assignment:

3.Individual Assignment:

4. Files, Planning, Tools:

Files:

Planning:

Tools:


  1. Optical Communication[This Part Can Be Skipped]:
  2. Right off the bat, I wanted to do something with Optical Communication. I downloaded the OpenCV-Python, and made a small program that will detect english alphabets, based on Visual Morse Code transmitted via phone flash light.
  3. Use this command to minimize a video using FFMPEG:  ffmpeg -i input.mp4 -vcodec libx265 -crf 28 output.mp4
  4.                 
                      import cv2 as cv
    import sys
    import numpy as np
    import threading
    
    cap=cv.VideoCapture(0)
    #NUMBER OF TIMES THE BRIGHTNESS HAS BEEN ABOVE 6900
    LIGHT_COUNT = 0
    
    #NO. OF TIMES THE BRIGHTNESS HAS BEEN BELOW 2000
    BREAK_COUNT = 0
    
    #to know if the transmission started
    e = False
    #timer
    timer = 0
    
    str = ''
    
    def convertMorseToText(s):
    
        morseDict={
            '' : 'Check',
        '.-': 'a',
        '-...': 'b',
        '-.-.': 'c',
        '-..': 'd',
        '.': 'e',
        '..-.': 'f',
        '--.': 'g',
        '....': 'h',
        '..': 'i',
        '.---': 'j',
        '-.-': 'k',
        '.-..': 'l',
        '--': 'm',
        '-.': 'n',
        '---': 'o',
        '.--.': 'p',
        '--.-': 'q',
        '.-.': 'r',
        '...': 's',
        '-': 't',
        '..-': 'u',
        '...-': 'v',
        '.--': 'w',
        '-..-': 'x',
        '-.--': 'y',
        '--..': 'z',
        '.-.-': ' '
        }
    
        return (morseDict.get(s))
    
    while True:
        # Capture frame-by-frame
        ret, frame = cap.read()
        # if frame is read correctly ret is True
        if not ret:
            print("Can't receive frame (stream end?). Exiting ...")
            break
        # Our operations on the frame come here
        gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
        mask = np.zeros(gray.shape[:2], dtype="uint8")
        cv.circle(mask, (320, 240), 50, 255, -1)
        masked=cv.bitwise_and(gray,gray, mask=mask)
        hist = cv.calcHist([gray], [0], mask, [256], [0, 256])
        
        #at the first frame with brightness above 6900, toggle on the 'E'
        if hist.max() > 6900:
          e = True
        
        
    
    
        #if e is on:
        if e:
          if hist.max() >= 6000:
            LIGHT_COUNT += 1
          elif hist.max() < 2500:
            BREAK_COUNT += 1
          
          if BREAK_COUNT >= 3:
            if LIGHT_COUNT > 25:
              str += '-'
            else:
              str += '.'
            print('String Currently: {0} \n LIGHT_COUNT = {1} \n BREAK_COUNT = {2} \n Alphabet: {3}'
            .format(str, LIGHT_COUNT, BREAK_COUNT, convertMorseToText(str)))
            
            BREAK_COUNT = 0
            LIGHT_COUNT = 0
            e = False
    
        # Display the resulting frame
        cv.imshow('frame', masked)
        if cv.waitKey(1) == ord('q'):
            break
        if cv.waitKey(1) == ord('t'):
            str = ''
            LIGHT_COUNT = 0
            BREAK_COUNT = 0
            print('_')
    # When everything done, release the capture
    cap.release()
    cv.destroyAllWindows()
    
                    
                  
  5. RF Communication:
  6. Next, I had a tough time gaining a intuition of Radio Frequency bands. That's why I've spent the next day trying to do some practical things in RF. Therefore, I decoded SSTV .wav signals send by ISS, and decoded it using a RX-SSTV program.
  7. Decoding Yuri Gagarin's Image:
  8. Just like the last week, this week is also very important for the timely completion of my final project.
  9. A simplistic sketch of my final project's networked circuitry:
  10. I had a lot of confusion regarding various communication protocols. So, the first thing I did is to go through this article on how UART, SPI, I2C works, and how they compare with each other: https://www.seeedstudio.com/blog/2019/09/25/uart-vs-i2c-vs-spi-communication-protocols-and-uses/#:~:text=What%20is%20I2C%3F&text=It%20is%20a%20serial%20communications,devices%20connected%20to%20the%20bus.
  11. I will go through I2C here, because I am more interested in that:
  12. Credit:here
  13. For my final project, I am planning to have 3 seperate boards:
    (1) A Master board with a bluetooth module attached.
    (2) A Disciple 1 board which accepts step-response input
    (3) A Disciple 2 board which accepts commands from either the Master 1, or Disciple 1, and run a Servo Motor.
  14. I intend to connect those three board using I2C. In some cases, UART, if it is more preferable.
  15. Next, I used tinkerCad to make 2 circuits, and make the communicate.
  16. I have one Arduino(2 Arduino Uno R3) that accepts pushbutton input, and transmits.
  17. I have one Arduino(1 Arduino Uno R3) that accepts input from 2 Arduino Uno R3, and generates PWM to control the servo, and moves it 90.
  18. Code for Arduino 1(Servo Motor):
        
          #include 
            #include 
            
            Servo myservo;
            
            int val;
            
            void setup()
            {
                Wire.begin(4);
              Wire.onReceive(receiveEvent);
              Serial.begin(9600);
              myservo.attach(9);
            }
            
            
            void loop()
            {
              delay(100);
            }
            
            
            void receiveEvent(int howMany)
            {
              int x = Wire.read();    
              Serial.println(x);
             if (x == HIGH) {     
                val = 90; 
              }
              else {
            
                val = 0;
              }
              myservo.write(val);
              delay(15);
             
            }
            
        
      
  19. Code for Arduino 2:
        
          // C++ code
    //
    #include 
    
    int pushbutton=2;
    
    void setup()
    {
      Wire.begin();
      pinMode(pushbutton,INPUT);
    }
    
    int x = 0;
    
    void loop()
    {
      Wire.beginTransmission(4); 
      x=digitalRead(pushbutton);
       Wire.write(x);                
      Wire.endTransmission();   
      delay(500);
    }
        
      
  20. I will be using the boards I have desgined in Input Devices and Output Devices Weeks, and make them communicate. Right now, I am in a Covid-Related lockdown.

  21. After Reaching The Lab:

  22. I intend to make a I2C network with 3 nodes:
    1. Commander
    2. Troop1
    3. LCD
  23. One board is from "Project", and the other is from "Input Devices" Week
  24. Commander:

              
                  #include  // Library for LCD
                  #include 
                  
                  LiquidCrystal_I2C lcd(0x27, 20, 4);
                  
                  void setup() {
                    Wire.begin();
                    Serial.begin(115200);
                    
                    lcd.init(); //Intialize LCD
                    lcd.backlight(); //Switch-On BackLight for LCD
                    lcd.home();
                    lcd.print("Networking");  
                    lcd.setCursor(0, 1);
                    lcd.print("Communications");
                    lcd.setCursor(8, 3);
                    lcd.print("~Jai Hanani");
                    delay(2000);
                  }
                  
                  void loop() {
                    Wire.requestFrom(8, 3); //Request 3 bytes of data from Troop 1
                    int i = 0;
                    char x[3];
                    while(Wire.available())
                    {
                      char c = Wire.read();
                      x[i] = c;
                      i = i + 1;
                      
                    }
                    Serial.println(x);
                      lcd.clear();
                      lcd.home();
                      lcd.print(x);
                    delay(500); 
                  }
              
            
  25. Troop1:

                
                    #include 
    
                    int trigPin = 3; //Trig Pin for Ultrasonic Sensor
                    int echoPin = 2; //Echo Pin for Ultrasonic Sensor
                    int ledPin = 0;
                    
                    long duration;
                    int cm;
                    
                    char cstr[16];
                    
                    void setup() {
                      Wire.begin(8);     
                      Wire.onRequest(requestEvent);
                      pinMode(ledPin, OUTPUT);
                      digitalWrite(ledPin, HIGH);
                    
                      Serial.begin(9600);
                    }
                    
                    void loop() {
                    
                      pinMode(trigPin, OUTPUT); //Set Ultrasonic Trigger Pin as OUTPUT
                      digitalWrite(trigPin, LOW); //Write it to LOW
                      delayMicroseconds(2); // Wait for 2mus
                      digitalWrite(trigPin, HIGH); // Write the Trig Pin to HIGH
                      delayMicroseconds(10); // Wait for 10mus
                      digitalWrite(trigPin, LOW);// Write the Trig Pin to LOW
                      pinMode(echoPin, INPUT); //Set Ultrasonic Echo Pin as INPUT
                      
                      duration = pulseIn(echoPin, HIGH); //The duration the echoPin takes to go HIGH in microseconds
                      
                      cm = microsecondsToCentimeters(duration);
                      sprintf(cstr, "%03d", cm);
                    
                      Serial.println(cm);
                      delay(500);
                    
                    }
                    
                    long microsecondsToCentimeters(long microseconds){
                      return (microseconds / 29.15 ) / 2;
                    }
                    
                    void requestEvent() {
                      Wire.write(cstr);
                    }
                
              
  26. The "Commander" board has two 4.99K resistors on SDA and SCL lines, and it is connected to "Troop1" and "LCD" via I2C Protocol, and the "Ultrasonic Sensor" is connected to "Troop1" using a custom protocol, and the "Troop1" is connected to my Computer via Serial.
  27. Group Assignment:

    For this Group Assignment, I had to send a message between two projects. I selected Arduino as one board and my custom-made board as the secondary board. I had connected the TX/RX pins of Arduino to RX/TX pins of my board, respectively. The project, here, is to send data between Arduino and My Board and The Computer. The Arduino sends '8' to the Board with an LED blink and The Board checks whether the received message is '8'. If it is '8', The Board replies with Char 'H' coupled with an LED blink.

    Arduino Code:

                
                  
                char incomingByte;
                
                void setup() {
                  Serial.begin(9600);
                  pinMode(LED_BUILTIN, OUTPUT);
                }
                
                void loop() {
                
                       Serial.write('8'); //Send the letter
                       digitalWrite(LED_BUILTIN, HIGH);
                       delay(1000);
                        digitalWrite(LED_BUILTIN, LOW);
                      delay(1000);
                    
                      incomingByte = Serial.read();
                  Serial.print(incomingByte); //Recieve the letter
                  
                    }
                
              

    "The Board" Code:

                
                  const char node = '8';
                  char incomingByte;
                  
                  void setup() {
                    Serial.begin(9600);
                    pinMode(0, OUTPUT);
                  }
                  
                  void loop() {
                  
                      incomingByte = Serial.read();
                      if (incomingByte == node) {
                        digitalWrite(0, HIGH);   
                        delay(1000);
                        digitalWrite(0, LOW);   
                        Serial.write('H'); //Send H
                      }
                    }