• Home
  • my project
  • About
    • About me
    • @info
  • session/Assignments

    interface and application programming

    Group_Assignment
    in this week assignment is about programming and interfacing where we make a program that has an interface to display or shows a certain
    as an assignment mentions "write an application that interfaces a user with an input &/or output device that you made"

    what is software interface Software interfaces, also called programming interfaces, are the languages that various software applications
    use to communicate with each other and with a hardware's internal system. They typically control a system's resources LINK HERE
    and also Interfaces are tools and concepts that technology developers use as points of interaction between hardware and software components.
    They help all components within a system communicate with each other via an input-output system and detailed protocols while also allowing them to function independently.
    learn more

    Assignment

    for my work i have used python language as an application programming and also arduino development to integrates between two of them
    for my interface i have used python to create interface, this interface is a link between user interface and arduino program
    so that they can communicate between them

    how i made a program

    python has a feature package that is used for designing GUI (graphic user interface) tkinter it is builtin with python3
    learn more about tkinter and how to use it
    if tkinter is not already istalled in python for some reason follow how to add in it
    go to terminal then type in the following command
     pip install tkinter
    wait until the module is already installed
    another package needed to operate this process is a serial, this package helps to communicate to serial port and python
    and also head to terminal and tyoe the following command and hit enter to install serial package
    pip install pyserial
    i first made simple interface that read user input and giveout feedback on the interface
    and there's also a button that enables a user t send a word to interface
    as it is shown in the picture belo there's a text message wher we enter the input text below there's a button that send characters
    on interface


    let's see the codes how they follows
    
          from tkinter import *
    
          root = Tk()
          root.geometry('300x300')
          root.title("fabacademy")
    
          tit = Label(root, text="my fab interface:)", font=('Bauhaus 93', 25))
          tit.pack(padx=20)
    
          userI = Entry(root, width=20)
          userI.pack()
    
          def printt():
              printtt = Label(root, text=userI.get())
              printtt.pack()
          BTN = Button(root, text="send 2 MCU", command = printt)
          BTN.pack()
    
    
    
          root.mainloop()
    
        
    as i mentioned above we have to import the library for the package the first line indicates how the library imported
    from tkinter import *
    the following lines create an application window
          
             root = Tk()
             root.geometry('300x300')
             root.title("fabacademy")
            
        
    this is the title written above the texte box these are labels under the variable name tit, the keyword pad means to set the object
    in center or aside of the window
          
          tit = Label(root, text="my fab interface:)", font=('Bauhaus 93', 25))
          tit.pack(padx=20)
          
        
    this create a text box that enables us to input the characters with keyword "Entry"
          
          userI = Entry(root, width=20)
          userI.pack()
          
        
    so i created a function that reads user input and prit it out
    this function enables the program to display out the the input datas
    with this keyword function "get()" so a "variable.get()" it captures the input entered
          
          def printt():
              printtt = Label(root, text=userI.get())
              printtt.pack()
          
        
    then the last there's also a button i added that i give a command to do a certain thing
    i gave it a command that calls a function we made previously
          
          BTN = Button(root, text="send 2 MCU", command = printt)
          BTN.pack()
    
          root.mainloop() #we loop forever
          
        

    arduino codes

    this arduino is based on Cprograming language so i made a code that writes an output over serial port
    so we the data written on the port are the ones to be sent on user interface we made above
          
          void setup() {
            Serial.begin(9600);
            }
            
            void loop() {
            int r = random(1, 100);
            Serial.println(r);
            
            }
          
        
    at first we start and set the baud rate to (9600)
    and then i created the object that generates random numbers and print it over serial port
    the hardware
    i hoocked my board that i made it is esp 32 based microcontroller it is also used to read data over serial port
    and then tried to read data over serial monitor to check if it works
    after finding that it is working good i stepped over python to make program to read over serial port here're the codes
          
            import serial
            serdata =serial.Serial('COM6', baudrate=9600, timeout=1)
            while 1:
                print(serdata)
          
        
    the first line includes the library of the serial module
    the second line under the variable name serdata, has first with serial object created, that has serial port for it COM6, baudrate and timeout
    i run the program here's the output


    to complete my assignment, then next i created the interface with two button to turn on/off the led on the board
    when i click on turn on the led on the board turn on and if i click on turn off led on board turn off
    in python codes i have declared that the turn on button has a command to send character "Y"
    though turnoff button has a command to send character "N"

    through serial communication as i mentioned above i have also used arduino to integrate with a microcontroller

    the above sketch codes will help us to read 2 different characters and then they compare with the characters stored before in codes condition
    the following codes indicates how they compare characters received over serial
          
            if (serial.read() == 'Y'){
              digitalWrite(LED, HIGH);
            }
    
            else if (serial.read() == 'N'){
              digitalWrite(LED, LOW);
            }
          
        
    these codes check if we have received character 'Y' and then the LED turn HIGH and if we receive character 'N' LED turn LOW

    python all codes

          
            import serial
            from tkinter import *
    
            ser = serial.Serial('COM4',9600, timeout=1)
            root = Tk()
            root.geometry('300x300')
            root.title("fabacademy")
    
            tit = Label(root, text="my fab interface", font=('Bauhaus 93', 25))
            tit.pack(padx=20) 
    
            # userI = Entry(root, width=20)
            # userI.pack(padx=30, pady=10)
    
    
            def turnON():
                ser.write(b'Y')
    
            def turnOFF():
                ser.write(b'N')
              
    
            BTN = Button(root, text="TURN ON", command = turnON)
            BTN.pack()
    
            BTN1 = Button(root, text="TURN OFF", command = turnOFF)
            BTN1.pack()
                
            root.mainloop()
          
        

    arduino codes

        
          char _data;
          void setup() {
          Serial.begin(9600);
          pinMode(25, OUTPUT);
          }
    
          void loop() {
          if (Serial.available() > 0){
              _data = Serial.read();
              if (_data == 'Y'){
                  digitalWrite(25, HIGH);
                  Serial.print("ON");
              }
              else if (_data == 'N'){
                  digitalWrite(25, LOW);
                  Serial.print("OFF");
              }
          }
          }
        
       


@weekly session